Merge pull request #1184 from braydonf/bug/NaN-output
Added precondition to transaction.to for a positive integer as an amount. Closes #1183
This commit is contained in:
commit
f268298f25
|
@ -7,6 +7,7 @@ var bufferUtil = require('../util/buffer');
|
||||||
var JSUtil = require('../util/js');
|
var JSUtil = require('../util/js');
|
||||||
var BufferWriter = require('../encoding/bufferwriter');
|
var BufferWriter = require('../encoding/bufferwriter');
|
||||||
var Script = require('../script');
|
var Script = require('../script');
|
||||||
|
var $ = require('../util/preconditions');
|
||||||
|
|
||||||
function Output(params) {
|
function Output(params) {
|
||||||
if (!(this instanceof Output)) {
|
if (!(this instanceof Output)) {
|
||||||
|
@ -50,6 +51,10 @@ Object.defineProperty(Output.prototype, 'satoshis', {
|
||||||
this._satoshisBN = BN.fromNumber(num);
|
this._satoshisBN = BN.fromNumber(num);
|
||||||
this._satoshis = num;
|
this._satoshis = num;
|
||||||
}
|
}
|
||||||
|
$.checkState(
|
||||||
|
JSUtil.isPositiveInteger(this._satoshis),
|
||||||
|
'Output satoshis is not a positive integer'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -639,6 +639,10 @@ Transaction.prototype.getChangeOutput = function() {
|
||||||
* @return {Transaction} this, for chaining
|
* @return {Transaction} this, for chaining
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.to = function(address, amount) {
|
Transaction.prototype.to = function(address, amount) {
|
||||||
|
$.checkArgument(
|
||||||
|
JSUtil.isPositiveInteger(amount),
|
||||||
|
'Amount is expected to be a positive integer'
|
||||||
|
);
|
||||||
this.addOutput(new Output({
|
this.addOutput(new Output({
|
||||||
script: Script(new Address(address)),
|
script: Script(new Address(address)),
|
||||||
satoshis: amount
|
satoshis: amount
|
||||||
|
|
|
@ -59,7 +59,7 @@ module.exports = {
|
||||||
* @param {Object} values - An object of properties
|
* @param {Object} values - An object of properties
|
||||||
* @return {Object} The target object
|
* @return {Object} The target object
|
||||||
*/
|
*/
|
||||||
defineImmutable: function defineImmutable(target, values){
|
defineImmutable: function defineImmutable(target, values) {
|
||||||
Object.keys(values).forEach(function(key){
|
Object.keys(values).forEach(function(key){
|
||||||
Object.defineProperty(target, key, {
|
Object.defineProperty(target, key, {
|
||||||
configurable: false,
|
configurable: false,
|
||||||
|
@ -68,5 +68,17 @@ module.exports = {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return target;
|
return target;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Checks that a value is a positive integer
|
||||||
|
*
|
||||||
|
* @param {*} value
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
isPositiveInteger: function isPositiveInteger(value) {
|
||||||
|
return typeof value === 'number' &&
|
||||||
|
isFinite(value) &&
|
||||||
|
Math.floor(value) === value &&
|
||||||
|
value >= 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,6 +30,52 @@ describe('Output', function() {
|
||||||
newOutput.satoshis.should.equal(100);
|
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) {
|
var expectEqualOutputs = function(a, b) {
|
||||||
a.satoshis.should.equal(b.satoshis);
|
a.satoshis.should.equal(b.satoshis);
|
||||||
a.script.toString().should.equal(b.script.toString());
|
a.script.toString().should.equal(b.script.toString());
|
||||||
|
|
|
@ -50,9 +50,10 @@ describe('Transaction', function() {
|
||||||
object.outputs[0].satoshis.should.equal(testAmount - 10000);
|
object.outputs[0].satoshis.should.equal(testAmount - 10000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can take a string argument as an amount', function() {
|
it('will not accept NaN as an amount', function() {
|
||||||
var stringTx = new Transaction().to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', '10000');
|
(function() {
|
||||||
(stringTx.outputAmount).should.equal(10000);
|
var stringTx = new Transaction().to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', NaN);
|
||||||
|
}).should.throw('Amount is expected to be a positive integer');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns the fee correctly', function() {
|
it('returns the fee correctly', function() {
|
||||||
|
|
|
@ -32,4 +32,47 @@ describe('js utils', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('isPositiveInteger', function() {
|
||||||
|
it('false for float', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger(0.1);
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('false for string float', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger('0.1');
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('false for string integer', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger('1');
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('false for negative integer', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger(-1);
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('false for negative integer string', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger('-1');
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('false for infinity', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger(Infinity);
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('false for NaN', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger(NaN);
|
||||||
|
a.should.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('true for positive integer', function() {
|
||||||
|
var a = JSUtil.isPositiveInteger(1000);
|
||||||
|
a.should.equal(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue