From 50a1d6022937092ef4e433c8e1d5d7b5d9a7ff97 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 31 Mar 2022 07:24:07 -0700 Subject: [PATCH] fix(Transaction.get()): Handling coinbase txs --- src/psf-slp-indexer.js | 6 ++++++ src/raw-transactions.js | 21 +++++++++++++------ src/transaction.js | 1 + .../chains/bchn/transaction-integration.js | 9 ++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/psf-slp-indexer.js b/src/psf-slp-indexer.js index a9b6561..683d3d2 100644 --- a/src/psf-slp-indexer.js +++ b/src/psf-slp-indexer.js @@ -288,6 +288,8 @@ class PsfSlpIndexer { */ async tx (txid) { try { + // console.log('txid: ', txid) + // Handle single address. if (typeof txid === 'string') { const response = await axios.post( @@ -295,8 +297,11 @@ class PsfSlpIndexer { { txid }, this.axiosOptions ) + // console.log('response: ', response) + return response.data } + throw new Error('Input txid must be a string.') } catch (error) { // console.log('error: ', error) @@ -316,6 +321,7 @@ class PsfSlpIndexer { // Check if this txid belongs to a blacklisted token. const isInBlacklist = await this.checkBlacklist(txid) + // console.log('isInBlacklist: ', isInBlacklist) // Get the TX Details from the full node. const txDetails = await this.rawTransaction.getTxData(txid) diff --git a/src/raw-transactions.js b/src/raw-transactions.js index 067595c..81230ac 100644 --- a/src/raw-transactions.js +++ b/src/raw-transactions.js @@ -381,6 +381,8 @@ class RawTransactions { // Appends the BCH address to the inputs of the transaction. async getTxData (txid) { try { + // console.log('getTxData() txid: ', txid) + if (typeof txid !== 'string') { throw new Error( 'Input to raw-transaction.js/getTxData() must be a string containg a TXID.' @@ -391,17 +393,24 @@ class RawTransactions { const txDetails = await this.getRawTransaction(txid, true) // console.log(`txDetails: ${JSON.stringify(txDetails, null, 2)}`) - const inAddrs = await this._getInputAddrs(txDetails) - // console.log(`inAddrs: ${JSON.stringify(inAddrs, null, 2)}`) + try { + const inAddrs = await this._getInputAddrs(txDetails) + // console.log(`inAddrs: ${JSON.stringify(inAddrs, null, 2)}`) - // Add the input address to the transaction data. - for (let i = 0; i < inAddrs.length; i++) { - txDetails.vin[i].address = inAddrs[i].address - txDetails.vin[i].value = inAddrs[i].value + // Add the input address to the transaction data. + for (let i = 0; i < inAddrs.length; i++) { + txDetails.vin[i].address = inAddrs[i].address + txDetails.vin[i].value = inAddrs[i].value + } + } catch (err) { + // Coinbase transactions will throw an error. Just ignore them and + // pass back the raw transaction data. + /* exit quietly */ } return txDetails } catch (error) { + // console.log('error: ', error) if (error.error) throw new Error(error.error) // This case handles rate limit errors. diff --git a/src/transaction.js b/src/transaction.js index e2a7a59..a01bef7 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -41,6 +41,7 @@ class Transaction { * })() */ async get (txid) { + // console.log('transaction.get() txid: ', txid) return await this.psfSlpIndexer.tx(txid) } diff --git a/test/integration/chains/bchn/transaction-integration.js b/test/integration/chains/bchn/transaction-integration.js index 598d824..61b95d5 100644 --- a/test/integration/chains/bchn/transaction-integration.js +++ b/test/integration/chains/bchn/transaction-integration.js @@ -24,5 +24,14 @@ describe('#Transaction', () => { assert.property(result.txData, 'vout') assert.equal(result.txData.isValidSlp, false) }) + + it('should handle a coinbase transaction', async () => { + const txid = 'cca1d2dd3a533d2f501448dec03face2cb2814afd59a533a611e9a2909f2302b' + + const details = await bchjs.Transaction.get(txid) + // console.log(`details: ${JSON.stringify(details, null, 2)}`) + + assert.property(details.txData, 'txid') + }) }) })