2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
|
2014-05-13 08:44:29 +02:00
|
|
|
var assert = require('assert')
|
2014-05-16 05:42:37 +02:00
|
|
|
var crypto = require('../src/crypto')
|
2014-06-03 13:43:10 +02:00
|
|
|
var networks = require('../src/networks')
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
var BigInteger = require('bigi')
|
2014-05-13 09:55:53 +02:00
|
|
|
var ECPubKey = require('../src/ecpubkey')
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-06-07 08:24:27 +02:00
|
|
|
var ecurve = require('ecurve')
|
|
|
|
var curve = ecurve.getCurveByName('secp256k1')
|
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/ecpubkey.json')
|
2014-05-16 05:42:37 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('ECPubKey', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var Q
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-06-10 10:36:00 +02:00
|
|
|
Q = ecurve.Point.fromAffine(
|
2014-05-16 05:42:37 +02:00
|
|
|
curve,
|
2014-06-07 08:24:27 +02:00
|
|
|
new BigInteger(fixtures.Q.x),
|
|
|
|
new BigInteger(fixtures.Q.y)
|
2014-05-16 05:42:37 +02:00
|
|
|
)
|
|
|
|
})
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('constructor', function () {
|
|
|
|
it('defaults to compressed', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = new ECPubKey(Q)
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
assert.equal(pubKey.compressed, true)
|
2014-05-13 08:44:29 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('supports the uncompressed flag', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = new ECPubKey(Q, false)
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
assert.equal(pubKey.compressed, false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('uses the secp256k1 curve by default', function () {
|
2014-10-15 16:25:39 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromHex/toHex', function () {
|
|
|
|
it('supports compressed points', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = ECPubKey.fromHex(fixtures.compressed.hex)
|
|
|
|
|
|
|
|
assert(pubKey.Q.equals(Q))
|
|
|
|
assert.equal(pubKey.toHex(), fixtures.compressed.hex)
|
|
|
|
assert.equal(pubKey.compressed, true)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('supports uncompressed points', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = ECPubKey.fromHex(fixtures.uncompressed.hex)
|
|
|
|
|
|
|
|
assert(pubKey.Q.equals(Q))
|
|
|
|
assert.equal(pubKey.toHex(), fixtures.uncompressed.hex)
|
|
|
|
assert.equal(pubKey.compressed, false)
|
2014-05-13 08:44:29 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getAddress', function () {
|
|
|
|
it('calculates the expected hash (compressed)', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = new ECPubKey(Q, true)
|
|
|
|
var address = pubKey.getAddress()
|
|
|
|
|
|
|
|
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160)
|
2014-05-13 08:44:29 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('calculates the expected hash (uncompressed)', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = new ECPubKey(Q, false)
|
|
|
|
var address = pubKey.getAddress()
|
|
|
|
|
|
|
|
assert.equal(address.hash.toString('hex'), fixtures.uncompressed.hash160)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('supports alternative networks', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey = new ECPubKey(Q)
|
2014-06-03 13:43:10 +02:00
|
|
|
var address = pubKey.getAddress(networks.testnet)
|
2014-05-16 05:42:37 +02:00
|
|
|
|
2014-06-17 13:05:18 +02:00
|
|
|
assert.equal(address.version, networks.testnet.pubKeyHash)
|
2014-05-16 05:42:37 +02:00
|
|
|
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160)
|
2014-05-13 08:44:29 +02:00
|
|
|
})
|
2014-05-16 05:42:37 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('verify', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var pubKey, signature
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
pubKey = new ECPubKey(Q)
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
signature = {
|
|
|
|
r: new BigInteger(fixtures.signature.r),
|
|
|
|
s: new BigInteger(fixtures.signature.s)
|
2014-05-13 08:44:29 +02:00
|
|
|
}
|
|
|
|
})
|
2014-05-16 05:42:37 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('verifies a valid signature', function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var hash = crypto.sha256(fixtures.message)
|
|
|
|
|
2014-08-20 01:17:55 +02:00
|
|
|
assert(pubKey.verify(hash, signature))
|
2014-05-16 05:42:37 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it("doesn't verify the wrong signature", function () {
|
2014-05-16 05:42:37 +02:00
|
|
|
var hash = crypto.sha256('mushrooms')
|
|
|
|
|
2014-08-20 01:17:55 +02:00
|
|
|
assert(!pubKey.verify(hash, signature))
|
2014-05-16 05:42:37 +02:00
|
|
|
})
|
2014-05-13 08:44:29 +02:00
|
|
|
})
|
|
|
|
})
|