Browse Source

message: data-driven tests

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
2114deba95
  1. 67
      test/fixtures/message.json
  2. 71
      test/message.js

67
test/fixtures/message.json

@ -1,4 +1,5 @@
{ {
"valid": {
"magicHash": [ "magicHash": [
{ {
"network": "bitcoin", "network": "bitcoin",
@ -15,5 +16,71 @@
"message": "Vires is Numeris", "message": "Vires is Numeris",
"magicHash": "c0963d20d0accd0ea0df6c1020bf85a7e629a40e7b5363f2c3e9dcafd5638f12" "magicHash": "c0963d20d0accd0ea0df6c1020bf85a7e629a40e7b5363f2c3e9dcafd5638f12"
} }
],
"verify": [
{
"message": "vires is numeris",
"network": "bitcoin",
"address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
"signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=",
"compressed": {
"address": "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs",
"signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
}
},
{
"message": "vires is numeris",
"network": "dogecoin",
"address": "DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE",
"signature": "H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s="
}
],
"signing": [
{
"description": "gives equal r, s values irrespective of point compression",
"message": "vires is numeris",
"network": "bitcoin",
"D": "1",
"signature": "HF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8=",
"compressed": {
"signature": "IF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8="
}
},
{
"description": "supports alternative networks",
"message": "vires is numeris",
"network": "dogecoin",
"D": "1",
"signature": "G6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s="
}
]
},
"invalid": {
"verify": [
{
"description": "will fail for the wrong message",
"message": "foobar",
"address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
"signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
},
{
"description": "will fail for the wrong address",
"message": "vires is numeris",
"address": "1111111111111111111114oLvT2",
"signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
},
{
"description": "does not cross verify (uncompressed address, compressed signature)",
"message": "vires is numeris",
"address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
"signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
},
{
"description": "does not cross verify (compressed address, uncompressed signature)",
"message": "vires is numeris",
"address": "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs",
"signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0="
}
] ]
} }
}

71
test/message.js

@ -8,15 +8,9 @@ var Message = require('../src/message')
var fixtures = require('./fixtures/message.json') var fixtures = require('./fixtures/message.json')
describe('Message', function() { describe('Message', function() {
var message
beforeEach(function() {
message = 'vires is numeris'
})
describe('magicHash', function() { describe('magicHash', function() {
it('matches the test vectors', function() { it('matches the test vectors', function() {
fixtures.magicHash.forEach(function(f) { fixtures.valid.magicHash.forEach(function(f) {
var network = networks[f.network] var network = networks[f.network]
var actual = Message.magicHash(f.message, network) var actual = Message.magicHash(f.message, network)
@ -26,58 +20,45 @@ describe('Message', function() {
}) })
describe('verify', function() { describe('verify', function() {
var addr, sig, caddr, csig it('verifies a valid signature', function() {
fixtures.valid.verify.forEach(function(f) {
beforeEach(function() { var network = networks[f.network]
addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed
caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed
sig = new Buffer('G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=', 'base64') var s1 = new Buffer(f.signature, 'base64')
csig = new Buffer('H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=', 'base64') assert.ok(Message.verify(f.address, s1, f.message, network))
})
it('can verify a signed message', function() { if (f.compressed) {
assert.ok(Message.verify(addr, sig, message)) var s2 = new Buffer(f.compressed.signature, 'base64')
})
it('will fail for the wrong message', function() { assert.ok(Message.verify(f.compressed.address, s2, f.message, network))
assert.ok(!Message.verify(addr, sig, 'foobar')) }
}) })
it('will fail for the wrong address', function() {
assert.ok(!Message.verify('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, message))
}) })
it('does not cross verify (compressed/uncompressed)', function() { fixtures.invalid.verify.forEach(function(f) {
assert.ok(!Message.verify(addr, csig, message)) it(f.description, function() {
assert.ok(!Message.verify(caddr, sig, message)) var signature = new Buffer(f.signature, 'base64')
assert.ok(!Message.verify(f.address, signature, f.message))
}) })
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))
}) })
}) })
describe('signing', function() { describe('signing', function() {
it('gives matching signatures irrespective of point compression', function() { fixtures.valid.signing.forEach(function(f) {
var privKey = new ECKey(BigInteger.ONE, false) it(f.description, function() {
var compressedKey = new ECKey(privKey.D, true) var network = networks[f.network]
var sig = Message.sign(privKey, message)
var csig = Message.sign(compressedKey, message)
assert.notDeepEqual(sig.slice(0, 2), csig.slice(0, 2)) // unequal compression flags var k1 = new ECKey(new BigInteger(f.D), false)
assert.deepEqual(sig.slice(2), csig.slice(2)) // equal signatures var s1 = Message.sign(k1, f.message, network)
}) assert.equal(s1.toString('base64'), f.signature)
it('supports alternate networks', function() { if (f.compressed) {
var privKey = new ECKey(BigInteger.ONE) var k2 = new ECKey(new BigInteger(f.D))
var signature = Message.sign(privKey, message, networks.dogecoin) var s2 = Message.sign(k2, f.message)
assert.equal(signature.toString('base64'), 'H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=') assert.equal(s2.toString('base64'), f.compressed.signature)
}
})
}) })
}) })
}) })

Loading…
Cancel
Save