Skip to content

Commit

Permalink
Fixes auto-reconnection in case new addresses have been generated
Browse files Browse the repository at this point in the history
  • Loading branch information
markin-io committed Mar 8, 2022
1 parent 3ba7213 commit 9f528c5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 59 deletions.
1 change: 0 additions & 1 deletion packages/wallet-lib/src/EVENTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
TRANSACTION: 'transaction',
BLOCKHEADER: 'blockheader',
FETCHED_ADDRESS: 'FETCHED/ADDRESS',
UPDATED_ADDRESS: 'UPDATED/ADDRESS',
ERROR_UPDATE_ADDRESS: 'ERROR/UPDATE_ADDRESS',
FETCHED_TRANSACTION: 'FETCHED/TRANSACTION',
FETCHED_UNCONFIRMED_TRANSACTION: 'FETCHED/UNCONFIRMED_TRANSACTION',
Expand Down
37 changes: 0 additions & 37 deletions packages/wallet-lib/src/types/Account/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class Account extends EventEmitter {
this.storage = wallet.storage;

// Forward all storage event
this.storage.on(EVENTS.UPDATED_ADDRESS, (ev) => this.emit(ev.type, ev));
this.storage.on(EVENTS.CONFIGURED, (ev) => this.emit(ev.type, ev));
this.storage.on(EVENTS.REHYDRATE_STATE_FAILED, (ev) => this.emit(ev.type, ev));
this.storage.on(EVENTS.REHYDRATE_STATE_SUCCESS, (ev) => this.emit(ev.type, ev));
Expand All @@ -115,30 +114,13 @@ class Account extends EventEmitter {
switch (this.walletType) {
case WALLET_TYPES.HDWALLET:
this.accountPath = getBIP44Path(this.network, this.index);
// this.storage
// .getWalletStore(this.walletId)
// .createPathState(this.BIP44PATH);
// this.storage.createAccount(
// this.walletId,
// this.BIP44PATH,
// this.network,
// this.label,
// );
break;
case WALLET_TYPES.HDPUBLIC:
case WALLET_TYPES.PRIVATEKEY:
case WALLET_TYPES.PUBLICKEY:
case WALLET_TYPES.ADDRESS:
case WALLET_TYPES.SINGLE_ADDRESS:
this.accountPath = 'm/0';
// this.storage
// .getWalletStore(this.walletId)
// .createPathState(this);
// this.storage.createSingleAddress(
// this.walletId,
// this.network,
// this.label,
// );
break;
default:
throw new Error(`Invalid wallet type ${this.walletType}`);
Expand Down Expand Up @@ -196,25 +178,6 @@ class Account extends EventEmitter {
watchers: {},
};

// // Handle import of cache
// if (opts.cache) {
// if (opts.cache.addresses) {
// try {
// this.storage.importAddresses(opts.cache.addresses, this.walletId);
// } catch (e) {
// this.disconnect();
// throw e;
// }
// }
// if (opts.cache.transactions) {
// try {
// this.storage.importTransactions(opts.cache.transactions);
// } catch (e) {
// this.disconnect();
// throw e;
// }
// }
// }
this.emit(EVENTS.CREATED, { type: EVENTS.CREATED, payload: null });

/**
Expand Down
11 changes: 0 additions & 11 deletions packages/wallet-lib/src/types/Account/_initializeAccount.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
const logger = require('../../logger');
const EVENTS = require('../../EVENTS');
const preparePlugins = require('./_preparePlugins');
const { UPDATED_ADDRESS } = require('../../EVENTS');

// eslint-disable-next-line no-underscore-dangle
async function _initializeAccount(account, userUnsafePlugins) {
const self = account;

function markAddressAsUsed(props) {
const { address } = props.payload;
// This works if the TX cames from our main address, but not in all cases...
self.keyChainStore
.getMasterKeyChain()
.markAddressAsUsed(address);
}

self.on(UPDATED_ADDRESS, markAddressAsUsed);

const accountStore = account.storage
.getWalletStore(account.walletId)
.getPathState(account.accountPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = async function importTransactions(transactionsWithMayBeMetadata
keyChainStore,
} = this;

let addressesGenerated = 0;

const chainStore = storage.getChainStore(network);
const accountStore = storage
.getWalletStore(walletId)
Expand All @@ -36,6 +38,7 @@ module.exports = async function importTransactions(transactionsWithMayBeMetadata
keyChains.forEach((keyChain) => {
const issuedPaths = keyChain.markAddressAsUsed(address);
if (issuedPaths) {
addressesGenerated += issuedPaths.length;
issuedPaths.forEach((issuedPath) => {
if (keyChain.keyChainId === masterKeyChain.keyChainId) {
logger.silly(`Account.importTransactions - newly issued paths ${issuedPath.length}`);
Expand All @@ -48,5 +51,5 @@ module.exports = async function importTransactions(transactionsWithMayBeMetadata
});
});
logger.silly(`Account.importTransactions(len: ${transactionsWithMayBeMetadata.length})`);
return 0;
return addressesGenerated;
};
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ describe('TransactionSyncStreamWorker', function suite() {
// the last used address
expect(Object.keys(addressesInStorage).length).to.be.equal(40);
// It should reconnect after the gap limit is reached
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(1);
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(2);
// 20 external and 20 internal
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[0].length).to.be.equal(40);
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[1]).to.be.deep.equal({ fromBlockHeight: 40, count: 2});
// 20 more of external, since the last address is used.
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 2});
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 2});

expect(worker.stream).to.be.null;
expect(transactionsInStorage.length).to.be.equal(2);
Expand Down Expand Up @@ -427,13 +427,13 @@ describe('TransactionSyncStreamWorker', function suite() {
// the last used address
expect(Object.keys(addressesInStorage).length).to.be.equal(40);
// It should reconnect after the gap limit is reached
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(1);
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(2);
// 20 external and 20 internal
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[0].length).to.be.equal(40);
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[1]).to.be.deep.equal({ fromBlockHeight: 40, count: 0});
// 20 more of external, since the last address is used.
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});

expect(worker.stream).to.be.null;
expect(transactionsInStorage.length).to.be.equal(2);
Expand Down Expand Up @@ -691,13 +691,13 @@ describe('TransactionSyncStreamWorker', function suite() {
expect(Object.keys(externalAddressesInStorage).length).to.be.equal(40);
expect(Object.keys(internalAddressesInStorage).length).to.be.equal(20);
// It should reconnect after the gap limit is reached
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(1);
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(2);
// 20 external and 20 internal
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[1]).to.be.deep.equal({ fromBlockHeight: 40, count: 0});
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[0].length).to.be.equal(40);
// 20 more of external, since the last address is used, Merkle Block received
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});
expect(worker.stream).to.be.null;
expect(transactionsInStorage.length).to.be.equal(2);
expect(transactionsInStorage).to.have.deep.members(expectedTransactions);
Expand Down

0 comments on commit 9f528c5

Please sign in to comment.