Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet-lib): optional sync of the account #1830

Merged
merged 2 commits into from
Apr 25, 2024
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
1 change: 1 addition & 0 deletions packages/js-dash-sdk/src/SDK/Client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { contractId as dashpayContractId } from '@dashevo/dashpay-contract/lib/systemIds';
import { contractId as masternodeRewardSharesContractId } from '@dashevo/masternode-reward-shares-contract/lib/systemIds';
import { contractId as withdrawalsContractId } from '@dashevo/withdrawals-contract/lib/systemIds';
import { Platform } from './Platform';

Check warning on line 10 in packages/js-dash-sdk/src/SDK/Client/Client.ts

View workflow job for this annotation

GitHub Actions / JS packages (dash) / Linting

Dependency cycle via ./Platform:1
import { ClientApps, ClientAppsOptions } from './ClientApps';

export interface WalletOptions extends Wallet.IWalletOptions {
Expand Down Expand Up @@ -163,8 +163,9 @@
throw new Error('Wallet is not initialized, pass `wallet` option to Client');
}

options = {

Check warning on line 166 in packages/js-dash-sdk/src/SDK/Client/Client.ts

View workflow job for this annotation

GitHub Actions / JS packages (dash) / Linting

Assignment to function parameter 'options'
index: this.defaultAccountIndex,
synchronize: true,
...options,
};

Expand Down
1 change: 1 addition & 0 deletions packages/wallet-lib/src/types/Account/Account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export declare interface getUTXOSOptions {
export declare namespace Account {
interface Options {
index?: number,
synchronize?: boolean,
network?: Network;
debug?: boolean;
label?: string;
Expand Down
7 changes: 2 additions & 5 deletions packages/wallet-lib/src/types/Account/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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}`);
Expand Down Expand Up @@ -223,16 +224,12 @@ 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;
}

Expand Down
16 changes: 0 additions & 16 deletions packages/wallet-lib/src/types/Account/_initializeAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion packages/wallet-lib/src/types/Wallet/methods/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 8 additions & 3 deletions packages/wallet-lib/src/types/Wallet/methods/getAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
}
Expand All @@ -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;
Loading