Skip to content

Commit

Permalink
refactor: use react-native-quick-bip39
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 committed Dec 17, 2024
1 parent 21978bc commit 63c0337
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
diff --git a/dist/index.js b/dist/index.js
index fd317674caba8bba044ea5fd24c8a67fad859ad8..0a66313a7062e9e478d202c0b0669ce023d5b4f0 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -2371,8 +2371,7 @@ __export(eth_hd_keyring_exports, {
module.exports = __toCommonJS(eth_hd_keyring_exports);
var import_hdkey = require("ethereum-cryptography/hdkey");
var import_eth_simple_keyring = __toESM(require("@rabby-wallet/eth-simple-keyring"));
-var bip39 = __toESM(require("@scure/bip39"));
-var import_english = require("@scure/bip39/wordlists/english");
+var bip39 = __toESM(require("react-native-quick-bip39"));
var sigUtil = __toESM(require("eth-sig-util"));
var import_util = require("@ethereumjs/util");
var import_slip39 = __toESM(require_slip392());
@@ -2469,7 +2468,7 @@ var _HdKeyring = class _HdKeyring extends import_eth_simple_keyring.default {
}
addAccounts(numberOfAccounts = 1) {
if (!this.hdWallet) {
- this.initFromMnemonic(bip39.generateMnemonic(import_english.wordlist));
+ this.initFromMnemonic(bip39.generateMnemonic(bip39.wordlists.english));
}
let count = numberOfAccounts;
let currentIdx = 0;
@@ -2612,7 +2611,7 @@ var _HdKeyring = class _HdKeyring extends import_eth_simple_keyring.default {
return (0, import_util.bytesToHex)((0, import_util.publicToAddress)(publicKey, true)).toLowerCase();
}
generateMnemonic() {
- return bip39.generateMnemonic(import_english.wordlist);
+ return bip39.generateMnemonic(bip39.wordlists.english);
}
setHdPath(hdPath = HD_PATH_BASE["BIP44" /* BIP44 */]) {
this.hdPath = hdPath;
@@ -2690,7 +2689,7 @@ var _HdKeyring = class _HdKeyring extends import_eth_simple_keyring.default {
if (this.checkMnemonicIsSlip39(mnemonic)) {
return true;
}
- return bip39.validateMnemonic(mnemonic, import_english.wordlist);
+ return bip39.validateMnemonic(mnemonic, bip39.wordlists.english);
}
};
_HdKeyring.type = type;
30 changes: 0 additions & 30 deletions .yarn/patches/@scure-bip39-npm-1.3.0-1d74c5c469.patch

This file was deleted.

12 changes: 12 additions & 0 deletions apps/mobile/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ PODS:
- nanopb/encode (= 2.30909.1)
- nanopb/decode (2.30909.1)
- nanopb/encode (2.30909.1)
- OpenSSL-Universal (3.3.2000)
- PromisesObjC (2.3.1)
- RCT-Folly (2021.07.22.00):
- boost
Expand Down Expand Up @@ -464,6 +465,11 @@ PODS:
- react-native-pager-view (6.2.3):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-quick-crypto (0.7.10):
- OpenSSL-Universal
- RCT-Folly (= 2021.07.22.00)
- React
- React-Core
- react-native-randombytes (3.6.1):
- React-Core
- react-native-safe-area-context (4.8.2):
Expand Down Expand Up @@ -703,6 +709,7 @@ DEPENDENCIES:
- react-native-mmkv (from `../node_modules/react-native-mmkv`)
- "react-native-netinfo (from `../node_modules/@react-native-community/netinfo`)"
- react-native-pager-view (from `../node_modules/react-native-pager-view`)
- react-native-quick-crypto (from `../node_modules/react-native-quick-crypto`)
- react-native-randombytes (from `../node_modules/react-native-randombytes`)
- react-native-safe-area-context (from `../node_modules/react-native-safe-area-context`)
- react-native-splash-screen (from `../node_modules/react-native-splash-screen`)
Expand Down Expand Up @@ -771,6 +778,7 @@ SPEC REPOS:
- MMKVCore
- MultiplatformBleAdapter
- nanopb
- OpenSSL-Universal
- PromisesObjC
- SDWebImage
- SDWebImageWebPCoder
Expand Down Expand Up @@ -849,6 +857,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/@react-native-community/netinfo"
react-native-pager-view:
:path: "../node_modules/react-native-pager-view"
react-native-quick-crypto:
:path: "../node_modules/react-native-quick-crypto"
react-native-randombytes:
:path: "../node_modules/react-native-randombytes"
react-native-safe-area-context:
Expand Down Expand Up @@ -970,6 +980,7 @@ SPEC CHECKSUMS:
MMKVCore: 9e2e5fd529b64a9fe15f1a7afb3d73b2e27b4db9
MultiplatformBleAdapter: b1fddd0d499b96b607e00f0faa8e60648343dc1d
nanopb: d4d75c12cd1316f4a64e3c6963f879ecd4b5e0d5
OpenSSL-Universal: b60a3702c9fea8b3145549d421fdb018e53ab7b4
PromisesObjC: c50d2056b5253dadbd6c2bea79b0674bd5a52fa4
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: 83bca1c184feb4d2e51c72c8369b83d641443f95
Expand Down Expand Up @@ -998,6 +1009,7 @@ SPEC CHECKSUMS:
react-native-mmkv: 436ee510dcc43b58a49d7be0451f285e06a9fd1d
react-native-netinfo: 8a7fd3f7130ef4ad2fb4276d5c9f8d3f28d2df3d
react-native-pager-view: cf96a223846458cc7e932017636b5c6d8cc2c549
react-native-quick-crypto: 64b9662703b8613b41d909fdc85ff79552d21fde
react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846
react-native-safe-area-context: 0ee144a6170530ccc37a0fd9388e28d06f516a89
react-native-splash-screen: 4312f786b13a81b5169ef346d76d33bc0c6dc457
Expand Down
3 changes: 2 additions & 1 deletion apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"@rematch/core": "^2.2.0",
"@rneui/base": "^4.0.0-rc.8",
"@rneui/themed": "^4.0.0-rc.8",
"@scure/bip39": "^1.3.0",
"@sentry/react-native": "5.31.0",
"@types/highlight-words-core": "1.2.3",
"@types/lodash.debounce": "^4.0.9",
Expand Down Expand Up @@ -152,6 +151,8 @@
"react-native-pager-view": "^6.2.3",
"react-native-permissions": "^4.1.2",
"react-native-qrcode-svg": "^6.2.0",
"react-native-quick-bip39": "0.0.9",
"react-native-quick-crypto": "0.7.10",
"react-native-randombytes": "^3.6.1",
"react-native-reanimated": "^3.6.1",
"react-native-root-toast": "^3.5.1",
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/core/utils/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as bip39 from '@scure/bip39';
import * as bip39 from 'react-native-quick-bip39';
import { HDKey } from 'ethereum-cryptography/hdkey';
import * as sigUtil from 'eth-sig-util';
import { bytesToHex, publicToAddress, privateToPublic } from '@ethereumjs/util';
Expand Down
7 changes: 3 additions & 4 deletions apps/mobile/src/screens/Address/ImportSeedPhraseScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { useScanner } from '../Scanner/ScannerScreen';
import { requestKeyring } from '@/core/apis/keyring';
import { toast } from '@/components/Toast';
import { useFocusEffect } from '@react-navigation/native';
import { wordlist } from '@scure/bip39/wordlists/english';
import * as bip39 from '@scure/bip39';
import * as bip39 from 'react-native-quick-bip39';

const getStyles = (colors: AppColorsVariants) =>
StyleSheet.create({
Expand Down Expand Up @@ -112,13 +111,13 @@ export const ImportSeedPhraseScreen = () => {
});
} else {
try {
bip39.mnemonicToEntropy(mnemonics?.trim(), wordlist);
bip39.mnemonicToEntropy(mnemonics?.trim(), bip39.wordlists.english);
} catch (e) {
if ((e as any).message.includes('Unknown letter:')) {
let errorWords: string[] = [];
mnemonics.split(/\s+/).forEach(word => {
let v = word?.trim();
if (v && !wordlist.includes(v)) {
if (v && !bip39.wordlists.english.includes(v)) {
errorWords.push(v);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { useScanner } from '../Scanner/ScannerScreen';
import { requestKeyring } from '@/core/apis/keyring';
import { toast } from '@/components2024/Toast';
import { useFocusEffect } from '@react-navigation/native';
import { wordlist } from '@scure/bip39/wordlists/english';
import * as bip39 from '@scure/bip39';
import * as bip39 from 'react-native-quick-bip39';
import PasteButton from '@/components2024/PasteButton';
import { NextInput } from '@/components2024/Form/Input';
import { createGetStyles2024, makeDebugBorder } from '@/utils/styles';
Expand Down Expand Up @@ -192,13 +191,13 @@ export const ImportSeedPhraseScreen2024 = () => {
});
} else {
try {
bip39.mnemonicToEntropy(mnemonics?.trim(), wordlist);
bip39.mnemonicToEntropy(mnemonics?.trim(), bip39.wordlists.english);
} catch (e) {
if ((e as any).message.includes('Unknown letter:')) {
let errorWords: string[] = [];
mnemonics.split(/\s+/).forEach(word => {
let v = word?.trim();
if (v && !wordlist.includes(v)) {
if (v && !bip39.wordlists.english.includes(v)) {
errorWords.push(v);
}
});
Expand Down
54 changes: 25 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@
"yarn workspace dev-console-cra lint:commit:fix"
]
},
"resolutions": {
"@debank/[email protected]": "patch:@debank/common@npm%3A0.3.58#./.yarn/patches/@debank-common-npm-0.3.58-0aa25cb07b.patch",
"@debank/common@^0.3.54": "patch:@debank/common@npm%3A0.3.58#./.yarn/patches/@debank-common-npm-0.3.58-0aa25cb07b.patch",
"@keystonehq/bc-ur-registry@^0.5.0-alpha.4": "patch:@keystonehq/bc-ur-registry@npm%3A0.6.4#./.yarn/patches/@keystonehq-bc-ur-registry-npm-0.6.4-7922ce4e4c.patch",
"@keystonehq/bc-ur-registry@^0.6.0": "patch:@keystonehq/bc-ur-registry@npm%3A0.6.4#./.yarn/patches/@keystonehq-bc-ur-registry-npm-0.6.4-7922ce4e4c.patch",
"@ledgerhq/hw-app-eth@^6.40.0": "patch:@ledgerhq/hw-app-eth@npm%3A6.40.2#./.yarn/patches/@ledgerhq-hw-app-eth-npm-6.40.2-ffdc4add8c.patch",
"@ledgerhq/hw-transport@^6.31.4": "patch:@ledgerhq/hw-transport@npm%3A6.31.4#./.yarn/patches/@ledgerhq-hw-transport-npm-6.31.4-b439e9a1f8.patch",
"@metamask/[email protected]": "patch:@metamask/eth-sig-util@npm%3A5.1.0#./.yarn/patches/@metamask-eth-sig-util-npm-5.1.0-fb33750b27.patch",
"@metamask/eth-sig-util@^4.0.0": "patch:@metamask/eth-sig-util@npm%3A5.1.0#./.yarn/patches/@metamask-eth-sig-util-npm-5.1.0-fb33750b27.patch",
"@metamask/eth-sig-util@^5.0.0": "patch:@metamask/eth-sig-util@npm%3A5.1.0#./.yarn/patches/@metamask-eth-sig-util-npm-5.1.0-fb33750b27.patch",
"@onekeyfe/hd-transport-react-native@^1.0.10": "patch:@onekeyfe/hd-transport-react-native@npm%3A1.0.10#./.yarn/patches/@onekeyfe-hd-transport-react-native-npm-1.0.10-09c8057879.patch",
"@rabby-wallet/[email protected]": "patch:@rabby-wallet/rabby-api@npm%3A0.8.5#./.yarn/patches/@rabby-wallet-rabby-api-npm-0.8.5-b09d6e3f2f.patch",
"@rabby-wallet/[email protected]": "patch:@rabby-wallet/rabby-api@npm%3A0.8.5#./.yarn/patches/@rabby-wallet-rabby-api-npm-0.8.5-b09d6e3f2f.patch",
"@react-native-menu/[email protected]": "patch:@react-native-menu/menu@npm%3A1.1.3#./.yarn/patches/@react-native-menu-menu-npm-1.1.3-6b0ecb2e6c.patch",
"eth-rpc-errors@^4.0.2": "patch:eth-rpc-errors@npm%3A4.0.3#./.yarn/patches/eth-rpc-errors-npm-4.0.3-d154190eb6.patch",
"eth-rpc-errors@^4.0.3": "patch:eth-rpc-errors@npm%3A4.0.3#./.yarn/patches/eth-rpc-errors-npm-4.0.3-d154190eb6.patch",
"nativewind@^2.0.11": "patch:nativewind@npm%3A2.0.11#./.yarn/patches/nativewind-npm-2.0.11-dc2ccf9fb5.patch",
"react-native-fs@^2.20.0": "patch:react-native-fs@npm%3A2.20.0#./.yarn/patches/react-native-fs-npm-2.20.0-a38fe24051.patch",
"[email protected]": "patch:react-native-keychain@npm%3A8.0.0#./.yarn/patches/react-native-keychain-npm-8.0.0-22c1f5c020.patch",
"react-native-mmkv@^2.12.2": "patch:react-native-mmkv@npm%3A2.12.2#./.yarn/patches/react-native-mmkv-npm-2.12.2-9efa7abf70.patch",
"[email protected]": "patch:react-native-webview@npm%3A13.10.5#./.yarn/patches/react-native-webview-npm-13.10.5-714eb41569.patch",
"[email protected]": "patch:react-use@npm%3A17.4.2#./.yarn/patches/react-use-npm-17.4.2-d0e8220eaa.patch",
"[email protected]": "patch:zeego@npm%3A1.10.0#./.yarn/patches/zeego-npm-1.10.0-1958a3ea63.patch",
"@rabby-wallet/[email protected]": "patch:@rabby-wallet/eth-hd-keyring@npm%3A4.3.1#./.yarn/patches/@rabby-wallet-eth-hd-keyring-npm-4.3.1-a1d8bbe289.patch"
},
"dependencies": {
"@babel/runtime": "^7.20.0",
"@debank/common": "^0.3.54",
Expand Down Expand Up @@ -110,34 +135,5 @@
"ethereumjs-util>ethereum-cryptography>secp256k1": true,
"simple-git-hooks": false
}
},
"resolutions": {
"@debank/common@^0.3.54": "patch:@debank/common@npm%3A0.3.58#./.yarn/patches/@debank-common-npm-0.3.58-0aa25cb07b.patch",
"@debank/[email protected]": "patch:@debank/common@npm%3A0.3.58#./.yarn/patches/@debank-common-npm-0.3.58-0aa25cb07b.patch",
"@metamask/[email protected]": "patch:@metamask/eth-sig-util@npm%3A5.1.0#./.yarn/patches/@metamask-eth-sig-util-npm-5.1.0-fb33750b27.patch",
"@metamask/eth-sig-util@^4.0.0": "patch:@metamask/eth-sig-util@npm%3A5.1.0#./.yarn/patches/@metamask-eth-sig-util-npm-5.1.0-fb33750b27.patch",
"@metamask/eth-sig-util@^5.0.0": "patch:@metamask/eth-sig-util@npm%3A5.1.0#./.yarn/patches/@metamask-eth-sig-util-npm-5.1.0-fb33750b27.patch",
"@onekeyfe/hd-transport-react-native@^1.0.10": "patch:@onekeyfe/hd-transport-react-native@npm%3A1.0.10#./.yarn/patches/@onekeyfe-hd-transport-react-native-npm-1.0.10-09c8057879.patch",
"@rabby-wallet/[email protected]": "patch:@rabby-wallet/rabby-api@npm%3A0.8.5#./.yarn/patches/@rabby-wallet-rabby-api-npm-0.8.5-b09d6e3f2f.patch",
"@rabby-wallet/[email protected]": "patch:@rabby-wallet/rabby-api@npm%3A0.8.5#./.yarn/patches/@rabby-wallet-rabby-api-npm-0.8.5-b09d6e3f2f.patch",
"@react-native-menu/[email protected]": "patch:@react-native-menu/menu@npm%3A1.1.3#./.yarn/patches/@react-native-menu-menu-npm-1.1.3-6b0ecb2e6c.patch",
"eth-rpc-errors@^4.0.3": "patch:eth-rpc-errors@npm%3A4.0.3#./.yarn/patches/eth-rpc-errors-npm-4.0.3-d154190eb6.patch",
"eth-rpc-errors@^4.0.2": "patch:eth-rpc-errors@npm%3A4.0.3#./.yarn/patches/eth-rpc-errors-npm-4.0.3-d154190eb6.patch",
"nativewind@^2.0.11": "patch:nativewind@npm%3A2.0.11#./.yarn/patches/nativewind-npm-2.0.11-dc2ccf9fb5.patch",
"react-native-fs@^2.20.0": "patch:react-native-fs@npm%3A2.20.0#./.yarn/patches/react-native-fs-npm-2.20.0-a38fe24051.patch",
"[email protected]": "patch:react-native-keychain@npm%3A8.0.0#./.yarn/patches/react-native-keychain-npm-8.0.0-22c1f5c020.patch",
"react-native-mmkv@^2.12.2": "patch:react-native-mmkv@npm%3A2.12.2#./.yarn/patches/react-native-mmkv-npm-2.12.2-9efa7abf70.patch",
"[email protected]": "patch:react-native-webview@npm%3A13.10.5#./.yarn/patches/react-native-webview-npm-13.10.5-714eb41569.patch",
"[email protected]": "patch:react-use@npm%3A17.4.2#./.yarn/patches/react-use-npm-17.4.2-d0e8220eaa.patch",
"[email protected]": "patch:zeego@npm%3A1.10.0#./.yarn/patches/zeego-npm-1.10.0-1958a3ea63.patch",
"@keystonehq/bc-ur-registry@^0.6.0": "patch:@keystonehq/bc-ur-registry@npm%3A0.6.4#./.yarn/patches/@keystonehq-bc-ur-registry-npm-0.6.4-7922ce4e4c.patch",
"@keystonehq/bc-ur-registry@^0.5.0-alpha.4": "patch:@keystonehq/bc-ur-registry@npm%3A0.6.4#./.yarn/patches/@keystonehq-bc-ur-registry-npm-0.6.4-7922ce4e4c.patch",
"@ledgerhq/hw-app-eth@^6.40.0": "patch:@ledgerhq/hw-app-eth@npm%3A6.40.2#./.yarn/patches/@ledgerhq-hw-app-eth-npm-6.40.2-ffdc4add8c.patch",
"@ledgerhq/hw-transport@^6.31.4": "patch:@ledgerhq/hw-transport@npm%3A6.31.4#./.yarn/patches/@ledgerhq-hw-transport-npm-6.31.4-b439e9a1f8.patch",
"@scure/bip39@^1.3.0": "patch:@scure/bip39@npm%3A1.3.0#./.yarn/patches/@scure-bip39-npm-1.3.0-1d74c5c469.patch",
"@scure/[email protected]": "patch:@scure/bip39@npm%3A1.3.0#./.yarn/patches/@scure-bip39-npm-1.3.0-1d74c5c469.patch",
"@scure/[email protected]": "patch:@scure/bip39@npm%3A1.3.0#./.yarn/patches/@scure-bip39-npm-1.3.0-1d74c5c469.patch",
"@scure/bip39@^1.2.1": "patch:@scure/bip39@npm%3A1.3.0#./.yarn/patches/@scure-bip39-npm-1.3.0-1d74c5c469.patch",
"@scure/[email protected]": "patch:@scure/bip39@npm%3A1.3.0#./.yarn/patches/@scure-bip39-npm-1.3.0-1d74c5c469.patch"
}
}
8 changes: 4 additions & 4 deletions packages/service-keyring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
"changelog:validate": "../../scripts/validate-changelog.sh @rabby-wallet/service-keyring",
"cov:report": "open ./coverage/index.html",
"publish:preview": "yarn npm publish --tag preview",
"tool:decrypt": "yarn ../../ build:deps && node ./scripts/decyrptKeyringState.js",
"test": "jest --reporters=jest-silent-reporter",
"test:clean": "jest --clearCache",
"test:verbose": "jest --verbose",
"test:watch": "jest --watch"
"test:watch": "jest --watch",
"tool:decrypt": "yarn ../../ build:deps && node ./scripts/decyrptKeyringState.js"
},
"dependencies": {
"@metamask/obs-store": "^9.0.0",
"@rabby-wallet/base-utils": "workspace:^",
"@rabby-wallet/eth-keyring-watch": "^0.1.0",
"@rabby-wallet/keyring-utils": "workspace:^",
"@rabby-wallet/service-address": "workspace:^",
"@scure/bip39": "^1.3.0",
"@types/readable-stream": "^4.0.10",
"ethereumjs-util": "^7.1.5",
"loglevel": "^1.8.1"
"loglevel": "^1.8.1",
"react-native-quick-bip39": "0.0.9"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.3",
Expand Down
1 change: 1 addition & 0 deletions packages/service-keyring/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'react-native-quick-bip39';
7 changes: 3 additions & 4 deletions packages/service-keyring/src/keyringService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import type {
KeyringTypeName,
} from '@rabby-wallet/keyring-utils';
import type { ContactBookService } from '@rabby-wallet/service-address';
import * as bip39 from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import * as ethUtil from 'ethereumjs-util';
import log from 'loglevel';
import * as bip39 from 'react-native-quick-bip39';

import type { KeyringClassType, KeyringInstance } from './types';
import { keyringSdks } from './types';
Expand Down Expand Up @@ -1020,7 +1019,7 @@ export class KeyringService extends RNEventEmitter {
* Mnemonic Phrase
*/
generateMnemonic(): string {
return bip39.generateMnemonic(wordlist);
return bip39.generateMnemonic();
}

async generatePreMnemonic(options?: {
Expand Down Expand Up @@ -1067,7 +1066,7 @@ export class KeyringService extends RNEventEmitter {
* @returns {Promise<Object>} A Promise that resolves to the state.
*/
createKeyringWithMnemonics(seed: string): Promise<any> {
if (!bip39.validateMnemonic(seed, wordlist)) {
if (!bip39.validateMnemonic(seed, bip39.wordlists.english)) {
return Promise.reject(new Error('background.error.invalidMnemonic'));
}

Expand Down
Loading

0 comments on commit 63c0337

Please sign in to comment.