diff --git a/lib/address.js b/lib/address.js index e56c429..248b5cb 100644 --- a/lib/address.js +++ b/lib/address.js @@ -1,5 +1,6 @@ 'use strict'; +var $ = require('./util/preconditions'); var base58check = require('./encoding/base58check'); var Networks = require('./networks'); var Hash = require('./crypto/hash'); @@ -316,6 +317,10 @@ Address.fromJSON = function fromJSON(json) { if (JSUtil.isValidJSON(json)) { json = JSON.parse(json); } + $.checkState( + JSUtil.isHexa(json.hash), + 'Unexpected hash property, "' + json.hash + '", expected to be hex.' + ); var hashBuffer = new Buffer(json.hash, 'hex'); return new Address(hashBuffer, json.network, json.type); }; @@ -390,7 +395,7 @@ Address.prototype.toBuffer = function() { /** * @returns {Object} An object of the address */ -Address.prototype.toJSON = function toJSON() { +Address.prototype.toObject = function toObject() { return { hash: this.hashBuffer.toString('hex'), type: this.type, @@ -398,6 +403,13 @@ Address.prototype.toJSON = function toJSON() { }; }; +/** + * @returns {Object} An object of the address + */ +Address.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); +}; + /** * Will return a the string representation of the address * diff --git a/test/address.js b/test/address.js index 704a3c3..5491861 100644 --- a/test/address.js +++ b/test/address.js @@ -363,7 +363,7 @@ describe('Address', function() { }); it('should output/input a JSON string', function() { - var json = JSON.stringify(new Address(str).toJSON()); + var json = new Address(str).toJSON(); var address = Address.fromJSON(json); address.toString().should.equal(str); });