Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 19 additions & 2 deletions packages/wallet-lib/src/types/Wallet/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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,
};
Expand Down