diff --git a/src/plugins/Workers/BIP44Worker/ensureEnoughAddress.js b/src/plugins/Workers/BIP44Worker/ensureEnoughAddress.js index 94623036..9f95f419 100644 --- a/src/plugins/Workers/BIP44Worker/ensureEnoughAddress.js +++ b/src/plugins/Workers/BIP44Worker/ensureEnoughAddress.js @@ -1,56 +1,9 @@ const { BIP44_ADDRESS_GAP, WALLET_TYPES } = require('../../../CONSTANTS'); const is = require('../../../utils/is'); -const isContiguousPath = (currPath, prevPath) => { - if (is.undef(currPath)) return false; - const splitedCurrPath = currPath.split('/'); - const currIndex = parseInt(splitedCurrPath[5], 10); - if (is.undef(prevPath)) { - if (currIndex !== 0) return false; - return true; - } - const splitedPrevPath = prevPath.split('/'); - const prevIndex = parseInt(splitedPrevPath[5], 10); - if (prevIndex !== currIndex - 1) return false; - return true; -}; -const getMissingIndexes = (paths, fromOrigin = true) => { - if (!is.arr(paths)) return false; - - let sortedIndexes = []; - - paths.forEach((path) => { - const splitedPath = path.split('/'); - const index = parseInt(splitedPath[5], 10); - sortedIndexes.push(index); - }); - - sortedIndexes = sortedIndexes.sort((a, b) => a - b); - - let missingIndex = sortedIndexes.reduce((acc, cur, ind, arr) => { - const diff = cur - arr[ind - 1]; - if (diff > 1) { - let i = 1; - while (i < diff) { - acc.push(arr[ind - 1] + i); - i += 1; - } - } - return acc; - }, []); +const getMissingIndexes = require('./utils/getMissingIndexes'); +const isContiguousPath = require('./utils/isContiguousPath'); - // Will fix missing index before our first known indexes - if (fromOrigin) { - if (sortedIndexes[0] > 0) { - for (let i = sortedIndexes[0] - 1; i >= 0; i -= 1) { - missingIndex.push(i); - } - } - } - - missingIndex = missingIndex.sort((a, b) => a - b); - return missingIndex; -}; module.exports = function ensureEnoughAddress() { let generated = 0; let unusedAddress = 0; diff --git a/src/plugins/Workers/BIP44Worker/utils/getMissingIndexes.js b/src/plugins/Workers/BIP44Worker/utils/getMissingIndexes.js new file mode 100644 index 00000000..b2aeb4d5 --- /dev/null +++ b/src/plugins/Workers/BIP44Worker/utils/getMissingIndexes.js @@ -0,0 +1,39 @@ +const is = require('../../../../utils/is'); + +module.exports = function getMissingIndexes(paths, fromOrigin = true) { + if (!is.arr(paths)) return false; + + let sortedIndexes = []; + + paths.forEach((path) => { + const splitedPath = path.split('/'); + const index = parseInt(splitedPath[5], 10); + sortedIndexes.push(index); + }); + + sortedIndexes = sortedIndexes.sort((a, b) => a - b); + + let missingIndex = sortedIndexes.reduce((acc, cur, ind, arr) => { + const diff = cur - arr[ind - 1]; + if (diff > 1) { + let i = 1; + while (i < diff) { + acc.push(arr[ind - 1] + i); + i += 1; + } + } + return acc; + }, []); + + // Will fix missing index before our first known indexes + if (fromOrigin) { + if (sortedIndexes[0] > 0) { + for (let i = sortedIndexes[0] - 1; i >= 0; i -= 1) { + missingIndex.push(i); + } + } + } + + missingIndex = missingIndex.sort((a, b) => a - b); + return missingIndex; +}; diff --git a/src/plugins/Workers/BIP44Worker/utils/isContiguousPath.js b/src/plugins/Workers/BIP44Worker/utils/isContiguousPath.js new file mode 100644 index 00000000..dd3a6b52 --- /dev/null +++ b/src/plugins/Workers/BIP44Worker/utils/isContiguousPath.js @@ -0,0 +1,15 @@ +const is = require('../../../../utils/is'); + +module.exports = function isContiguousPath(currPath, prevPath) { + if (is.undef(currPath)) return false; + + const splitedCurrPath = currPath.split('/'); + const currIndex = parseInt(splitedCurrPath[5], 10); + + if (is.undef(prevPath)) { + return currIndex === 0; + } + const splitedPrevPath = prevPath.split('/'); + const prevIndex = parseInt(splitedPrevPath[5], 10); + return prevIndex === currIndex - 1; +}; diff --git a/src/types/Storage/methods/importTransaction.js b/src/types/Storage/methods/importTransaction.js index 2c3a9273..024bacf3 100644 --- a/src/types/Storage/methods/importTransaction.js +++ b/src/types/Storage/methods/importTransaction.js @@ -14,14 +14,16 @@ const importTransaction = function importTransaction(transaction) { let hasUpdateStorage = false; let outputIndex = -1; + const processedAddressesForTx = []; - if (transactions[transaction.hash]) { - return; + // If we already had this transaction locally, we won't add it again, + // but we still need to continue processing it as we might have new + // address generated (on BIP44 wallets) since the first checkup. + if (!transactions[transaction.hash]) { + transactions[transaction.hash] = transaction; } - transactions[transaction.hash] = transaction; - - [...inputs, ...outputs].forEach((element) => { + [...inputs, ...outputs].forEach((element, elementIndex) => { const isOutput = (element instanceof Output); if (isOutput) outputIndex += 1; @@ -33,10 +35,22 @@ const importTransaction = function importTransaction(transaction) { const addressObject = store.wallets[walletId].addresses[type][path]; if (!addressObject.used) addressObject.used = true; - if (!addressObject.transactions.includes(transaction.hash)) { + if (elementIndex === 0) { + // If the transactions has already been processed in a previous insertion, + // we can skip the processing now + if (addressObject.transactions.includes(transaction.hash)) { + // We mark it as already processed + processedAddressesForTx.push(addressObject.address); + return; + } addressObject.transactions.push(transaction.hash); hasUpdateStorage = true; } + // If mark as already procesed on first run, skipping. + if (processedAddressesForTx.includes(addressObject.address)) { + return; + } + if (!isOutput) { const vin = element; const utxoKey = `${vin.prevTxId.toString('hex')}-${vin.outputIndex}`;