diff --git a/lib/transaction/output.js b/lib/transaction/output.js index b5493c6..66385fc 100644 --- a/lib/transaction/output.js +++ b/lib/transaction/output.js @@ -7,6 +7,7 @@ var bufferUtil = require('../util/buffer'); var JSUtil = require('../util/js'); var BufferWriter = require('../encoding/bufferwriter'); var Script = require('../script'); +var $ = require('../util/preconditions'); function Output(params) { if (!(this instanceof Output)) { @@ -50,6 +51,10 @@ Object.defineProperty(Output.prototype, 'satoshis', { this._satoshisBN = BN.fromNumber(num); this._satoshis = num; } + $.checkState( + JSUtil.isPositiveInteger(this._satoshis), + 'Output satoshis is not a positive integer' + ); } }); diff --git a/test/transaction/output.js b/test/transaction/output.js index 93a35c1..fdade46 100644 --- a/test/transaction/output.js +++ b/test/transaction/output.js @@ -30,6 +30,52 @@ describe('Output', function() { newOutput.satoshis.should.equal(100); }); + it('can be assigned a satoshi amount with a string', function() { + var newOutput = new Output({ + satoshis: '100', + script: Script.empty() + }); + newOutput.satoshis.should.equal(100); + }); + + describe('will error if output is not a positive integer', function() { + it('-100', function() { + (function() { + var newOutput = new Output({ + satoshis: -100, + script: Script.empty() + }); + }).should.throw('Output satoshis is not a positive integer'); + }); + + it('1.1', function() { + (function() { + var newOutput = new Output({ + satoshis: 1.1, + script: Script.empty() + }); + }).should.throw('Output satoshis is not a positive integer'); + }); + + it('NaN', function() { + (function() { + var newOutput = new Output({ + satoshis: NaN, + script: Script.empty() + }); + }).should.throw('Output satoshis is not a positive integer'); + }); + + it('Infinity', function() { + (function() { + var newOutput = new Output({ + satoshis: Infinity, + script: Script.empty() + }); + }).should.throw('Output satoshis is not a positive integer'); + }); + }); + var expectEqualOutputs = function(a, b) { a.satoshis.should.equal(b.satoshis); a.script.toString().should.equal(b.script.toString());