Browse Source

Merge pull request #266 from dcousens/mesb64

Message: allow base64 strings as input
hk-custom-address
Wei Lu 11 years ago
parent
commit
43d135c4b3
  1. 9
      src/message.js
  2. 4
      test/ecpubkey.js
  3. 14
      test/message.js
  4. 2
      test/wallet.js

9
src/message.js

@ -35,14 +35,19 @@ function sign(privKey, message, network) {
} }
// TODO: network could be implied from address // TODO: network could be implied from address
function verify(address, signatureBuffer, message, network) { function verify(address, signature, message, network) {
if (!Buffer.isBuffer(signature)) {
signature = new Buffer(signature, 'base64')
}
if (address instanceof Address) { if (address instanceof Address) {
address = address.toString() address = address.toString()
} }
network = network || networks.bitcoin network = network || networks.bitcoin
var hash = magicHash(message, network) var hash = magicHash(message, network)
var parsed = ECSignature.parseCompact(signatureBuffer) var parsed = ECSignature.parseCompact(signature)
var e = BigInteger.fromBuffer(hash) var e = BigInteger.fromBuffer(hash)
var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i) var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)

4
test/ecpubkey.js

@ -91,13 +91,13 @@ describe('ECPubKey', function() {
it('verifies a valid signature', function() { it('verifies a valid signature', function() {
var hash = crypto.sha256(fixtures.message) var hash = crypto.sha256(fixtures.message)
assert.ok(pubKey.verify(hash, signature)) assert(pubKey.verify(hash, signature))
}) })
it('doesn\'t verify the wrong signature', function() { it('doesn\'t verify the wrong signature', function() {
var hash = crypto.sha256('mushrooms') var hash = crypto.sha256('mushrooms')
assert.ok(!pubKey.verify(hash, signature)) assert(!pubKey.verify(hash, signature))
}) })
}) })
}) })

14
test/message.js

@ -26,29 +26,25 @@ describe('Message', function() {
var network = networks[f.network] var network = networks[f.network]
var address = Address.fromBase58Check(f.address) var address = Address.fromBase58Check(f.address)
var signature = new Buffer(f.signature, 'base64') assert(Message.verify(address, f.signature, f.message, network))
assert.ok(Message.verify(address, signature, f.message, network))
}) })
fixtures.valid.verify.forEach(function(f) { fixtures.valid.verify.forEach(function(f) {
it('verifies a valid signature for \"' + f.message + '\" (' + f.network + ')', function() { it('verifies a valid signature for \"' + f.message + '\" (' + f.network + ')', function() {
var network = networks[f.network] var network = networks[f.network]
var signature = new Buffer(f.signature, 'base64') var signature = f.signature
assert.ok(Message.verify(f.address, signature, f.message, network)) assert(Message.verify(f.address, f.signature, f.message, network))
if (f.compressed) { if (f.compressed) {
var compressedSignature = new Buffer(f.compressed.signature, 'base64') assert(Message.verify(f.compressed.address, f.compressed.signature, f.message, network))
assert.ok(Message.verify(f.compressed.address, compressedSignature, f.message, network))
} }
}) })
}) })
fixtures.invalid.verify.forEach(function(f) { fixtures.invalid.verify.forEach(function(f) {
it(f.description, function() { it(f.description, function() {
var signature = new Buffer(f.signature, 'base64') assert(!Message.verify(f.address, f.signature, f.message))
assert.ok(!Message.verify(f.address, signature, f.message))
}) })
}) })
}) })

2
test/wallet.js

@ -63,7 +63,7 @@ describe('Wallet', function() {
describe('when seed is not specified', function(){ describe('when seed is not specified', function(){
it('generates a seed', function(){ it('generates a seed', function(){
var wallet = new Wallet() var wallet = new Wallet()
assert.ok(wallet.getMasterKey()) assert(wallet.getMasterKey())
}) })
}) })

Loading…
Cancel
Save