EC*Key: add .curve static property for public API
This commit is contained in:
parent
cba6c7a7b2
commit
9510af10e3
4 changed files with 45 additions and 8 deletions
13
src/eckey.js
13
src/eckey.js
|
@ -9,18 +9,21 @@ var BigInteger = require('bigi')
|
|||
var ECPubKey = require('./ecpubkey')
|
||||
|
||||
var ecurve = require('ecurve')
|
||||
var curve = ecurve.getCurveByName('secp256k1')
|
||||
var secp256k1 = ecurve.getCurveByName('secp256k1')
|
||||
|
||||
function ECKey(d, compressed) {
|
||||
assert(d.signum() > 0, 'Private key must be greater than 0')
|
||||
assert(d.compareTo(curve.n) < 0, 'Private key must be less than the curve order')
|
||||
assert(d.compareTo(ECKey.curve.n) < 0, 'Private key must be less than the curve order')
|
||||
|
||||
var Q = curve.G.multiply(d)
|
||||
var Q = ECKey.curve.G.multiply(d)
|
||||
|
||||
this.d = d
|
||||
this.pub = new ECPubKey(Q, compressed)
|
||||
}
|
||||
|
||||
// Constants
|
||||
ECKey.curve = secp256k1
|
||||
|
||||
// Static constructors
|
||||
ECKey.fromWIF = function(string) {
|
||||
var payload = base58check.decode(string)
|
||||
|
@ -51,7 +54,7 @@ ECKey.makeRandom = function(compressed, rng) {
|
|||
assert.equal(buffer.length, 32, 'Expected 256-bit Buffer from RNG')
|
||||
|
||||
var d = BigInteger.fromBuffer(buffer)
|
||||
d = d.mod(curve.n)
|
||||
d = d.mod(ECKey.curve.n)
|
||||
|
||||
return new ECKey(d, compressed)
|
||||
}
|
||||
|
@ -75,7 +78,7 @@ ECKey.prototype.toWIF = function(network) {
|
|||
|
||||
// Operations
|
||||
ECKey.prototype.sign = function(hash) {
|
||||
return ecdsa.sign(curve, hash, this.d)
|
||||
return ecdsa.sign(ECKey.curve, hash, this.d)
|
||||
}
|
||||
|
||||
module.exports = ECKey
|
||||
|
|
|
@ -6,7 +6,7 @@ var networks = require('./networks')
|
|||
var Address = require('./address')
|
||||
|
||||
var ecurve = require('ecurve')
|
||||
var curve = ecurve.getCurveByName('secp256k1')
|
||||
var secp256k1 = ecurve.getCurveByName('secp256k1')
|
||||
|
||||
function ECPubKey(Q, compressed) {
|
||||
if (compressed === undefined) compressed = true
|
||||
|
@ -18,9 +18,12 @@ function ECPubKey(Q, compressed) {
|
|||
this.Q = Q
|
||||
}
|
||||
|
||||
// Constants
|
||||
ECPubKey.curve = secp256k1
|
||||
|
||||
// Static constructors
|
||||
ECPubKey.fromBuffer = function(buffer) {
|
||||
var Q = ecurve.Point.decodeFrom(curve, buffer)
|
||||
var Q = ecurve.Point.decodeFrom(ECPubKey.curve, buffer)
|
||||
return new ECPubKey(Q, Q.compressed)
|
||||
}
|
||||
|
||||
|
@ -36,7 +39,7 @@ ECPubKey.prototype.getAddress = function(network) {
|
|||
}
|
||||
|
||||
ECPubKey.prototype.verify = function(hash, signature) {
|
||||
return ecdsa.verify(curve, hash, signature, this.Q)
|
||||
return ecdsa.verify(ECPubKey.curve, hash, signature, this.Q)
|
||||
}
|
||||
|
||||
// Export functions
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
var assert = require('assert')
|
||||
var crypto = require('crypto')
|
||||
var ecurve = require('ecurve')
|
||||
var networks = require('../src/networks')
|
||||
var sinon = require('sinon')
|
||||
|
||||
|
@ -42,6 +43,21 @@ describe('ECKey', function() {
|
|||
})
|
||||
})
|
||||
|
||||
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 = ECKey.curve[property]
|
||||
var expected = secp256k1[property]
|
||||
|
||||
assert.deepEqual(actual, expected)
|
||||
}
|
||||
})
|
||||
|
||||
describe('fromWIF', function() {
|
||||
fixtures.valid.forEach(function(f) {
|
||||
f.WIFs.forEach(function(wif) {
|
||||
|
|
|
@ -35,6 +35,21 @@ describe('ECPubKey', function() {
|
|||
})
|
||||
})
|
||||
|
||||
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 = ECPubKey.curve[property]
|
||||
var expected = secp256k1[property]
|
||||
|
||||
assert.deepEqual(actual, expected)
|
||||
}
|
||||
})
|
||||
|
||||
describe('fromHex/toHex', function() {
|
||||
it('supports compressed points', function() {
|
||||
var pubKey = ECPubKey.fromHex(fixtures.compressed.hex)
|
||||
|
|
Loading…
Add table
Reference in a new issue