From 3fd920db63010b259b3a89c1f6d0a8d59df15823 Mon Sep 17 00:00:00 2001 From: Djavid Gabibiyan Date: Wed, 3 Nov 2021 20:03:51 +0500 Subject: [PATCH] feat: do not sync transactions if mnemonic is absent --- .../TransactionSyncStreamWorker.js | 10 +++++++++ .../wallet-lib/src/types/Wallet/Wallet.js | 21 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/wallet-lib/src/plugins/Workers/TransactionSyncStreamWorker/TransactionSyncStreamWorker.js b/packages/wallet-lib/src/plugins/Workers/TransactionSyncStreamWorker/TransactionSyncStreamWorker.js index 1d4b82edb46..73d4bfda672 100644 --- a/packages/wallet-lib/src/plugins/Workers/TransactionSyncStreamWorker/TransactionSyncStreamWorker.js +++ b/packages/wallet-lib/src/plugins/Workers/TransactionSyncStreamWorker/TransactionSyncStreamWorker.js @@ -9,6 +9,8 @@ const sleep = require('../../../utils/sleep'); const Worker = require('../../Worker'); const isBrowser = require('../../../utils/isBrowser'); +const logger = require('../../../logger'); + class TransactionSyncStreamWorker extends Worker { constructor(options) { super({ @@ -132,8 +134,16 @@ class TransactionSyncStreamWorker extends Worker { // instead of usual injection process const { skipSynchronizationBeforeHeight, + skipSynchronization, } = (this.storage.store.syncOptions || {}); + if (skipSynchronization) { + logger.debug('TransactionSyncStreamWorker - Wallet created from a new mnemonic. Sync from the best block height.'); + const bestBlockHeight = this.storage.store.chains[this.network.toString()].blockHeight; + this.setLastSyncedBlockHeight(bestBlockHeight); + return; + } + if (skipSynchronizationBeforeHeight) { this.setLastSyncedBlockHeight( skipSynchronizationBeforeHeight, diff --git a/packages/wallet-lib/src/types/Wallet/Wallet.js b/packages/wallet-lib/src/types/Wallet/Wallet.js index 2186d900721..644e8da1b72 100644 --- a/packages/wallet-lib/src/types/Wallet/Wallet.js +++ b/packages/wallet-lib/src/types/Wallet/Wallet.js @@ -79,8 +79,14 @@ class Wallet extends EventEmitter { this.network = network.toString(); + let createdFromNewMnemonic = false; if ('mnemonic' in opts) { - this.fromMnemonic((opts.mnemonic === null) ? generateNewMnemonic() : opts.mnemonic); + let { mnemonic } = opts; + if (mnemonic === null) { + mnemonic = generateNewMnemonic(); + createdFromNewMnemonic = true; + } + this.fromMnemonic(mnemonic); } else if ('seed' in opts) { this.fromSeed(opts.seed); } else if ('HDPrivateKey' in opts) { @@ -97,6 +103,7 @@ class Wallet extends EventEmitter { this.fromAddress(opts.address); } else { this.fromMnemonic(generateNewMnemonic()); + createdFromNewMnemonic = true; } // Notice : Most of the time, wallet id is deterministic @@ -114,10 +121,20 @@ class Wallet extends EventEmitter { this.store = this.storage.store; - if (this.unsafeOptions.skipSynchronizationBeforeHeight) { + if (createdFromNewMnemonic) { // As it is pretty complicated to pass any of wallet options // to a specific plugin, using `store` as an options mediator // is easier. + + this.store.syncOptions = { + skipSynchronization: true, + }; + + if (this.unsafeOptions.skipSynchronizationBeforeHeight) { + throw new Error('"unsafeOptions.skipSynchronizationBeforeHeight" will have no effect because wallet has been' + + ' created from the new mnemonic'); + } + } else if (this.unsafeOptions.skipSynchronizationBeforeHeight) { this.store.syncOptions = { skipSynchronizationBeforeHeight: this.unsafeOptions.skipSynchronizationBeforeHeight, };