From 9f14535cf71f9b211bdabee724ec4afbe643656c Mon Sep 17 00:00:00 2001 From: Kenneth Heutmaker Date: Thu, 21 Jul 2016 14:27:44 -0700 Subject: [PATCH 1/2] Message accepts string or Buffer. --- lib/message.js | 6 ++++-- test/message.js | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/message.js b/lib/message.js index 2e99ad6..dfd533f 100644 --- a/lib/message.js +++ b/lib/message.js @@ -22,7 +22,8 @@ var Message = function Message(message) { if (!(this instanceof Message)) { return new Message(message); } - $.checkArgument(_.isString(message), 'First argument should be a string'); + $.checkArgument(_.isString(message) || message instanceof Buffer, + 'First argument must be a string or Buffer'); this.message = message; return this; @@ -32,7 +33,8 @@ Message.MAGIC_BYTES = new Buffer('Bitcoin Signed Message:\n'); Message.prototype.magicHash = function magicHash() { var prefix1 = BufferWriter.varintBufNum(Message.MAGIC_BYTES.length); - var messageBuffer = new Buffer(this.message); + var messageBuffer = this.message instanceof Buffer ? + this.message : new Buffer(this.message); var prefix2 = BufferWriter.varintBufNum(messageBuffer.length); var buf = Buffer.concat([prefix1, Message.MAGIC_BYTES, prefix2, messageBuffer]); var hash = sha256sha256(buf); diff --git a/test/message.js b/test/message.js index 279c661..2f60cfb 100644 --- a/test/message.js +++ b/test/message.js @@ -27,7 +27,7 @@ describe('Message', function() { it('will error with incorrect message type', function() { expect(function() { return new Message(new Date()); - }).to.throw('First argument should be a string'); + }).to.throw('First argument must be a string or Buffer'); }); it('will instantiate without "new"', function() { @@ -37,8 +37,10 @@ describe('Message', function() { var signature2; var signature3; + var signature4; + var signature5; - it('can sign a message', function() { + it('can sign a text message', function() { var message2 = new Message(text); signature2 = message2._sign(privateKey); signature3 = Message(text).sign(privateKey); @@ -46,6 +48,14 @@ describe('Message', function() { should.exist(signature3); }); + it('can sign a Buffer', function() { + var message2 = new Message(new Buffer(text)); + signature4 = message2._sign(privateKey); + signature5 = Message(text).sign(privateKey); + should.exist(signature4); + should.exist(signature5); + }); + it('sign will error with incorrect private key argument', function() { expect(function() { var message3 = new Message(text); @@ -88,6 +98,13 @@ describe('Message', function() { it('can verify a message with address and generated signature string', function() { var message9 = new Message(text); + var verified = message9.verify(address, signature5); + should.not.exist(message9.error); + verified.should.equal(true); + }); + + it('can verify a message build from a Buffer with address and generated signature string', function() { + var message9 = new Message(new Buffer(text)); var verified = message9.verify(address, signature3); should.not.exist(message9.error); verified.should.equal(true); From dd1eb348feb55857ccd3dd9b1f98040ce6e570a5 Mon Sep 17 00:00:00 2001 From: Kenneth Heutmaker Date: Wed, 27 Jul 2016 13:07:48 -0700 Subject: [PATCH 2/2] Message also accepts an ArrayBuffer --- lib/message.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/message.js b/lib/message.js index dfd533f..3846f5d 100644 --- a/lib/message.js +++ b/lib/message.js @@ -22,7 +22,9 @@ var Message = function Message(message) { if (!(this instanceof Message)) { return new Message(message); } - $.checkArgument(_.isString(message) || message instanceof Buffer, + // When lodash is updated to v4.3.0 or better, change this to _.isBuffer and _.isArrayBuffer + $.checkArgument(_.isString(message) || (Buffer && Buffer.isBuffer(message)) || + (message instanceof Object && message.toString() === "[object ArrayBuffer]"), 'First argument must be a string or Buffer'); this.message = message; @@ -33,8 +35,7 @@ Message.MAGIC_BYTES = new Buffer('Bitcoin Signed Message:\n'); Message.prototype.magicHash = function magicHash() { var prefix1 = BufferWriter.varintBufNum(Message.MAGIC_BYTES.length); - var messageBuffer = this.message instanceof Buffer ? - this.message : new Buffer(this.message); + var messageBuffer = new Buffer(this.message); var prefix2 = BufferWriter.varintBufNum(messageBuffer.length); var buf = Buffer.concat([prefix1, Message.MAGIC_BYTES, prefix2, messageBuffer]); var hash = sha256sha256(buf);