Browse Source

add migrationNonce mecanism in the walletName generation

master
Gaëtan Renaudeau 7 years ago
parent
commit
3c22dbb6a4
No known key found for this signature in database GPG Key ID: 7B66B85F042E5451
  1. 41
      src/helpers/libcore.js

41
src/helpers/libcore.js

@ -29,6 +29,15 @@ const SPLITTED_CURRENCIES = {
}, },
} }
// each time there is a breaking change that needs use to clear cache on both libcore and js side,
// we should bump a nonce in this map
// tech notes:
// - this is used to generate walletName for libcore to completely re-sync to a new wallet.
// - by changing walletName we also make the JS db refresh because we handle the accountId changes (walletName is in accountId).
const migrationNonceByCurrency = {
digibyte: 1,
}
export function isValidAddress(core: *, currency: *, address: string): boolean { export function isValidAddress(core: *, currency: *, address: string): boolean {
const addr = new core.NJSAddress(address, currency) const addr = new core.NJSAddress(address, currency)
return addr.isValid(address, currency) return addr.isValid(address, currency)
@ -319,14 +328,16 @@ const createWalletConfig = (core, configMap = {}) => {
async function getOrCreateWallet( async function getOrCreateWallet(
core: *, core: *,
WALLET_IDENTIFIER: string, walletName: string,
currencyId: string, currencyId: string,
isSegwit: boolean, isSegwit: boolean,
isUnsplit: boolean, isUnsplit: boolean,
): NJSWallet { ): NJSWallet {
const migrationNonce = migrationNonceByCurrency[currencyId]
const walletId = walletName + (migrationNonce ? `_${migrationNonce}` : '')
const pool = core.getPoolInstance() const pool = core.getPoolInstance()
try { try {
const wallet = await timeoutTagged('getWallet', 5000, pool.getWallet(WALLET_IDENTIFIER)) const wallet = await timeoutTagged('getWallet', 5000, pool.getWallet(walletId))
return wallet return wallet
} catch (err) { } catch (err) {
const currency = await timeoutTagged('getCurrency', 5000, pool.getCurrency(currencyId)) const currency = await timeoutTagged('getCurrency', 5000, pool.getCurrency(currencyId))
@ -346,7 +357,7 @@ async function getOrCreateWallet(
const wallet = await timeoutTagged( const wallet = await timeoutTagged(
'createWallet', 'createWallet',
10000, 10000,
core.getPoolInstance().createWallet(WALLET_IDENTIFIER, currency, njsWalletConfig), core.getPoolInstance().createWallet(walletId, currency, njsWalletConfig),
) )
return wallet return wallet
} }
@ -513,23 +524,13 @@ export async function syncAccount({
const decodedAccountId = accountIdHelper.decode(accountId) const decodedAccountId = accountIdHelper.decode(accountId)
const isSegwit = isSegwitPath(freshAddressPath) const isSegwit = isSegwitPath(freshAddressPath)
const isUnsplit = isUnsplitPath(freshAddressPath, SPLITTED_CURRENCIES[currencyId]) const isUnsplit = isUnsplitPath(freshAddressPath, SPLITTED_CURRENCIES[currencyId])
let njsWallet const njsWallet = await getOrCreateWallet(
try { core,
njsWallet = await timeoutTagged( decodedAccountId.walletName,
'getWallet', currencyId,
10000, isSegwit,
core.getPoolInstance().getWallet(decodedAccountId.walletName), isUnsplit,
) )
} catch (e) {
logger.warn(`Have to reimport the account... (${e})`)
njsWallet = await getOrCreateWallet(
core,
decodedAccountId.walletName,
currencyId,
isSegwit,
isUnsplit,
)
}
let njsAccount let njsAccount
try { try {

Loading…
Cancel
Save