Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show First 20 Lines • Show All 373 Lines • ▼ Show 20 Lines | const getWallet = async () => { | ||||
} | } | ||||
return wallet; | return wallet; | ||||
}; | }; | ||||
const migrateLegacyWallet = async (BCH, wallet) => { | const migrateLegacyWallet = async (BCH, wallet) => { | ||||
console.log(`migrateLegacyWallet`); | console.log(`migrateLegacyWallet`); | ||||
console.log(`legacyWallet`, wallet); | console.log(`legacyWallet`, wallet); | ||||
const mnemonic = wallet.mnemonic; | const mnemonic = wallet.mnemonic; | ||||
const rootSeedBuffer = await BCH.Mnemonic.toSeed(mnemonic); | const rootSeedBuffer = await bip39.mnemonicToSeed(mnemonic, ''); | ||||
const masterHDNode = Bitcoin.HDNode.fromSeedBuffer( | const masterHDNode = Bitcoin.HDNode.fromSeedBuffer( | ||||
rootSeedBuffer, | rootSeedBuffer, | ||||
coininfo.bitcoincash.main.toBitcoinJS(), | coininfo.bitcoincash.main.toBitcoinJS(), | ||||
); | ); | ||||
// temporary comparison | |||||
const localRootSeedBufffer = await bip39.mnemonicToSeed(mnemonic, ''); | |||||
if ( | |||||
JSON.stringify(rootSeedBuffer) === | |||||
JSON.stringify(localRootSeedBufffer) | |||||
) { | |||||
console.log('mnemonicToSeed() output match'); | |||||
} | |||||
const Path245 = await deriveAccount(BCH, { | const Path245 = await deriveAccount(BCH, { | ||||
masterHDNode, | masterHDNode, | ||||
path: "m/44'/245'/0'/0/0", | path: "m/44'/245'/0'/0/0", | ||||
}); | }); | ||||
const Path145 = await deriveAccount(BCH, { | const Path145 = await deriveAccount(BCH, { | ||||
masterHDNode, | masterHDNode, | ||||
path: "m/44'/145'/0'/0/0", | path: "m/44'/145'/0'/0/0", | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | const useWallet = () => { | ||||
}; | }; | ||||
const getWalletDetails = async wallet => { | const getWalletDetails = async wallet => { | ||||
if (!wallet) { | if (!wallet) { | ||||
return false; | return false; | ||||
} | } | ||||
// Since this info is in localforage now, only get the var | // Since this info is in localforage now, only get the var | ||||
const mnemonic = wallet.mnemonic; | const mnemonic = wallet.mnemonic; | ||||
const rootSeedBuffer = await BCH.Mnemonic.toSeed(mnemonic); | const rootSeedBuffer = await bip39.mnemonicToSeed(mnemonic, ''); | ||||
const masterHDNode = Bitcoin.HDNode.fromSeedBuffer( | const masterHDNode = Bitcoin.HDNode.fromSeedBuffer( | ||||
rootSeedBuffer, | rootSeedBuffer, | ||||
coininfo.bitcoincash.main.toBitcoinJS(), | coininfo.bitcoincash.main.toBitcoinJS(), | ||||
); | ); | ||||
// temporary comparison | |||||
const localRootSeedBufffer = await bip39.mnemonicToSeed(mnemonic, ''); | |||||
if ( | |||||
JSON.stringify(rootSeedBuffer) === | |||||
JSON.stringify(localRootSeedBufffer) | |||||
) { | |||||
console.log('mnemonicToSeed() output match'); | |||||
} | |||||
const Path245 = await deriveAccount(BCH, { | const Path245 = await deriveAccount(BCH, { | ||||
masterHDNode, | masterHDNode, | ||||
path: "m/44'/245'/0'/0/0", | path: "m/44'/245'/0'/0/0", | ||||
}); | }); | ||||
const Path145 = await deriveAccount(BCH, { | const Path145 = await deriveAccount(BCH, { | ||||
masterHDNode, | masterHDNode, | ||||
path: "m/44'/145'/0'/0/0", | path: "m/44'/145'/0'/0/0", | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 288 Lines • ▼ Show 20 Lines | const deleteWallet = async walletToBeDeleted => { | ||||
} | } | ||||
return true; | return true; | ||||
}; | }; | ||||
const addNewSavedWallet = async importMnemonic => { | const addNewSavedWallet = async importMnemonic => { | ||||
// Add a new wallet to savedWallets from importMnemonic or just new wallet | // Add a new wallet to savedWallets from importMnemonic or just new wallet | ||||
const lang = 'english'; | const lang = 'english'; | ||||
if ( | |||||
JSON.stringify(bip39.wordlists) === | |||||
JSON.stringify(BCH.Mnemonic.wordLists()) | |||||
) { | |||||
console.log('bip39 wordlists matches BCH.Mnemonic wordlists'); | |||||
} | |||||
// create 128 bit BIP39 mnemonic | // create 128 bit BIP39 mnemonic | ||||
const Bip39128BitMnemonic = importMnemonic | const Bip39128BitMnemonic = importMnemonic | ||||
? importMnemonic | ? importMnemonic | ||||
: BCH.Mnemonic.generate(128, BCH.Mnemonic.wordLists()[lang]); | : bip39.generateMnemonic(128, randomBytes, bip39.wordlists[lang]); | ||||
// temporary comparison | |||||
// reference: https://github.com/Permissionless-Software-Foundation/bch-js/blob/62e56c832b35731880fe448269818b853c76dd80/src/mnemonic.js#L52 | |||||
// Note: by virtue of randombytes being used by BCH-JS the generated mnemonics will never match | |||||
const localBip39128BitMnemonic = bip39.generateMnemonic( | |||||
128, | |||||
randomBytes, | |||||
BCH.Mnemonic.wordLists()[lang], | |||||
); | |||||
console.log('localBip39128BitMnemonic: ' + localBip39128BitMnemonic); | |||||
const newSavedWallet = await getWalletDetails({ | const newSavedWallet = await getWalletDetails({ | ||||
mnemonic: Bip39128BitMnemonic.toString(), | mnemonic: Bip39128BitMnemonic.toString(), | ||||
}); | }); | ||||
// Get saved wallets | // Get saved wallets | ||||
let savedWallets; | let savedWallets; | ||||
try { | try { | ||||
savedWallets = await localforage.getItem('savedWallets'); | savedWallets = await localforage.getItem('savedWallets'); | ||||
Show All 35 Lines | const addNewSavedWallet = async importMnemonic => { | ||||
console.log(err); | console.log(err); | ||||
} | } | ||||
return true; | return true; | ||||
}; | }; | ||||
const createWallet = async importMnemonic => { | const createWallet = async importMnemonic => { | ||||
const lang = 'english'; | const lang = 'english'; | ||||
if ( | |||||
JSON.stringify(bip39.wordlists) === | |||||
JSON.stringify(BCH.Mnemonic.wordLists()) | |||||
) { | |||||
console.log('bip39 wordlists matches BCH.Mnemonic wordlists'); | |||||
} | |||||
// create 128 bit BIP39 mnemonic | // create 128 bit BIP39 mnemonic | ||||
const Bip39128BitMnemonic = importMnemonic | const Bip39128BitMnemonic = importMnemonic | ||||
? importMnemonic | ? importMnemonic | ||||
: BCH.Mnemonic.generate(128, BCH.Mnemonic.wordLists()[lang]); | : bip39.generateMnemonic(128, randomBytes, bip39.wordlists[lang]); | ||||
// temporary comparison | |||||
// reference: https://github.com/Permissionless-Software-Foundation/bch-js/blob/62e56c832b35731880fe448269818b853c76dd80/src/mnemonic.js#L52 | |||||
// Note: by virtue of randombytes being used by BCH-JS the generated mnemonics will never match | |||||
const localBip39128BitMnemonic = bip39.generateMnemonic( | |||||
128, | |||||
randomBytes, | |||||
BCH.Mnemonic.wordLists()[lang], | |||||
); | |||||
console.log('localBip39128BitMnemonic: ' + localBip39128BitMnemonic); | |||||
const wallet = await getWalletDetails({ | const wallet = await getWalletDetails({ | ||||
mnemonic: Bip39128BitMnemonic.toString(), | mnemonic: Bip39128BitMnemonic.toString(), | ||||
}); | }); | ||||
try { | try { | ||||
await localforage.setItem('wallet', wallet); | await localforage.setItem('wallet', wallet); | ||||
} catch (err) { | } catch (err) { | ||||
Show All 9 Lines | const createWallet = async importMnemonic => { | ||||
console.log( | console.log( | ||||
`Error setting wallet to savedWallets indexedDb in createWallet()`, | `Error setting wallet to savedWallets indexedDb in createWallet()`, | ||||
); | ); | ||||
console.log(err); | console.log(err); | ||||
} | } | ||||
return wallet; | return wallet; | ||||
}; | }; | ||||
const validateMnemonic = ( | const validateMnemonic = (mnemonic, wordlist = bip39.wordlists.english) => { | ||||
mnemonic, | |||||
wordlist = BCH.Mnemonic.wordLists().english, | |||||
) => { | |||||
let mnemonicTestOutput; | let mnemonicTestOutput; | ||||
// see function input param | |||||
if ( | |||||
JSON.stringify(bip39.wordlists.english) === | |||||
JSON.stringify(BCH.Mnemonic.wordLists().english) | |||||
) { | |||||
console.log( | |||||
'bip39 english wordlist matches BCH.Mnemonic english wordlist', | |||||
); | |||||
} | |||||
try { | try { | ||||
mnemonicTestOutput = BCH.Mnemonic.validate(mnemonic, wordlist); | mnemonicTestOutput = validateMnemonicWordList(mnemonic, wordlist); | ||||
const localMnemonicTestOutput = validateMnemonicWordList( | |||||
mnemonic, | |||||
wordlist, | |||||
); | |||||
if (mnemonicTestOutput === localMnemonicTestOutput) { | |||||
console.log( | |||||
'mnemonicTestOutput matches localMnemonicTestOutput', | |||||
); | |||||
} | |||||
if (mnemonicTestOutput === 'Valid mnemonic') { | if (mnemonicTestOutput === 'Valid mnemonic') { | ||||
return true; | return true; | ||||
} else { | } else { | ||||
return false; | return false; | ||||
} | } | ||||
} catch (err) { | } catch (err) { | ||||
console.log(err); | console.log(err); | ||||
▲ Show 20 Lines • Show All 640 Lines • Show Last 20 Lines |