Browse Source

Address: Added toObject method and changed toJSON to return a string

patch-2
Braydon Fuller 10 years ago
parent
commit
354c03987a
  1. 14
      lib/address.js
  2. 2
      test/address.js

14
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
*

2
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);
});

Loading…
Cancel
Save