From 4d34ebebcfcf31cd3ea41457e53dc8423ae329a7 Mon Sep 17 00:00:00 2001 From: Nik <2661899+CodeSchwert@users.noreply.github.com> Date: Tue, 16 Apr 2024 13:16:10 +1200 Subject: [PATCH] fix: elliptic import (#1660) --- packages/x-client/package.json | 2 - packages/x-client/src/exportUtils.ts | 2 +- packages/x-client/src/utils/index.ts | 4 +- .../utils/stark/legacy/crypto/constants.ts | 8 ++- .../src/utils/stark/legacy/crypto/crypto.ts | 20 +++--- .../x-client/src/utils/stark/starkCurve.ts | 10 ++- .../x-client/src/utils/stark/starkSigner.ts | 7 +- packages/x-client/tsconfig.json | 2 +- sdk/package.json | 1 - sdk/rollup.config.js | 3 +- yarn.lock | 66 ++----------------- 11 files changed, 36 insertions(+), 89 deletions(-) diff --git a/packages/x-client/package.json b/packages/x-client/package.json index c9340e6e00..323dc0b589 100644 --- a/packages/x-client/package.json +++ b/packages/x-client/package.json @@ -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", @@ -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", diff --git a/packages/x-client/src/exportUtils.ts b/packages/x-client/src/exportUtils.ts index f50d5465b6..f82c4ee9ca 100644 --- a/packages/x-client/src/exportUtils.ts +++ b/packages/x-client/src/exportUtils.ts @@ -2,4 +2,4 @@ export { generateLegacyStarkPrivateKey, generateStarkPrivateKey, createStarkSigner, -} from '@imtbl/core-sdk'; +} from './utils'; diff --git a/packages/x-client/src/utils/index.ts b/packages/x-client/src/utils/index.ts index 3ef356d579..d3d802a898 100644 --- a/packages/x-client/src/utils/index.ts +++ b/packages/x-client/src/utils/index.ts @@ -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'; diff --git a/packages/x-client/src/utils/stark/legacy/crypto/constants.ts b/packages/x-client/src/utils/stark/legacy/crypto/constants.ts index 793f02d75c..73fc31b236 100644 --- a/packages/x-client/src/utils/stark/legacy/crypto/constants.ts +++ b/packages/x-client/src/utils/stark/legacy/crypto/constants.ts @@ -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'; @@ -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, diff --git a/packages/x-client/src/utils/stark/legacy/crypto/crypto.ts b/packages/x-client/src/utils/stark/legacy/crypto/crypto.ts index 2e93a86108..239b6383d9 100644 --- a/packages/x-client/src/utils/stark/legacy/crypto/crypto.ts +++ b/packages/x-client/src/utils/stark/legacy/crypto/crypto.ts @@ -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 { @@ -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'); } diff --git a/packages/x-client/src/utils/stark/starkCurve.ts b/packages/x-client/src/utils/stark/starkCurve.ts index 1169a08b35..7255713b53 100644 --- a/packages/x-client/src/utils/stark/starkCurve.ts +++ b/packages/x-client/src/utils/stark/starkCurve.ts @@ -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 @@ -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() diff --git a/packages/x-client/src/utils/stark/starkSigner.ts b/packages/x-client/src/utils/stark/starkSigner.ts index feea1364cf..6674db2678 100644 --- a/packages/x-client/src/utils/stark/starkSigner.ts +++ b/packages/x-client/src/utils/stark/starkSigner.ts @@ -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'; @@ -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'); @@ -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), diff --git a/packages/x-client/tsconfig.json b/packages/x-client/tsconfig.json index ab5a7f300c..15fcf40c38 100644 --- a/packages/x-client/tsconfig.json +++ b/packages/x-client/tsconfig.json @@ -18,5 +18,5 @@ "skipLibCheck": true }, "include": ["src"], - "exclude": ["node_modules", "dist"] + "exclude": ["node_modules", "dist", "node_modules/elliptic"] } diff --git a/sdk/package.json b/sdk/package.json index fb36c0ebd2..2ce91c0a01 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -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", diff --git a/sdk/rollup.config.js b/sdk/rollup.config.js index e3e14cfbb9..ca21a67786 100644 --- a/sdk/rollup.config.js +++ b/sdk/rollup.config.js @@ -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'; @@ -48,8 +47,8 @@ const getFileBuild = (inputFilename) => [ nodeResolve({ resolveOnly: getPackages(), }), - commonJs(), json(), + commonJs(), typescript({ declaration: true, declarationDir: './dist/types', diff --git a/yarn.lock b/yarn.lock index b6934a1c9c..0625894127 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: @@ -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: @@ -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: @@ -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/cryptofiat@0.0.0, @imtbl/cryptofiat@workspace:packages/internal/cryptofiat": version: 0.0.0-use.local resolution: "@imtbl/cryptofiat@workspace:packages/internal/cryptofiat" @@ -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 @@ -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 @@ -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" @@ -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" @@ -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" @@ -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: @@ -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"