Skip to content

Commit 6473817

Browse files
chore: removed extra wallet creation + added patch to use native crypto in bip39
1 parent 2ff9351 commit 6473817

File tree

7 files changed

+100
-23
lines changed

7 files changed

+100
-23
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
diff --git a/src/index.js b/src/index.js
2+
index b83863f4b3d09df1159eb2c05d43e20a05d55767..926895a1f697905d48bf32f32fc538de3ce1b252 100644
3+
--- a/src/index.js
4+
+++ b/src/index.js
5+
@@ -38,20 +38,68 @@ function salt(password) {
6+
function mnemonicToSeedSync(mnemonic, password) {
7+
const mnemonicBuffer = Uint8Array.from(Buffer.from(normalize(mnemonic), 'utf8'));
8+
const saltBuffer = Uint8Array.from(Buffer.from(salt(normalize(password)), 'utf8'));
9+
- const res = pbkdf2_1.pbkdf2(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
10+
- c: 2048,
11+
- dkLen: 64,
12+
- });
13+
- return Buffer.from(res);
14+
+
15+
+ // Use react-native-quick-crypto's native PBKDF2
16+
+ try {
17+
+ var crypto = require("crypto");
18+
+ const res = crypto.pbkdf2Sync(
19+
+ mnemonicBuffer,
20+
+ saltBuffer,
21+
+ 2048, // iterations
22+
+ 64, // key length
23+
+ 'sha512'
24+
+ );
25+
+ return Buffer.from(res);
26+
+ } catch (e) {
27+
+ console.warn('Native pbkdf2Sync failed, falling back to JS implementation', e);
28+
+ // Fallback to original implementation
29+
+ const res = pbkdf2_1.pbkdf2(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
30+
+ c: 2048,
31+
+ dkLen: 64,
32+
+ });
33+
+ return Buffer.from(res);
34+
+ }
35+
}
36+
exports.mnemonicToSeedSync = mnemonicToSeedSync;
37+
+
38+
function mnemonicToSeed(mnemonic, password) {
39+
const mnemonicBuffer = Uint8Array.from(Buffer.from(normalize(mnemonic), 'utf8'));
40+
const saltBuffer = Uint8Array.from(Buffer.from(salt(normalize(password)), 'utf8'));
41+
- return pbkdf2_1.pbkdf2Async(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
42+
- c: 2048,
43+
- dkLen: 64,
44+
- }).then((res) => Buffer.from(res));
45+
+
46+
+ // Use react-native-quick-crypto's native PBKDF2
47+
+ try {
48+
+ var crypto = require("crypto");
49+
+ return new Promise((resolve, reject) => {
50+
+ crypto.pbkdf2(
51+
+ mnemonicBuffer,
52+
+ saltBuffer,
53+
+ 2048, // iterations
54+
+ 64, // key length
55+
+ 'sha512',
56+
+ (err, res) => {
57+
+ if (err) {
58+
+ reject(err);
59+
+ } else {
60+
+ resolve(Buffer.from(res));
61+
+ }
62+
+ }
63+
+ );
64+
+ }).catch((err) => {
65+
+ console.warn('Native pbkdf2 failed, falling back to JS implementation', err);
66+
+ // Fallback to original implementation
67+
+ return pbkdf2_1.pbkdf2Async(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
68+
+ c: 2048,
69+
+ dkLen: 64,
70+
+ }).then((res) => Buffer.from(res));
71+
+ });
72+
+ } catch (e) {
73+
+ console.warn('Native pbkdf2 not available, using JS implementation', e);
74+
+ // Fallback to original implementation
75+
+ return pbkdf2_1.pbkdf2Async(sha512_1.sha512, mnemonicBuffer, saltBuffer, {
76+
+ c: 2048,
77+
+ dkLen: 64,
78+
+ }).then((res) => Buffer.from(res));
79+
+ }
80+
}
81+
exports.mnemonicToSeed = mnemonicToSeed;
82+
function mnemonicToEntropy(mnemonic, wordlist) {

wallets/rn_cli_wallet/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import 'react-native-gesture-handler';
22
import '@walletconnect/react-native-compat';
33
import {AppRegistry} from 'react-native';
44
import {name as appName} from './app.json';
5-
import crypto from 'react-native-quick-crypto';
5+
import crypto, { install } from 'react-native-quick-crypto';
66

7-
import App from './src/screens/App';
7+
install();
88

99
const polyfillDigest = async (algorithm, data) => {
1010
const algo = algorithm.replace('-', '').toLowerCase();
@@ -13,11 +13,10 @@ const polyfillDigest = async (algorithm, data) => {
1313
return hash.digest();
1414
};
1515

16-
// eslint-disable-next-line no-undef
17-
globalThis.crypto = crypto;
1816
// eslint-disable-next-line no-undef
1917
globalThis.crypto.subtle = {
2018
digest: polyfillDigest,
2119
};
2220

21+
import App from './src/screens/App';
2322
AppRegistry.registerComponent(appName, () => App);

wallets/rn_cli_wallet/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@
106106
"brace-expansion": "1.1.12",
107107
"@ton/[email protected]": "patch:@ton/crypto-primitives@npm%3A2.1.0#./.yarn/patches/@ton-crypto-primitives-npm-2.1.0-3d1ea62176.patch",
108108
"@ethersproject/[email protected]": "patch:@ethersproject/pbkdf2@npm%3A5.8.0#./.yarn/patches/@ethersproject-pbkdf2-npm-5.8.0-b720e81bcc.patch",
109-
"@ethersproject/pbkdf2@^5.8.0": "patch:@ethersproject/pbkdf2@npm%3A5.8.0#./.yarn/patches/@ethersproject-pbkdf2-npm-5.8.0-b720e81bcc.patch"
109+
"@ethersproject/pbkdf2@^5.8.0": "patch:@ethersproject/pbkdf2@npm%3A5.8.0#./.yarn/patches/@ethersproject-pbkdf2-npm-5.8.0-b720e81bcc.patch",
110+
"[email protected]": "patch:bip39@npm%3A3.1.0#./.yarn/patches/bip39-npm-3.1.0-03958ed434.patch"
110111
},
111112
"packageManager": "[email protected]"
112113
}

