From 14ee1f8f03818dc3a61e616948672af0fc88ae76 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 30 Apr 2017 06:42:21 -0700 Subject: [PATCH] address: expose bech32. --- lib/primitives/address.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 5c11d730..8fab8695 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -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(); };