diff --git a/lib/primitives/txmeta.js b/lib/primitives/txmeta.js index 195838c6..8b725c19 100644 --- a/lib/primitives/txmeta.js +++ b/lib/primitives/txmeta.js @@ -161,7 +161,7 @@ class TXMeta { json.time = this.time; json.confirmations = 0; - if (chainHeight != null) + if (chainHeight != null && this.height !== -1) json.confirmations = chainHeight - this.height + 1; return json; diff --git a/test/txmeta-test.js b/test/txmeta-test.js new file mode 100644 index 00000000..83ce4b07 --- /dev/null +++ b/test/txmeta-test.js @@ -0,0 +1,26 @@ +/* eslint-env mocha */ +/* eslint prefer-arrow-callback: "off" */ + +'use strict'; + +const assert = require('./util/assert'); +const Network = require('../lib/protocol/network'); +const TXMeta = require('../lib/primitives/txmeta'); + +const network = Network.get('regtest'); + + +describe('TXMeta', function() { + it('should return JSON for txmeta', async () => { + // unconfirmed at height 100 + const txmeta1 = new TXMeta(); + const txJSON1 = txmeta1.getJSON(network, null, 100) + assert.strictEqual(txJSON1.confirmations, 0); + + // confirmed once at height 100 + const txmeta2 = TXMeta.fromOptions( {height: 100} ); + txmeta2.height = 100; + const txJSON2 = txmeta2.getJSON(network, null, 100) + assert.strictEqual(txJSON2.confirmations, 1); + }); +}); \ No newline at end of file