diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index 9ece5f43..93cb76ed 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -173,19 +173,25 @@ Address.prototype.fromBase58 = function fromBase58(data) { assert(type != null, 'Unknown address prefix.'); if (data.length > 25) { + assert(network.address.witness[type], 'Non-witness address too long.'); 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 { + assert(data.length === 25, 'Address too short.'); version = -1; - assert(data.length === 25); } - if (data.length === 39) - hash = p.readBytes(32); - else - hash = p.readBytes(20); + hash = p.readBytes(p.left() - 4); + + 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.'); + else if (version === 1 && type === 'witnessscripthash') + assert(hash.length === 32, 'Hash is the wrong size.'); p.verifyChecksum(); diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 0f57be06..2d5a98d0 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -283,8 +283,8 @@ Mnemonic.prototype.fromPhrase = function fromPhrase(phrase) { for (i = 0; i < cbits; i++) { bit = i % 8; oct = (i - bit) / 8; - b = (chk[oct] >>> (7 - bit)) & 1; - j = (ent[oct] >>> (7 - bit)) & 1; + b = (ent[oct] >>> (7 - bit)) & 1; + j = (chk[oct] >>> (7 - bit)) & 1; if (b !== j) throw new Error('Invalid checksum.'); } diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index ef3060d7..3fa974a7 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -380,13 +380,13 @@ main.address = { }, /** - * {@link Base58Address} versions. + * {@link Base58Address} witness types. * @enum {Number} */ - versions: { - witnesspubkeyhash: 0, - witnessscripthash: 0 + witness: { + witnesspubkeyhash: true, + witnessscripthash: true } }; @@ -398,14 +398,6 @@ main.address = { main.address.prefixesByVal = utils.revMap(main.address.prefixes); -/** - * {@link Base58Address} versions by value. - * @type {RevMap} - * @default - */ - -main.address.versionsByVal = utils.revMap(main.address.versions); - /** * Default value for whether the mempool * accepts non-standard transactions. @@ -598,14 +590,13 @@ testnet.address = { witnesspubkeyhash: 3, witnessscripthash: 40 }, - versions: { - witnesspubkeyhash: 0, - witnessscripthash: 0 + witness: { + witnesspubkeyhash: true, + witnessscripthash: true } }; testnet.address.prefixesByVal = utils.revMap(testnet.address.prefixes); -testnet.address.versionsByVal = utils.revMap(testnet.address.versions); testnet.requireStandard = false; @@ -745,14 +736,13 @@ regtest.address = { witnesspubkeyhash: 3, witnessscripthash: 40 }, - versions: { - witnesspubkeyhash: 0, - witnessscripthash: 0 + witness: { + witnesspubkeyhash: true, + witnessscripthash: true } }; regtest.address.prefixesByVal = utils.revMap(regtest.address.prefixes); -regtest.address.versionsByVal = utils.revMap(regtest.address.versions); regtest.requireStandard = false; @@ -876,14 +866,13 @@ segnet3.address = { witnesspubkeyhash: 3, witnessscripthash: 40 }, - versions: { - witnesspubkeyhash: 0, - witnessscripthash: 0 + witness: { + witnesspubkeyhash: true, + witnessscripthash: true } }; segnet3.address.prefixesByVal = utils.revMap(segnet3.address.prefixes); -segnet3.address.versionsByVal = utils.revMap(segnet3.address.versions); segnet3.requireStandard = false; @@ -1022,14 +1011,13 @@ segnet4.address = { witnesspubkeyhash: 3, witnessscripthash: 40 }, - versions: { - witnesspubkeyhash: 0, - witnessscripthash: 0 + witness: { + witnesspubkeyhash: true, + witnessscripthash: true } }; segnet4.address.prefixesByVal = utils.revMap(segnet4.address.prefixes); -segnet4.address.versionsByVal = utils.revMap(segnet4.address.versions); segnet4.requireStandard = false;