Skip to content

Commit

Permalink
fix: elliptic import (#1660)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSchwert authored Apr 16, 2024
1 parent 6147d80 commit 4d34ebe
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 89 deletions.
2 changes: 0 additions & 2 deletions packages/x-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@ethersproject/strings": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@imtbl/config": "0.0.0",
"@imtbl/core-sdk": "^2.7.0",
"@imtbl/generated-clients": "0.0.0",
"axios": "^1.6.5",
"bn.js": "^5.2.1",
Expand All @@ -24,7 +23,6 @@
"devDependencies": {
"@rollup/plugin-typescript": "^11.0.0",
"@swc/jest": "^0.2.24",
"@types/elliptic": "^6.4.18",
"@types/jest": "^29.4.3",
"eslint": "^8.40.0",
"jest": "^29.4.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/x-client/src/exportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export {
generateLegacyStarkPrivateKey,
generateStarkPrivateKey,
createStarkSigner,
} from '@imtbl/core-sdk';
} from './utils';
4 changes: 2 additions & 2 deletions packages/x-client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './convertToSignableToken';
export * from './formatError';
export * from './crypto';
// export * from './stark/starkCurve';
// export * from './stark/starkSigner';
export * from './stark/starkCurve';
export * from './stark/starkSigner';
8 changes: 5 additions & 3 deletions packages/x-client/src/utils/stark/legacy/crypto/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line @typescript-eslint/naming-convention
import BN from 'bn.js';
import { curves, ec as Ec } from 'elliptic';
// @ts-ignore
import elliptic from 'elliptic';
import hashJS from 'hash.js';

import { constantPointsHex } from './points';
Expand All @@ -25,8 +26,9 @@ const order = new BN(
16,
);

