From 6acef06cbc87a3051ba238a2fb640562e718135e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 12 Apr 2017 23:13:29 -0700 Subject: [PATCH] address: implement bech32 addresses. --- lib/primitives/address.js | 93 ++++++++++++ lib/protocol/networks.js | 15 +- lib/utils/bech32.js | 295 ++++++++++++++++++++++++++++++++++++++ lib/utils/index.js | 1 + test/bech32-test.js | 264 ++++++++++++++++++++++++++++++++++ 5 files changed, 663 insertions(+), 5 deletions(-) create mode 100644 lib/utils/bech32.js create mode 100644 test/bech32-test.js diff --git a/lib/primitives/address.js b/lib/primitives/address.js index d5251cd5..5c11d730 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -17,6 +17,7 @@ var crypto = require('../crypto/crypto'); var BufferReader = require('../utils/reader'); var StaticWriter = require('../utils/staticwriter'); var base58 = require('../utils/base58'); +var bech32 = require('../utils/bech32'); /** * Represents an address. @@ -211,6 +212,33 @@ Address.prototype.toBase58 = function toBase58(network) { return base58.encode(this.toRaw(network)); }; +/** + * Compile the address object to a bech32 address. + * @param {{NetworkType|Network)?} network + * @returns {String} + * @throws Error on bad hash/prefix. + */ + +Address.prototype.toBech32 = function toBech32(network) { + var version = this.version; + var hash = this.hash; + var hrp, data; + + assert(version !== -1, + 'Cannot convert non-segwit address to bech32.'); + + if (!network) + network = this.network; + + network = Network.get(network); + + hrp = network.addressPrefix.bech32; + + data = bech32.bitsify(hash, 65, 8, 5, version, 0); + + return bech32.encode(hrp, data); +}; + /** * Convert the Address to a string. * @returns {Base58Address} @@ -311,6 +339,70 @@ Address.fromBase58 = function fromBase58(address, network) { return new Address().fromBase58(address, network); }; +/** + * Inject properties from bech32 address. + * @private + * @param {String} data + * @param {Network?} network + * @throws Parse error + */ + +Address.prototype.fromBech32 = function fromBech32(data, network) { + var i, addr, hash, type, version; + + assert(typeof data === 'string'); + + addr = bech32.decode(data); + + if (addr.data.length < 1) + throw new Error('Invalid bech32 data length.'); + + version = addr.data[0]; + + if (network) { + network = Network.get(network); + if (addr.hrp !== network.addressPrefix.bech32) + throw new Error('Network mismatch for bech32 address.'); + } else { + for (i = 0; i < networks.types.length; i++) { + network = networks[networks.types[i]]; + if (addr.hrp === network.addressPrefix.bech32) + break; + } + + assert(i < networks.types.length, 'Unknown bech32 address prefix.'); + } + + hash = bech32.bitsify(addr.data, 84, 5, 8, -1, 1); + + // TODO: Remove this by dropping old segwit addrs. + switch (hash.length) { + case 20: + type = Address.types.WITNESSPUBKEYHASH; + break; + case 32: + type = Address.types.WITNESSSCRIPTHASH; + break; + default: + assert(false, 'Unknown witness program data length.'); + break; + } + + return this.fromHash(hash, type, version, network.type); +}; + +/** + * Create an address object from a bech32 address. + * @param {String} address + * @param {Network?} network + * @returns {Address} + * @throws Parse error. + */ + +Address.fromBech32 = function fromBech32(address, network) { + return new Address().fromBech32(address, network); +}; + /** * Inject properties from output script. * @private @@ -497,6 +589,7 @@ Address.prototype.fromHash = function fromHash(hash, type, version, network) { assert(hash.length === 32, 'Hash is the wrong size.'); else if (version === 1 && type === Address.types.WITNESSSCRIPTHASH) assert(hash.length === 32, 'Hash is the wrong size.'); + assert(hash.length >= 2 && hash.length <= 40, 'Hash is the wrong size.'); } this.hash = hash; diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index 2df66ef9..a6b114f4 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -401,7 +401,8 @@ main.addressPrefix = { pubkeyhash: 0x00, scripthash: 0x05, witnesspubkeyhash: 0x06, - witnessscripthash: 0x0a + witnessscripthash: 0x0a, + bech32: 'bc' }; /** @@ -611,7 +612,8 @@ testnet.addressPrefix = { pubkeyhash: 0x6f, scripthash: 0xc4, witnesspubkeyhash: 0x03, - witnessscripthash: 0x28 + witnessscripthash: 0x28, + bech32: 'tb' }; testnet.requireStandard = false; @@ -758,7 +760,8 @@ regtest.addressPrefix = { pubkeyhash: 0x6f, scripthash: 0xc4, witnesspubkeyhash: 0x03, - witnessscripthash: 0x28 + witnessscripthash: 0x28, + bech32: 'tb' }; regtest.requireStandard = false; @@ -899,7 +902,8 @@ segnet4.addressPrefix = { pubkeyhash: 0x1e, scripthash: 0x32, witnesspubkeyhash: 0x03, - witnessscripthash: 0x28 + witnessscripthash: 0x28, + bech32: 'tb' }; segnet4.requireStandard = false; @@ -1048,7 +1052,8 @@ simnet.addressPrefix = { pubkeyhash: 0x3f, scripthash: 0x7b, witnesspubkeyhash: 0x19, - witnessscripthash: 0x28 + witnessscripthash: 0x28, + bech32: 'tb' }; simnet.requireStandard = false; diff --git a/lib/utils/bech32.js b/lib/utils/bech32.js new file mode 100644 index 00000000..5f63f0b2 --- /dev/null +++ b/lib/utils/bech32.js @@ -0,0 +1,295 @@ +/*! + * bech32.js - bech32 for bcoin + * Copyright (c) 2017, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + * + * Parts of this software are based on "bech32". + * https://github.com/sipa/bech32 + * + * Copyright (c) 2017 Pieter Wuille + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +'use strict'; + +/** + * @module utils/bech32 + */ + +var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; +var TABLE = {}; +var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; +var ZERO6 = new Buffer('000000000000', 'hex'); +var POOL6 = new Buffer(6); +var POOL10 = new Buffer(10); +var i; + +for (i = 0; i < CHARSET.length; i++) + TABLE[CHARSET[i]] = i; + +/** + * Allocate a buffer from the pool. + * @ignore + * @param {Number} size + * @returns {Buffer} + */ + +function alloc(size) { + if (size > 10) + return new Buffer(size); + return POOL10.slice(0, size); +} + +/** + * Update checksum. + * @ignore + * @param {Buffer} values + * @param {Number} chk + * @returns {Number} + */ + +function polymod(values, chk) { + var i, j, top; + + for (i = 0; i < values.length; i++) { + top = chk >> 25; + chk = (chk & 0x1ffffff) << 5 ^ values[i]; + + for (j = 0; j < 5; j++) { + if ((top >> j) & 1) + chk ^= GENERATOR[j]; + } + } + + return chk; +} + +/** + * Expand human readable part. + * @ignore + * @param {String} hrp + * @returns {Buffer} + */ + +function expand(hrp) { + var ret = alloc(hrp.length * 2 + 1); + var p = 0; + var i; + + for (i = 0; i < hrp.length; i++) + ret[p++] = hrp.charCodeAt(i) >> 5; + + ret[p++] = 0; + + for (i = 0; i < hrp.length; i++) + ret[p++] = hrp.charCodeAt(i) & 31; + + return ret; +} + +/** + * Verify checksum against hrp and data. + * @ignore + * @param {String} hrp + * @param {Buffer} data + * @returns {Boolean} + */ + +function verify(hrp, data) { + var chk = 1; + + chk = polymod(expand(hrp), chk); + chk = polymod(data, chk); + + return chk === 1; +} + +/** + * Create checksum from hrp and data. + * @ignore + * @param {String} hrp + * @param {Buffer} data + * @returns {Buffer} + */ + +function checksum(hrp, data) { + var chk = 1; + var ret = POOL6; + var p = 0; + var i, mod; + + chk = polymod(expand(hrp), chk); + chk = polymod(data, chk); + chk = polymod(ZERO6, chk); + + mod = chk ^ 1; + + for (i = 0; i < 6; i++) + ret[p++] = (mod >> 5 * (5 - i)) & 31; + + return ret; +} + +/** + * Encode hrp and data as a bech32 string. + * @ignore + * @param {String} hrp + * @param {Buffer} data + * @returns {String} + */ + +function encode(hrp, data) { + var chk = checksum(hrp, data); + var str = hrp + '1'; + var i; + + for (i = 0; i < data.length; i++) + str += CHARSET[data[i]]; + + for (i = 0; i < chk.length; i++) + str += CHARSET[chk[i]]; + + return str; +} + +/** + * Decode a bech32 string. + * @param {String} str + * @returns {Bech32Result} + */ + +function decode(str) { + var lower = false; + var upper = false; + var p = 0; + var i, ch, pos, hrp, data; + + for (i = 0; i < str.length; i++) { + ch = str.charCodeAt(i); + + if (ch < 33 || ch > 126) + throw new Error('Bech32 character out of range.'); + + if (ch >= 97 && ch <= 122) + lower = true; + + if (ch >= 65 && ch <= 90) + upper = true; + } + + if (lower && upper) + throw new Error('Invalid bech32 casing.'); + + str = str.toLowerCase(); + + pos = str.lastIndexOf('1'); + + if (pos < 1 || pos + 7 > str.length || str.length > 90) + throw new Error('Invalid bech32 data section.'); + + hrp = str.substring(0, pos); + data = new Buffer(str.length - (pos + 1)); + + for (i = pos + 1; i < str.length; i++) { + ch = TABLE[str[i]]; + + if (ch == null) + throw new Error('Invalid bech32 character.'); + + data[p++] = ch; + } + + if (!verify(hrp, data)) + throw new Error('Invalid bech32 checksum.'); + + return new Bech32Result(hrp, data.slice(0, -6)); +} + +/** + * Convert serialized data to bits, + * suitable to be serialized as bech32. + * @param {Buffer} data + * @param {Number} size + * @param {Number} frombits + * @param {Number} tobits + * @param {Number} pad + * @param {Number} off + * @returns {Buffer} + */ + +function bitsify(data, size, frombits, tobits, pad, off) { + var acc = 0; + var bits = 0; + var maxv = (1 << tobits) - 1; + var ret = new Buffer(size); + var p = 0; + var i, value; + + if (pad !== -1) + ret[p++] = pad; + + for (i = off; i < data.length; i++) { + value = data[i]; + + if ((value >> frombits) !== 0) + throw new Error('Invalid value in bech32 bits.'); + + acc = (acc << frombits) | value; + bits += frombits; + + while (bits >= tobits) { + bits -= tobits; + ret[p++] = (acc >> bits) & maxv; + } + } + + if (pad !== -1) { + if (bits > 0) + ret[p++] = (acc << (tobits - bits)) & maxv; + } else { + if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) + throw new Error('Bad bech32 bits.'); + } + + return ret.slice(0, p); +} + +/** + * Bech32Result + * @constructor + * @private + * @param {String} hrp + * @param {Buffer} data + * @property {String} hrp + * @property {Buffer} data + */ + +function Bech32Result(hrp, data) { + this.hrp = hrp; + this.data = data; +} + +/* + * Expose + */ + +exports.decode = decode; +exports.encode = encode; +exports.bitsify = bitsify; diff --git a/lib/utils/index.js b/lib/utils/index.js index ced85e0f..c890169b 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -8,6 +8,7 @@ exports.ASN1 = require('./asn1'); exports.AsyncObject = require('./asyncobject'); exports.base32 = require('./base32'); exports.base58 = require('./base58'); +exports.bech32 = require('./bech32'); exports.Bloom = require('./bloom'); exports.RollingFilter = exports.Bloom.Rolling; exports.co = require('./co'); diff --git a/test/bech32-test.js b/test/bech32-test.js new file mode 100644 index 00000000..be6ab58d --- /dev/null +++ b/test/bech32-test.js @@ -0,0 +1,264 @@ +// Parts of this software are based on "bech32". +// https://github.com/sipa/bech32 +// +// Copyright (c) 2017 Pieter Wuille +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +'use strict'; + +var assert = require('assert'); +var bech32 = require('../lib/utils/bech32'); +var Address = require('../lib/primitives/address'); + +describe('Bech32', function() { + var VALID_CHECKSUM = [ + 'A12UEL5L', + 'an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs', + 'abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw', + '11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j', + 'split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w' + ]; + + var VALID_ADDRESS = [ + [ + 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4', + new Buffer([ + 0x00, 0x14, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, + 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6 + ]) + ], + [ + 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7', + new Buffer([ + 0x00, 0x20, 0x18, 0x63, 0x14, 0x3c, 0x14, 0xc5, 0x16, 0x68, 0x04, + 0xbd, 0x19, 0x20, 0x33, 0x56, 0xda, 0x13, 0x6c, 0x98, 0x56, 0x78, + 0xcd, 0x4d, 0x27, 0xa1, 0xb8, 0xc6, 0x32, 0x96, 0x04, 0x90, 0x32, + 0x62 + ]) + ], + [ + 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx', + new Buffer([ + 0x81, 0x28, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, + 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6, + 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, + 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6 + ]) + ], + [ + 'BC1SW50QA3JX3S', + new Buffer([ + 0x90, 0x02, 0x75, 0x1e + ]) + ], + [ + 'bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj', + new Buffer([ + 0x82, 0x10, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, + 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23 + ]) + ], + [ + 'tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy', + new Buffer([ + 0x00, 0x20, 0x00, 0x00, 0x00, 0xc4, 0xa5, 0xca, 0xd4, 0x62, 0x21, + 0xb2, 0xa1, 0x87, 0x90, 0x5e, 0x52, 0x66, 0x36, 0x2b, 0x99, 0xd5, + 0xe9, 0x1c, 0x6c, 0xe2, 0x4d, 0x16, 0x5d, 0xab, 0x93, 0xe8, 0x64, + 0x33 + ]) + ] + ]; + + var INVALID_ADDRESS = [ + 'tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty', + 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5', + 'BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2', + 'bc1rw5uspcuh', + 'bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90', + 'BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P', + 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7', + 'tb1pw508d6qejxtdg4y5r3zarqfsj6c3', + 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv', + ]; + + function fromAddress(hrp, addr) { + var dec = bech32.decode(addr); + var data; + + if (dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) + throw new Error('Invalid bech32 prefix or data length.'); + + data = bech32.bitsify(dec.data, 84, 5, 8, -1, 1); + + if (data.length < 2 || data.length > 40) + throw new Error('Invalid witness program size.'); + + if (dec.data[0] === 0 && data.length !== 20 && data.length !== 32) + throw new Error('Malformed witness program.'); + + return { + version: dec.data[0], + program: data + }; + } + + function toAddress(hrp, version, program) { + var data = bech32.bitsify(program, 65, 8, 5, version, 0); + var ret = bech32.encode(hrp, data); + + fromAddress(hrp, ret); + + return ret; + } + + function createProgram(version, program) { + var ver = new Buffer([version ? version + 0x80 : 0, program.length]); + return Buffer.concat([ver, program]); + } + + VALID_CHECKSUM.forEach(function(test) { + it('should have valid checksum for ' + test, function() { + var ret = bech32.decode(test); + assert(ret); + }); + }); + + VALID_ADDRESS.forEach(function(test) { + var address = test[0]; + var scriptpubkey = test[1]; + it('should have valid address for ' + address, function() { + var hrp = 'bc'; + var ret, ok, output, recreate; + + try { + ret = fromAddress(hrp, address); + } catch (e) { + ret = null; + } + + if (ret === null) { + hrp = 'tb'; + try { + ret = fromAddress(hrp, address); + } catch (e) { + ret = null; + } + } + + ok = ret !== null; + + if (ok) { + output = createProgram(ret.version, ret.program); + ok = output.compare(scriptpubkey) === 0; + } + + if (ok) { + recreate = toAddress(hrp, ret.version, ret.program); + ok = (recreate === address.toLowerCase()); + } + + assert(ok); + }); + }); + + INVALID_ADDRESS.forEach(function(test) { + it('should have invalid address for ' + test, function() { + var ok1, ok2, ok; + + try { + ok1 = fromAddress('bc', test); + } catch (e) { + ok1 = null; + } + + try { + ok2 = fromAddress('tb', test); + } catch (e) { + ok2 = null; + } + + ok = ok1 === null && ok2 === null; + assert(ok); + }); + }); + + VALID_ADDRESS.forEach(function(test, i) { + var address = test[0]; + var scriptpubkey = test[1]; + + // TODO: Fix. (wrong length for program) + // Need to drop old segwit addrs. + if (i >= 2 && i <= 4) + return; + + it('should have valid address for ' + address, function() { + var ret, ok, output, recreate; + + try { + ret = Address.fromBech32(address, 'main'); + } catch (e) { + ret = null; + } + + if (ret === null) { + try { + ret = Address.fromBech32(address, 'testnet'); + } catch (e) { + ret = null; + } + } + + ok = ret !== null; + + if (ok) { + output = createProgram(ret.version, ret.hash); + ok = output.compare(scriptpubkey) === 0; + } + + if (ok) { + recreate = ret.toBech32(); + ok = (recreate === address.toLowerCase()); + } + + assert(ok); + }); + }); + + INVALID_ADDRESS.forEach(function(test) { + it('should have invalid address for ' + test, function() { + var ok1, ok2, ok; + + try { + ok1 = Address.fromBech32(test, 'main'); + } catch (e) { + ok1 = null; + } + + try { + ok2 = Address.fromBech32(test, 'testnet'); + } catch (e) { + ok2 = null; + } + + ok = ok1 === null && ok2 === null; + assert(ok); + }); + }); +});