Browse Source

Merge branch 'ref/deps-updats'

patch-2
Chris Kleeschulte 7 years ago
parent
commit
484683f854
No known key found for this signature in database GPG Key ID: 33195D27EF6BDB7F
  1. 2
      .travis.yml
  2. 76142
      bitcore-lib.js
  3. 12
      lib/crypto/ecdsa.js
  4. 31
      lib/crypto/point.js
  5. 1
      lib/crypto/signature.js
  6. 2
      lib/encoding/base58.js
  7. 10
      lib/hdprivatekey.js
  8. 4
      lib/hdpublickey.js
  9. 2
      lib/networks.js
  10. 6
      lib/transaction/transaction.js
  11. 12
      package.json
  12. 5
      test/crypto/point.js
  13. 8
      test/publickey.js
  14. 4
      test/transaction/input/multisig.js
  15. 4
      test/transaction/input/multisigscripthash.js

2
.travis.yml

@ -2,7 +2,7 @@ language: node_js
sudo: false sudo: false
node_js: node_js:
- '6' - '6'
- '7' - '8'
before_install: before_install:
- npm install -g bower - npm install -g bower
- export DISPLAY=:99.0 - export DISPLAY=:99.0

76142
bitcore-lib.js

File diff suppressed because it is too large

12
lib/crypto/ecdsa.js

