From b5093878ab91342935a264359d4a936993b5da59 Mon Sep 17 00:00:00 2001 From: Trevin Hofmann Date: Sun, 8 Feb 2015 03:00:30 -0600 Subject: [PATCH 1/3] Add some standard methods to Message This adds `fromString`, `fromJSON`, `toObject`, `toJSON`, `toString`, and `inspect` to Message. These methods are trivial since Message only contains a string message, but they should probably be added for consistency. Should toBuffer/fromBuffer be added as well? --- lib/message.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/message.js b/lib/message.js index b12afb5..d370fc7 100644 --- a/lib/message.js +++ b/lib/message.js @@ -8,6 +8,7 @@ var BufferWriter = bitcore.encoding.BufferWriter; var ECDSA = bitcore.crypto.ECDSA; var Signature = bitcore.crypto.Signature; var sha256sha256 = bitcore.crypto.Hash.sha256sha256; +var JSUtil = bitcore.util.js; /** * Will construct a new message to sign and verify. @@ -107,5 +108,61 @@ Message.prototype.verify = function verify(bitcoinAddress, signatureString) { return this._verify(publicKey, signature); }; -module.exports = Message; +/** + * Instantiate a message from a message string + * + * @param {String} str - A string of the message + * @returns {Message} A new instance of a Message + */ +Message.fromString = function(str) { + return new Message(str); +}; +/** + * Instantiate a message from JSON + * + * @param {String} json - An JSON string or Object with keys: message + * @returns {Message} A new instance of a Message + */ +Message.fromJSON = function fromJSON(json) { + if (JSUtil.isValidJSON(json)) { + json = JSON.parse(json); + } + return new Message(json.message); +}; + +/** + * @returns {Object} A plain object with the message information + */ +Message.prototype.toObject = function toObject() { + return { + message: this.message + }; +}; + +/** + * @returns {String} A JSON representation of the message information + */ +Message.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); +}; + +/** + * Will return a the string representation of the message + * + * @returns {String} Message + */ +Message.prototype.toString = function() { + return this.message; +}; + +/** + * Will return a string formatted for the console + * + * @returns {String} Message + */ +Message.prototype.inspect = function() { + return ''; +}; + +module.exports = Message; From af680e4f23579908881e172cadb27ee0c9d52c82 Mon Sep 17 00:00:00 2001 From: Trevin Hofmann Date: Sun, 8 Feb 2015 03:16:18 -0600 Subject: [PATCH 2/3] Add some tests for new methods It should now test `toJSON`, `fromJSON`, `toString`, `fromString`, and `inspect`. --- test/message.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/message.js b/test/message.js index c37e5d2..9e00cc0 100644 --- a/test/message.js +++ b/test/message.js @@ -104,4 +104,47 @@ describe('Message', function() { verified.should.equal(true); }); + describe('#json', function() { + + it('roundtrip to-from-to', function() { + var json = new Message(text).toJSON(); + var message = Message.fromJSON(json); + message.toString().should.equal(message); + }); + + it('checks that the string parameter is valid JSON', function() { + expect(function() { + return Message.fromJSON('ยน'); + }).to.throw(); + }); + + }); + + describe('#toString', function() { + + it('message string', function() { + var message = new Message(text); + message.toString().should.equal(text); + }); + + it('roundtrip to-from-to', function() { + var str = new Message(text).toString(); + var message = Message.fromString(str); + message.toString().should.equal(message); + }); + + }); + + describe('#inspect', function() { + + it('should output formatted output correctly', function() { + var message = new Message(text); + var output = ''; + message.inspect().should.equal(output); + }); + + }); + + + }); From 84e52ef09547744254933e73ec9863b3bb2b0fd9 Mon Sep 17 00:00:00 2001 From: Trevin Hofmann Date: Mon, 9 Feb 2015 12:00:35 -0600 Subject: [PATCH 3/3] Fix roundtrip tests They were testing `toString` against the message itself (`inspect`) instead of against the original text. --- test/message.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/message.js b/test/message.js index 9e00cc0..6143b8e 100644 --- a/test/message.js +++ b/test/message.js @@ -109,7 +109,7 @@ describe('Message', function() { it('roundtrip to-from-to', function() { var json = new Message(text).toJSON(); var message = Message.fromJSON(json); - message.toString().should.equal(message); + message.toString().should.equal(text); }); it('checks that the string parameter is valid JSON', function() { @@ -130,7 +130,7 @@ describe('Message', function() { it('roundtrip to-from-to', function() { var str = new Message(text).toString(); var message = Message.fromString(str); - message.toString().should.equal(message); + message.toString().should.equal(text); }); });