Browse Source

Merge pull request #101 from dcousens/messagevers

Adds version support to Message.verifyMessage
hk-custom-address
Wei Lu 11 years ago
parent
commit
3dfeecb503
  1. 8
      src/message.js
  2. 23
      test/message.js

8
src/message.js

@ -1,8 +1,9 @@
/// Implements Bitcoin's feature for signing arbitrary messages. /// Implements Bitcoin's feature for signing arbitrary messages.
var SHA256 = require('crypto-js/sha256') var Address = require('./address')
var ecdsa = require('./ecdsa')
var convert = require('./convert') var convert = require('./convert')
var ecdsa = require('./ecdsa')
var SHA256 = require('crypto-js/sha256')
var Message = {} var Message = {}
@ -59,7 +60,8 @@ Message.verifyMessage = function (address, sig, message) {
pubKey.compressed = isCompressed pubKey.compressed = isCompressed
// Compare address to expected address // Compare address to expected address
return address === pubKey.getAddress().toString() address = new Address(address)
return address.toString() === pubKey.getAddress(address.version).toString()
} }
module.exports = Message module.exports = Message

23
test/message.js

@ -1,8 +1,7 @@
/* global describe, it */
var assert = require('assert'); var assert = require('assert');
var Message = require('../src/message.js'); var convert = require('../src/convert')
var ECKey = require('../src/eckey.js').ECKey; var Message = require('../src/message')
var hexToBytes = require('../src/convert.js').hexToBytes; var ECKey = require('../src/eckey').ECKey
var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725'; var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725';
var addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'; var addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM';
@ -11,7 +10,7 @@ var msg = 'foobar';
describe('Message', function() { describe('Message', function() {
describe('verify', function() { describe('verify', function() {
it('passes case 1', function() { it('passes case 1', function() {
var key = new ECKey(hexToBytes(priv)); var key = new ECKey(priv)
assert.equal(key.getAddress().toString(), addr); assert.equal(key.getAddress().toString(), addr);
var sig = Message.signMessage(key, msg); var sig = Message.signMessage(key, msg);
@ -25,14 +24,13 @@ describe('Message', function() {
}) })
it('passes case 2', function() { it('passes case 2', function() {
var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh'; var key = new ECKey('5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh')
var key = new ECKey(hexToBytes(priv));
var sig = Message.signMessage(key, msg); var sig = Message.signMessage(key, msg);
assert.ok(!Message.verifyMessage(addr, sig, msg)); assert.ok(!Message.verifyMessage(addr, sig, msg));
}) })
it('handles compressed keys', function() { it('handles compressed keys', function() {
var key = new ECKey(hexToBytes(priv)); var key = new ECKey(priv)
key.compressed = true key.compressed = true
var addr = key.getAddress().toString() var addr = key.getAddress().toString()
@ -46,5 +44,14 @@ describe('Message', function() {
// wrong address // wrong address
assert.ok(!Message.verifyMessage('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg)); assert.ok(!Message.verifyMessage('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg));
}) })
it('handle testnet addresses properly', function() {
var addr = 'mgdnNWji2bXYSi7E9c1DQBSp64kCemaS7V'
var msg = 'vires is numeris'
var sig = convert.base64ToBytes('H+7Ohg6VIlPd9GXNHFrqdqsWKHruCTvm9n0ZbDn1B1Q28EB6TlBpTmlWwGEI+rhgjev5VU115XwRD3xRKm6xXQo=')
sig = convert.bytesToHex(sig)
assert(Message.verifyMessage(addr, sig, msg))
})
}) })
}) })

Loading…
Cancel
Save