@ -143,7 +143,7 @@ ECDSA.prototype.toPublicKey = function() {
} }
// Compute -e from e // Compute -e from e
var eNeg = e.neg().mod(n); var eNeg = e.neg().umod(n);
// 1.6.1 Compute Q = r^-1 (sR - eG) // 1.6.1 Compute Q = r^-1 (sR - eG)
// Q = r^-1 (sR + -eG) // Q = r^-1 (sR + -eG)
@ -174,15 +174,15 @@ ECDSA.prototype.sigError = function() {
} : undefined); } : undefined);
var n = Point.getN(); var n = Point.getN();
var sinv = s.invm(n); var sinv = s.invm(n);
var u1 = sinv.mul(e).mod(n); var u1 = sinv.mul(e).umod(n);
var u2 = sinv.mul(r).mod(n); var u2 = sinv.mul(r).umod(n);
var p = Point.getG().mulAdd(u1, this.pubkey.point, u2); var p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
if (p.isInfinity()) { if (p.isInfinity()) {
return 'p is infinity'; return 'p is infinity';
} }
if (p.getX().mod(n).cmp(r) !== 0) { if (p.getX().umod(n).cmp(r) !== 0) {
return 'Invalid signature'; return 'Invalid signature';
} else { } else {
return false; return false;
@ -211,8 +211,8 @@ ECDSA.prototype._findSignature = function(d, e) {
badrs++; badrs++;
k = this.k; k = this.k;
Q = G.mul(k); Q = G.mul(k);
r = Q.x.mod(N); r = Q.x.umod(N);
s = k.invm(N).mul(e.add(d.mul(r))).mod(N); s = k.invm(N).mul(e.add(d.mul(r))).umod(N);
} while (r.cmp(BN.Zero) <= 0 || s.cmp(BN.Zero) <= 0); } while (r.cmp(BN.Zero) <= 0 || s.cmp(BN.Zero) <= 0);
s = ECDSA.toLowS(s); s = ECDSA.toLowS(s);

31
lib/crypto/point.js

@ -2,7 +2,9 @@
var BN = require('./bn'); var BN = require('./bn');
var BufferUtil = require('../util/buffer'); var BufferUtil = require('../util/buffer');
var ec = require('elliptic').curves.secp256k1;
var EC = require('elliptic').ec;
var ec = new EC('secp256k1');
var ecPoint = ec.curve.point.bind(ec.curve); var ecPoint = ec.curve.point.bind(ec.curve);
var ecPointFromX = ec.curve.pointFromX.bind(ec.curve); var ecPointFromX = ec.curve.pointFromX.bind(ec.curve);
@ -19,7 +21,11 @@ var ecPointFromX = ec.curve.pointFromX.bind(ec.curve);
* @constructor * @constructor
*/ */
var Point = function Point(x, y, isRed) { var Point = function Point(x, y, isRed) {
var point = ecPoint(x, y, isRed); try {
var point = ecPoint(x, y, isRed);
} catch (e) {
throw new Error('Invalid Point');
}
point.validate(); point.validate();
return point; return point;
}; };
@ -36,7 +42,11 @@ Point.prototype = Object.getPrototypeOf(ec.curve.point());
* @returns {Point} An instance of Point * @returns {Point} An instance of Point
*/ */
Point.fromX = function fromX(odd, x){ Point.fromX = function fromX(odd, x){
var point = ecPointFromX(odd, x); try {
var point = ecPointFromX(x, odd);
} catch (e) {
throw new Error('Invalid X');
}
point.validate(); point.validate();
return point; return point;
}; };
@ -102,22 +112,17 @@ Point.prototype.validate = function validate() {
throw new Error('Point cannot be equal to Infinity'); throw new Error('Point cannot be equal to Infinity');
} }
if (this.getX().cmp(BN.Zero) === 0 || this.getY().cmp(BN.Zero) === 0){ var p2;
throw new Error('Invalid x,y value for curve, cannot equal 0.'); try {
p2 = ecPointFromX(this.getX(), this.getY().isOdd());
} catch (e) {
throw new Error('Point does not lie on the curve');
} }
var p2 = ecPointFromX(this.getY().isOdd(), this.getX());
if (p2.y.cmp(this.y) !== 0) { if (p2.y.cmp(this.y) !== 0) {
throw new Error('Invalid y value for curve.'); throw new Error('Invalid y value for curve.');
} }
var xValidRange = (this.getX().gt(BN.Minus1) && this.getX().lt(Point.getN()));
var yValidRange = (this.getY().gt(BN.Minus1) && this.getY().lt(Point.getN()));
if ( !xValidRange || !yValidRange ) {
throw new Error('Point does not lie on the curve');
}
//todo: needs test case //todo: needs test case
if (!(this.mul(Point.getN()).isInfinity())) { if (!(this.mul(Point.getN()).isInfinity())) {

1
lib/crypto/signature.js

@ -25,6 +25,7 @@ var Signature = function Signature(r, s) {
Signature.prototype.set = function(obj) { Signature.prototype.set = function(obj) {
this.r = obj.r || this.r || undefined; this.r = obj.r || this.r || undefined;
this.s = obj.s || this.s || undefined; this.s = obj.s || this.s || undefined;
this.i = typeof obj.i !== 'undefined' ? obj.i : this.i; //public key recovery parameter in range [0, 3] this.i = typeof obj.i !== 'undefined' ? obj.i : this.i; //public key recovery parameter in range [0, 3]
this.compressed = typeof obj.compressed !== 'undefined' ? this.compressed = typeof obj.compressed !== 'undefined' ?
obj.compressed : this.compressed; //whether the recovered pubkey is compressed obj.compressed : this.compressed; //whether the recovered pubkey is compressed

2
lib/encoding/base58.js

@ -26,7 +26,7 @@ Base58.validCharacters = function validCharacters(chars) {
if (buffer.Buffer.isBuffer(chars)) { if (buffer.Buffer.isBuffer(chars)) {
chars = chars.toString(); chars = chars.toString();
} }
return _.all(_.map(chars, function(char) { return _.contains(ALPHABET, char); })); return _.every(_.map(chars, function(char) { return _.includes(ALPHABET, char); }));
}; };
Base58.prototype.set = function(obj) { Base58.prototype.set = function(obj) {

10
lib/hdprivatekey.js

@ -74,7 +74,7 @@ function HDPrivateKey(arg) {
HDPrivateKey.isValidPath = function(arg, hardened) { HDPrivateKey.isValidPath = function(arg, hardened) {
if (_.isString(arg)) { if (_.isString(arg)) {
var indexes = HDPrivateKey._getDerivationIndexes(arg); var indexes = HDPrivateKey._getDerivationIndexes(arg);
return indexes !== null && _.all(indexes, HDPrivateKey.isValidPath); return indexes !== null && _.every(indexes, HDPrivateKey.isValidPath);
} }
if (_.isNumber(arg)) { if (_.isNumber(arg)) {
@ -99,11 +99,11 @@ HDPrivateKey._getDerivationIndexes = function(path) {
var steps = path.split('/'); var steps = path.split('/');
// Special cases: // Special cases:
if (_.contains(HDPrivateKey.RootElementAlias, path)) { if (_.includes(HDPrivateKey.RootElementAlias, path)) {
return []; return [];
} }
if (!_.contains(HDPrivateKey.RootElementAlias, steps[0])) { if (!_.includes(HDPrivateKey.RootElementAlias, steps[0])) {
return null; return null;
} }
@ -123,7 +123,7 @@ HDPrivateKey._getDerivationIndexes = function(path) {
return index; return index;
}); });
return _.any(indexes, isNaN) ? null : indexes; return _.some(indexes, isNaN) ? null : indexes;
}; };
/** /**
@ -254,7 +254,7 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian
}); });
var chainCode = hash.slice(32, 64); var chainCode = hash.slice(32, 64);
var privateKey = leftPart.add(this.privateKey.toBigNumber()).mod(Point.getN()).toBuffer({ var privateKey = leftPart.add(this.privateKey.toBigNumber()).umod(Point.getN()).toBuffer({
size: 32 size: 32
}); });

4
lib/hdpublickey.js

@ -75,7 +75,7 @@ function HDPublicKey(arg) {
HDPublicKey.isValidPath = function(arg) { HDPublicKey.isValidPath = function(arg) {
if (_.isString(arg)) { if (_.isString(arg)) {
var indexes = HDPrivateKey._getDerivationIndexes(arg); var indexes = HDPrivateKey._getDerivationIndexes(arg);
return indexes !== null && _.all(indexes, HDPublicKey.isValidPath); return indexes !== null && _.every(indexes, HDPublicKey.isValidPath);
} }
if (_.isNumber(arg)) { if (_.isNumber(arg)) {
@ -184,7 +184,7 @@ HDPublicKey.prototype._deriveWithNumber = function(index, hardened) {
HDPublicKey.prototype._deriveFromString = function(path) { HDPublicKey.prototype._deriveFromString = function(path) {
/* jshint maxcomplexity: 8 */ /* jshint maxcomplexity: 8 */
if (_.contains(path, "'")) { if (_.includes(path, "'")) {
throw new hdErrors.InvalidIndexCantDeriveHardened(); throw new hdErrors.InvalidIndexCantDeriveHardened();
} else if (!HDPublicKey.isValidPath(path)) { } else if (!HDPublicKey.isValidPath(path)) {
throw new hdErrors.InvalidPath(path); throw new hdErrors.InvalidPath(path);

2
lib/networks.js

@ -38,7 +38,7 @@ function get(arg, keys) {
return networks[index][key] === arg; return networks[index][key] === arg;
}; };
for (var index in networks) { for (var index in networks) {
if (_.any(keys, containsArg)) { if (_.some(keys, containsArg)) {
return networks[index]; return networks[index];
} }
} }

6
lib/transaction/transaction.js

@ -541,7 +541,7 @@ Transaction.prototype.from = function(utxo, pubkeys, threshold) {
}); });
return this; return this;
} }
var exists = _.any(this.inputs, function(input) { var exists = _.some(this.inputs, function(input) {
// TODO: Maybe prevTxId should be a string? Or defined as read only property? // TODO: Maybe prevTxId should be a string? Or defined as read only property?
return input.prevTxId.toString('hex') === utxo.txId && input.outputIndex === utxo.outputIndex; return input.prevTxId.toString('hex') === utxo.txId && input.outputIndex === utxo.outputIndex;
}); });
@ -647,7 +647,7 @@ Transaction.prototype.uncheckedAddInput = function(input) {
* @return {boolean} * @return {boolean}
*/ */
Transaction.prototype.hasAllUtxoInfo = function() { Transaction.prototype.hasAllUtxoInfo = function() {
return _.all(this.inputs.map(function(input) { return _.every(this.inputs.map(function(input) {
return !!input.output; return !!input.output;
})); }));
}; };
@ -1102,7 +1102,7 @@ Transaction.prototype.isFullySigned = function() {
); );
} }
}); });
return _.all(_.map(this.inputs, function(input) { return _.every(_.map(this.inputs, function(input) {
return input.isFullySigned(); return input.isFullySigned();
})); }));
}; };

