From fcf6254d29f1c6c16d069c1db7679724b9b606f5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 12 Dec 2016 06:50:13 -0800 Subject: [PATCH] address: refactor. --- lib/primitives/address.js | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 23d4d7bf..b79d649a 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -47,6 +47,13 @@ function Address(options) { this.fromOptions(options); } +/** + * Address types. + * @enum {Number} + */ + +Address.types = scriptTypes; + /** * Inject properties from options object. * @private @@ -129,16 +136,11 @@ Address.prototype.getType = function getType() { */ Address.prototype.getSize = function getSize() { - var size = 0; - - size += 1; + var size = 5 + this.hash.length; if (this.version !== -1) size += 2; - size += this.hash.length; - size += 4; - return size; }; @@ -152,21 +154,17 @@ Address.prototype.getSize = function getSize() { Address.prototype.toRaw = function toRaw(network) { var size = this.getSize(); var bw = new StaticWriter(size); - var prefix; - - if (!network) - network = this.network; - - network = Network.get(network); - prefix = Address.getPrefix(this.type, network); + var prefix = this.getPrefix(network); assert(prefix !== -1, 'Not a valid address prefix.'); bw.writeU8(prefix); + if (this.version !== -1) { bw.writeU8(this.version); bw.writeU8(0); } + bw.writeBytes(this.hash); bw.writeChecksum(); @@ -186,17 +184,11 @@ Address.prototype.toBase58 = function toBase58(network) { /** * Convert the Address to a string. - * @returns {Base58String} + * @returns {Base58Address} */ -Address.prototype.toString = function toString(enc) { - if (enc === 'hex') - return this.getHash('hex'); - - if (enc === 'base58') - enc = null; - - return this.toBase58(enc); +Address.prototype.toString = function toString() { + return this.toBase58(); }; /**