diff --git a/web/cashtab/src/hooks/useWallet.js b/web/cashtab/src/hooks/useWallet.js --- a/web/cashtab/src/hooks/useWallet.js +++ b/web/cashtab/src/hooks/useWallet.js @@ -114,10 +114,12 @@ const deriveAccount = async (BCH, { masterHDNode, path }) => { const node = BCH.HDNode.derivePath(masterHDNode, path); + const publicKey = BCH.HDNode.toPublicKey(node).toString('hex'); const cashAddress = BCH.HDNode.toCashAddress(node); const slpAddress = BCH.SLP.Address.toSLPAddress(cashAddress); return { + publicKey, cashAddress, slpAddress, fundingWif: BCH.HDNode.toWIF(node), @@ -128,7 +130,7 @@ const loadWalletFromStorageOnStartup = async setWallet => { // get wallet object from localforage - const wallet = await getWallet(); + let wallet = await getWallet(); // If wallet object in storage is valid, use it to set state on startup if (isValidStoredWallet(wallet)) { // Convert all the token balance figures to big numbers @@ -331,10 +333,24 @@ // 3 - null if error // If the wallet does not have Path1899, add it - if (existingWallet && !existingWallet.Path1899) { - console.log(`Wallet does not have Path1899`); - existingWallet = await migrateLegacyWallet(BCH, existingWallet); + // or each Path1899, Path145, Path245 does not have a public key, add them + if (existingWallet) { + if ( + !existingWallet.Path1899 || + !existingWallet.Path1899.publicKey || + !existingWallet.Path145.publicKey || + !existingWallet.Path245.publicKey + ) { + console.log( + `Wallet does not have Path1899 or does not have public key`, + ); + existingWallet = await migrateLegacyWallet( + BCH, + existingWallet, + ); + } } + // If not in localforage then existingWallet = false, check localstorage if (!existingWallet) { console.log(`no existing wallet, checking local storage`); @@ -384,11 +400,22 @@ } else { masterHDNode = BCH.HDNode.fromSeed(rootSeedBuffer, 'testnet'); } + + const Path245 = await deriveAccount(BCH, { + masterHDNode, + path: "m/44'/245'/0'/0/0", + }); + const Path145 = await deriveAccount(BCH, { + masterHDNode, + path: "m/44'/145'/0'/0/0", + }); const Path1899 = await deriveAccount(BCH, { masterHDNode, path: "m/44'/1899'/0'/0/0", }); + wallet.Path245 = Path245; + wallet.Path145 = Path145; wallet.Path1899 = Path1899; try { @@ -511,7 +538,7 @@ } /* When a legacy user runs cashtab.com/, their active wallet will be migrated to Path1899 by - the getWallet function + the getWallet function. getWallet function also makes sure that each Path has a public key Wallets in savedWallets are migrated when they are activated, in this function @@ -522,6 +549,9 @@ 2 - walletToActivate does not have Path1899 > Update walletToActivate with Path1899 before activation + + NOTE: since publicKey property is added later, + wallet without public key in Path1899 is also considered legacy and required migration. */ // Need to handle a similar situation with state @@ -534,9 +564,16 @@ if (savedWallets[i].name === currentlyActiveWallet.name) { walletInSavedWallets = true; // Check savedWallets for unmigrated currentlyActiveWallet - if (!savedWallets[i].Path1899) { + if ( + !savedWallets[i].Path1899 || + !savedWallets[i].Path1899.publicKey || + !savedWallets[i].Path145.publicKey || + !savedWallets[i].Path245.publicKey + ) { // Case 1, described above savedWallets[i].Path1899 = currentlyActiveWallet.Path1899; + savedWallets[i].Path145 = currentlyActiveWallet.Path145; + savedWallets[i].Path245 = currentlyActiveWallet.Path245; } /* @@ -572,12 +609,20 @@ } } // If wallet does not have Path1899, add it + // or each of the Path1800, Path145, Path245 does not have a public key, add them - if (!walletToActivate.Path1899) { + if ( + !walletToActivate.Path1899 || + !walletToActivate.Path1899.publicKey || + !walletToActivate.Path145.publicKey || + !walletToActivate.Path245.publicKey + ) { // Case 2, described above - console.log(`Case 2: Wallet to activate does not have Path1899`); console.log( - `Wallet to activate from SavedWallets does not have Path1899`, + `Case 2: Wallet to activate does not have Path1899 or does not have public key in each Path`, + ); + console.log( + `Wallet to activate from SavedWallets does not have Path1899 or does not have public key in each Path`, ); console.log(`walletToActivate`, walletToActivate); walletToActivate = await migrateLegacyWallet(BCH, walletToActivate);