12
package.json

@ -34,15 +34,15 @@
"request": "browser-request" "request": "browser-request"
}, },
"dependencies": { "dependencies": {
"bn.js": "=2.0.4", "bn.js": "=4.11.8",
"bs58": "=2.0.0", "bs58": "=4.0.1",
"buffer-compare": "=1.0.0", "buffer-compare": "=1.1.1",
"elliptic": "=3.0.3", "elliptic": "=6.4.0",
"inherits": "=2.0.1", "inherits": "=2.0.1",
"lodash": "=3.10.1" "lodash": "=4.17.4"
}, },
"devDependencies": { "devDependencies": {
"bitcore-build": "bitpay/bitcore-build", "bitcore-build": "https://github.com/bitpay/bitcore-build.git#d4e8b2b2f1e2c065c3a807dcb6a6250f61d67ab3",
"brfs": "^1.2.0", "brfs": "^1.2.0",
"chai": "^1.10.0", "chai": "^1.10.0",
"gulp": "^3.8.10", "gulp": "^3.8.10",

5
test/crypto/point.js

@ -132,7 +132,7 @@ describe('Point', function() {
var y = '0000000000000000000000000000000000000000000000000000000000000000'; var y = '0000000000000000000000000000000000000000000000000000000000000000';
(function() { (function() {
var p = Point(x, y); var p = Point(x, y);
}).should.throw('Invalid x,y value for curve, cannot equal 0.'); }).should.throw('Invalid y value for curve.');
}); });
@ -160,13 +160,12 @@ describe('Point', function() {
it('should describe this point as invalid because out of curve bounds', function() { it('should describe this point as invalid because out of curve bounds', function() {
// point larger than max
var x = '0000000000000000000000000000000000000000000000000000000000000000'; var x = '0000000000000000000000000000000000000000000000000000000000000000';
(function() { (function() {
// set the point // set the point
var p = Point.fromX(false, x); var p = Point.fromX(false, x);
}).should.throw('Invalid x,y value for curve, cannot equal 0.'); }).should.throw('Invalid X');
}); });
}); });

