diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index e2856908..45a0bb3f 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -38,7 +38,7 @@ function Address(options) { this.hash = null; this.type = null; this.version = null; - this.network = bcoin.network.get().type; + this.network = null; if (options) this.fromOptions(options); @@ -78,10 +78,26 @@ Address.prototype.getHash = function getHash(enc) { */ Address.prototype.toBase58 = function toBase58(network) { + var p = new BufferWriter(); + var prefix; + if (!network) network = this.network; - return Address.toBase58(this.hash, this.type, this.version, network); + network = bcoin.network.get(network); + prefix = network.address.prefixes[this.type]; + + assert(prefix != null, 'Not a valid address prefix.'); + + p.writeU8(prefix); + if (this.version !== -1) { + p.writeU8(this.version); + p.writeU8(0); + } + p.writeBytes(this.hash); + p.writeChecksum(); + + return utils.toBase58(p.render()); }; /** @@ -115,53 +131,6 @@ Address.prototype.inspect = function inspect() { + '>'; }; -/** - * Compile a hash to an address. - * @param {Hash|Buffer} hash - * @param {AddressType?} type - * @param {Number?} version - Witness version. - * @returns {Base58Address} - * @throws Error on bad hash/prefix. - */ - -Address.toBase58 = function toBase58(hash, type, version, network) { - var p, prefix; - - if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); - - if (!type) - type = 'pubkeyhash'; - - network = bcoin.network.get(network); - - prefix = network.address.prefixes[type]; - - if (!(version >= 0)) - version = network.address.versions[type]; - - assert(prefix != null, 'Not a valid address prefix.'); - - if (!(version >= 0)) - assert(hash.length === 20, 'Hash is the wrong size.'); - else if (version === 0 && type === 'witnesspubkeyhash') - assert(hash.length === 20, 'Hash is the wrong size.'); - else if (version === 0 && type === 'witnessscripthash') - assert(hash.length === 32, 'Hash is the wrong size.'); - - p = new BufferWriter(); - - p.writeU8(prefix); - if (version != null) { - p.writeU8(version); - p.writeU8(0); - } - p.writeBytes(hash); - p.writeChecksum(); - - return utils.toBase58(p.render()); -}; - /** * Parse a base58 address. * @param {Base58Address} address @@ -170,7 +139,7 @@ Address.toBase58 = function toBase58(hash, type, version, network) { */ Address.prototype.fromBase58 = function fromBase58(address) { - var i, prefix, type, version, hash, network, p; + var i, prefix, type, version, nversion, hash, network, p; if (typeof address === 'string') address = utils.fromBase58(address); @@ -187,12 +156,17 @@ Address.prototype.fromBase58 = function fromBase58(address) { assert(type != null, 'Unknown address prefix.'); - version = network.address.versions[type]; + nversion = network.address.versions[type]; - if (version != null) { + if (nversion == null) + nversion = -1; + + if (nversion !== -1) { version = p.readU8(); assert(version >= 0 && version <= 16, 'Bad program version.'); assert(p.readU8() === 0, 'Address version padding is non-zero.'); + } else { + version = -1; } if (type === 'witnessscripthash') @@ -205,7 +179,7 @@ Address.prototype.fromBase58 = function fromBase58(address) { this.network = network.type; this.type = type; this.hash = hash; - this.version = version == null ? -1 : version; + this.version = version; return this; }; @@ -372,13 +346,37 @@ Address.fromScript = function fromScript(script) { */ Address.prototype.fromHash = function fromHash(hash, type, version, network) { + var p, prefix, nversion; + if (typeof hash === 'string') hash = new Buffer(hash, 'hex'); + if (!type) + type = 'pubkeyhash'; + + if (version == null) + version = -1; + + network = bcoin.network.get(network); + prefix = network.address.prefixes[type]; + nversion = network.address.versions[type]; + + if (version === -1 && nversion != null) + version = nversion; + + assert(prefix != null, 'Not a valid address prefix.'); + + if (version === -1) + assert(hash.length === 20, 'Hash is the wrong size.'); + else if (version === 0 && type === 'witnesspubkeyhash') + assert(hash.length === 20, 'Hash is the wrong size.'); + else if (version === 0 && type === 'witnessscripthash') + assert(hash.length === 32, 'Hash is the wrong size.'); + this.hash = hash; - this.type = type || 'pubkeyhash'; - this.version = version == null ? -1 : version; - this.network = bcoin.network.get(network).type; + this.type = type; + this.version = version; + this.network = network.type; return this; }; diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 79aa5189..30aa98cf 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -61,11 +61,11 @@ Coin.prototype.fromOptions = function fromOptions(options) { assert(utils.isNumber(this.version)); assert(utils.isNumber(this.height)); - assert(typeof this.value === 'number'); + assert(utils.isNumber(this.value)); assert(this.script instanceof bcoin.script); assert(typeof this.coinbase === 'boolean'); assert(!this.hash || typeof this.hash === 'string'); - assert(!this.index || typeof this.index === 'number'); + assert(!this.index || utils.isNumber(this.index)); return this; }; diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index 7fb15e1b..e7bee97a 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -317,7 +317,7 @@ KeyRing.prototype.getKeyAddress = function getKeyAddress() { */ KeyRing.prototype.compile = function compile(hash, type, version) { - return bcoin.address.toBase58(hash, type, version, this.network); + return bcoin.address.fromHash(hash, type, version, this.network).toBase58(); }; /**