Further isolates failure tests and joins [un]/compressed key tests
This commit is contained in:
parent
38f09b49e9
commit
78e91e8648
2 changed files with 49 additions and 40 deletions
|
@ -26,7 +26,7 @@ function sign(key, message) {
|
||||||
var hash = magicHash(message)
|
var hash = magicHash(message)
|
||||||
var sig = key.sign(hash)
|
var sig = key.sign(hash)
|
||||||
var obj = ecdsa.parseSig(sig)
|
var obj = ecdsa.parseSig(sig)
|
||||||
var i = ecdsa.calcPubkeyRecoveryParam(key.getPub(), obj.r, obj.s, hash)
|
var i = ecdsa.calcPubkeyRecoveryParam(key.getPub(key.compressed), obj.r, obj.s, hash)
|
||||||
|
|
||||||
i += 27
|
i += 27
|
||||||
if (key.compressed) {
|
if (key.compressed) {
|
||||||
|
|
|
@ -1,65 +1,74 @@
|
||||||
var assert = require('assert');
|
var assert = require('assert')
|
||||||
var Message = require('../src/message')
|
var convert = require('../').convert
|
||||||
var convert = require('../src/convert')
|
|
||||||
var ECKey = require('../src/eckey').ECKey
|
var ECKey = require('../src/eckey').ECKey
|
||||||
var testnet = require('../src/network.js').testnet.addressVersion
|
var Message = require('../').Message
|
||||||
|
var testnet = require('../').network.testnet.addressVersion
|
||||||
|
|
||||||
describe('Message', function() {
|
describe('Message', function() {
|
||||||
var msg = 'vires is numeris'
|
var msg = 'vires is numeris'
|
||||||
|
|
||||||
describe('verify', function() {
|
describe('verify', function() {
|
||||||
it('works for mainnet address, messaged signed with uncompressed key', function() {
|
var addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' // uncompressed
|
||||||
var addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM';
|
var caddr = '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs' // compressed
|
||||||
|
|
||||||
var sig = convert.hexToBytes('1bc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d')
|
var sig = convert.hexToBytes('1bc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d')
|
||||||
assert.ok(Message.verify(addr, sig, msg));
|
var csig = convert.hexToBytes('1fc25ac0fb503abc9bad23f558742740fafaec1f52deaaf106b9759a5ce84c93921c4a669c5ec3dfeb7e2d7d177a2f49db407900874f6de2f701a4c16783776d8d')
|
||||||
verifyNegativeCases(addr, sig, msg)
|
|
||||||
|
it('can verify a signed message', function() {
|
||||||
|
assert.ok(Message.verify(addr, sig, msg))
|
||||||
|
assert.ok(Message.verify(caddr, csig, msg))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('works for testnet address, message signed with compressed key', function() {
|
it('will fail for the wrong message', function() {
|
||||||
var addr = 'mgdnNWji2bXYSi7E9c1DQBSp64kCemaS7V'
|
assert.ok(!Message.verify(addr, sig, 'foobar'))
|
||||||
var sig = convert.hexToBytes('1feece860e952253ddf465cd1c5aea76ab16287aee093be6f67d196c39f5075436f0407a4e50694e6956c06108fab8608debf9554d75e57c110f7c512a6eb15d0a')
|
assert.ok(!Message.verify(caddr, csig, 'foobar'))
|
||||||
|
|
||||||
assert(Message.verify(addr, sig, msg))
|
|
||||||
verifyNegativeCases(addr, sig, msg)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
function verifyNegativeCases(addr, sig, msg){
|
it('will fail for the wrong public key', function() {
|
||||||
var wrongMsg = 'vires in numeris'
|
assert.ok(!Message.verify('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg))
|
||||||
assert.ok(!Message.verify(addr, sig, wrongMsg));
|
assert.ok(!Message.verify('1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', csig, msg))
|
||||||
|
|
||||||
var wrongAddress = new ECKey(null).getAddress()
|
|
||||||
assert.ok(!Message.verify(wrongAddress, sig, msg));
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('sign', function() {
|
it('supports alternate network addresses', function() {
|
||||||
describe('uncompressed key', function(){
|
var taddr = 'mxnQZKxSKjzaMgrdXzk35rif3u62TLDrg9'
|
||||||
it('works', function(){
|
var tsig = convert.base64ToBytes('IGucnrTku3KLCCHUMwq9anawfrlN8RK1HWMN+10LhsHJeysBdWfj5ohJcS/+oqrlVFNvEgbgEeAQUL6r3sZwnj8=')
|
||||||
var key = new ECKey(null)
|
|
||||||
var sig = Message.sign(key, msg);
|
|
||||||
|
|
||||||
var addr = key.getAddress()
|
assert.ok(Message.verify(taddr, tsig, msg))
|
||||||
assert(Message.verify(addr, sig, 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))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('compressed key', function(){
|
describe('signing', function() {
|
||||||
it('works', function(){
|
var key = new ECKey(null) // uncompressed
|
||||||
var key = new ECKey(null, true)
|
|
||||||
var sig = Message.sign(key, msg);
|
|
||||||
|
|
||||||
var addr = key.getAddress()
|
describe('using the uncompressed public key', function(){
|
||||||
assert(Message.verify(addr, sig, msg));
|
it('gives same signature as a compressed public key', function() {
|
||||||
|
var sig = Message.sign(key, msg)
|
||||||
|
|
||||||
|
var compressedKey = new ECKey(key, true) // compressed clone
|
||||||
|
var csig = Message.sign(compressedKey, msg) // FIXME: bad compression support
|
||||||
|
|
||||||
|
var addr = key.getPub().getAddress()
|
||||||
|
var caddr = compressedKey.getPub().getAddress()
|
||||||
|
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
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('testnet address', function(){
|
describe('testnet address', function(){
|
||||||
it('works', function(){
|
it('works', function(){
|
||||||
var key = new ECKey(null)
|
var key = new ECKey(null)
|
||||||
var sig = Message.sign(key, msg);
|
var sig = Message.sign(key, msg)
|
||||||
|
|
||||||
var addr = key.getAddress(testnet)
|
var addr = key.getAddress(testnet)
|
||||||
assert(Message.verify(addr, sig, msg));
|
assert(Message.verify(addr, sig, msg))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue