diff --git a/src/wallet/index.ts b/src/wallet/index.ts index 4f09920..0c261b7 100644 --- a/src/wallet/index.ts +++ b/src/wallet/index.ts @@ -1,3 +1,4 @@ +import * as bip39 from 'bip39'; import * as bitcoin from 'bitcoinjs-lib'; import { Network, networks } from 'bitcoinjs-lib'; import BIP32Factory, { BIP32Interface } from 'bip32'; @@ -109,7 +110,6 @@ import { Transaction } from '../transaction'; import { GAP_LIMIT, GAP_LIMIT_CHANGE, TRANSACTION_DEFAULTS } from './constants'; import cloneDeep from 'lodash.clonedeep'; import { btcToSats } from '../utils/conversion'; -import * as bip39 from 'bip39'; const bip32 = BIP32Factory(ecc); @@ -1974,7 +1974,6 @@ export class Wallet { [key: string]: number[]; } = {}; for (const tx of receivedTxs) { - this.sendMessage('transactionReceived', tx); // No need to scan an address with a saved UTXO. if (utxoScriptHashes.has(tx.transaction.scriptHash)) continue; for (const addressType in addresses) { @@ -2011,6 +2010,10 @@ export class Wallet { } } + for (const tx of receivedTxs) { + this.sendMessage('transactionReceived', tx); + } + return ok(notificationTxid); } diff --git a/tests/receive.test.ts b/tests/receive.test.ts index a1dff33..ed94520 100644 --- a/tests/receive.test.ts +++ b/tests/receive.test.ts @@ -72,6 +72,7 @@ beforeEach(async function () { lookAheadChange: 2, lookBehindChange: 2 }, + addressTypesToMonitor: [EAddressType.p2wpkh], onMessage: ml.onMessage }); if (res.isErr()) throw res.error; @@ -118,8 +119,7 @@ describe('Receive', async function () { expect(wallet.utxos.length).to.equal(1); }); - // failing, WIP - it.skip('Should track multiple receiving addresses', async () => { + it('Should track multiple receiving addresses', async () => { const r1 = await wallet.getNextAvailableAddress(); if (r1.isErr()) throw r1.error; const address1 = r1.value.addressIndex.address; @@ -141,22 +141,17 @@ describe('Receive', async function () { // After a transaction, addresses should differ expect(address1).to.not.equal(address3); - const receivePromise = ml.waitFor('transactionReceived'); - // Second transaction + const receivePromise = ml.waitFor('transactionReceived'); await rpc.sendToAddress(address3, '0.2'); - await rpc.generateToAddress(1, await rpc.getNewAddress()); - await waitForElectrum(); - - // await sleep(1000); await receivePromise; - while (wallet.isRefreshing) { - console.info('wallet.isRefreshing', wallet.isRefreshing); - await sleep(10); - } - // await wallet.refreshWallet(); - console.log(wallet.data); + // unfortinately it is possible that Electrum server still does not + // have second transaction parsed so we need to wait a bit + while (wallet.data.utxos.length === 1) { + await sleep(100); + await wallet.refreshWallet(); + } // Check balances expect(wallet.balance).to.equal(0.3 * 10e7); // 0.3 BTC in sats @@ -167,7 +162,7 @@ describe('Receive', async function () { expect(balance1.value.confirmed).to.equal(0.1 * 10e7); const balance3 = await wallet.getAddressBalance(address3); if (balance3.isErr()) throw balance3.error; - expect(balance3.value.confirmed).to.equal(0.2 * 10e7); + expect(balance3.value.unconfirmed).to.equal(0.2 * 10e7); // Test getAddressesBalance const combinedBalance = await wallet.getAddressesBalance([