Update bn.js to 2.0.4 to handle 53 bit precision number input.
This commit is contained in:
parent
a49348b027
commit
3d447ded79
|
@ -48,12 +48,16 @@ Object.defineProperty(Output.prototype, 'satoshis', {
|
||||||
this._satoshis = parseInt(num);
|
this._satoshis = parseInt(num);
|
||||||
this._satoshisBN = BN.fromNumber(this._satoshis);
|
this._satoshisBN = BN.fromNumber(this._satoshis);
|
||||||
} else {
|
} else {
|
||||||
|
$.checkArgument(
|
||||||
|
JSUtil.isNaturalNumber(num),
|
||||||
|
'Output satoshis is not a natural number'
|
||||||
|
);
|
||||||
this._satoshisBN = BN.fromNumber(num);
|
this._satoshisBN = BN.fromNumber(num);
|
||||||
this._satoshis = num;
|
this._satoshis = num;
|
||||||
}
|
}
|
||||||
$.checkState(
|
$.checkState(
|
||||||
JSUtil.isPositiveInteger(this._satoshis),
|
JSUtil.isNaturalNumber(this._satoshis),
|
||||||
'Output satoshis is not a positive integer'
|
'Output satoshis is not a natural number'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -60,6 +60,7 @@ function Transaction(serialized) {
|
||||||
|
|
||||||
var CURRENT_VERSION = 1;
|
var CURRENT_VERSION = 1;
|
||||||
var DEFAULT_NLOCKTIME = 0;
|
var DEFAULT_NLOCKTIME = 0;
|
||||||
|
var MAX_SAFE_INTEGER = 0x1fffffffffffff;
|
||||||
|
|
||||||
// Minimum amount for an output for it not to be considered a dust output
|
// Minimum amount for an output for it not to be considered a dust output
|
||||||
Transaction.DUST_AMOUNT = 546;
|
Transaction.DUST_AMOUNT = 546;
|
||||||
|
@ -644,7 +645,7 @@ Transaction.prototype.getChangeOutput = function() {
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.to = function(address, amount) {
|
Transaction.prototype.to = function(address, amount) {
|
||||||
$.checkArgument(
|
$.checkArgument(
|
||||||
JSUtil.isPositiveInteger(amount),
|
JSUtil.isNaturalNumber(amount),
|
||||||
'Amount is expected to be a positive integer'
|
'Amount is expected to be a positive integer'
|
||||||
);
|
);
|
||||||
this.addOutput(new Output({
|
this.addOutput(new Output({
|
||||||
|
@ -990,7 +991,16 @@ Transaction.prototype.verify = function() {
|
||||||
var valueoutbn = new BN(0);
|
var valueoutbn = new BN(0);
|
||||||
for (var i = 0; i < this.outputs.length; i++) {
|
for (var i = 0; i < this.outputs.length; i++) {
|
||||||
var txout = this.outputs[i];
|
var txout = this.outputs[i];
|
||||||
var valuebn = txout._satoshisBN;
|
|
||||||
|
if (txout._satoshis > MAX_SAFE_INTEGER) {
|
||||||
|
return 'transaction txout ' + i + ' satoshis greater than max safe integer';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (txout._satoshis !== txout._satoshisBN.toNumber()) {
|
||||||
|
return 'transaction txout ' + i + ' satoshis has corrupted value';
|
||||||
|
}
|
||||||
|
|
||||||
|
var valuebn = new BN(txout._satoshis, 10);
|
||||||
if (valuebn.lt(BN.Zero)) {
|
if (valuebn.lt(BN.Zero)) {
|
||||||
return 'transaction txout ' + i + ' negative';
|
return 'transaction txout ' + i + ' negative';
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,12 +70,12 @@ module.exports = {
|
||||||
return target;
|
return target;
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Checks that a value is a positive integer
|
* Checks that a value is a natural number, a positive integer or zero.
|
||||||
*
|
*
|
||||||
* @param {*} value
|
* @param {*} value
|
||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
isPositiveInteger: function isPositiveInteger(value) {
|
isNaturalNumber: function isNaturalNumber(value) {
|
||||||
return typeof value === 'number' &&
|
return typeof value === 'number' &&
|
||||||
isFinite(value) &&
|
isFinite(value) &&
|
||||||
Math.floor(value) === value &&
|
Math.floor(value) === value &&
|
||||||
|
|
|
@ -1,59 +1,44 @@
|
||||||
{
|
{
|
||||||
"name": "bitcore",
|
"name": "bitcore",
|
||||||
"version": "0.9.0",
|
"version": "0.12.3",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bn.js": {
|
"bn.js": {
|
||||||
"version": "0.16.1",
|
"version": "2.0.4",
|
||||||
"from": "bn.js@0.16.1",
|
"from": "bn.js@=2.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-0.16.1.tgz"
|
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-2.0.4.tgz"
|
||||||
},
|
},
|
||||||
"bs58": {
|
"bs58": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"from": "bs58@2.0.0",
|
"from": "bs58@=2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/bs58/-/bs58-2.0.0.tgz"
|
"resolved": "https://registry.npmjs.org/bs58/-/bs58-2.0.0.tgz"
|
||||||
},
|
},
|
||||||
"elliptic": {
|
"elliptic": {
|
||||||
"version": "0.16.0",
|
"version": "3.0.3",
|
||||||
"from": "elliptic@0.16.0",
|
"from": "elliptic@=3.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/elliptic/-/elliptic-0.16.0.tgz",
|
"resolved": "https://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bn.js": {
|
|
||||||
"version": "0.16.1",
|
|
||||||
"from": "bn.js@0.16.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-0.16.1.tgz"
|
|
||||||
},
|
|
||||||
"brorand": {
|
"brorand": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.5",
|
||||||
"from": "brorand@1.0.1",
|
"from": "brorand@^1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/brorand/-/brorand-1.0.1.tgz"
|
"resolved": "https://registry.npmjs.org/brorand/-/brorand-1.0.5.tgz"
|
||||||
|
},
|
||||||
|
"hash.js": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"from": "hash.js@^1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.0.2.tgz"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"hash.js": {
|
"hash.js": {
|
||||||
"version": "0.3.2",
|
"version": "0.3.2",
|
||||||
"from": "hash.js@0.3.2",
|
"from": "hash.js@=0.3.2",
|
||||||
"resolved": "https://registry.npmjs.org/hash.js/-/hash.js-0.3.2.tgz"
|
"resolved": "https://registry.npmjs.org/hash.js/-/hash.js-0.3.2.tgz"
|
||||||
},
|
},
|
||||||
"inherits": {
|
"inherits": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"from": "inherits@^2.0.1"
|
"from": "inherits@=2.0.1",
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"inherits": {
|
|
||||||
"version": "2.0.1",
|
|
||||||
"from": "inherits@2.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz"
|
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz"
|
||||||
},
|
},
|
||||||
"hash.js": {
|
|
||||||
"version": "0.3.2",
|
|
||||||
"from": "hash.js@0.3.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/hash.js/-/hash.js-0.3.2.tgz",
|
|
||||||
"dependencies": {
|
|
||||||
"inherits": {
|
|
||||||
"version": "2.0.1",
|
|
||||||
"from": "inherits@^2.0.1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"lodash": {
|
"lodash": {
|
||||||
"version": "2.4.1",
|
"version": "2.4.1",
|
||||||
"from": "lodash@=2.4.1",
|
"from": "lodash@=2.4.1",
|
||||||
|
|
|
@ -79,9 +79,9 @@
|
||||||
"request": "browser-request"
|
"request": "browser-request"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bn.js": "=0.16.1",
|
"bn.js": "=2.0.4",
|
||||||
"bs58": "=2.0.0",
|
"bs58": "=2.0.0",
|
||||||
"elliptic": "=0.16.0",
|
"elliptic": "=3.0.3",
|
||||||
"hash.js": "=0.3.2",
|
"hash.js": "=0.3.2",
|
||||||
"inherits": "=2.0.1",
|
"inherits": "=2.0.1",
|
||||||
"lodash": "=2.4.1",
|
"lodash": "=2.4.1",
|
||||||
|
|
|
@ -10,6 +10,7 @@ describe('Transaction deserialization', function() {
|
||||||
describe('valid transaction test case', function() {
|
describe('valid transaction test case', function() {
|
||||||
var index = 0;
|
var index = 0;
|
||||||
vectors_valid.forEach(function(vector) {
|
vectors_valid.forEach(function(vector) {
|
||||||
|
it('vector #' + index, function() {
|
||||||
if (vector.length > 1) {
|
if (vector.length > 1) {
|
||||||
var hexa = vector[1];
|
var hexa = vector[1];
|
||||||
Transaction(hexa).serialize(true).should.equal(hexa);
|
Transaction(hexa).serialize(true).should.equal(hexa);
|
||||||
|
@ -17,9 +18,11 @@ describe('Transaction deserialization', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
describe('invalid transaction test case', function() {
|
describe('invalid transaction test case', function() {
|
||||||
var index = 0;
|
var index = 0;
|
||||||
vectors_invalid.forEach(function(vector) {
|
vectors_invalid.forEach(function(vector) {
|
||||||
|
it('invalid vector #' + index, function() {
|
||||||
if (vector.length > 1) {
|
if (vector.length > 1) {
|
||||||
var hexa = vector[1];
|
var hexa = vector[1];
|
||||||
Transaction(hexa).serialize(true).should.equal(hexa);
|
Transaction(hexa).serialize(true).should.equal(hexa);
|
||||||
|
@ -28,3 +31,4 @@ describe('Transaction deserialization', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -45,7 +45,7 @@ describe('Output', function() {
|
||||||
satoshis: -100,
|
satoshis: -100,
|
||||||
script: Script.empty()
|
script: Script.empty()
|
||||||
});
|
});
|
||||||
}).should.throw('Output satoshis is not a positive integer');
|
}).should.throw('Output satoshis is not a natural number');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('1.1', function() {
|
it('1.1', function() {
|
||||||
|
@ -54,7 +54,7 @@ describe('Output', function() {
|
||||||
satoshis: 1.1,
|
satoshis: 1.1,
|
||||||
script: Script.empty()
|
script: Script.empty()
|
||||||
});
|
});
|
||||||
}).should.throw('Output satoshis is not a positive integer');
|
}).should.throw('Output satoshis is not a natural number');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('NaN', function() {
|
it('NaN', function() {
|
||||||
|
@ -63,7 +63,7 @@ describe('Output', function() {
|
||||||
satoshis: NaN,
|
satoshis: NaN,
|
||||||
script: Script.empty()
|
script: Script.empty()
|
||||||
});
|
});
|
||||||
}).should.throw('Output satoshis is not a positive integer');
|
}).should.throw('Output satoshis is not a natural number');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Infinity', function() {
|
it('Infinity', function() {
|
||||||
|
@ -72,7 +72,7 @@ describe('Output', function() {
|
||||||
satoshis: Infinity,
|
satoshis: Infinity,
|
||||||
script: Script.empty()
|
script: Script.empty()
|
||||||
});
|
});
|
||||||
}).should.throw('Output satoshis is not a positive integer');
|
}).should.throw('Output satoshis is not a natural number');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -32,44 +32,44 @@ describe('js utils', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('isPositiveInteger', function() {
|
describe('isNaturalNumber', function() {
|
||||||
it('false for float', function() {
|
it('false for float', function() {
|
||||||
var a = JSUtil.isPositiveInteger(0.1);
|
var a = JSUtil.isNaturalNumber(0.1);
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false for string float', function() {
|
it('false for string float', function() {
|
||||||
var a = JSUtil.isPositiveInteger('0.1');
|
var a = JSUtil.isNaturalNumber('0.1');
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false for string integer', function() {
|
it('false for string integer', function() {
|
||||||
var a = JSUtil.isPositiveInteger('1');
|
var a = JSUtil.isNaturalNumber('1');
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false for negative integer', function() {
|
it('false for negative integer', function() {
|
||||||
var a = JSUtil.isPositiveInteger(-1);
|
var a = JSUtil.isNaturalNumber(-1);
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false for negative integer string', function() {
|
it('false for negative integer string', function() {
|
||||||
var a = JSUtil.isPositiveInteger('-1');
|
var a = JSUtil.isNaturalNumber('-1');
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false for infinity', function() {
|
it('false for infinity', function() {
|
||||||
var a = JSUtil.isPositiveInteger(Infinity);
|
var a = JSUtil.isNaturalNumber(Infinity);
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('false for NaN', function() {
|
it('false for NaN', function() {
|
||||||
var a = JSUtil.isPositiveInteger(NaN);
|
var a = JSUtil.isNaturalNumber(NaN);
|
||||||
a.should.equal(false);
|
a.should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('true for positive integer', function() {
|
it('true for positive integer', function() {
|
||||||
var a = JSUtil.isPositiveInteger(1000);
|
var a = JSUtil.isNaturalNumber(1000);
|
||||||
a.should.equal(true);
|
a.should.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue