From c0bbf96dc10aefdb78b8626dd3424ae75e7bc2ec Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 12 Dec 2014 18:47:15 -0500 Subject: [PATCH] Keys: Added toObject method and changed toJSON to return a string --- lib/privatekey.js | 10 ++++++---- lib/publickey.js | 27 ++++++--------------------- test/privatekey.js | 4 ++-- test/publickey.js | 4 ++-- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 948e3a5..e9b5ea3 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -310,11 +310,9 @@ PrivateKey.prototype.toAddress = function() { }; /** - * Will output the PrivateKey to a WIF string - * - * @returns {String} A WIF representation of the private key + * @returns {Object} A plain object representation */ -PrivateKey.prototype.toJSON = function() { +PrivateKey.prototype.toObject = function toObject() { return { bn: this.bn.toString('hex'), compressed: this.compressed, @@ -322,6 +320,10 @@ PrivateKey.prototype.toJSON = function() { }; }; +PrivateKey.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); +}; + /** * Will output the PrivateKey to a WIF string * diff --git a/lib/publickey.js b/lib/publickey.js index d9fbc2b..a50afdf 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -6,7 +6,6 @@ var Point = require('./crypto/point'); var JSUtil = require('./util/js'); /** - * * Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'. * * @example @@ -137,7 +136,6 @@ PublicKey._transformDER = function(buf){ }; /** - * * Internal function to transform X into a public key point * * @param {Boolean} odd - If the point is above or below the x axis @@ -155,7 +153,6 @@ PublicKey._transformX = function(odd, x){ }; /** - * * Instantiate a PublicKey from JSON * * @param {String} json - A JSON string @@ -172,7 +169,6 @@ PublicKey.fromJSON = function(json) { }; /** - * * Instantiate a PublicKey from a PrivateKey * * @param {PrivateKey} privkey - An instance of PrivateKey @@ -184,7 +180,6 @@ PublicKey.fromPrivateKey = function(privkey) { }; /** - * * Instantiate a PublicKey from a Buffer * * @param {Buffer} buf - A DER hex buffer @@ -196,7 +191,6 @@ PublicKey.fromBuffer = function(buf) { }; /** - * * Instantiate a PublicKey from a Point * * @param {Point} point - A Point instance @@ -210,7 +204,6 @@ PublicKey.fromPoint = function(point, compressed){ }; /** - * * Instantiate a PublicKey from a DER Buffer * * @param {Buffer} buf - A DER Buffer @@ -222,7 +215,6 @@ PublicKey.fromDER = function(buf) { }; /** - * * Instantiate a PublicKey from a DER hex encoded string * * @param {String} str - A DER hex string @@ -236,7 +228,6 @@ PublicKey.fromString = function(str, encoding) { }; /** - * * Instantiate a PublicKey from an X Point * * @param {Boolean} odd - If the point is above or below the x axis @@ -250,7 +241,6 @@ PublicKey.fromX = function(odd, x) { /** - * * Check if there would be any errors when initializing a PublicKey * * @param {String} data - The encoded data in various formats @@ -268,7 +258,6 @@ PublicKey.getValidationError = function(data) { }; /** - * * Check if the parameters are valid * * @param {String} data - The encoded data in various formats @@ -280,12 +269,9 @@ PublicKey.isValid = function(data) { }; /** - * - * Will output the PublicKey to JSON - * - * @returns {Object} A JSON object + * @returns {Object} A plain object of the PublicKey */ -PublicKey.prototype.toJSON = function() { +PublicKey.prototype.toObject = function toObject() { return { x: this.point.getX().toString('hex'), y: this.point.getY().toString('hex'), @@ -293,8 +279,11 @@ PublicKey.prototype.toJSON = function() { }; }; +PublicKey.prototype.toJSON = function toJSON(){ + return JSON.stringify(this.toObject()); +}; + /** - * * Will output the PublicKey to a Buffer * * @returns {Buffer} A DER hex encoded buffer @@ -305,7 +294,6 @@ PublicKey.prototype.toBuffer = function() { }; /** - * * Will output the PublicKey to a DER Buffer * * @returns {Buffer} A DER hex encoded buffer @@ -338,7 +326,6 @@ PublicKey.prototype.toDER = function(compressed) { }; /** - * * Will return an address for the public key * * @returns {Address} An address generated from the public key @@ -348,7 +335,6 @@ PublicKey.prototype.toAddress = function(network) { }; /** - * * Will output the PublicKey to a DER encoded hex string * * @returns {String} A DER hex encoded string @@ -359,7 +345,6 @@ PublicKey.prototype.toString = function() { }; /** - * * Will return a string formatted for the console * * @returns {String} Public key diff --git a/test/privatekey.js b/test/privatekey.js index 8ba951e..92dac71 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -132,11 +132,11 @@ describe('PrivateKey', function() { describe('#json', function() { it('should input/output json', function() { - var json = { + var json = JSON.stringify({ bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a', compressed: false, network: 'livenet' - }; + }); PrivateKey.fromJSON(json).toJSON().should.deep.equal(json); }); diff --git a/test/publickey.js b/test/publickey.js index fd74f32..f93e951 100644 --- a/test/publickey.js +++ b/test/publickey.js @@ -124,11 +124,11 @@ describe('PublicKey', function() { describe('#json', function() { it('should input/ouput json', function() { - var json = { + var json = JSON.stringify({ x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', compressed: false - }; + }); PublicKey.fromJSON(json).toJSON().should.deep.equal(json); });