diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index b40c5ccf..b40d4ac9 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -161,17 +161,17 @@ Address.prototype.inspect = function inspect() { /** * Inject properties from base58 address. * @private - * @param {Base58Address} address + * @param {Base58Address} data * @throws Parse error */ -Address.prototype.fromBase58 = function fromBase58(address) { - var i, prefix, type, version, nversion, hash, network, p; +Address.prototype.fromBase58 = function fromBase58(data) { + var i, prefix, type, version, hash, network, p; - if (typeof address === 'string') - address = utils.fromBase58(address); + if (typeof data === 'string') + data = utils.fromBase58(data); - p = new BufferReader(address, true); + p = new BufferReader(data, true); prefix = p.readU8(); for (i = 0; i < networks.types.length; i++) { @@ -183,20 +183,17 @@ Address.prototype.fromBase58 = function fromBase58(address) { assert(type != null, 'Unknown address prefix.'); - nversion = network.address.versions[type]; - - if (nversion == null) - nversion = -1; - - if (nversion !== -1) { + if (data.length > 25) { version = p.readU8(); + assert(data.length === 27 || data.length === 39); assert(version >= 0 && version <= 16, 'Bad program version.'); assert(p.readU8() === 0, 'Address version padding is non-zero.'); } else { version = -1; + assert(data.length === 25); } - if (type === 'witnessscripthash') + if (data.length === 39) hash = p.readBytes(32); else hash = p.readBytes(20); @@ -233,6 +230,7 @@ Address.prototype.fromScript = function fromScript(script) { if (script.isProgram()) { program = script.toProgram(); + // TODO: MAST support if (program.isUnknown()) return; this.hash = program.data; @@ -377,7 +375,7 @@ Address.fromScript = function fromScript(script) { */ Address.prototype.fromHash = function fromHash(hash, type, version, network) { - var prefix, nversion; + var prefix; if (typeof hash === 'string') hash = new Buffer(hash, 'hex'); @@ -390,10 +388,6 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) { 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.'); @@ -403,6 +397,8 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) { assert(hash.length === 20, 'Hash is the wrong size.'); else if (version === 0 && type === 'witnessscripthash') assert(hash.length === 32, 'Hash is the wrong size.'); + else if (version === 1 && type === 'witnessscripthash') + assert(hash.length === 32, 'Hash is the wrong size.'); this.hash = hash; this.type = type; @@ -434,11 +430,15 @@ Address.fromHash = function fromHash(hash, type, version, network) { */ Address.prototype.fromData = function fromData(data, type, version, network) { - if (type === 'witnessscripthash') + if (type === 'witnessscripthash') { + assert(version === 0); data = utils.sha256(data); - else + } else if (type === 'witnesspubkeyhash') { + assert(version === 0); data = utils.hash160(data); - + } else { + data = utils.hash160(data); + } return this.fromHash(data, type, version, network); }; diff --git a/lib/bcoin/network.js b/lib/bcoin/network.js index c4c92f01..a8f32877 100644 --- a/lib/bcoin/network.js +++ b/lib/bcoin/network.js @@ -137,7 +137,7 @@ Network.prototype.getRate = function getRate() { */ Network.create = function create(options) { - var net; + var network; if (typeof options === 'string') options = networks[options]; @@ -147,14 +147,14 @@ Network.create = function create(options) { if (Network[options.type]) return Network[options.type]; - net = new Network(options); + network = new Network(options); - Network[net.type] = net; + Network[network.type] = network; if (!Network.primary) - Network.primary = net; + Network.primary = network; - return net; + return network; }; /** diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index f6144347..6cd95e58 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -2911,7 +2911,8 @@ Script.prototype.toProgram = function toProgram() { }; /** - * Test whether the output script is a pay-to-witness-pubkeyhash script. + * Test whether the output script is + * a pay-to-witness-pubkeyhash program. * @returns {Boolean} */ @@ -2922,7 +2923,8 @@ Script.prototype.isWitnessPubkeyhash = function isWitnessPubkeyhash() { }; /** - * Test whether the output script is a pay-to-witness-scripthash script. + * Test whether the output script is + * a pay-to-witness-scripthash program. * @returns {Boolean} */ @@ -2932,6 +2934,18 @@ Script.prototype.isWitnessScripthash = function isWitnessScripthash() { && this.raw[1] === 0x20; }; +/** + * Test whether the output script + * is a pay-to-mast program. + * @returns {Boolean} + */ + +Script.prototype.isMAST = function isMAST() { + return this.raw.length === 34 + && this.raw[0] === opcodes.OP_1 + && this.raw[1] === 0x20; +}; + /** * Test whether the output script is unspendable. * @returns {Boolean} @@ -4622,6 +4636,7 @@ function Program(version, data) { this.data = data; this.type = null; + // TODO: MAST support if (version > 0) { // No interpretation of script (anyone can spend) this.type = 'unknown';