Address: Added toObject method and changed toJSON to return a string
This commit is contained in:
parent
feb3aa3150
commit
354c03987a
|
@ -1,5 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var $ = require('./util/preconditions');
|
||||||
var base58check = require('./encoding/base58check');
|
var base58check = require('./encoding/base58check');
|
||||||
var Networks = require('./networks');
|
var Networks = require('./networks');
|
||||||
var Hash = require('./crypto/hash');
|
var Hash = require('./crypto/hash');
|
||||||
|
@ -316,6 +317,10 @@ Address.fromJSON = function fromJSON(json) {
|
||||||
if (JSUtil.isValidJSON(json)) {
|
if (JSUtil.isValidJSON(json)) {
|
||||||
json = JSON.parse(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');
|
var hashBuffer = new Buffer(json.hash, 'hex');
|
||||||
return new Address(hashBuffer, json.network, json.type);
|
return new Address(hashBuffer, json.network, json.type);
|
||||||
};
|
};
|
||||||
|
@ -390,7 +395,7 @@ Address.prototype.toBuffer = function() {
|
||||||
/**
|
/**
|
||||||
* @returns {Object} An object of the address
|
* @returns {Object} An object of the address
|
||||||
*/
|
*/
|
||||||
Address.prototype.toJSON = function toJSON() {
|
Address.prototype.toObject = function toObject() {
|
||||||
return {
|
return {
|
||||||
hash: this.hashBuffer.toString('hex'),
|
hash: this.hashBuffer.toString('hex'),
|
||||||
type: this.type,
|
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
|
* Will return a the string representation of the address
|
||||||
*
|
*
|
||||||
|
|
|
@ -363,7 +363,7 @@ describe('Address', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should output/input a JSON string', 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);
|
var address = Address.fromJSON(json);
|
||||||
address.toString().should.equal(str);
|
address.toString().should.equal(str);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue