From 518a4249554a9fd3b3c6e2048637b841ecd67bb8 Mon Sep 17 00:00:00 2001 From: daviortega Date: Tue, 28 Jul 2020 01:26:22 -0300 Subject: [PATCH] Fix logic to handle multiple outputs in ExplorerWallet.buildTXHex(). Add tests. --- src/modules/wallets/ExplorerWallet.js | 24 +++------------- src/modules/wallets/RPCWallet.js | 10 +++---- .../modules/wallets/ExplorerWallet.test.js | 28 +++++++++++++++++-- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/modules/wallets/ExplorerWallet.js b/src/modules/wallets/ExplorerWallet.js index 4e077af..5f5cd3a 100644 --- a/src/modules/wallets/ExplorerWallet.js +++ b/src/modules/wallets/ExplorerWallet.js @@ -175,28 +175,12 @@ class ExplorerWallet { inputs.forEach(input => txb.addInput(input.txId, input.vout)) - // Check if we are paying to ourself, if so, merge the outputs to just a single output. - // Check if we have two outputs (i.e. pay to and change) - if (outputs.length === 2) { - // If the first input is sending to the from address, and there is a change output, - // then merge the outputs. - if (outputs[0].address === this.p2pkh && !outputs[1].address) { - let totalToSend = outputs[0].value + outputs[1].value - outputs = [{ - address: this.p2pkh, - value: totalToSend - }] - } else { - // send the original amount to the first address and send the rest to yourself as change - if (outputs[0].address !== this.p2pkh && !outputs[1].address) { - outputs[1].address = this.p2pkh - } - } - } + // console.log(outputs) outputs.forEach(output => { if (!output.address) { - throw new Error(`Missing output address: ${outputs}`) + output.address = this.p2pkh + // throw new Error(`Missing output address: ${outputs}`) } txb.addOutput(output.address, output.value) }) @@ -209,7 +193,7 @@ class ExplorerWallet { } let builtHex - + // console.log('txb:', txb) try { builtHex = txb.build().toHex() } catch (err) { diff --git a/src/modules/wallets/RPCWallet.js b/src/modules/wallets/RPCWallet.js index bdcbc32..f5d6f99 100644 --- a/src/modules/wallets/RPCWallet.js +++ b/src/modules/wallets/RPCWallet.js @@ -734,10 +734,10 @@ class RPCWallet { // If we do not already have a loop going to make sure confirmations get fired off, create one if (!this.onConfirmationInterval) { - this.onConfirmationInterval = setInterval((async () => { + this.onConfirmationInterval = setInterval(async () => { await this.checkAncestorCount(true) - await this.checkForConfirmations() - }).bind(this), CONFIRMATION_CHECK_LENGTH) + await this.checkForConfirmations() + }, CONFIRMATION_CHECK_LENGTH) } } @@ -746,13 +746,13 @@ class RPCWallet { * @return {Promise} Returns a promise that resolves once all of the available confirmation callbacks have been run */ async checkForConfirmations () { - if (this.getConfirmationSubscriptionCount() === 0) { + if (this.getConfirmationSubscriptionCount() === 0) { // Sanity check to clear out the interval if for whatever reason it currently exists. if (this.onConfirmationInterval) { clearInterval(this.onConfirmationInterval) this.onConfirmationInterval = undefined } - return + return } console.log(`[RPC Wallet] Checking ${this.getConfirmationSubscriptionCount()} transations for confirmations...`) diff --git a/test/integration/modules/wallets/ExplorerWallet.test.js b/test/integration/modules/wallets/ExplorerWallet.test.js index 7f623f5..b2daa2d 100644 --- a/test/integration/modules/wallets/ExplorerWallet.test.js +++ b/test/integration/modules/wallets/ExplorerWallet.test.js @@ -56,14 +56,14 @@ describe(`ExplorerWallet`, () => { // } // done() // }, 250 * 100 * 100) - it.skip('build and broadcast TX hex | sendDataToChain', async () => { + it('build and broadcast TX hex | sendDataToChain', async () => { let wallet = new ExplorerWallet({ wif, network: 'testnet' }) let txid = await wallet.sendDataToChain(`RC`) expect(typeof txid === 'string').toBeTruthy() // console.log(txid) }) - it.skip('flotx w custom output | sendTx', async () => { - let wallet = new ExplorerWallet(wif, 'testnet') + it('flotx w custom output | sendTx', async () => { + let wallet = new ExplorerWallet({ wif, network: 'testnet' }) let output = { address: 'oNAydz5TjkhdP3RPuu3nEirYQf49Jrzm4S', value: Math.floor(0.0001 * floTestnet.satPerCoin) @@ -73,6 +73,28 @@ describe(`ExplorerWallet`, () => { expect(txid).toBeDefined() expect(typeof txid === 'string').toBeTruthy() }) + it('flotx w multiple custom output | sendTx', async () => { + jest.setTimeout(10000) + let wallet = new ExplorerWallet({ wif, network: 'testnet' }) + let outputs = [ + { + address: 'oNAydz5TjkhdP3RPuu3nEirYQf49Jrzm4S', + value: Math.floor(0.0001 * floTestnet.satPerCoin) + }, + { + address: 'ofbB67gqjgaYi45u8Qk2U3hGoCmyZcgbN4', + value: Math.floor(0.0001 * floTestnet.satPerCoin) + }, + { + address: 'oV5qwoq9CSaXersHk4DQVHhoMTDjRNWRF2', + value: Math.floor(0.0001 * floTestnet.satPerCoin) + } + ] + let txid = await wallet.sendTx(outputs, 'to testnet') + console.log(txid) + expect(txid).toBeDefined() + expect(typeof txid === 'string').toBeTruthy() + }) it('add and remove spent transaction from utxo', async () => { let wallet = new ExplorerWallet({ wif, network: 'testnet' }) let utxo = await wallet.getUTXO()