From 13c3be27753080f0b6afdd370445c45edd6bf5fc Mon Sep 17 00:00:00 2001 From: "markin.io" Date: Thu, 25 Apr 2024 12:15:06 +0100 Subject: [PATCH 1/2] feat: optional sync of the wallet --- packages/js-dash-sdk/src/SDK/Client/Client.ts | 1 + .../wallet-lib/src/types/Account/Account.d.ts | 1 + .../wallet-lib/src/types/Account/Account.js | 9 ++------ .../src/types/Account/_initializeAccount.js | 16 ------------- .../src/types/Wallet/methods/createAccount.js | 23 ++++++++++++++++++- .../src/types/Wallet/methods/getAccount.js | 11 ++++++--- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Client.ts b/packages/js-dash-sdk/src/SDK/Client/Client.ts index aab3eecfbab..87df6d55265 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Client.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Client.ts @@ -165,6 +165,7 @@ export class Client extends EventEmitter { options = { index: this.defaultAccountIndex, + synchronize: true, ...options, }; diff --git a/packages/wallet-lib/src/types/Account/Account.d.ts b/packages/wallet-lib/src/types/Account/Account.d.ts index 68e07f2b3c4..974440babe9 100644 --- a/packages/wallet-lib/src/types/Account/Account.d.ts +++ b/packages/wallet-lib/src/types/Account/Account.d.ts @@ -88,6 +88,7 @@ export declare interface getUTXOSOptions { export declare namespace Account { interface Options { index?: number, + synchronize?: boolean, network?: Network; debug?: boolean; label?: string; diff --git a/packages/wallet-lib/src/types/Account/Account.js b/packages/wallet-lib/src/types/Account/Account.js index a51a0145794..03f9dcc5963 100644 --- a/packages/wallet-lib/src/types/Account/Account.js +++ b/packages/wallet-lib/src/types/Account/Account.js @@ -44,7 +44,6 @@ const defaultOptions = { /* eslint-disable no-underscore-dangle */ const _initializeAccount = require('./_initializeAccount'); -const _addAccountToWallet = require('./_addAccountToWallet'); const _loadStrategy = require('./_loadStrategy'); const getNetwork = require('./_getNetwork'); @@ -56,6 +55,7 @@ class Account extends EventEmitter { if (!wallet || wallet.constructor.name !== Wallet.name) throw new Error('Expected wallet to be passed as param'); if (!_.has(wallet, 'walletId')) throw new Error('Missing walletID to create an account'); this.walletId = wallet.walletId; + this.wallet = wallet; this.logger = logger.getForWallet(this.walletId); this.logger.debug(`Loading up wallet ${this.walletId}`); @@ -223,16 +223,11 @@ class Account extends EventEmitter { return `${EVENTS.INSTANT_LOCK}:${transactionHash}`; } - // It's actually Account that mutates wallet.accounts to add itself. - // We might want to get rid of that as it can be really confusing. - // It would gives that responsability to createAccount to create - // (and therefore push to accounts). async init(wallet) { if (this.state.isInitialized) { return true; } - await _addAccountToWallet(this, wallet); - await _initializeAccount(this, wallet.plugins); + await _initializeAccount(this, wallet ? wallet.plugins : this.wallet.plugins); return true; } diff --git a/packages/wallet-lib/src/types/Account/_initializeAccount.js b/packages/wallet-lib/src/types/Account/_initializeAccount.js index 3a034f21e6f..65c69b7a207 100644 --- a/packages/wallet-lib/src/types/Account/_initializeAccount.js +++ b/packages/wallet-lib/src/types/Account/_initializeAccount.js @@ -6,22 +6,6 @@ const preparePlugins = require('./_preparePlugins'); async function _initializeAccount(account, userUnsafePlugins) { const self = account; - // Add default derivation paths - account.addDefaultPaths(); - - // Issue additional derivation paths in case we have transactions in the store - // at the moment of initialization (from persistent storage) - account.createPathsForTransactions(); - - // Add block headers from storage into the SPV chain if there are any - const chainStore = self.storage.getDefaultChainStore(); - const { blockHeaders, lastSyncedHeaderHeight } = chainStore.state; - if (!self.offlineMode) { - const { blockHeadersProvider } = self.transport.client; - const firstHeaderHeight = lastSyncedHeaderHeight - blockHeaders.length + 1; - await blockHeadersProvider.initializeChainWith(blockHeaders, firstHeaderHeight); - } - // We run faster in offlineMode to speed up the process when less happens. const readinessIntervalTime = (account.offlineMode) ? 50 : 200; // TODO: perform rejection with a timeout diff --git a/packages/wallet-lib/src/types/Wallet/methods/createAccount.js b/packages/wallet-lib/src/types/Wallet/methods/createAccount.js index 8f49c6ec83b..f74c4bb885c 100644 --- a/packages/wallet-lib/src/types/Wallet/methods/createAccount.js +++ b/packages/wallet-lib/src/types/Wallet/methods/createAccount.js @@ -28,8 +28,29 @@ async function createAccount(accountOpts) { const opts = Object.assign(baseOpts, accountOpts); const account = new Account(this, opts); + + // Add default derivation paths + account.addDefaultPaths(); + + // Issue additional derivation paths in case we have transactions in the store + // at the moment of initialization (from persistent storage) + account.createPathsForTransactions(); + + // Add block headers from storage into the SPV chain if there are any + const chainStore = this.storage.getDefaultChainStore(); + const { blockHeaders, lastSyncedHeaderHeight } = chainStore.state; + if (!this.offlineMode && blockHeaders.length > 0) { + const { blockHeadersProvider } = this.transport.client; + blockHeadersProvider.initializeChainWith(blockHeaders, lastSyncedHeaderHeight); + } + + this.accounts.push(account); + try { - await account.init(this); + if (opts.synchronize) { + await account.init(this); + } + return account; } catch (e) { await account.disconnect(); diff --git a/packages/wallet-lib/src/types/Wallet/methods/getAccount.js b/packages/wallet-lib/src/types/Wallet/methods/getAccount.js index d333c1209ff..8e3f33c7128 100644 --- a/packages/wallet-lib/src/types/Wallet/methods/getAccount.js +++ b/packages/wallet-lib/src/types/Wallet/methods/getAccount.js @@ -2,14 +2,19 @@ const _ = require('lodash'); const { is } = require('../../../utils'); const EVENTS = require('../../../EVENTS'); +const defaultOpts = { + index: 0, + synchronize: true, +}; + /** * Get a specific account per accounts index * @param accountOpts - If the account doesn't exist yet, we create it passing these options * @param accountOpts.index - Default: 0, set a specific index to get + * @param accountOpts.synchronize - Default: true, specifies whether account has to be synchronized * @return {Account} */ - -async function getAccount(accountOpts = {}) { +async function getAccount(accountOpts = defaultOpts) { if (!this.storage.configured) { await new Promise((resolve) => { this.storage.once(EVENTS.CONFIGURED, resolve); }); } @@ -27,7 +32,7 @@ async function getAccount(accountOpts = {}) { const baseOpts = { index: accountIndex }; const opts = Object.assign(baseOpts, accountOpts); - return (acc[0]) || this.createAccount(opts); + return acc[0] || this.createAccount(opts); } module.exports = getAccount; From 5d4fb73adab9be6d1fbfc4eb988ab6b83d119ad3 Mon Sep 17 00:00:00 2001 From: "markin.io" Date: Thu, 25 Apr 2024 12:43:08 +0100 Subject: [PATCH 2/2] fix bug with index --- packages/wallet-lib/src/types/Account/Account.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/wallet-lib/src/types/Account/Account.js b/packages/wallet-lib/src/types/Account/Account.js index 03f9dcc5963..b8240f621b2 100644 --- a/packages/wallet-lib/src/types/Account/Account.js +++ b/packages/wallet-lib/src/types/Account/Account.js @@ -44,6 +44,7 @@ const defaultOptions = { /* eslint-disable no-underscore-dangle */ const _initializeAccount = require('./_initializeAccount'); +const _addAccountToWallet = require('./_addAccountToWallet'); const _loadStrategy = require('./_loadStrategy'); const getNetwork = require('./_getNetwork'); @@ -227,6 +228,7 @@ class Account extends EventEmitter { if (this.state.isInitialized) { return true; } + await _addAccountToWallet(this, wallet); await _initializeAccount(this, wallet ? wallet.plugins : this.wallet.plugins); return true; }