address: expose bech32.

This commit is contained in:
Christopher Jeffrey 2017-04-30 06:42:21 -07:00
parent 4f09065a3e
commit 14ee1f8f03
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -239,12 +239,46 @@ Address.prototype.toBech32 = function toBech32(network) {
return bech32.encode(hrp, data);
};
/**
* Inject properties from string.
* @private
* @param {String} addr
* @param {(Network|NetworkType)?} network
* @returns {Address}
*/
Address.prototype.fromString = function fromString(addr, network) {
assert(typeof addr === 'string');
assert(addr.length > 2);
if (addr[0] === 'b' && addr[1] === 'c')
return this.fromBech32(addr, network);
if (addr[0] === 't' && addr[1] === 'b')
return this.fromBech32(addr, network);
return this.fromBase58(addr, network);
};
/**
* Instantiate address from string.
* @param {String} addr
* @param {(Network|NetworkType)?} network
* @returns {Address}
*/
Address.fromString = function fromString(addr) {
return this.fromString(addr);
};
/**
* Convert the Address to a string.
* @returns {Base58Address}
*/
Address.prototype.toString = function toString() {
if (this.version !== -1)
return this.toBech32();
return this.toBase58();
};