-
Notifications
You must be signed in to change notification settings - Fork 3
/
WalletImportHelper.js
53 lines (40 loc) · 1.45 KB
/
WalletImportHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import "react-native-get-random-values"
// Import the the ethers shims (**BEFORE** ethers)
import "@ethersproject/shims"
// Import the ethers library
import { ethers } from "ethers";
import * as SecureStore from 'expo-secure-store';
import { MNEMONIC_KEY } from "./constants";
const { getAccountIndexesArray, setAccountIndexesArray, getNextAccountIndex} = require('./AccountHelper');
const { getItem, setItem } = require('./Storage');
storeWallet = async (privateKey, mnemonicPhrase, mnemonicPathIndex, mnemonicPassword) => {
const nextAccountIndex = await getNextAccountIndex();
const key = MNEMONIC_KEY + nextAccountIndex;
const currentWallet = await SecureStore.getItemAsync(key);
if (currentWallet) {
throw new Error("Do not override existing wallet, this should not happen btw.");
}
let mnemonic = {};
if (mnemonicPhrase) {
mnemonic.phrase = mnemonicPhrase;
mnemonic.pathIndex = mnemonicPathIndex ? mnemonicPathIndex : 0;
mnemonic.password = mnemonicPassword ? mnemonicPassword : "";
}
const secrets = {
"privateKey": privateKey,
"mnemonic": mnemonic
};
try {
await SecureStore.setItemAsync(key, JSON.stringify(secrets));
}
finally {
await setAccountIndexesArray(nextAccountIndex);
}
return nextAccountIndex;
}
const generateWallet = () => {
return new ethers.Wallet.createRandom();
}
module.exports = {
generateWallet, storeWallet
}