bitcoinjs-lib/test/ecpubkey.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-02-23 00:36:57 +01:00
/* global describe, it, beforeEach */
2014-05-13 08:44:29 +02:00
var assert = require('assert')
var crypto = require('../src/crypto')
var networks = require('../src/networks')
2014-05-13 08:44:29 +02:00
var BigInteger = require('bigi')
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')
2015-02-23 00:36:57 +01:00
describe('ECPubKey', function () {
var Q
2015-02-23 00:36:57 +01:00
beforeEach(function () {
2014-06-10 10:36:00 +02:00
Q = ecurve.Point.fromAffine(
curve,
2014-06-07 08:24:27 +02:00
new BigInteger(fixtures.Q.x),
new BigInteger(fixtures.Q.y)
)
})
2014-05-13 08:44:29 +02:00
2015-02-23 00:36:57 +01:00
describe('constructor', function () {
it('defaults to compressed', function () {
var pubKey = new ECPubKey(Q)
2014-05-13 08:44:29 +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 () {
var pubKey = new ECPubKey(Q, false)
2014-05-13 08:44:29 +02:00
assert.equal(pubKey.compressed, false)
})
})
2015-02-23 00:36:57 +01:00
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)
}
})
2015-02-23 00:36:57 +01:00
describe('fromHex/toHex', function () {
it('supports compressed points', function () {
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 () {
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 () {
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 () {
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 () {
var pubKey = new ECPubKey(Q)
var address = pubKey.getAddress(networks.testnet)
2014-06-17 13:05:18 +02:00
assert.equal(address.version, networks.testnet.pubKeyHash)
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
describe('verify', function () {
var pubKey, signature
2015-02-23 00:36:57 +01:00
beforeEach(function () {
pubKey = new ECPubKey(Q)
2014-05-13 08:44:29 +02:00
signature = {
r: new BigInteger(fixtures.signature.r),
s: new BigInteger(fixtures.signature.s)
2014-05-13 08:44:29 +02:00
}
})
2015-02-23 00:36:57 +01:00
it('verifies a valid signature', function () {
var hash = crypto.sha256(fixtures.message)
2014-08-20 01:17:55 +02:00
assert(pubKey.verify(hash, signature))
})
2015-02-23 00:36:57 +01:00
it("doesn't verify the wrong signature", function () {
var hash = crypto.sha256('mushrooms')
2014-08-20 01:17:55 +02:00
assert(!pubKey.verify(hash, signature))
})
2014-05-13 08:44:29 +02:00
})
})