bitcoinjs-lib/test/ecpubkey.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

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')
var ECPoint = ecurve.Point
2014-05-18 11:47:39 +02:00
var fixtures = require('./fixtures/ecpubkey.json')
2014-05-13 08:44:29 +02:00
describe('ECPubKey', function() {
var Q
beforeEach(function() {
2014-06-07 08:24:27 +02:00
Q = ECPoint.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
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
})
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)
})
})
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() {
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
})
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)
var address = pubKey.getAddress(networks.testnet)
assert.equal(address.version, networks.testnet.pubKeyHash)
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160)
2014-05-13 08:44:29 +02:00
})
})
describe('verify', function() {
var pubKey, signature
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
}
})
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
})
})