2014-10-17 04:31:01 +02:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
/* eslint-disable no-new */
|
|
|
|
|
|
|
|
var assert = require('assert')
|
|
|
|
var ecdsa = require('../src/ecdsa')
|
|
|
|
var ecurve = require('ecurve')
|
|
|
|
var proxyquire = require('proxyquire')
|
|
|
|
var sinon = require('sinon')
|
|
|
|
|
|
|
|
var BigInteger = require('bigi')
|
|
|
|
var ECPair = require('../src/ecpair')
|
|
|
|
|
|
|
|
var fixtures = require('./fixtures/ecpair.json')
|
2015-08-19 07:12:55 +02:00
|
|
|
var secp256k1 = ecurve.getCurveByName('secp256k1')
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2015-07-28 09:47:18 +02:00
|
|
|
var NETWORKS = require('../src/networks')
|
2015-07-28 08:42:57 +02:00
|
|
|
var NETWORKS_LIST = [] // Object.values(NETWORKS)
|
|
|
|
for (var networkName in NETWORKS) {
|
|
|
|
NETWORKS_LIST.push(NETWORKS[networkName])
|
|
|
|
}
|
|
|
|
|
2014-10-17 04:31:01 +02:00
|
|
|
describe('ECPair', function () {
|
|
|
|
describe('constructor', function () {
|
|
|
|
it('defaults to compressed', function () {
|
|
|
|
var keyPair = new ECPair(BigInteger.ONE)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(keyPair.compressed, true)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports the uncompressed option', function () {
|
|
|
|
var keyPair = new ECPair(BigInteger.ONE, null, {
|
|
|
|
compressed: false
|
|
|
|
})
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(keyPair.compressed, false)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports the network option', function () {
|
|
|
|
var keyPair = new ECPair(BigInteger.ONE, null, {
|
|
|
|
compressed: false,
|
2015-07-28 08:42:57 +02:00
|
|
|
network: NETWORKS.testnet
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
|
2015-07-28 08:42:57 +02:00
|
|
|
assert.strictEqual(keyPair.network, NETWORKS.testnet)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if compressed option is not a bool', function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
new ECPair(null, null, {
|
|
|
|
compressed: 2
|
|
|
|
}, /Expected Boolean, got 2/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if public and private key given', function () {
|
|
|
|
var qBuffer = new Buffer('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 'hex')
|
2015-08-19 07:12:55 +02:00
|
|
|
var Q = ecurve.Point.decodeFrom(secp256k1, qBuffer)
|
2014-10-17 04:31:01 +02:00
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
new ECPair(BigInteger.ONE, Q)
|
|
|
|
}, /Unexpected publicKey parameter/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if network is missing pubKeyHash constants', function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
new ECPair(null, null, {
|
|
|
|
network: {}
|
|
|
|
}, /Unknown pubKeyHash constants for network/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('calculates the public point for ' + f.WIF, function () {
|
|
|
|
var d = new BigInteger(f.d)
|
|
|
|
var keyPair = new ECPair(d, null, {
|
|
|
|
compressed: f.compressed
|
|
|
|
})
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(keyPair.getPublicKeyBuffer().toString('hex'), f.Q)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.constructor.forEach(function (f) {
|
|
|
|
it('throws on ' + f.d, function () {
|
|
|
|
var d = new BigInteger(f.d)
|
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
new ECPair(d)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPublicKeyBuffer', function () {
|
|
|
|
var keyPair
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
keyPair = new ECPair(BigInteger.ONE)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('wraps Q.getEncoded', sinon.test(function () {
|
|
|
|
this.mock(keyPair.Q).expects('getEncoded')
|
|
|
|
.once().calledWith(keyPair.compressed)
|
|
|
|
|
|
|
|
keyPair.getPublicKeyBuffer()
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fromWIF', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-07-28 08:42:57 +02:00
|
|
|
it('imports ' + f.WIF + ' (' + f.network + ')', function () {
|
|
|
|
var network = NETWORKS[f.network]
|
|
|
|
var keyPair = ECPair.fromWIF(f.WIF, network)
|
|
|
|
|
|
|
|
assert.strictEqual(keyPair.d.toString(), f.d)
|
|
|
|
assert.strictEqual(keyPair.compressed, f.compressed)
|
|
|
|
assert.strictEqual(keyPair.network, network)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.WIF + ' (via list of networks)', function () {
|
|
|
|
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(keyPair.d.toString(), f.d)
|
|
|
|
assert.strictEqual(keyPair.compressed, f.compressed)
|
2015-07-28 08:42:57 +02:00
|
|
|
assert.strictEqual(keyPair.network, NETWORKS[f.network])
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.fromWIF.forEach(function (f) {
|
2015-08-20 12:16:57 +02:00
|
|
|
it('throws on ' + f.WIF, function () {
|
2014-10-17 04:31:01 +02:00
|
|
|
assert.throws(function () {
|
2015-08-20 12:16:57 +02:00
|
|
|
ECPair.fromWIF(f.WIF)
|
2014-10-17 04:31:01 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('toWIF', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-07-28 08:42:57 +02:00
|
|
|
it('exports ' + f.WIF, function () {
|
|
|
|
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
|
2014-10-17 04:31:01 +02:00
|
|
|
var result = keyPair.toWIF()
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(result, f.WIF)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('makeRandom', function () {
|
|
|
|
var d = new Buffer('0404040404040404040404040404040404040404040404040404040404040404', 'hex')
|
|
|
|
var exWIF = 'KwMWvwRJeFqxYyhZgNwYuYjbQENDAPAudQx5VEmKJrUZcq6aL2pv'
|
|
|
|
|
|
|
|
describe('uses randombytes RNG', function () {
|
|
|
|
it('generates a ECPair', function () {
|
|
|
|
var stub = { randombytes: function () { return d } }
|
|
|
|
var ProxiedECPair = proxyquire('../src/ecpair', stub)
|
|
|
|
|
|
|
|
var keyPair = ProxiedECPair.makeRandom()
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(keyPair.toWIF(), exWIF)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('passes the options param', sinon.test(function () {
|
|
|
|
var options = {
|
|
|
|
compressed: true
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: waiting on https://github.com/cjohansen/Sinon.JS/issues/613
|
|
|
|
// this.mock(ECPair).expects('constructor')
|
|
|
|
// .once().calledWith(options)
|
|
|
|
|
|
|
|
ECPair.makeRandom(options)
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows a custom RNG to be used', function () {
|
|
|
|
var keyPair = ECPair.makeRandom({
|
|
|
|
rng: function (size) { return d.slice(0, size) }
|
|
|
|
})
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(keyPair.toWIF(), exWIF)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getAddress', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('returns ' + f.address + ' for ' + f.WIF, function () {
|
2015-07-28 08:42:57 +02:00
|
|
|
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2015-07-08 07:56:21 +02:00
|
|
|
assert.strictEqual(keyPair.getAddress(), f.address)
|
2014-10-17 04:31:01 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('ecdsa wrappers', function () {
|
|
|
|
var keyPair, hash
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
keyPair = ECPair.makeRandom()
|
|
|
|
hash = new Buffer(32)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('signing', function () {
|
|
|
|
it('wraps ecdsa.sign', sinon.test(function () {
|
|
|
|
this.mock(ecdsa).expects('sign')
|
2015-08-19 07:12:55 +02:00
|
|
|
.once().calledWith(secp256k1, hash, keyPair.d)
|
2014-10-17 04:31:01 +02:00
|
|
|
|
|
|
|
keyPair.sign(hash)
|
|
|
|
}))
|
|
|
|
|
|
|
|
it('throws if no private key is found', function () {
|
|
|
|
keyPair.d = null
|
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
keyPair.sign(hash)
|
|
|
|
}, /Missing private key/)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('verify', function () {
|
|
|
|
var signature
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
signature = keyPair.sign(hash)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('wraps ecdsa.verify', sinon.test(function () {
|
|
|
|
this.mock(ecdsa).expects('verify')
|
2015-08-19 07:12:55 +02:00
|
|
|
.once().calledWith(secp256k1, hash, signature, keyPair.Q)
|
2014-10-17 04:31:01 +02:00
|
|
|
|
|
|
|
keyPair.verify(hash, signature)
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|