diff --git a/src/blockchain.js b/src/blockchain.js index 6677797..a953181 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -68,8 +68,8 @@ class Blockchain { * @apiName getBlock * @apiGroup Blockchain * @apiDescription - * If verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'. If verbose is true, returns an Object with information about block hash. - * + * If verbose is 0, returns a string that is serialized, hex-encoded data for block 'hash'. If verbose is 1, returns an Object with information about block hash. + * If verbose is 2, returns an Object with information about block hash and information about tx. * @apiExample Example usage: * (async () => { * try { @@ -80,28 +80,38 @@ class Blockchain { * } * })() * - * // { hash: '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09', - * // confirmations: 528236, - * // size: 216, - * // height: 1000, - * // version: 1, - * // versionHex: '00000001', - * // merkleroot: 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33', - * // tx: - * // [ 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33' ], - * // time: 1232346882, - * // mediantime: 1232344831, - * // nonce: 2595206198, - * // bits: '1d00ffff', - * // difficulty: 1, - * // chainwork: '000000000000000000000000000000000000000000000000000003e903e903e9', - * // previousblockhash: '0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d', - * // nextblockhash: '00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6' } + * // { + * // hash: '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09', + * // confirmations: 528236, + * // size: 216, + * // height: 1000, + * // version: 1, + * // versionHex: '00000001', + * // merkleroot: 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33', + * // tx: + * // [ 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33' ], + * // time: 1232346882, + * // mediantime: 1232344831, + * // nonce: 2595206198, + * // bits: '1d00ffff', + * // difficulty: 1, + * // chainwork: '000000000000000000000000000000000000000000000000000003e903e903e9', + * // previousblockhash: '0000000008e647742775a230787d66fdf92c46a48c896bfbc85cdc8acc67e87d', + * // nextblockhash: '00000000a2887344f8db859e372e7e4bc26b23b9de340f725afbf2edb265b4c6' + * // } */ - async getBlock (blockhash, verbose = true) { + async getBlock (blockhash, verbosity = 1) { try { - const response = await axios.get( - `${this.restURL}blockchain/getBlock/${blockhash}?verbose=${verbose}`, + // Input validation + if (!blockhash || typeof blockhash !== 'string') { + throw new Error('blockhash must be a string') + } + const response = await axios.post( + `${this.restURL}blockchain/getblock`, + { + blockhash, + verbosity + }, _this.axiosOptions ) return response.data diff --git a/test/integration/blockchain.js b/test/integration/blockchain.js index 3cdb90d..6b44b0e 100644 --- a/test/integration/blockchain.js +++ b/test/integration/blockchain.js @@ -276,6 +276,43 @@ describe('#blockchain', () => { assert.equal(result, null) }) }) + describe('#getBlock', () => { + it('should get block information with default verbosity', async () => { + const blockhash = + '0000000000000000008e8d83cba6d45a9314bc2ef4538d4e0577c6bed8593536' + const result = await bchjs.Blockchain.getBlock(blockhash) + + assert.hasAllKeys(result, [ + 'hash', + 'confirmations', + 'size', + 'height', + 'version', + 'versionHex', + 'merkleroot', + 'tx', + 'time', + 'mediantime', + 'nonce', + 'bits', + 'difficulty', + 'chainwork', + 'nTx', + 'previousblockhash', + 'nextblockhash' + ]) + assert.isArray(result.tx) + }) + + it('should get block information with verbosity 0', async () => { + const blockhash = + '0000000000000000008e8d83cba6d45a9314bc2ef4538d4e0577c6bed8593536' + const verbosity = 0 + const result = await bchjs.Blockchain.getBlock(blockhash, verbosity) + + assert.isString(result) + }) + }) }) function sleep (ms) { diff --git a/test/unit/blockchain.js b/test/unit/blockchain.js index f1415e3..66cbf81 100644 --- a/test/unit/blockchain.js +++ b/test/unit/blockchain.js @@ -61,16 +61,40 @@ describe('#Blockchain', () => { it('should get block by hash', done => { const resolved = new Promise(resolve => resolve({ data: data })) - sandbox.stub(axios, 'get').returns(resolved) + sandbox.stub(axios, 'post').returns(resolved) - bchjs.Blockchain.getBlock( + const blockhash = '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09' - ) + bchjs.Blockchain.getBlock(blockhash) .then(result => { assert.deepStrictEqual(data, result) }) .then(done, done) }) + it('should throw error if blockhash is not provided', async () => { + try { + await bchjs.Blockchain.getBlock() + assert2.fail('Unexpected result') + } catch (err) { + assert2.include(err.message, 'blockhash must be a string') + } + }) + it('should handle response error', async () => { + try { + const error = new Error() + error.response = { + data: 'Test Error' + } + sandbox.stub(axios, 'post').throws(error) + + const blockhash = + '00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09' + await bchjs.Blockchain.getBlock(blockhash) + assert2.fail('Unexpected result') + } catch (err) { + assert2.include(err, 'Test Error') + } + }) }) describe('#getBlockchainInfo', () => { @@ -355,10 +379,7 @@ describe('#Blockchain', () => { try { await bchjs.Blockchain.getTxOut('badtxid') } catch (err) { - assert2.include( - err.message, - 'txid needs to be a proper transaction ID' - ) + assert2.include(err.message, 'txid needs to be a proper transaction ID') } })