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-16 05:42:37 +02:00
|
|
|
var sec = require('../src/sec')
|
|
|
|
var ecparams = sec('secp256k1')
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
var BigInteger = require('bigi')
|
|
|
|
var ECPointFp = require('../src/ec').ECPointFp
|
2014-05-13 09:55:53 +02:00
|
|
|
var ECPubKey = require('../src/ecpubkey')
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-18 11:47:39 +02:00
|
|
|
var fixtures = require('./fixtures/ecpubkey.json')
|
2014-05-16 05:42:37 +02:00
|
|
|
|
2014-05-13 08:44:29 +02:00
|
|
|
describe('ECPubKey', function() {
|
2014-05-16 05:42:37 +02:00
|
|
|
var Q
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
var curve = ecparams.getCurve()
|
|
|
|
|
|
|
|
Q = new ECPointFp(
|
|
|
|
curve,
|
|
|
|
curve.fromBigInteger(new BigInteger(fixtures.Q.x)),
|
|
|
|
curve.fromBigInteger(new BigInteger(fixtures.Q.y))
|
|
|
|
)
|
|
|
|
})
|
2014-05-13 08:44:29 +02:00
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
describe('constructor', function() {
|
|
|
|
it('defaults to compressed', function() {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2014-05-16 05:42:37 +02:00
|
|
|
it('supports the uncompressed flag', function() {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getAddress', function() {
|
2014-05-16 05:42:37 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2014-05-16 05:42:37 +02: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)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports alternative networks', function() {
|
|
|
|
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-03 13:43:10 +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
|
|
|
})
|
|
|
|
|
|
|
|
describe('verify', function() {
|
|
|
|
var pubKey, signature
|
|
|
|
beforeEach(function() {
|
|
|
|
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
|
|
|
|
|
|
|
it('verifies a valid signature', function() {
|
|
|
|
var hash = crypto.sha256(fixtures.message)
|
|
|
|
|
|
|
|
assert.ok(pubKey.verify(hash, signature))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('doesn\'t verify the wrong signature', function() {
|
|
|
|
var hash = crypto.sha256('mushrooms')
|
|
|
|
|
|
|
|
assert.ok(!pubKey.verify(hash, signature))
|
|
|
|
})
|
2014-05-13 08:44:29 +02:00
|
|
|
})
|
|
|
|
})
|