Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Ref/deps updates #167

Merged
merged 7 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: node_js
sudo: false
node_js:
- '6'
- '7'
- '8'
before_install:
- npm install -g bower
- export DISPLAY=:99.0
Expand Down
76,310 changes: 37,693 additions & 38,617 deletions bitcore-lib.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/crypto/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ ECDSA.prototype.toPublicKey = function() {
}

// 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)
// Q = r^-1 (sR + -eG)
Expand Down Expand Up @@ -174,15 +174,15 @@ ECDSA.prototype.sigError = function() {
} : undefined);
var n = Point.getN();
var sinv = s.invm(n);
var u1 = sinv.mul(e).mod(n);
var u2 = sinv.mul(r).mod(n);
var u1 = sinv.mul(e).umod(n);
var u2 = sinv.mul(r).umod(n);

var p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
if (p.isInfinity()) {
return 'p is infinity';
}

if (p.getX().mod(n).cmp(r) !== 0) {
if (p.getX().umod(n).cmp(r) !== 0) {
return 'Invalid signature';
} else {
return false;
Expand Down Expand Up @@ -211,8 +211,8 @@ ECDSA.prototype._findSignature = function(d, e) {
badrs++;
k = this.k;
Q = G.mul(k);
r = Q.x.mod(N);
s = k.invm(N).mul(e.add(d.mul(r))).mod(N);
r = Q.x.umod(N);
s = k.invm(N).mul(e.add(d.mul(r))).umod(N);
} while (r.cmp(BN.Zero) <= 0 || s.cmp(BN.Zero) <= 0);

s = ECDSA.toLowS(s);
Expand Down
31 changes: 18 additions & 13 deletions lib/crypto/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

var BN = require('./bn');
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 ecPointFromX = ec.curve.pointFromX.bind(ec.curve);

Expand All @@ -19,7 +21,11 @@ var ecPointFromX = ec.curve.pointFromX.bind(ec.curve);
* @constructor
*/
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();
return point;
};
Expand All @@ -36,7 +42,11 @@ Point.prototype = Object.getPrototypeOf(ec.curve.point());
* @returns {Point} An instance of Point
*/
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();
return point;
};
Expand Down Expand Up @@ -102,22 +112,17 @@ Point.prototype.validate = function validate() {
throw new Error('Point cannot be equal to Infinity');
}

