From c9f367ac36750b572acb505f389b42708ef69186 Mon Sep 17 00:00:00 2001 From: Otto Allmendinger Date: Tue, 7 Jan 2020 14:45:37 +0100 Subject: [PATCH] src/networks.js: add tests Make sure values are distinct unless explicitly grouped. This method uncovered bug BG-16466. Issue: BG-16465 --- src/networks.js | 2 +- test/networks.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 test/networks.js diff --git a/src/networks.js b/src/networks.js index 95f0e7c2..3ecf97b4 100644 --- a/src/networks.js +++ b/src/networks.js @@ -24,7 +24,6 @@ const coins = { DASH: 'dash' } - module.exports = { // https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp @@ -171,6 +170,7 @@ module.exports = { }, pubKeyHash: 0x6f, scriptHash: 0x3a, + // FIXME(BG-16466): should be 0xef instead wif: 0xb0, coin: coins.LTC }, diff --git a/test/networks.js b/test/networks.js new file mode 100644 index 00000000..ad9e5224 --- /dev/null +++ b/test/networks.js @@ -0,0 +1,118 @@ +/* global describe, it */ +const assert = require('assert') + +const { networks, coins } = require('../src') + +describe('networks', function () { + // Ideally, all properties for all coins should be distinct. + // However, there are some exceptions and some networks share the same properties. + + // Here we define some groups of networks that are allowed to share properties. + const bitcoinSharedMessagePrefix = [ + 'bitcoin', 'testnet', + 'bitcoincash', 'bitcoincashTestnet', + 'bitcoinsv', 'bitcoinsvTestnet' + ] + + const bitcoinMainnetSharedPubkeyPrefix = [ + 'bitcoin', + 'bitcoincash', + 'bitcoinsv' + ] + + const bitcoinMainnetSharedScriptPrefix = bitcoinMainnetSharedPubkeyPrefix.filter(() => true) + + const bitcoinTestnetSharedPubkeyPrefix = [ + 'testnet', + 'bitcoincashTestnet', + 'bitcoinsvTestnet', + 'litecoinTest' + ] + + const bitcoinTestnetSharedScriptPrefix = bitcoinTestnetSharedPubkeyPrefix.filter(n => n !== 'litecoinTest') + + const bitcoinMainnetSharedWIFPrefix = [ + 'bitcoin', + 'bitcoincash', + 'bitcoingold', + 'bitcoinsv', + 'zcash' + ] + + const bitcoinTestnetSharedWIFPrefix = [ + 'testnet', + 'bitcoincashTestnet', + 'bitcoinsvTestnet', + 'dashTest', + 'zcashTest' + ] + + // FIXME(BG-16466): this is a bug, they should be distinct + const litecoinSharedWIFPrefix = [ + 'litecoin', + 'litecoinTest' + ] + + const bech32Coins = [ + 'bitcoin', 'testnet', + 'bitcoingold', + 'litecoin', 'litecoinTest' + ] + + function sameGroup (group, name, otherName) { + return group.includes(name) && group.includes(otherName) + } + + for (const name in networks) { + const network = networks[name] + + describe(`networks.${name}`, function () { + it('has expected properties', function () { + assert.strictEqual(typeof network, 'object') + assert.strictEqual(typeof network.messagePrefix, 'string') + assert.strictEqual(typeof network.bech32, bech32Coins.includes(name) ? 'string' : 'undefined') + assert.strictEqual(typeof network.bip32, 'object') + assert.strictEqual(typeof network.pubKeyHash, 'number') + assert.strictEqual(typeof network.scriptHash, 'number') + assert.strictEqual(typeof network.wif, 'number') + assert.strictEqual(typeof network.coin, 'string') + }) + + for (const otherName in networks) { + if (name === otherName) { + continue + } + + it(`has distinct properties with ${otherName}`, function () { + const otherNetwork = networks[otherName] + + assert.strictEqual( + (network.messagePrefix === otherNetwork.messagePrefix), + (network.coin === otherNetwork.coin) || + sameGroup(bitcoinSharedMessagePrefix, name, otherName) + ) + + assert.strictEqual( + (network.pubKeyHash === otherNetwork.pubKeyHash), + sameGroup(bitcoinMainnetSharedPubkeyPrefix, name, otherName) || + sameGroup(bitcoinTestnetSharedPubkeyPrefix, name, otherName) + ) + + assert.strictEqual( + (network.scriptHash === otherNetwork.scriptHash), + sameGroup(bitcoinMainnetSharedScriptPrefix, name, otherName) || + sameGroup(bitcoinTestnetSharedScriptPrefix, name, otherName) + ) + + assert.strictEqual( + (network.wif === otherNetwork.wif), + sameGroup(bitcoinMainnetSharedWIFPrefix, name, otherName) || + sameGroup(bitcoinTestnetSharedWIFPrefix, name, otherName) || + // FIXME(BG-16466): this group should not exist + sameGroup(litecoinSharedWIFPrefix, name, otherName) + ) + }) + } + }) + } +})