From d1052e4996d946811510fac78eddfc400c3172ea Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 14 Aug 2017 17:15:34 +1000 Subject: [PATCH] add from/toBech32 --- package.json | 1 + src/address.js | 40 +++++++++++++++++ test/address.js | 52 ++++++++++++++++++++-- test/fixtures/address.json | 91 +++++++++++++++++++++++++++++++++++++- 4 files changed, 179 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 61ccdcc..1f0646f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "src" ], "dependencies": { + "bech32": "0.0.3", "bigi": "^1.4.0", "bip66": "^1.1.0", "bitcoin-ops": "^1.3.0", diff --git a/src/address.js b/src/address.js index 8799015..5bc49ba 100644 --- a/src/address.js +++ b/src/address.js @@ -1,4 +1,5 @@ var Buffer = require('safe-buffer').Buffer +var bech32 = require('bech32') var bs58check = require('bs58check') var bscript = require('./script') var networks = require('./networks') @@ -16,6 +17,28 @@ function fromBase58Check (address) { return { hash: hash, version: version } } +function fromBech32 (address, expectedPrefix) { + var result = bech32.decode(address) + var prefix = result.prefix + var words = result.words + if (expectedPrefix !== undefined) { + if (prefix !== expectedPrefix) throw new Error('Expected ' + expectedPrefix + ', got ' + prefix) + } + + var version = words[0] + if (version > 16) throw new Error('Invalid version (' + version + ')') + var program = bech32.fromWords(words.slice(1)) + + if (version === 0) { + if (program.length !== 20 && program.length !== 32) throw new Error('Unknown program') + } else { + if (program.length < 2) throw new Error('Program too short') + if (program.length > 40) throw new Error('Program too long') + } + + return { version, prefix, program: Buffer.from(program) } +} + function toBase58Check (hash, version) { typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments) @@ -26,6 +49,21 @@ function toBase58Check (hash, version) { return bs58check.encode(payload) } +function toBech32 (prefix, version, program) { + if (version > 16) throw new Error('Invalid version (' + version + ')') + if (version === 0) { + if (program.length !== 20 && program.length !== 32) throw new Error('Unknown program') + } else { + if (program.length < 2) throw new Error('Program too short') + if (program.length > 40) throw new Error('Program too long') + } + + var words = bech32.toWords(program) + words.unshift(version) + + return bech32.encode(prefix, words) +} + function fromOutputScript (outputScript, network) { network = network || networks.bitcoin @@ -47,7 +85,9 @@ function toOutputScript (address, network) { module.exports = { fromBase58Check: fromBase58Check, + fromBech32: fromBech32, fromOutputScript: fromOutputScript, toBase58Check: toBase58Check, + toBech32: toBech32, toOutputScript: toOutputScript } diff --git a/test/address.js b/test/address.js index 56d264f..13bea6a 100644 --- a/test/address.js +++ b/test/address.js @@ -8,7 +8,7 @@ var fixtures = require('./fixtures/address.json') describe('address', function () { describe('fromBase58Check', function () { - fixtures.valid.forEach(function (f) { + fixtures.standard.forEach(function (f) { it('decodes ' + f.base58check, function () { var decode = baddress.fromBase58Check(f.base58check) @@ -26,8 +26,30 @@ describe('address', function () { }) }) + describe('fromBech32', function () { + fixtures.bech32.forEach((f) => { + it('encodes ' + f.address, function () { + var actual = baddress.fromBech32(f.address) + + assert.strictEqual(actual.prefix, f.prefix) + assert.strictEqual(actual.program.toString('hex'), f.program) + assert.strictEqual(actual.version, f.version) + }) + }) + + fixtures.invalid.bech32.forEach((f, i) => { + if (f.address === undefined) return + + it('decode fails for ' + f.address + '(' + f.exception + ')', function () { + assert.throws(function () { + baddress.fromBech32(f.address, f.prefix) + }, new RegExp(f.exception)) + }) + }) + }) + describe('fromOutputScript', function () { - fixtures.valid.forEach(function (f) { + fixtures.standard.forEach(function (f) { it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { var script = bscript.fromASM(f.script) var address = baddress.fromOutputScript(script, networks[f.network]) @@ -48,7 +70,7 @@ describe('address', function () { }) describe('toBase58Check', function () { - fixtures.valid.forEach(function (f) { + fixtures.standard.forEach(function (f) { it('formats ' + f.hash + ' (' + f.network + ')', function () { var address = baddress.toBase58Check(Buffer.from(f.hash, 'hex'), f.version) @@ -57,8 +79,30 @@ describe('address', function () { }) }) + describe('toBech32', function () { + fixtures.bech32.forEach((f, i) => { + // unlike the reference impl., we don't support mixed/uppercase + var string = f.address.toLowerCase() + var program = Buffer.from(f.program, 'hex') + + it('encode ' + string, function () { + assert.deepEqual(baddress.toBech32(f.prefix, f.version, program), string) + }) + }) + + fixtures.invalid.bech32.forEach((f, i) => { + if (!f.prefix || f.version === undefined || f.program === undefined) return + + it('encode fails (' + f.exception, function () { + assert.throws(function () { + baddress.toBech32(f.prefix, f.version, Buffer.from(f.program, 'hex')) + }, new RegExp(f.exception)) + }) + }) + }) + describe('toOutputScript', function () { - fixtures.valid.forEach(function (f) { + fixtures.standard.forEach(function (f) { var network = networks[f.network] it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { diff --git a/test/fixtures/address.json b/test/fixtures/address.json index 0d4a632..3aba4b2 100644 --- a/test/fixtures/address.json +++ b/test/fixtures/address.json @@ -1,5 +1,5 @@ { - "valid": [ + "standard": [ { "network": "bitcoin", "version": 0, @@ -43,7 +43,96 @@ "script": "OP_HASH160 cd7b44d0b03f2d026d1e586d7ae18903b0d385f6 OP_EQUAL" } ], + "bech32": [ + { + "address": "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", + "prefix": "bc", + "program": "751e76e8199196d454941c45d1b3a323f1433bd6", + "version": 0 + }, + { + "address": "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", + "prefix": "tb", + "program": "1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262", + "version": 0 + }, + { + "address": "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx", + "prefix": "bc", + "program": "751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6", + "version": 1 + }, + { + "address": "BC1SW50QA3JX3S", + "prefix": "bc", + "program": "751e", + "version": 16 + }, + { + "address": "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj", + "prefix": "bc", + "program": "751e76e8199196d454941c45d1b3a323", + "version": 2 + }, + { + "address": "tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", + "prefix": "tb", + "program": "000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433", + "version": 0 + } + ], "invalid": { + "bech32": [ + { + "address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", + "exception": "Invalid checksum" + }, + { + "address": "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2", + "prefix": "bc", + "version": 17, + "program": "751e76e8199196d454941c45d1b3a323f1433bd6", + "exception": "Invalid version \\(17\\)" + }, + { + "address": "BC1SW50QA3JX3S", + "prefix": "foo", + "exception": "Expected foo, got bc" + }, + { + "address": "bc1rw5uspcuh", + "prefix": "bc", + "version": 1, + "program": "75", + "exception": "Program too short" + }, + { + "address": "bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90", + "prefix": "bc", + "version": 1, + "program": "751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675", + "exception": "Program too long" + }, + { + "address": "BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P", + "prefix": "bc", + "version": 0, + "program": "1d1e76e8199196d454941c45d1b3a323", + "exception": "Unknown program" + }, + { + "address": "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7", + "exception": "Mixed-case string" + }, + { + "address": "tb1pw508d6qejxtdg4y5r3zarqfsj6c3", + "exception": "Excess padding" + }, + { + "address": "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv", + "exception": "Non-zero padding" + } + ], "fromBase58Check": [ { "address": "7SeEnXWPaCCALbVrTnszCVGfRU8cGfx",