bitcoinjs-lib/test/eckey.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-03-31 05:47:47 +02:00
var assert = require('assert')
var crypto = require('../src/crypto')
2014-05-13 08:35:07 +02:00
var BigInteger = require('bigi')
var ECKey = require('../src/eckey')
2014-05-18 11:47:39 +02:00
var fixtures = require('./fixtures/eckey.json')
var networks = require('../src/networks')
describe('ECKey', function() {
2014-03-31 05:47:47 +02:00
describe('constructor', function() {
it('defaults to compressed', function() {
var privKey = new ECKey(BigInteger.ONE)
assert.equal(privKey.pub.compressed, true)
2014-03-22 08:19:56 +01:00
})
it('supports the uncompressed flag', function() {
var privKey = new ECKey(BigInteger.ONE, false)
2014-03-31 05:47:47 +02:00
assert.equal(privKey.pub.compressed, false)
2014-03-24 17:48:50 +01:00
})
2014-03-24 23:11:34 +01:00
it('calculates the matching pubKey', function() {
fixtures.valid.forEach(function(f) {
var privKey = new ECKey(new BigInteger(f.D))
2014-03-31 05:47:47 +02:00
assert.equal(privKey.pub.Q.toString(), f.Q.toString())
})
2014-03-31 05:47:47 +02:00
})
fixtures.invalid.constructor.forEach(function(f) {
it('throws on ' + f.description, function() {
assert.throws(function() {
new ECKey(new BigInteger(f.D))
})
})
2014-03-31 05:47:47 +02:00
})
})
describe('fromWIF', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) {
f.WIFs.forEach(function(wif) {
var privKey = ECKey.fromWIF(wif.string)
2014-03-31 05:47:47 +02:00
assert.equal(privKey.D.toString(), f.D)
assert.equal(privKey.pub.compressed, wif.compressed)
})
})
2014-03-31 05:47:47 +02:00
})
fixtures.invalid.WIF.forEach(function(f) {
it('throws on ' + f.description, function() {
assert.throws(function() {
ECKey.fromWIF(f.string)
})
})
})
})
2014-03-31 05:47:47 +02:00
describe('toWIF', function() {
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) {
f.WIFs.forEach(function(wif) {
var privKey = ECKey.fromWIF(wif.string)
var version = networks[wif.network].wif
var result = privKey.toWIF(version)
assert.equal(result, wif.string)
})
})
2014-03-31 05:47:47 +02:00
})
})
describe('signing', function() {
var hash = crypto.sha256('Vires in numeris')
var priv = ECKey.makeRandom()
var signature = priv.sign(hash)
2014-03-31 05:47:47 +02:00
it('should verify against the public key', function() {
assert(priv.pub.verify(hash, signature))
})
2014-03-31 05:47:47 +02:00
it('should not verify against the wrong public key', function() {
var priv2 = ECKey.makeRandom()
2014-03-31 05:47:47 +02:00
assert(!priv2.pub.verify(hash, signature))
2014-03-31 05:47:47 +02:00
})
})
})