8
test/publickey.js

@ -28,13 +28,13 @@ describe('PublicKey', function() {
it('errors if an invalid point is provided', function() { it('errors if an invalid point is provided', function() {
(function() { (function() {
return new PublicKey(invalidPoint); return new PublicKey(invalidPoint);
}).should.throw('Invalid x,y value for curve, cannot equal 0.'); }).should.throw('Point does not lie on the curve');
}); });
it('errors if a point not on the secp256k1 curve is provided', function() { it('errors if a point not on the secp256k1 curve is provided', function() {
(function() { (function() {
return new PublicKey(new Point(1000, 1000)); return new PublicKey(new Point(1000, 1000));
}).should.throw('Invalid y value for curve.'); }).should.throw('Point does not lie on the curve');
}); });
it('errors if the argument is of an unrecognized type', function() { it('errors if the argument is of an unrecognized type', function() {
@ -132,7 +132,7 @@ describe('PublicKey', function() {
it('should recieve an invalid point error', function() { it('should recieve an invalid point error', function() {
var error = PublicKey.getValidationError(invalidPoint); var error = PublicKey.getValidationError(invalidPoint);
should.exist(error); should.exist(error);
error.message.should.equal('Invalid x,y value for curve, cannot equal 0.'); error.message.should.equal('Point does not lie on the curve');
}); });
it('should recieve a boolean as false', function() { it('should recieve a boolean as false', function() {
@ -409,7 +409,7 @@ describe('PublicKey', function() {
var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000'; var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
(function() { (function() {
return PublicKey.fromString(hex); return PublicKey.fromString(hex);
}).should.throw('Invalid x,y value for curve, cannot equal 0.'); }).should.throw('Invalid y value for curve.');
}); });
it('should throw an error if pubkey is invalid', function() { it('should throw an error if pubkey is invalid', function() {

4
test/transaction/input/multisig.js

@ -71,14 +71,14 @@ describe('MultiSigInput', function() {
.to(address, 1000000); .to(address, 1000000);
var input = transaction.inputs[0]; var input = transaction.inputs[0];
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) { _.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString(); var serialized = publicKeyMissing.toString();
return serialized === public1.toString() || return serialized === public1.toString() ||
serialized === public2.toString() || serialized === public2.toString() ||
serialized === public3.toString(); serialized === public3.toString();
}).should.equal(true); }).should.equal(true);
transaction.sign(privateKey1); transaction.sign(privateKey1);
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) { _.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString(); var serialized = publicKeyMissing.toString();
return serialized === public2.toString() || return serialized === public2.toString() ||
serialized === public3.toString(); serialized === public3.toString();

4
test/transaction/input/multisigscripthash.js

@ -54,14 +54,14 @@ describe('MultiSigScriptHashInput', function() {
.to(address, 1000000); .to(address, 1000000);
var input = transaction.inputs[0]; var input = transaction.inputs[0];
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) { _.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString(); var serialized = publicKeyMissing.toString();
return serialized === public1.toString() || return serialized === public1.toString() ||
serialized === public2.toString() || serialized === public2.toString() ||
serialized === public3.toString(); serialized === public3.toString();
}).should.equal(true); }).should.equal(true);
transaction.sign(privateKey1); transaction.sign(privateKey1);
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) { _.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString(); var serialized = publicKeyMissing.toString();
return serialized === public2.toString() || return serialized === public2.toString() ||
serialized === public3.toString(); serialized === public3.toString();

Loading…
Cancel
Save