diff --git a/packages/login/package.json b/packages/login/package.json index 72480db2..25466b65 100644 --- a/packages/login/package.json +++ b/packages/login/package.json @@ -55,6 +55,7 @@ "@portkey/services": "1.3.0-alpha.3", "@portkey/types": "1.3.0-alpha.3", "@portkey/utils": "1.3.0-alpha.3", + "@types/bn.js": "^5.1.3", "aelf-bridge": "^0.0.10", "aelf-sdk": "^3.2.40", "ahooks": "^3.7.7", diff --git a/packages/login/src/utils/zeroFill.ts b/packages/login/src/utils/zeroFill.ts new file mode 100644 index 00000000..e590d338 --- /dev/null +++ b/packages/login/src/utils/zeroFill.ts @@ -0,0 +1,4 @@ +import BN, { isBN } from 'bn.js'; +export function zeroFill(str: string | BN) { + return isBN(str) ? str.toString(16, 64) : str.padStart(64, '0'); +} diff --git a/packages/login/src/wallets/discover/useDiscover.ts b/packages/login/src/wallets/discover/useDiscover.ts index c020d3b6..dd8a5dc1 100644 --- a/packages/login/src/wallets/discover/useDiscover.ts +++ b/packages/login/src/wallets/discover/useDiscover.ts @@ -18,7 +18,8 @@ import { DiscoverOptions } from 'src/types'; import useChainIdsSync from './useChainIdsSync'; import { ERR_CODE, makeError } from '../../errors'; import wait from '../../utils/waitForSeconds'; -import BN from 'bn.js'; +import { zeroFill } from '../../utils/zeroFill'; + export type DiscoverDetectState = 'unknown' | 'detected' | 'not-detected'; export type DiscoverInterface = WalletHookInterface & { discoverDetected: DiscoverDetectState; @@ -263,12 +264,11 @@ export function useDiscover({ data: signInfo || params.hexToBeSign, }, }); - signedMsgObject.r = '1'; - signedMsgObject.s = '2'; - const r = BN.isBN(signedMsgObject.r) ? signedMsgObject.r.toString(16, 64) : signedMsgObject.r.padStart(64, '0'); - const s = BN.isBN(signedMsgObject.s) ? signedMsgObject.s.toString(16, 64) : signedMsgObject.s.padStart(64, '0'); - const signedMsgString = [r, s, `0${signedMsgObject.recoveryParam!.toString()}`].join(''); - + const signedMsgString = [ + zeroFill(signedMsgObject.r), + zeroFill(signedMsgObject.s), + `0${signedMsgObject.recoveryParam!.toString()}`, + ].join(''); return { error: 0, errorMessage: '', diff --git a/packages/login/src/wallets/elf/useElf.ts b/packages/login/src/wallets/elf/useElf.ts index a8427a23..a73d8998 100644 --- a/packages/login/src/wallets/elf/useElf.ts +++ b/packages/login/src/wallets/elf/useElf.ts @@ -8,7 +8,7 @@ import { WalletType, WebLoginState, WebLoginEvents } from '../../constants'; import isMobile from '../../utils/isMobile'; import checkSignatureParams from '../../utils/signatureParams'; import detectNightElf from './detectNightElf'; -import BN from 'bn.js'; +import { zeroFill } from '../../utils/zeroFill'; export function useElf({ options, @@ -235,9 +235,11 @@ export function useElf({ signedMsgObject.errorMessage.message || signedMsgObject.errorMessage || signedMsgObject.message, ); } - const r = BN.isBN(signedMsgObject.r) ? signedMsgObject.r.toString(16, 64) : signedMsgObject.r.padStart(64, '0'); - const s = BN.isBN(signedMsgObject.s) ? signedMsgObject.s.toString(16, 64) : signedMsgObject.s.padStart(64, '0'); - const signedMsgString = [r, s, `0${signedMsgObject.recoveryParam.toString()}`].join(''); + const signedMsgString = [ + zeroFill(signedMsgObject.r), + zeroFill(signedMsgObject.s), + `0${signedMsgObject.recoveryParam.toString()}`, + ].join(''); return { error: 0, errorMessage: '', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c38de50..7a2418fb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -133,6 +133,7 @@ importers: '@portkey/services': 1.3.0-alpha.3 '@portkey/types': 1.3.0-alpha.3 '@portkey/utils': 1.3.0-alpha.3 + '@types/bn.js': ^5.1.3 '@types/uuid': ^8.3.4 aelf-bridge: ^0.0.10 aelf-sdk: ^3.2.40 @@ -152,6 +153,7 @@ importers: '@portkey/services': registry.npmjs.org/@portkey/services/1.3.0-alpha.3 '@portkey/types': registry.npmjs.org/@portkey/types/1.3.0-alpha.3 '@portkey/utils': registry.npmjs.org/@portkey/utils/1.3.0-alpha.3 + '@types/bn.js': registry.npmjs.org/@types/bn.js/5.1.3 aelf-bridge: registry.npmjs.org/aelf-bridge/0.0.10 aelf-sdk: registry.npmjs.org/aelf-sdk/3.2.44 ahooks: registry.npmjs.org/ahooks/3.7.8