Browse Source

Changed *Json to *JSON and returned an object so that methods can be called from other toJSON methods without the double-stringification issue.

patch-2
Braydon Fuller 10 years ago
parent
commit
94cc10b1a3
  1. 4
      lib/block.js
  2. 4
      lib/blockheader.js
  3. 18
      lib/hdprivatekey.js
  4. 18
      lib/hdpublickey.js
  5. 2
      lib/util/js.js
  6. 4
      test/hdprivatekey.js
  7. 4
      test/hdpublickey.js

4
lib/block.js

@ -37,7 +37,7 @@ Block._from = function _from(arg) {
var info = {}; var info = {};
if (bu.isBuffer(arg)) { if (bu.isBuffer(arg)) {
info = Block._fromBufferReader(BufferReader(arg)); info = Block._fromBufferReader(BufferReader(arg));
} else if (ju.isValidJson(arg)) { } else if (ju.isValidJSON(arg)) {
info = Block._fromJSON(arg); info = Block._fromJSON(arg);
} else if (_.isObject(arg)) { } else if (_.isObject(arg)) {
info = { info = {
@ -59,7 +59,7 @@ Block._from = function _from(arg) {
* @private * @private
*/ */
Block._fromJSON = function _fromJSON(data) { Block._fromJSON = function _fromJSON(data) {
if (ju.isValidJson(data)) { if (ju.isValidJSON(data)) {
data = JSON.parse(data); data = JSON.parse(data);
} }
var txs = []; var txs = [];

4
lib/blockheader.js

@ -34,7 +34,7 @@ BlockHeader._from = function _from(arg) {
var info = {}; var info = {};
if (bu.isBuffer(arg)) { if (bu.isBuffer(arg)) {
info = BlockHeader._fromBufferReader(BufferReader(arg)); info = BlockHeader._fromBufferReader(BufferReader(arg));
} else if (ju.isValidJson(arg)) { } else if (ju.isValidJSON(arg)) {
info = BlockHeader._fromJSON(arg); info = BlockHeader._fromJSON(arg);
} else if (_.isObject(arg)) { } else if (_.isObject(arg)) {
info = { info = {
@ -57,7 +57,7 @@ BlockHeader._from = function _from(arg) {
* @private * @private
*/ */
BlockHeader._fromJSON = function _fromJSON(data) { BlockHeader._fromJSON = function _fromJSON(data) {
if (ju.isValidJson(data)) { if (ju.isValidJSON(data)) {
data = JSON.parse(data); data = JSON.parse(data);
} }
var info = { var info = {

18
lib/hdprivatekey.js

@ -45,8 +45,8 @@ function HDPrivateKey(arg) {
if (_.isString(arg) || bufferUtil.isBuffer(arg)) { if (_.isString(arg) || bufferUtil.isBuffer(arg)) {
if (HDPrivateKey.isValidSerialized(arg)) { if (HDPrivateKey.isValidSerialized(arg)) {
this._buildFromSerialized(arg); this._buildFromSerialized(arg);
} else if (jsUtil.isValidJson(arg)) { } else if (jsUtil.isValidJSON(arg)) {
this._buildFromJson(arg); this._buildFromJSON(arg);
} else { } else {
throw HDPrivateKey.getSerializedError(arg); throw HDPrivateKey.getSerializedError(arg);
} }
@ -212,7 +212,7 @@ HDPrivateKey._validateNetwork = function(data, networkArg) {
return null; return null;
}; };
HDPrivateKey.prototype._buildFromJson = function(arg) { HDPrivateKey.prototype._buildFromJSON = function(arg) {
return this._buildFromObject(JSON.parse(arg)); return this._buildFromObject(JSON.parse(arg));
}; };
@ -406,7 +406,7 @@ HDPrivateKey.prototype.toString = function() {
* *
* @return {Object} * @return {Object}
*/ */
HDPrivateKey.prototype.toObject = function() { HDPrivateKey.prototype.toJSON = function() {
return { return {
network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name, network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name,
depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth), depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth),
@ -420,16 +420,6 @@ HDPrivateKey.prototype.toObject = function() {
}; };
}; };
/**
* Returns a string with the results from <tt>toObject</tt>
*
* @see {HDPrivateKey#toObject}
* @return {string}
*/
HDPrivateKey.prototype.toJson = function() {
return JSON.stringify(this.toObject());
};
HDPrivateKey.DefaultDepth = 0; HDPrivateKey.DefaultDepth = 0;
HDPrivateKey.DefaultFingerprint = 0; HDPrivateKey.DefaultFingerprint = 0;
HDPrivateKey.DefaultChildIndex = 0; HDPrivateKey.DefaultChildIndex = 0;

18
lib/hdpublickey.js

@ -41,8 +41,8 @@ function HDPublicKey(arg) {
var error = HDPublicKey.getSerializedError(arg); var error = HDPublicKey.getSerializedError(arg);
if (!error) { if (!error) {
return this._buildFromSerialized(arg); return this._buildFromSerialized(arg);
} else if (jsUtil.isValidJson(arg)) { } else if (jsUtil.isValidJSON(arg)) {
return this._buildFromJson(arg); return this._buildFromJSON(arg);
} else { } else {
if (error instanceof hdErrors.ArgumentIsPrivateExtended) { if (error instanceof hdErrors.ArgumentIsPrivateExtended) {
return new HDPrivateKey(arg).hdPublicKey; return new HDPrivateKey(arg).hdPublicKey;
@ -211,7 +211,7 @@ HDPublicKey._validateNetwork = function (data, networkArg) {
return null; return null;
}; };
HDPublicKey.prototype._buildFromJson = function (arg) { HDPublicKey.prototype._buildFromJSON = function (arg) {
return this._buildFromObject(JSON.parse(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 * * xpubkey: the string with the base58 representation of this extended key
* * checksum: the base58 checksum of xpubkey * * checksum: the base58 checksum of xpubkey
*/ */
HDPublicKey.prototype.toObject = function () { HDPublicKey.prototype.toJSON = function () {
return { return {
network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name, network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name,
depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth), depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth),
@ -380,16 +380,6 @@ HDPublicKey.prototype.toObject = function () {
}; };
}; };
/**
* Returns the JSON representation of this key's <tt>toObject</tt> result
*
* @see {HDPublicKey#toObject}
* @return {string}
*/
HDPublicKey.prototype.toJson = function () {
return JSON.stringify(this.toObject());
};
HDPublicKey.Hardened = 0x80000000; HDPublicKey.Hardened = 0x80000000;
HDPublicKey.RootElementAlias = ['m', 'M']; HDPublicKey.RootElementAlias = ['m', 'M'];

2
lib/util/js.js

@ -23,7 +23,7 @@ module.exports = {
* @param {string} arg * @param {string} arg
* @return {Object|boolean} false if the argument is not a JSON string. * @return {Object|boolean} false if the argument is not a JSON string.
*/ */
isValidJson: function isValidJson(arg) { isValidJSON: function isValidJSON(arg) {
try { try {
return JSON.parse(arg); return JSON.parse(arg);
} catch (e) { } catch (e) {

4
test/hdprivatekey.js

@ -68,8 +68,8 @@ describe('HDPrivate key interface', function() {
it('builds a json keeping the structure and same members', function() { it('builds a json keeping the structure and same members', function() {
assert(_.isEqual( assert(_.isEqual(
JSON.parse(new HDPrivateKey(json).toJson()), new HDPrivateKey(json).toJSON(),
JSON.parse(new HDPrivateKey(xprivkey).toJson()) new HDPrivateKey(xprivkey).toJSON()
)); ));
}); });

4
test/hdpublickey.js

@ -106,8 +106,8 @@ describe('HDPublicKey interface', function() {
it('can generate a json that has a particular structure', function() { it('can generate a json that has a particular structure', function() {
assert(_.isEqual( assert(_.isEqual(
JSON.parse(new HDPublicKey(json).toJson()), new HDPublicKey(json).toJSON(),
JSON.parse(new HDPublicKey(xpubkey).toJson()) new HDPublicKey(xpubkey).toJSON()
)); ));
}); });

Loading…
Cancel
Save