Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show First 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | const deriveAccount = async (BCH, { masterHDNode, path }) => { | ||||
cashAddress, | cashAddress, | ||||
slpAddress, | slpAddress, | ||||
fundingWif: BCH.HDNode.toWIF(node), | fundingWif: BCH.HDNode.toWIF(node), | ||||
fundingAddress: BCH.SLP.Address.toSLPAddress(cashAddress), | fundingAddress: BCH.SLP.Address.toSLPAddress(cashAddress), | ||||
legacyAddress: BCH.SLP.Address.toLegacyAddress(cashAddress), | legacyAddress: BCH.SLP.Address.toLegacyAddress(cashAddress), | ||||
}; | }; | ||||
}; | }; | ||||
const loadWalletFromStorageOnStartup = async () => { | |||||
// get wallet object from localforage | |||||
const 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 | |||||
const liveWalletState = loadStoredWallet(wallet.state); | |||||
wallet.state = liveWalletState; | |||||
return wallet; | |||||
} | |||||
// If no wallet in local storage or no valid wallet in local storage, return blank wallet object | |||||
// This is what app used for initial wallet state before all items were stored in localforage | |||||
return { | |||||
balances: {}, | |||||
hydratedUtxoDetails: {}, | |||||
tokens: [], | |||||
slpBalancesAndUtxos: {}, | |||||
parsedTxHistory: [], | |||||
utxos: [], | |||||
}; | |||||
}; | |||||
const haveUtxosChanged = (wallet, utxos, previousUtxos) => { | const haveUtxosChanged = (wallet, utxos, previousUtxos) => { | ||||
// Relevant points for this array comparing exercise | // Relevant points for this array comparing exercise | ||||
// https://stackoverflow.com/questions/13757109/triple-equal-signs-return-false-for-arrays-in-javascript-why | // https://stackoverflow.com/questions/13757109/triple-equal-signs-return-false-for-arrays-in-javascript-why | ||||
// https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript | // https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript | ||||
// If this is initial state | // If this is initial state | ||||
if (utxos === null) { | if (utxos === null) { | ||||
// Then make sure to get slpBalancesAndUtxos | // Then make sure to get slpBalancesAndUtxos | ||||
▲ Show 20 Lines • Show All 429 Lines • ▼ Show 20 Lines | */ | ||||
await localforage.setItem('wallet', walletToActivate); | await localforage.setItem('wallet', walletToActivate); | ||||
} catch (err) { | } catch (err) { | ||||
console.log( | console.log( | ||||
`Error in localforage.setItem("wallet", walletToActivate) in activateWallet()`, | `Error in localforage.setItem("wallet", walletToActivate) in activateWallet()`, | ||||
); | ); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
// Make sure stored wallet is in correct format to be used as live wallet | |||||
if (isValidStoredWallet(walletToActivate)) { | |||||
// Convert all the token balance figures to big numbers | |||||
const liveWalletState = loadStoredWallet(walletToActivate.state); | |||||
walletToActivate.state = liveWalletState; | |||||
} | |||||
return walletToActivate; | return walletToActivate; | ||||
}; | }; | ||||
const renameWallet = async (oldName, newName) => { | const renameWallet = async (oldName, newName) => { | ||||
// Load savedWallets | // Load savedWallets | ||||
let savedWallets; | let savedWallets; | ||||
try { | try { | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | ) => { | ||||
} | } | ||||
} catch (err) { | } catch (err) { | ||||
console.log(err); | console.log(err); | ||||
return false; | return false; | ||||
} | } | ||||
}; | }; | ||||
const handleUpdateWallet = async setWallet => { | const handleUpdateWallet = async setWallet => { | ||||
const wallet = await getWallet(); | const wallet = await loadWalletFromStorageOnStartup(); | ||||
setWallet(wallet); | setWallet(wallet); | ||||
setLoading(false); | |||||
}; | }; | ||||
// Parse for incoming BCH transactions | // Parse for incoming BCH transactions | ||||
// Only notify if websocket is not connected | // Only notify if websocket is not connected | ||||
if ( | if ( | ||||
(ws === null || ws.readyState !== 1) && | (ws === null || ws.readyState !== 1) && | ||||
previousBalances && | previousBalances && | ||||
balances && | balances && | ||||
▲ Show 20 Lines • Show All 495 Lines • ▼ Show 20 Lines | return { | ||||
wallet: newWallet, | wallet: newWallet, | ||||
setWalletState, | setWalletState, | ||||
}).finally(() => setLoading(false)); | }).finally(() => setLoading(false)); | ||||
}, | }, | ||||
activateWallet: async walletToActivate => { | activateWallet: async walletToActivate => { | ||||
setLoading(true); | setLoading(true); | ||||
const newWallet = await activateWallet(walletToActivate); | const newWallet = await activateWallet(walletToActivate); | ||||
setWallet(newWallet); | setWallet(newWallet); | ||||
update({ | setLoading(false); | ||||
wallet: newWallet, | |||||
setWalletState, | |||||
}).finally(() => setLoading(false)); | |||||
}, | }, | ||||
addNewSavedWallet, | addNewSavedWallet, | ||||
renameWallet, | renameWallet, | ||||
deleteWallet, | deleteWallet, | ||||
}; | }; | ||||
}; | }; | ||||
export default useWallet; | export default useWallet; |