const starkEc = new Ec(
new curves.PresetCurve({
// eslint-disable-next-line new-cap
const starkEc = new elliptic.ec(
new elliptic.curves.PresetCurve({
type: 'short',
prime: null,
p: prime as any,
Expand Down
20 changes: 11 additions & 9 deletions packages/x-client/src/utils/stark/legacy/crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import assert from 'assert';
// eslint-disable-next-line @typescript-eslint/naming-convention
import BN from 'bn.js';
import { ec } from 'elliptic';
// @ts-ignore
import elliptic from 'elliptic';
import * as encUtils from 'enc-utils';
import { hdkey } from 'ethereumjs-wallet';
// import { hdkey } from 'ethereumjs-wallet';
import * as ethereumJsWallet from 'ethereumjs-wallet';
import hashJS from 'hash.js';

import {
Expand Down Expand Up @@ -81,37 +83,37 @@ export function grindKey(privateKey: string): string {
return key.mod(ORDER).toString('hex');
}

export function getKeyPair(privateKey: string): ec.KeyPair {
export function getKeyPair(privateKey: string): elliptic.ec.KeyPair {
return starkEc.keyFromPrivate(privateKey, 'hex');
}

export function getPrivateKeyFromPath(seed: string, path: string): string {
return hdkey
return ethereumJsWallet.hdkey
.fromMasterSeed(Buffer.from(seed.slice(2), 'hex')) // assuming seed is '0x...'
.derivePath(path)
.getWallet()
.getPrivateKeyString();
}

export function getKeyPairFromPath(seed: string, path: string): ec.KeyPair {
export function getKeyPairFromPath(seed: string, path: string): elliptic.ec.KeyPair {
assert(isHexPrefixed(seed), MISSING_HEX_PREFIX);
const privateKey = getPrivateKeyFromPath(seed, path);
return getKeyPair(grindKey(privateKey));
}

export function getPublic(keyPair: ec.KeyPair, compressed = false): string {
export function getPublic(keyPair: elliptic.ec.KeyPair, compressed = false): string {
return keyPair.getPublic(compressed, 'hex');
}

export function getStarkPublicKey(keyPair: ec.KeyPair): string {
export function getStarkPublicKey(keyPair: elliptic.ec.KeyPair): string {
return getPublic(keyPair, true);
}

export function getKeyPairFromPublicKey(publicKey: string): ec.KeyPair {
export function getKeyPairFromPublicKey(publicKey: string): elliptic.ec.KeyPair {
return starkEc.keyFromPublic(encUtils.hexToArray(publicKey));
}

export function getKeyPairFromPrivateKey(privateKey: string): ec.KeyPair {
export function getKeyPairFromPrivateKey(privateKey: string): elliptic.ec.KeyPair {
return starkEc.keyFromPrivate(privateKey, 'hex');
}

Expand Down
10 changes: 7 additions & 3 deletions packages/x-client/src/utils/stark/starkCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
import { Signer } from '@ethersproject/abstract-signer';
import { splitSignature } from '@ethersproject/bytes';
import hash from 'hash.js';
import { curves, ec } from 'elliptic';
// @ts-ignore - elliptic types cause build to break...
import elliptic from 'elliptic';
import * as encUtils from 'enc-utils';
// eslint-disable-next-line @typescript-eslint/naming-convention
import BN from 'bn.js';
import { hdkey } from 'ethereumjs-wallet';
// import { hdkey } from 'ethereumjs-wallet';
import * as ethereumJsWallet from 'ethereumjs-wallet';
import { createStarkSigner } from './starkSigner';
import * as legacy from './legacy/crypto';
import { getStarkPublicKeyFromImx } from './getStarkPublicKeyFromImx';

const { curves, ec } = elliptic;

/*
Stark-friendly elliptic curve
Expand Down Expand Up @@ -154,7 +158,7 @@ export function checkIfHashedKeyIsAboveLimit(keySeed: BN) {
}

export function getPrivateKeyFromPath(seed: string, path: string): BN {
const privateKey = hdkey
const privateKey = ethereumJsWallet.hdkey
.fromMasterSeed(Buffer.from(seed.slice(2), 'hex')) // assuming seed is '0x...'
.derivePath(path)
.getWallet()
Expand Down
7 changes: 4 additions & 3 deletions packages/x-client/src/utils/stark/starkSigner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ec } from 'elliptic';
// @ts-ignore
import elliptic from 'elliptic';
import * as encUtils from 'enc-utils';
// eslint-disable-next-line @typescript-eslint/naming-convention
import BN from 'bn.js';
Expand All @@ -7,7 +8,7 @@ import { starkEc } from './legacy/crypto';
import { Errors } from './errors';

export class StandardStarkSigner implements StarkSigner {
private keyPair: ec.KeyPair;
private keyPair: elliptic.ec.KeyPair;

constructor(private privateKey: string) {
this.keyPair = starkEc.keyFromPrivate(privateKey, 'hex');
Expand All @@ -23,7 +24,7 @@ export class StandardStarkSigner implements StarkSigner {
}

// eslint-disable-next-line class-methods-use-this
private serialize(sig: ec.Signature): string {
private serialize(sig: elliptic.ec.Signature): string {
return encUtils.addHexPrefix(
encUtils.padLeft(sig.r.toString('hex'), 64)
+ encUtils.padLeft(sig.s.toString('hex'), 64),
Expand Down
2 changes: 1 addition & 1 deletion packages/x-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "dist", "node_modules/elliptic"]
}
1 change: 0 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@ethersproject/providers": "^5.7.2",
"@ethersproject/strings": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@imtbl/core-sdk": "^2.7.0",
"@imtbl/react-analytics": "0.2.1-alpha",
"@jest/globals": "^29.5.0",
"@magic-ext/oidc": "4.2.0",
Expand Down
3 changes: 1 addition & 2 deletions sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import terser from '@rollup/plugin-terser';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import babel from '@rollup/plugin-babel';


// RELEASE_TYPE environment variable is set by the CI/CD pipeline
const releaseType = process.env.RELEASE_TYPE || 'alpha';

Expand Down Expand Up @@ -48,8 +47,8 @@ const getFileBuild = (inputFilename) => [
nodeResolve({
resolveOnly: getPackages(),
}),
commonJs(),
json(),
commonJs(),
typescript({
declaration: true,
declarationDir: './dist/types',
Expand Down
66 changes: 4 additions & 62 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,7 @@ __metadata:
languageName: node
linkType: hard

"@ethersproject/abi@npm:5.7.0, @ethersproject/abi@npm:^5.0.0, @ethersproject/abi@npm:^5.0.12, @ethersproject/abi@npm:^5.5.0, @ethersproject/abi@npm:^5.7.0":
"@ethersproject/abi@npm:5.7.0, @ethersproject/abi@npm:^5.0.12, @ethersproject/abi@npm:^5.5.0, @ethersproject/abi@npm:^5.7.0":
version: 5.7.0
resolution: "@ethersproject/abi@npm:5.7.0"
dependencies:
Expand Down Expand Up @@ -2990,7 +2990,7 @@ __metadata:
languageName: node
linkType: hard

"@ethersproject/bytes@npm:5.7.0, @ethersproject/bytes@npm:^5.0.0, @ethersproject/bytes@npm:^5.7.0":
"@ethersproject/bytes@npm:5.7.0, @ethersproject/bytes@npm:^5.7.0":
version: 5.7.0
resolution: "@ethersproject/bytes@npm:5.7.0"
dependencies:
Expand Down Expand Up @@ -3129,7 +3129,7 @@ __metadata:
languageName: node
linkType: hard

"@ethersproject/providers@npm:5.7.2, @ethersproject/providers@npm:^5.0.0, @ethersproject/providers@npm:^5.7.2":
"@ethersproject/providers@npm:5.7.2, @ethersproject/providers@npm:^5.7.2":
version: 5.7.2
resolution: "@ethersproject/providers@npm:5.7.2"
dependencies:
Expand Down Expand Up @@ -3575,24 +3575,6 @@ __metadata:
languageName: unknown
linkType: soft

"@imtbl/core-sdk@npm:^2.7.0":
version: 2.7.0
resolution: "@imtbl/core-sdk@npm:2.7.0"
dependencies:
"@ethersproject/abi": ^5.0.0
"@ethersproject/bytes": ^5.0.0
"@ethersproject/providers": ^5.0.0
axios: ^0.26.1
bn.js: ^5.2.0
elliptic: ^6.5.4
enc-utils: ^3.0.0
ethereumjs-wallet: ^1.0.2
ethers: ^5.7.1
hash.js: ^1.1.7
checksum: ea9bed1eceda1563d9ba50db29935ad0b4131cce3f1bf8e50f6bad28f3f301969b399319e8d6417e7546098c77850aa70c316ea48ecbfa7d28e85a27a206f234
languageName: node
linkType: hard

"@imtbl/[email protected], @imtbl/cryptofiat@workspace:packages/internal/cryptofiat":
version: 0.0.0-use.local
resolution: "@imtbl/cryptofiat@workspace:packages/internal/cryptofiat"
Expand Down Expand Up @@ -3862,7 +3844,6 @@ __metadata:
"@imtbl/checkout-sdk": 0.0.0
"@imtbl/checkout-widgets": 0.0.0
"@imtbl/config": 0.0.0
"@imtbl/core-sdk": ^2.7.0
"@imtbl/cryptofiat": 0.0.0
"@imtbl/generated-clients": 0.0.0
"@imtbl/guardian": 0.0.0
Expand Down Expand Up @@ -3992,11 +3973,9 @@ __metadata:
"@ethersproject/strings": ^5.7.0
"@ethersproject/wallet": ^5.7.0
"@imtbl/config": 0.0.0
"@imtbl/core-sdk": ^2.7.0
"@imtbl/generated-clients": 0.0.0
"@rollup/plugin-typescript": ^11.0.0
"@swc/jest": ^0.2.24
"@types/elliptic": ^6.4.18
"@types/jest": ^29.4.3
axios: ^1.6.5
bn.js: ^5.2.1
Expand Down Expand Up @@ -9042,15 +9021,6 @@ __metadata:
languageName: node
linkType: hard

"@types/bn.js@npm:*":
version: 5.1.5
resolution: "@types/bn.js@npm:5.1.5"
dependencies:
"@types/node": "*"
checksum: c87b28c4af74545624f8a3dae5294b16aa190c222626e8d4b2e327b33b1a3f1eeb43e7a24d914a9774bca43d8cd6e1cb0325c1f4b3a244af6693a024e1d918e6
languageName: node
linkType: hard

"@types/bn.js@npm:^4.11.5":
version: 4.11.6
resolution: "@types/bn.js@npm:4.11.6"
Expand Down Expand Up @@ -9135,15 +9105,6 @@ __metadata:
languageName: node
linkType: hard

"@types/elliptic@npm:^6.4.18":
version: 6.4.18
resolution: "@types/elliptic@npm:6.4.18"
dependencies:
"@types/bn.js": "*"
checksum: c27613c530fb95441e5e6b456c8c9bc26568ca14c546aae6d7c1d8d46869f79a2272feaef266ac00bdb68b2671e6351ed01b91b82266eac30ca9092720825d16
languageName: node
linkType: hard

"@types/emscripten@npm:^1.39.6":
version: 1.39.6
resolution: "@types/emscripten@npm:1.39.6"
Expand Down Expand Up @@ -11788,15 +11749,6 @@ __metadata:
languageName: node
linkType: hard

"axios@npm:^0.26.1":
version: 0.26.1
resolution: "axios@npm:0.26.1"
dependencies:
follow-redirects: ^1.14.8
checksum: d9eb58ff4bc0b36a04783fc9ff760e9245c829a5a1052ee7ca6013410d427036b1d10d04e7380c02f3508c5eaf3485b1ae67bd2adbfec3683704745c8d7a6e1a
languageName: node
linkType: hard

"axios@npm:^1.6.5":
version: 1.6.5
resolution: "axios@npm:1.6.5"
Expand Down Expand Up @@ -16067,7 +16019,7 @@ __metadata:
languageName: node
linkType: hard

"ethers@npm:^5.7.1, ethers@npm:^5.7.2":
"ethers@npm:^5.7.2":
version: 5.7.2
resolution: "ethers@npm:5.7.2"
dependencies:
Expand Down Expand Up @@ -16680,16 +16632,6 @@ __metadata:
languageName: node
linkType: hard

"follow-redirects@npm:^1.14.8":
version: 1.15.6
resolution: "follow-redirects@npm:1.15.6"
peerDependenciesMeta:
debug:
optional: true
checksum: a62c378dfc8c00f60b9c80cab158ba54e99ba0239a5dd7c81245e5a5b39d10f0c35e249c3379eae719ff0285fff88c365dd446fab19dee771f1d76252df1bbf5
languageName: node
linkType: hard

"follow-redirects@npm:^1.15.4":
version: 1.15.4
resolution: "follow-redirects@npm:1.15.4"
Expand Down

0 comments on commit 4d34ebe

Please sign in to comment.