Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Message to be initialized with a Buffer #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ var Message = function Message(message) {
if (!(this instanceof Message)) {
return new Message(message);
}
$.checkArgument(_.isString(message), 'First argument should be a string');
// 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;

return this;
Expand Down
21 changes: 19 additions & 2 deletions test/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -37,15 +37,25 @@ 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);
should.exist(signature2);
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);
Expand Down Expand Up @@ -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);
Expand Down