From 94cc10b1a34da58daa91f3e258daa0c5c34d5c63 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 11 Dec 2014 21:00:51 -0500 Subject: [PATCH] Changed *Json to *JSON and returned an object so that methods can be called from other toJSON methods without the double-stringification issue. --- lib/block.js | 4 ++-- lib/blockheader.js | 4 ++-- lib/hdprivatekey.js | 18 ++++-------------- lib/hdpublickey.js | 18 ++++-------------- lib/util/js.js | 2 +- test/hdprivatekey.js | 4 ++-- test/hdpublickey.js | 4 ++-- 7 files changed, 17 insertions(+), 37 deletions(-) diff --git a/lib/block.js b/lib/block.js index 142a1ca..1a2f051 100644 --- a/lib/block.js +++ b/lib/block.js @@ -37,7 +37,7 @@ Block._from = function _from(arg) { var info = {}; if (bu.isBuffer(arg)) { info = Block._fromBufferReader(BufferReader(arg)); - } else if (ju.isValidJson(arg)) { + } else if (ju.isValidJSON(arg)) { info = Block._fromJSON(arg); } else if (_.isObject(arg)) { info = { @@ -59,7 +59,7 @@ Block._from = function _from(arg) { * @private */ Block._fromJSON = function _fromJSON(data) { - if (ju.isValidJson(data)) { + if (ju.isValidJSON(data)) { data = JSON.parse(data); } var txs = []; diff --git a/lib/blockheader.js b/lib/blockheader.js index 4b10931..0be94d4 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -34,7 +34,7 @@ BlockHeader._from = function _from(arg) { var info = {}; if (bu.isBuffer(arg)) { info = BlockHeader._fromBufferReader(BufferReader(arg)); - } else if (ju.isValidJson(arg)) { + } else if (ju.isValidJSON(arg)) { info = BlockHeader._fromJSON(arg); } else if (_.isObject(arg)) { info = { @@ -57,7 +57,7 @@ BlockHeader._from = function _from(arg) { * @private */ BlockHeader._fromJSON = function _fromJSON(data) { - if (ju.isValidJson(data)) { + if (ju.isValidJSON(data)) { data = JSON.parse(data); } var info = { diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 0d951d5..a336e8c 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -45,8 +45,8 @@ function HDPrivateKey(arg) { if (_.isString(arg) || bufferUtil.isBuffer(arg)) { if (HDPrivateKey.isValidSerialized(arg)) { this._buildFromSerialized(arg); - } else if (jsUtil.isValidJson(arg)) { - this._buildFromJson(arg); + } else if (jsUtil.isValidJSON(arg)) { + this._buildFromJSON(arg); } else { throw HDPrivateKey.getSerializedError(arg); } @@ -212,7 +212,7 @@ HDPrivateKey._validateNetwork = function(data, networkArg) { return null; }; -HDPrivateKey.prototype._buildFromJson = function(arg) { +HDPrivateKey.prototype._buildFromJSON = function(arg) { return this._buildFromObject(JSON.parse(arg)); }; @@ -406,7 +406,7 @@ HDPrivateKey.prototype.toString = function() { * * @return {Object} */ -HDPrivateKey.prototype.toObject = function() { +HDPrivateKey.prototype.toJSON = function() { return { network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name, depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth), @@ -420,16 +420,6 @@ HDPrivateKey.prototype.toObject = function() { }; }; -/** - * Returns a string with the results from toObject - * - * @see {HDPrivateKey#toObject} - * @return {string} - */ -HDPrivateKey.prototype.toJson = function() { - return JSON.stringify(this.toObject()); -}; - HDPrivateKey.DefaultDepth = 0; HDPrivateKey.DefaultFingerprint = 0; HDPrivateKey.DefaultChildIndex = 0; diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 99ef1ad..8f05419 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -41,8 +41,8 @@ function HDPublicKey(arg) { var error = HDPublicKey.getSerializedError(arg); if (!error) { return this._buildFromSerialized(arg); - } else if (jsUtil.isValidJson(arg)) { - return this._buildFromJson(arg); + } else if (jsUtil.isValidJSON(arg)) { + return this._buildFromJSON(arg); } else { if (error instanceof hdErrors.ArgumentIsPrivateExtended) { return new HDPrivateKey(arg).hdPublicKey; @@ -211,7 +211,7 @@ HDPublicKey._validateNetwork = function (data, networkArg) { return null; }; -HDPublicKey.prototype._buildFromJson = function (arg) { +HDPublicKey.prototype._buildFromJSON = function (arg) { return this._buildFromObject(JSON.parse(arg)); }; @@ -366,7 +366,7 @@ HDPublicKey.prototype.toString = function () { * * xpubkey: the string with the base58 representation of this extended key * * checksum: the base58 checksum of xpubkey */ -HDPublicKey.prototype.toObject = function () { +HDPublicKey.prototype.toJSON = function () { return { network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name, depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth), @@ -380,16 +380,6 @@ HDPublicKey.prototype.toObject = function () { }; }; -/** - * Returns the JSON representation of this key's toObject result - * - * @see {HDPublicKey#toObject} - * @return {string} - */ -HDPublicKey.prototype.toJson = function () { - return JSON.stringify(this.toObject()); -}; - HDPublicKey.Hardened = 0x80000000; HDPublicKey.RootElementAlias = ['m', 'M']; diff --git a/lib/util/js.js b/lib/util/js.js index 9b53732..689b0ac 100644 --- a/lib/util/js.js +++ b/lib/util/js.js @@ -23,7 +23,7 @@ module.exports = { * @param {string} arg * @return {Object|boolean} false if the argument is not a JSON string. */ - isValidJson: function isValidJson(arg) { + isValidJSON: function isValidJSON(arg) { try { return JSON.parse(arg); } catch (e) { diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index 2643ddc..b55d1f0 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -68,8 +68,8 @@ describe('HDPrivate key interface', function() { it('builds a json keeping the structure and same members', function() { assert(_.isEqual( - JSON.parse(new HDPrivateKey(json).toJson()), - JSON.parse(new HDPrivateKey(xprivkey).toJson()) + new HDPrivateKey(json).toJSON(), + new HDPrivateKey(xprivkey).toJSON() )); }); diff --git a/test/hdpublickey.js b/test/hdpublickey.js index 8f28b67..3deb3db 100644 --- a/test/hdpublickey.js +++ b/test/hdpublickey.js @@ -106,8 +106,8 @@ describe('HDPublicKey interface', function() { it('can generate a json that has a particular structure', function() { assert(_.isEqual( - JSON.parse(new HDPublicKey(json).toJson()), - JSON.parse(new HDPublicKey(xpubkey).toJson()) + new HDPublicKey(json).toJSON(), + new HDPublicKey(xpubkey).toJSON() )); });