if (this.getX().cmp(BN.Zero) === 0 || this.getY().cmp(BN.Zero) === 0){
throw new Error('Invalid x,y value for curve, cannot equal 0.');
var p2;
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) {
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
if (!(this.mul(Point.getN()).isInfinity())) {
Expand Down
1 change: 1 addition & 0 deletions lib/crypto/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var Signature = function Signature(r, s) {
Signature.prototype.set = function(obj) {
this.r = obj.r || this.r || 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.compressed = typeof obj.compressed !== 'undefined' ?
obj.compressed : this.compressed; //whether the recovered pubkey is compressed
Expand Down
2 changes: 1 addition & 1 deletion lib/encoding/base58.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Base58.validCharacters = function validCharacters(chars) {
if (buffer.Buffer.isBuffer(chars)) {
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) {
Expand Down
10 changes: 5 additions & 5 deletions lib/hdprivatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function HDPrivateKey(arg) {
HDPrivateKey.isValidPath = function(arg, hardened) {
if (_.isString(arg)) {
var indexes = HDPrivateKey._getDerivationIndexes(arg);
return indexes !== null && _.all(indexes, HDPrivateKey.isValidPath);
return indexes !== null && _.every(indexes, HDPrivateKey.isValidPath);
}

if (_.isNumber(arg)) {
Expand All @@ -99,11 +99,11 @@ HDPrivateKey._getDerivationIndexes = function(path) {
var steps = path.split('/');

// Special cases:
if (_.contains(HDPrivateKey.RootElementAlias, path)) {
if (_.includes(HDPrivateKey.RootElementAlias, path)) {
return [];
}

if (!_.contains(HDPrivateKey.RootElementAlias, steps[0])) {
if (!_.includes(HDPrivateKey.RootElementAlias, steps[0])) {
return null;
}

Expand All @@ -123,7 +123,7 @@ HDPrivateKey._getDerivationIndexes = function(path) {
return index;
});

return _.any(indexes, isNaN) ? null : indexes;
return _.some(indexes, isNaN) ? null : indexes;
};

/**
Expand Down Expand Up @@ -254,7 +254,7 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonComplian
});
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
});

Expand Down
4 changes: 2 additions & 2 deletions lib/hdpublickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function HDPublicKey(arg) {
HDPublicKey.isValidPath = function(arg) {
if (_.isString(arg)) {
var indexes = HDPrivateKey._getDerivationIndexes(arg);
return indexes !== null && _.all(indexes, HDPublicKey.isValidPath);
return indexes !== null && _.every(indexes, HDPublicKey.isValidPath);
}

if (_.isNumber(arg)) {
Expand Down Expand Up @@ -184,7 +184,7 @@ HDPublicKey.prototype._deriveWithNumber = function(index, hardened) {

HDPublicKey.prototype._deriveFromString = function(path) {
/* jshint maxcomplexity: 8 */
if (_.contains(path, "'")) {
if (_.includes(path, "'")) {
throw new hdErrors.InvalidIndexCantDeriveHardened();
} else if (!HDPublicKey.isValidPath(path)) {
throw new hdErrors.InvalidPath(path);
Expand Down
2 changes: 1 addition & 1 deletion lib/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function get(arg, keys) {
return networks[index][key] === arg;
};
for (var index in networks) {
if (_.any(keys, containsArg)) {
if (_.some(keys, containsArg)) {
return networks[index];
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ Transaction.prototype.from = function(utxo, pubkeys, threshold) {
});
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?
return input.prevTxId.toString('hex') === utxo.txId && input.outputIndex === utxo.outputIndex;
});
Expand Down Expand Up @@ -647,7 +647,7 @@ Transaction.prototype.uncheckedAddInput = function(input) {
* @return {boolean}
*/
Transaction.prototype.hasAllUtxoInfo = function() {
return _.all(this.inputs.map(function(input) {
return _.every(this.inputs.map(function(input) {
return !!input.output;
}));
};
Expand Down Expand Up @@ -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();
}));
};
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
"request": "browser-request"
},
"dependencies": {
"bn.js": "=2.0.4",
"bs58": "=2.0.0",
"buffer-compare": "=1.0.0",
"elliptic": "=3.0.3",
"bn.js": "=4.11.8",
"bs58": "=4.0.1",
"buffer-compare": "=1.1.1",
"elliptic": "=6.4.0",
"inherits": "=2.0.1",
"lodash": "=3.10.1"
"lodash": "=4.17.4"
},
"devDependencies": {
"bitcore-build": "bitpay/bitcore-build",
"bitcore-build": "https://github.com/bitpay/bitcore-build.git#d4e8b2b2f1e2c065c3a807dcb6a6250f61d67ab3",
"brfs": "^1.2.0",
"chai": "^1.10.0",
"gulp": "^3.8.10",
Expand Down
5 changes: 2 additions & 3 deletions test/crypto/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Point', function() {
var y = '0000000000000000000000000000000000000000000000000000000000000000';
(function() {
var p = Point(x, y);
}).should.throw('Invalid x,y value for curve, cannot equal 0.');
}).should.throw('Invalid y value for curve.');
});


Expand Down Expand Up @@ -160,13 +160,12 @@ describe('Point', function() {

it('should describe this point as invalid because out of curve bounds', function() {

// point larger than max
var x = '0000000000000000000000000000000000000000000000000000000000000000';

(function() {
// set the point
var p = Point.fromX(false, x);
}).should.throw('Invalid x,y value for curve, cannot equal 0.');
}).should.throw('Invalid X');
});

});
Expand Down
8 changes: 4 additions & 4 deletions test/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ describe('PublicKey', function() {
it('errors if an invalid point is provided', function() {
(function() {
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() {
(function() {
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() {
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('PublicKey', function() {
it('should recieve an invalid point error', function() {
var error = PublicKey.getValidationError(invalidPoint);
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() {
Expand Down Expand Up @@ -409,7 +409,7 @@ describe('PublicKey', function() {
var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
(function() {
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() {
Expand Down
4 changes: 2 additions & 2 deletions test/transaction/input/multisig.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ describe('MultiSigInput', function() {
.to(address, 1000000);
var input = transaction.inputs[0];

_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
_.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString();
return serialized === public1.toString() ||
serialized === public2.toString() ||
serialized === public3.toString();
}).should.equal(true);
transaction.sign(privateKey1);
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
_.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString();
return serialized === public2.toString() ||
serialized === public3.toString();
Expand Down
4 changes: 2 additions & 2 deletions test/transaction/input/multisigscripthash.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ describe('MultiSigScriptHashInput', function() {
.to(address, 1000000);
var input = transaction.inputs[0];

_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
_.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString();
return serialized === public1.toString() ||
serialized === public2.toString() ||
serialized === public3.toString();
}).should.equal(true);
transaction.sign(privateKey1);
_.all(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
_.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) {
var serialized = publicKeyMissing.toString();
return serialized === public2.toString() ||
serialized === public3.toString();
Expand Down