Skip to content

Commit

Permalink
Reset xpubKeys cache when discovering accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBenc committed Jan 15, 2021
1 parent 69059f1 commit 57a2627
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
6 changes: 4 additions & 2 deletions app/frontend/wallet/account-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const AccountManager = ({config, cryptoProvider, blockchainExplorer}) => {
}

async function discoverAccounts() {
// TODO: remove rejected promises from pubkey cache
// remove rejected promises from pubkey cache
cryptoProvider.cleanXpubCache()
async function _discoverNewAccount(accountIndex: number) {
const newAccount = accounts[accountIndex] || discoverNewAccount()
const isAccountUsed = await newAccount.isAccountUsed()
Expand All @@ -47,7 +48,8 @@ const AccountManager = ({config, cryptoProvider, blockchainExplorer}) => {
}

async function exploreNewAccount() {
// TODO: remove rejected promises from pubkey cache
// remove rejected promises from pubkey cache
cryptoProvider.cleanXpubCache()
const newAccount = discoverNewAccount()
await addNewAccount(newAccount)
return newAccount
Expand Down
15 changes: 10 additions & 5 deletions app/frontend/wallet/byron/cardano-ledger-crypto-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ const CardanoLedgerCryptoProvider = async ({config}) => {
const isHwWallet = () => true
const getHwWalletName = () => 'Ledger'

const deriveXpub = CachedDeriveXpubFactory(derivationScheme, false, async (absDerivationPath) => {
const response = await ledger.getExtendedPublicKeys(absDerivationPath)
const xpubHex = response.publicKeyHex + response.chainCodeHex
return Buffer.from(xpubHex, 'hex')
})
const {deriveXpub, cleanXpubCache} = CachedDeriveXpubFactory(
derivationScheme,
false,
async (absDerivationPath) => {
const response = await ledger.getExtendedPublicKeys(absDerivationPath)
const xpubHex = response.publicKeyHex + response.chainCodeHex
return Buffer.from(xpubHex, 'hex')
}
)

function deriveHdNode(childIndex) {
throw NamedError('UnsupportedOperationError', {
Expand Down Expand Up @@ -137,6 +141,7 @@ const CardanoLedgerCryptoProvider = async ({config}) => {
getHwWalletName,
_sign: sign,
_deriveHdNode: deriveHdNode,
cleanXpubCache,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ const CardanoWalletSecretCryptoProvider = ({
return derivationScheme
}

const deriveXpub = CachedDeriveXpubFactory(derivationScheme, false, (derivationPaths) => {
return derivationPaths.map((path) => deriveHdNode(path).extendedPublicKey)
})
const {deriveXpub, cleanXpubCache} = CachedDeriveXpubFactory(
derivationScheme,
false,
(derivationPaths) => {
return derivationPaths.map((path) => deriveHdNode(path).extendedPublicKey)
}
)

function getHdPassphrase() {
return xpubToHdPassphrase(masterHdNode.extendedPublicKey)
Expand Down Expand Up @@ -124,6 +128,7 @@ const CardanoWalletSecretCryptoProvider = ({
_deriveChildHdNode: deriveChildHdNode,
_signTxGetStructured: signTxGetStructured,
network,
cleanXpubCache,
}
}

Expand Down
18 changes: 15 additions & 3 deletions app/frontend/wallet/helpers/CachedDeriveXpubFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const BYRON_V2_PATH = [HARDENED_THRESHOLD + 44, HARDENED_THRESHOLD + 1815, HARDE
type BIP32Path = number[]

function CachedDeriveXpubFactory(derivationScheme, shouldExportPubKeyBulk, deriveXpubFn) {
const derivedXpubs = {}
let derivedXpubs = {}

async function deriveXpub(absDerivationPath: BIP32Path) {
const memoKey = JSON.stringify(absDerivationPath)
Expand Down Expand Up @@ -68,12 +68,24 @@ function CachedDeriveXpubFactory(derivationScheme, shouldExportPubKeyBulk, deriv
const _derivedXpubs = {}
xPubBulk.forEach((xpub: Buffer, i: number) => {
const memoKey = JSON.stringify(paths[i])
_derivedXpubs[memoKey] = xpub
_derivedXpubs[memoKey] = Promise.resolve(xpub)
})
return _derivedXpubs
}

return deriveXpub
function cleanXpubCache() {
const _derivedXpubs = {}
Object.entries(derivedXpubs).map(([key, xpubPromise]: [string, Promise<Buffer>]) => {
xpubPromise
.then((xpub) => {
_derivedXpubs[key] = Promise.resolve(xpub)
})
.catch((e) => null)
})
derivedXpubs = _derivedXpubs
}

return {deriveXpub, cleanXpubCache}
}

export default CachedDeriveXpubFactory
3 changes: 2 additions & 1 deletion app/frontend/wallet/shelley/shelley-js-crypto-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ShelleyJsCryptoProvider = ({

const getDerivationScheme = () => derivationScheme

const deriveXpub = CachedDeriveXpubFactory(
const {deriveXpub, cleanXpubCache} = CachedDeriveXpubFactory(
derivationScheme,
config.shouldExportPubKeyBulk,
(derivationPaths) => {
Expand Down Expand Up @@ -146,6 +146,7 @@ const ShelleyJsCryptoProvider = ({
_deriveHdNodeFromRoot: deriveHdNode,
_deriveChildHdNode: deriveChildHdNode,
checkVersion,
cleanXpubCache,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const ShelleyLedgerCryptoProvider = async ({network, config, forceWebUsb}) => {
return response
}

const deriveXpub = CachedDeriveXpubFactory(
const {deriveXpub, cleanXpubCache} = CachedDeriveXpubFactory(
derivationScheme,
config.shouldExportPubKeyBulk,
async (derivationPaths) => {
Expand Down Expand Up @@ -295,6 +295,7 @@ const ShelleyLedgerCryptoProvider = async ({network, config, forceWebUsb}) => {
_sign: sign,
_deriveHdNode: deriveHdNode,
checkVersion,
cleanXpubCache,
}
}

Expand Down

0 comments on commit 57a2627

Please sign in to comment.