From 8f9d59fb183d7e27adf239ec7afa0e435715ba2f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 19 Aug 2015 15:12:55 +1000 Subject: [PATCH] ECPair: stop exposing ECDSA curve --- src/ecpair.js | 33 +++++++++++++++++---------------- test/ecpair.js | 22 ++++------------------ test/integration/crypto.js | 11 +++++++---- 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/ecpair.js b/src/ecpair.js index bafc311..4599039 100644 --- a/src/ecpair.js +++ b/src/ecpair.js @@ -1,25 +1,29 @@ -var bs58check = require('bs58check') var bcrypto = require('./crypto') +var bs58check = require('bs58check') var ecdsa = require('./ecdsa') var ecurve = require('ecurve') -var NETWORKS = require('./networks') var randomBytes = require('randombytes') var typeforce = require('typeforce') var types = require('./types') +var NETWORKS = require('./networks') var BigInteger = require('bigi') +var secp256k1 = ecurve.getCurveByName('secp256k1') + function ECPair (d, Q, options) { - options = options || {} + if (options) { + typeforce({ + compressed: types.maybe(types.Boolean), + network: types.maybe(types.Network) + }, options) + } - typeforce({ - compressed: types.maybe(types.Boolean), - network: types.maybe(types.Network) - }, options) + options = options || {} if (d) { if (d.signum() <= 0) throw new Error('Private key must be greater than 0') - if (d.compareTo(ECPair.curve.n) >= 0) throw new Error('Private key must be less than the curve order') + if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order') if (Q) throw new TypeError('Unexpected publicKey parameter') this.d = d @@ -37,18 +41,15 @@ function ECPair (d, Q, options) { Object.defineProperty(ECPair.prototype, 'Q', { get: function () { if (!this.__Q && this.d) { - this.__Q = ECPair.curve.G.multiply(this.d) + this.__Q = secp256k1.G.multiply(this.d) } return this.__Q } }) -// Public access to secp256k1 curve -ECPair.curve = ecurve.getCurveByName('secp256k1') - ECPair.fromPublicKeyBuffer = function (buffer, network) { - var Q = ecurve.Point.decodeFrom(ECPair.curve, buffer) + var Q = ecurve.Point.decodeFrom(secp256k1, buffer) return new ECPair(null, Q, { compressed: Q.compressed, @@ -108,7 +109,7 @@ ECPair.makeRandom = function (options) { typeforce(types.Buffer256bit, buffer) var d = BigInteger.fromBuffer(buffer) - d = d.mod(ECPair.curve.n) + d = d.mod(secp256k1.n) return new ECPair(d, null, options) } @@ -147,11 +148,11 @@ ECPair.prototype.getPublicKeyBuffer = function () { ECPair.prototype.sign = function (hash) { if (!this.d) throw new Error('Missing private key') - return ecdsa.sign(ECPair.curve, hash, this.d) + return ecdsa.sign(secp256k1, hash, this.d) } ECPair.prototype.verify = function (hash, signature) { - return ecdsa.verify(ECPair.curve, hash, signature, this.Q) + return ecdsa.verify(secp256k1, hash, signature, this.Q) } module.exports = ECPair diff --git a/test/ecpair.js b/test/ecpair.js index 8c09a9c..b64f1d2 100644 --- a/test/ecpair.js +++ b/test/ecpair.js @@ -11,6 +11,7 @@ var BigInteger = require('bigi') var ECPair = require('../src/ecpair') var fixtures = require('./fixtures/ecpair.json') +var secp256k1 = ecurve.getCurveByName('secp256k1') var NETWORKS = require('../src/networks') var NETWORKS_LIST = [] // Object.values(NETWORKS) @@ -53,7 +54,7 @@ describe('ECPair', function () { it('throws if public and private key given', function () { var qBuffer = new Buffer('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 'hex') - var Q = ecurve.Point.decodeFrom(ECPair.curve, qBuffer) + var Q = ecurve.Point.decodeFrom(secp256k1, qBuffer) assert.throws(function () { new ECPair(BigInteger.ONE, Q) @@ -200,25 +201,10 @@ describe('ECPair', function () { hash = new Buffer(32) }) - it('uses the secp256k1 curve by default', function () { - var secp256k1 = ecurve.getCurveByName('secp256k1') - - for (var property in secp256k1) { - // FIXME: circular structures in ecurve - if (property === 'G') continue - if (property === 'infinity') continue - - var actual = ECPair.curve[property] - var expected = secp256k1[property] - - assert.deepEqual(actual, expected) - } - }) - describe('signing', function () { it('wraps ecdsa.sign', sinon.test(function () { this.mock(ecdsa).expects('sign') - .once().calledWith(ECPair.curve, hash, keyPair.d) + .once().calledWith(secp256k1, hash, keyPair.d) keyPair.sign(hash) })) @@ -241,7 +227,7 @@ describe('ECPair', function () { it('wraps ecdsa.verify', sinon.test(function () { this.mock(ecdsa).expects('verify') - .once().calledWith(ECPair.curve, hash, signature, keyPair.Q) + .once().calledWith(secp256k1, hash, signature, keyPair.Q) keyPair.verify(hash, signature) })) diff --git a/test/integration/crypto.js b/test/integration/crypto.js index 96c66ae..6bc0e88 100644 --- a/test/integration/crypto.js +++ b/test/integration/crypto.js @@ -7,10 +7,13 @@ var bitcoin = require('../../') var blockchain = require('./_blockchain') var crypto = require('crypto') +var ecurve = require('ecurve') +var secp256k1 = ecurve.getCurveByName('secp256k1') + describe('bitcoinjs-lib (crypto)', function () { it('can generate a single-key stealth address', function () { - var G = bitcoin.ECPair.curve.G - var n = bitcoin.ECPair.curve.n + var G = secp256k1.G + var n = secp256k1.n function stealthSend (Q) { var noncePair = bitcoin.ECPair.makeRandom() @@ -56,7 +59,7 @@ describe('bitcoinjs-lib (crypto)', function () { assert(!master.keyPair.d, 'You already have the parent private key') assert(child.keyPair.d, 'Missing child private key') - var curve = bitcoin.ECPair.curve + var curve = secp256k1 var QP = master.keyPair.Q var serQP = master.keyPair.getPublicKeyBuffer() @@ -162,7 +165,7 @@ describe('bitcoinjs-lib (crypto)', function () { async.parallel(tasks, function (err) { if (err) throw err - var n = bitcoin.ECPair.curve.n + var n = secp256k1.n for (var i = 0; i < inputs.length; ++i) { for (var j = i + 1; j < inputs.length; ++j) {