From 7990d0bffd75b844a2d09ca25f7b427a53fb8b8b Mon Sep 17 00:00:00 2001 From: Stoyan Zhekov Date: Tue, 10 Nov 2020 19:04:16 +0900 Subject: [PATCH] Add Ninsight TX Details --- src/ninsight.js | 73 ++++++++++++++++++++++++++++ test/integration/bchn/ninsight.js | 37 ++++++++++++++ test/integration/ninsight.js | 36 ++++++++++++++ test/integration/testnet/ninsight.js | 36 ++++++++++++++ test/unit/fixtures/ninsight-mock.js | 46 +++++++++++++++++- test/unit/ninsight.js | 57 ++++++++++++++++++++++ 6 files changed, 284 insertions(+), 1 deletion(-) diff --git a/src/ninsight.js b/src/ninsight.js index b9c2068..fe2e0e5 100644 --- a/src/ninsight.js +++ b/src/ninsight.js @@ -241,6 +241,79 @@ class Ninsight { else throw error } } + + /** + * @api Ninsight.txDetails() txDetails() + * @apiName Ninsight TX Details + * @apiGroup Ninsight + * @apiDescription Return transactions details for address(es). + * + * @apiExample Example usage: + * (async () => { + * try { + * let result = await bchjs.Ninsight.txDetails('fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33'); + * console.log(result); + * } catch(error) { + * console.error(error) + * } + * })() + * + * // [ + * // { + * // "txid": "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33", + * // "version": 1, + * // "locktime": 0, + * // "vin": [ + * // { + * // "sequence": 4294967295, + * // "n": 0 + * // } + * // ], + * // "vout": [ + * // ... + * // ], + * // "blockhash": "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", + * // "blockheight": 1000, + * // "confirmations": 659909, + * // "time": 1232346882, + * // "blocktime": 1232346882, + * // "firstSeenTime": null, + * // "valueOut": 50, + * // "size": 135 + * // } + * // ] + * + */ + async txDetails(txid) { + try { + if (typeof txid === "string") { + const response = await axios.post( + `${this.ninsightURL}/transaction/details`, + { + txids: [txid] + }, + _this.axiosOptions + ) + + return response.data + } else if (Array.isArray(txid)) { + const response = await axios.post( + `${this.ninsightURL}/transaction/details`, + { + txids: txid + }, + _this.axiosOptions + ) + + return response.data + } + + throw new Error(`Transaction ID must be a string or array of strings.`) + } catch (error) { + if (error.response && error.response.data) throw error.response.data + else throw error + } + } } module.exports = Ninsight diff --git a/test/integration/bchn/ninsight.js b/test/integration/bchn/ninsight.js index a938b97..75639af 100644 --- a/test/integration/bchn/ninsight.js +++ b/test/integration/bchn/ninsight.js @@ -89,4 +89,41 @@ describe(`#Ninsight`, () => { assert.property(result[0].txs[0], "vout") }) }) + describe(`#txDetails`, () => { + it(`should POST transactions details for a single address`, async () => { + const txid = "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33" + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "version") + assert.property(result[0], "locktime") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + assert.property(result[0], "blockhash") + assert.property(result[0], "blockheight") + assert.property(result[0], "confirmations") + assert.property(result[0], "time") + assert.property(result[0], "blocktime") + assert.property(result[0], "isCoinBase") + assert.property(result[0], "valueOut") + assert.property(result[0], "size") + }) + it(`should POST transactions details for an array of addresses`, async () => { + const txid = [ + "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33", + "4db095f34d632a4daf942142c291f1f2abb5ba2e1ccac919d85bdc2f671fb251" + ] + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + }) + }) }) diff --git a/test/integration/ninsight.js b/test/integration/ninsight.js index 06f5b95..911fef0 100644 --- a/test/integration/ninsight.js +++ b/test/integration/ninsight.js @@ -95,6 +95,42 @@ describe(`#Ninsight`, () => { assert.property(result[0].txs[0], "vout") }) }) + describe(`#txDetails`, () => { + it(`should POST transactions details for a single address`, async () => { + const txid = "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33" + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "version") + assert.property(result[0], "locktime") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + assert.property(result[0], "blockhash") + assert.property(result[0], "blockheight") + assert.property(result[0], "confirmations") + assert.property(result[0], "time") + assert.property(result[0], "blocktime") + assert.property(result[0], "valueOut") + assert.property(result[0], "size") + }) + it(`should POST transactions details for an array of addresses`, async () => { + const txid = [ + "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33", + "4db095f34d632a4daf942142c291f1f2abb5ba2e1ccac919d85bdc2f671fb251" + ] + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + }) + }) }) function sleep(ms) { diff --git a/test/integration/testnet/ninsight.js b/test/integration/testnet/ninsight.js index e417ada..9363b29 100644 --- a/test/integration/testnet/ninsight.js +++ b/test/integration/testnet/ninsight.js @@ -103,6 +103,42 @@ describe(`#Ninsight`, () => { assert.property(result[0].txs[0], "vout") }) }) + describe(`#txDetails`, () => { + it(`should POST transactions details for a single address`, async () => { + const txid = "76856d82e00b2696acd8d989e1fa6c46b431005046a285ce905814cac0ff8fea" + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "version") + assert.property(result[0], "locktime") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + assert.property(result[0], "blockhash") + assert.property(result[0], "blockheight") + assert.property(result[0], "confirmations") + assert.property(result[0], "time") + assert.property(result[0], "blocktime") + assert.property(result[0], "valueOut") + assert.property(result[0], "size") + }) + it(`should POST transactions details for an array of addresses`, async () => { + const txid = [ + "f191d1062789d7071da6f2168ba306869a829294f6b1c09d03492db4ca3a8e77", + "76856d82e00b2696acd8d989e1fa6c46b431005046a285ce905814cac0ff8fea" + ] + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + }) + }) }) function sleep(ms) { diff --git a/test/unit/fixtures/ninsight-mock.js b/test/unit/fixtures/ninsight-mock.js index ca6e1cd..d261f7e 100644 --- a/test/unit/fixtures/ninsight-mock.js +++ b/test/unit/fixtures/ninsight-mock.js @@ -116,11 +116,55 @@ const transactions = { const transactionsPost = [transactions, transactions] +const details = { + txid: "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33", + version: 1, + locktime: 0, + vin: [ + { + coinbase: "04ffff001d02fd04", + sequence: 4294967295, + n: 0 + } + ], + vout: [ + { + value: "50.00000000", + n: 0, + scriptPubKey: { + hex: + "4104f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446aac", + asm: + "04f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446a OP_CHECKSIG", + addresses: ["1BW18n7MfpU35q4MTBSk8pse3XzQF8XvzT"], + type: "pubkeyhash", + cashAddrs: ["bitcoincash:qpej6mkrwca4tvy2snq4crhrf88v4ljspysx0ueetk"] + }, + spentTxId: null, + spentIndex: null, + spentHeight: null + } + ], + blockhash: + "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09", + blockheight: 1000, + confirmations: 585610, + time: 1232346882, + blocktime: 1232346882, + isCoinBase: true, + valueOut: 50, + size: 135 +} + +const detailsPost = [details, details] + module.exports = { utxo, utxoPost, unconfirmed, unconfirmedPost, transactions, - transactionsPost + transactionsPost, + details, + detailsPost } diff --git a/test/unit/ninsight.js b/test/unit/ninsight.js index 67916e0..9670586 100644 --- a/test/unit/ninsight.js +++ b/test/unit/ninsight.js @@ -195,4 +195,61 @@ describe(`#Ninsight`, () => { assert.property(result[0].txs[0], "txid") }) }) + describe(`#txDetails`, () => { + it(`should throw an error for improper input`, async () => { + try { + const txid = 12345 + + await bchjs.Ninsight.txDetails(txid) + assert.equal(true, false, "Unexpected result!") + } catch (err) { + //console.log(`err: `, err) + assert.include( + err.message, + `Transaction ID must be a string or array of strings.` + ) + } + }) + it(`should POST transaction details for a single TxID`, async () => { + // Stub the network call. + sandbox.stub(axios, "post").resolves({ data: mockData.detailsPost }) + + const txid = "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33" + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "version") + assert.property(result[0], "locktime") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + assert.property(result[0], "blockhash") + assert.property(result[0], "blockheight") + assert.property(result[0], "confirmations") + assert.property(result[0], "time") + assert.property(result[0], "blocktime") + assert.property(result[0], "isCoinBase") + assert.property(result[0], "valueOut") + assert.property(result[0], "size") + }) + it(`should POST transaction details for an array of TxIDs`, async () => { + // Stub the network call. + sandbox.stub(axios, "post").resolves({ data: mockData.detailsPost }) + + const txid = [ + "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33", + "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33" + ] + + const result = await bchjs.Ninsight.txDetails(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "txid") + assert.property(result[0], "vin") + assert.property(result[0], "vout") + }) + }) })