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-16 07:39:17 +02:00
|
|
|
var BigInteger = require('bigi')
|
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-05-16 05:40:51 +02:00
|
|
|
var message
|
2014-03-30 23:21:38 +02:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-16 05:40:51 +02:00
|
|
|
message = 'vires is numeris'
|
2014-03-30 23:21:38 +02:00
|
|
|
})
|
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) {
|
2014-05-16 07:39:17 +02:00
|
|
|
var network = networks[f.network]
|
|
|
|
var actual = Message.magicHash(f.message, network)
|
2014-04-21 13:24:58 +02:00
|
|
|
|
2014-05-16 07:39:17 +02:00
|
|
|
assert.equal(actual.toString('hex'), f.magicHash)
|
2014-04-21 13:24:58 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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-05-16 08:41:57 +02:00
|
|
|
sig = new Buffer('G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=', 'base64')
|
|
|
|
csig = new Buffer('H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=', 'base64')
|
2014-03-30 23:21:38 +02:00
|
|
|
})
|
2014-03-28 19:52:15 +01:00
|
|
|
|
|
|
|
it('can verify a signed message', function() {
|
2014-05-16 05:40:51 +02:00
|
|
|
assert.ok(Message.verify(addr, sig, message))
|
|
|
|
assert.ok(Message.verify(caddr, csig, message))
|
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() {
|
2014-05-16 05:40:51 +02:00
|
|
|
assert.ok(!Message.verify('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, message))
|
|
|
|
assert.ok(!Message.verify('1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', csig, message))
|
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('does not cross verify (compressed/uncompressed)', function() {
|
2014-05-16 05:40:51 +02:00
|
|
|
assert.ok(!Message.verify(addr, csig, message))
|
|
|
|
assert.ok(!Message.verify(caddr, sig, message))
|
2014-03-28 19:52:15 +01:00
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
|
|
|
|
it('supports alternate networks', function() {
|
|
|
|
var dogeaddr = 'DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE'
|
|
|
|
var dogesig = new Buffer('H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=', 'base64')
|
|
|
|
|
|
|
|
assert.ok(Message.verify(dogeaddr, dogesig, message, networks.dogecoin))
|
|
|
|
})
|
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() {
|
2014-05-16 05:40:51 +02:00
|
|
|
it('gives matching signatures irrespective of point compression', function() {
|
2014-05-16 07:39:17 +02:00
|
|
|
var privKey = new ECKey(BigInteger.ONE, false)
|
2014-05-16 05:40:51 +02:00
|
|
|
var compressedKey = new ECKey(privKey.D, true)
|
2014-03-28 06:42:39 +01:00
|
|
|
|
2014-05-16 05:40:51 +02:00
|
|
|
var sig = Message.sign(privKey, message)
|
|
|
|
var csig = Message.sign(compressedKey, message)
|
2014-03-28 06:42:39 +01:00
|
|
|
|
2014-05-16 05:40:51 +02:00
|
|
|
assert.notDeepEqual(sig.slice(0, 2), csig.slice(0, 2)) // unequal compression flags
|
|
|
|
assert.deepEqual(sig.slice(2), csig.slice(2)) // equal signatures
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|
2014-05-16 07:39:17 +02:00
|
|
|
|
|
|
|
it('supports alternate networks', function() {
|
|
|
|
var privKey = new ECKey(BigInteger.ONE)
|
|
|
|
var signature = Message.sign(privKey, message, networks.dogecoin)
|
|
|
|
|
|
|
|
assert.equal(signature.toString('base64'), 'H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=')
|
|
|
|
})
|
2014-03-28 09:32:51 +01:00
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|