2014-03-28 19:52:15 +01:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var networks = require('../src/networks')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var ECKey = require('../src/eckey')
|
|
|
|
var Message = require('../src/message')
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-04-21 13:24:58 +02:00
|
|
|
var fixtures = require('./fixtures/message')
|
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
describe('Message', function() {
|
2014-03-30 23:21:38 +02:00
|
|
|
var msg
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
msg = 'vires is numeris'
|
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
|
2014-04-21 13:24:58 +02:00
|
|
|
describe('magicHash', function() {
|
|
|
|
it('matches the test vectors', function() {
|
|
|
|
fixtures.magicHash.forEach(function(f) {
|
|
|
|
var actual = Message.magicHash(f.message)
|
|
|
|
var expected = f.hash256
|
|
|
|
|
|
|
|
assert.equal(actual.toString('hex'), expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
describe('verify', function() {
|
2014-03-30 23:21:38 +02:00
|
|
|
var addr, sig, caddr, csig
|
2014-03-28 19:52:15 +01:00
|
|
|
|
2014-03-30 23:21:38 +02:00
|
|
|
beforeEach(function() {
|
|
|
|
addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed
|
|
|
|
caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed
|
|
|
|
|
2014-04-21 13:23:30 +02:00
|
|
|
sig = new Buffer('1bc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d', 'hex')
|
|
|
|
csig = new Buffer('1fc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d', 'hex')
|
2014-03-30 23:21:38 +02:00
|
|
|
})
|
2014-03-28 19:52:15 +01:00
|
|
|
|
|
|
|
it('can verify a signed message', function() {
|
|
|
|
assert.ok(Message.verify(addr, sig, msg))
|
|
|
|
assert.ok(Message.verify(caddr, csig, msg))
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2013-03-02 18:28:13 +01:00
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
it('will fail for the wrong message', function() {
|
|
|
|
assert.ok(!Message.verify(addr, sig, 'foobar'))
|
|
|
|
assert.ok(!Message.verify(caddr, csig, 'foobar'))
|
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
it('will fail for the wrong public key', function() {
|
|
|
|
assert.ok(!Message.verify('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg))
|
|
|
|
assert.ok(!Message.verify('1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', csig, msg))
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
it('supports alternate network addresses', function() {
|
|
|
|
var taddr = 'mxnQZKxSKjzaMgrdXzk35rif3u62TLDrg9'
|
2014-04-21 13:23:30 +02:00
|
|
|
var tsig = new Buffer('IGucnrTku3KLCCHUMwq9anawfrlN8RK1HWMN+10LhsHJeysBdWfj5ohJcS/+oqrlVFNvEgbgEeAQUL6r3sZwnj8=', 'base64')
|
2014-03-11 18:25:36 +01:00
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
assert.ok(Message.verify(taddr, tsig, msg))
|
|
|
|
assert.ok(!Message.verify(taddr, tsig, 'foobar'))
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not cross verify (compressed/uncompressed)', function() {
|
|
|
|
assert.ok(!Message.verify(addr, csig, msg))
|
|
|
|
assert.ok(!Message.verify(caddr, sig, msg))
|
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-03-11 18:25:36 +01:00
|
|
|
|
2014-03-28 19:52:15 +01:00
|
|
|
describe('signing', function() {
|
|
|
|
describe('using the uncompressed public key', function(){
|
|
|
|
it('gives same signature as a compressed public key', function() {
|
2014-04-17 11:08:16 +02:00
|
|
|
var key = ECKey.makeRandom(false) // uncompressed
|
2014-03-28 19:52:15 +01:00
|
|
|
var sig = Message.sign(key, msg)
|
2014-03-11 18:25:36 +01:00
|
|
|
|
2014-04-17 11:08:16 +02:00
|
|
|
var compressedKey = new ECKey(key.D, true) // compressed clone
|
|
|
|
var csig = Message.sign(compressedKey, msg)
|
2014-03-11 18:25:36 +01:00
|
|
|
|
2014-04-17 11:08:16 +02:00
|
|
|
var addr = key.pub.getAddress()
|
|
|
|
var caddr = compressedKey.pub.getAddress()
|
2014-03-28 19:52:15 +01:00
|
|
|
assert.ok(Message.verify(addr, sig, msg))
|
|
|
|
assert.ok(Message.verify(caddr, csig, msg))
|
|
|
|
assert.notDeepEqual(sig.slice(0, 2), csig.slice(0, 2)) // unequal compression flags
|
|
|
|
assert.deepEqual(sig.slice(2), csig.slice(2)) // equal signatures
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-28 06:42:39 +01:00
|
|
|
|
2014-03-28 09:32:51 +01:00
|
|
|
describe('testnet address', function(){
|
|
|
|
it('works', function(){
|
2014-04-17 11:08:16 +02:00
|
|
|
var key = ECKey.makeRandom()
|
2014-03-28 19:52:15 +01:00
|
|
|
var sig = Message.sign(key, msg)
|
2014-03-28 06:42:39 +01:00
|
|
|
|
2014-05-05 05:23:22 +02:00
|
|
|
var addr = key.pub.getAddress(networks.testnet.pubKeyHash)
|
2014-03-28 19:52:15 +01:00
|
|
|
assert(Message.verify(addr, sig, msg))
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|