wallets/rn_cli_wallet/src/screens/Scan/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import styles from './styles';
1818
type Props = RootStackScreenProps<'Scan'>;
1919

2020
export default function Scan({navigation}: Props) {
21-
const device = useCameraDevice('back');
21+
const device = useCameraDevice('back', {physicalDevices: ['wide-angle-camera']});
2222
const [showCamera, setShowCamera] = useState(false);
2323

2424
// 2. Only activate Camera when the app is focused and this screen is currently opened

wallets/rn_cli_wallet/src/utils/EIP155WalletUtil.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,27 @@ export let eip155Wallets: Record<string, EIP155Lib>;
77
export let eip155Addresses: string[];
88

99
let address1: string;
10-
let address2: string;
10+
// let address2: string;
1111

1212
/**
1313
* Utilities
1414
*/
1515
export async function createOrRestoreEIP155Wallet() {
1616
const mnemonic1 = await storage.getItem('EIP155_MNEMONIC_1');
17-
const mnemonic2 = await storage.getItem('EIP155_MNEMONIC_2');
1817

19-
if (mnemonic1 && mnemonic2) {
18+
if (mnemonic1) {
2019
wallet1 = EIP155Lib.init({mnemonic: mnemonic1});
21-
wallet2 = EIP155Lib.init({mnemonic: mnemonic2});
2220
} else {
2321
wallet1 = EIP155Lib.init({});
24-
wallet2 = EIP155Lib.init({});
2522

2623
// Don't store mnemonic in local storage in a production project!
2724
storage.setItem('EIP155_MNEMONIC_1', wallet1.getMnemonic());
28-
storage.setItem('EIP155_MNEMONIC_2', wallet2.getMnemonic());
2925
}
3026

3127
address1 = wallet1.getAddress();
32-
address2 = wallet2.getAddress();
3328

3429
eip155Wallets = {
3530
[address1]: wallet1,
36-
[address2]: wallet2,
3731
};
3832
eip155Addresses = Object.keys(eip155Wallets);
3933

wallets/rn_cli_wallet/src/utils/TonWalletUtil.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,25 @@ export let tonWallets: Record<string, TonLib>
77
export let tonAddresses: string[]
88

99
let address1: string
10-
let address2: string
1110

1211
/**
1312
* Utilities
1413
*/
1514
export async function createOrRestoreTonWallet() {
1615
const secretKey1 = await storage.getItem('TON_SECRET_KEY_1')
17-
const secretKey2 = await storage.getItem('TON_SECRET_KEY_2')
1816

19-
if (secretKey1 && secretKey2) {
17+
if (secretKey1) {
2018
wallet1 = await TonLib.init({ secretKey: secretKey1 })
21-
wallet2 = await TonLib.init({ secretKey: secretKey2 })
2219
} else {
2320
wallet1 = await TonLib.init({})
24-
wallet2 = await TonLib.init({})
25-
2621
// Don't store secretKey in local storage in a production project!
2722
await storage.setItem('TON_SECRET_KEY_1', wallet1.getSecretKey())
28-
await storage.setItem('TON_SECRET_KEY_2', wallet2.getSecretKey())
2923
}
3024

3125
address1 = await wallet1.getAddress()
32-
address2 = await wallet2.getAddress()
3326

3427
tonWallets = {
3528
[address1]: wallet1,
36-
[address2]: wallet2
3729
}
3830
tonAddresses = Object.keys(tonWallets)
3931

wallets/rn_cli_wallet/yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4846,6 +4846,15 @@ __metadata:
48464846
languageName: node
48474847
linkType: hard
48484848

4849+
"bip39@patch:bip39@npm%3A3.1.0#./.yarn/patches/bip39-npm-3.1.0-03958ed434.patch::locator=RNWeb3Wallet%40workspace%3A.":
4850+
version: 3.1.0
4851+
resolution: "bip39@patch:bip39@npm%3A3.1.0#./.yarn/patches/bip39-npm-3.1.0-03958ed434.patch::version=3.1.0&hash=6ad58d&locator=RNWeb3Wallet%40workspace%3A."
4852+
dependencies:
4853+
"@noble/hashes": ^1.2.0
4854+
checksum: 482916c2a55755aff5e354042932c71b1f37cbe7c8ff870306ea2dc185b932db3b2e775e61175f68e3263f532fcbf8588ee7fe4422b60f55b6b208b6527b51a5
4855+
languageName: node
4856+
linkType: hard
4857+
48494858
"bl@npm:^4.0.3, bl@npm:^4.1.0":
48504859
version: 4.1.0
48514860
resolution: "bl@npm:4.1.0"

0 commit comments

Comments
 (0)