From 88247bc40eedd4997540ff59ef0576fccdfee01c Mon Sep 17 00:00:00 2001 From: Philipp Walter Date: Mon, 2 Dec 2024 16:58:57 +0100 Subject: [PATCH] perf(wallet): switch to native bip39 (#2382) --- metro.config.js | 2 ++ package.json | 2 +- src/utils/lightning/index.ts | 12 ------------ src/utils/wallet/index.ts | 5 +++-- yarn.lock | 28 +++++++++++++++++++++++++++- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/metro.config.js b/metro.config.js index d9bb65bb2..e422e503e 100644 --- a/metro.config.js +++ b/metro.config.js @@ -21,12 +21,14 @@ const config = { assetExts: assetExts.filter((ext) => ext !== 'svg'), sourceExts: [...sourceExts, 'svg'], extraNodeModules: { + bip39: path.resolve(__dirname, './node_modules/react-native-quick-bip39'), buffer: path.resolve(__dirname, './node_modules/@craftzdog/react-native-buffer'), crypto: path.resolve(__dirname, './node_modules/react-native-quick-crypto'), stream: path.resolve(__dirname, './node_modules/readable-stream'), 'sodium-universal': path.resolve(__dirname, './node_modules/sodium-react-native-direct'), }, blacklistRE: exclusionList([ + /\/node_modules\/bip39\/.*/, /\/node_modules\/sodium-universal\/.*/, /\/android\/build\/*/, ]), diff --git a/package.json b/package.json index a9f916402..7ab697c96 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,6 @@ "beignet": "0.0.48", "bip21": "2.0.3", "bip32": "4.0.0", - "bip39": "3.1.0", "bitcoin-address-validation": "2.2.3", "bitcoin-units": "0.3.0", "bitcoinjs-lib": "6.1.5", @@ -102,6 +101,7 @@ "react-native-qrcode-svg": "6.3.0", "react-native-quick-actions": "0.3.13", "react-native-quick-base64": "2.1.2", + "react-native-quick-bip39": "0.0.9", "react-native-quick-crypto": "0.7.1", "react-native-reanimated": "3.15.0", "react-native-reanimated-carousel": "3.5.1", diff --git a/src/utils/lightning/index.ts b/src/utils/lightning/index.ts index 2b51fab36..eab000805 100644 --- a/src/utils/lightning/index.ts +++ b/src/utils/lightning/index.ts @@ -1,6 +1,5 @@ import { EmitterSubscription } from 'react-native'; import Keychain from 'react-native-keychain'; -import * as bitcoin from 'bitcoinjs-lib'; import ecc from '@bitcoinerlab/secp256k1'; import RNFS from 'react-native-fs'; import { err, ok, Result } from '@synonymdev/result'; @@ -1015,17 +1014,6 @@ export const getDefaultLdkAccount = async ({ }; }; -/** - * Get sha256 hash of a given string. - * @param {string} str - * @returns {string} - */ -export const getSha256 = (str: string): string => { - const buffer = Buffer.from(str, 'utf8'); - const hash = bitcoin.crypto.sha256(buffer); - return hash.toString('hex'); -}; - /** * Returns the last known block header when Electrum is connected. * @returns {Promise} diff --git a/src/utils/wallet/index.ts b/src/utils/wallet/index.ts index 042e5bd47..35cc5e9e1 100644 --- a/src/utils/wallet/index.ts +++ b/src/utils/wallet/index.ts @@ -382,15 +382,16 @@ export const seedHash = (seed: Buffer): string => { }; export const ldkSeed = async ( - mnumonic: string, + mnemonic: string, bip39Passphrase: string, selectedNetwork: EAvailableNetwork = getSelectedNetwork(), ): Promise => { - const mnemonicSeed = await bip39.mnemonicToSeed(mnumonic, bip39Passphrase); + const mnemonicSeed = await bip39.mnemonicToSeed(mnemonic, bip39Passphrase); const network = networks[selectedNetwork]; const root = bip32.fromSeed(mnemonicSeed, network); return root.privateKey!.toString('hex'); }; + export const keyDerivationAccountTypes: { onchain: TKeyDerivationAccount; } = { diff --git a/yarn.lock b/yarn.lock index c9232c85b..4dcd6aa9e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6626,7 +6626,6 @@ __metadata: beignet: 0.0.48 bip21: 2.0.3 bip32: 4.0.0 - bip39: 3.1.0 bitcoin-address-validation: 2.2.3 bitcoin-json-rpc: ^1.3.3 bitcoin-units: 0.3.0 @@ -6676,6 +6675,7 @@ __metadata: react-native-qrcode-svg: 6.3.0 react-native-quick-actions: 0.3.13 react-native-quick-base64: 2.1.2 + react-native-quick-bip39: 0.0.9 react-native-quick-crypto: 0.7.1 react-native-reanimated: 3.15.0 react-native-reanimated-carousel: 3.5.1 @@ -14461,6 +14461,16 @@ __metadata: languageName: node linkType: hard +"react-native-quick-bip39@npm:0.0.9": + version: 0.0.9 + resolution: "react-native-quick-bip39@npm:0.0.9" + dependencies: + "@craftzdog/react-native-buffer": ^6.0.5 + react-native-quick-crypto: ^0.7.7 + checksum: 1beb61bcf124fda8d803baaee18b9ca3f8aa6cf14dc18ed168c4c5e7f75095dc0edd619fd72049b40e7456d07cd90ee611b4e2071dcbeb1bcc64a2260c47727f + languageName: node + linkType: hard + "react-native-quick-crypto@npm:0.7.1": version: 0.7.1 resolution: "react-native-quick-crypto@npm:0.7.1" @@ -14474,6 +14484,22 @@ __metadata: languageName: node linkType: hard +"react-native-quick-crypto@npm:^0.7.7": + version: 0.7.7 + resolution: "react-native-quick-crypto@npm:0.7.7" + dependencies: + "@craftzdog/react-native-buffer": ^6.0.5 + events: ^3.3.0 + readable-stream: ^4.5.2 + string_decoder: ^1.3.0 + util: ^0.12.5 + peerDependencies: + react: "*" + react-native: "*" + checksum: 92974b2dd3113bf6021699c43e462abbbffea17021ce57f637526f5d387de1c89834467bc3b30fba59887854f7dcbb9c42a97fda88fe31cf1acb80b06a82a287 + languageName: node + linkType: hard + "react-native-reanimated-carousel@npm:3.5.1": version: 3.5.1 resolution: "react-native-reanimated-carousel@npm:3.5.1"