Skip to content

Commit 1bbb8b4

Browse files
Merge pull request #5164 from BitGo/BTC-1472.regtest
feat(utxo-bin): add support for regtest addresses
2 parents 6e7ee30 + 002f296 commit 1bbb8b4

File tree

64 files changed

+833
-124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+833
-124
lines changed

modules/utxo-bin/src/AddressParser.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as utxolib from '@bitgo/utxo-lib';
22
import * as bech32 from 'bech32';
33

44
import { Parser, ParserNode } from './Parser';
5+
import { getNetworkList, getNetworkName } from './args';
56

67
const bs58 = require('bs58');
78
const bs58check = require('bs58check');
@@ -98,21 +99,26 @@ export class AddressParser extends Parser {
9899
}
99100

100101
convert(outputScript: Buffer): ParserNode {
102+
function getWidth(list: readonly string[]): number {
103+
return Math.max(...list.map((s) => s.length));
104+
}
105+
const networkWith = getWidth(getNetworkList().map(getNetworkName));
106+
const addressFormatWith = getWidth(utxolib.addressFormat.addressFormats);
101107
return this.node(
102108
'converted',
103109
undefined,
104-
utxolib.getNetworkList().flatMap((network) =>
110+
getNetworkList().flatMap((network) =>
105111
utxolib.addressFormat.addressFormats
106112
.filter((f) => utxolib.addressFormat.isSupportedAddressFormat(f, network))
107113
.map((addressFormat) => {
108-
const name = utxolib.getNetworkName(network) as string;
114+
const label = `${getNetworkName(network).padEnd(networkWith)} ${addressFormat.padEnd(addressFormatWith)}`;
109115
try {
110116
return this.node(
111-
`${name} ${addressFormat}`,
117+
label,
112118
utxolib.addressFormat.fromOutputScriptWithFormat(outputScript, addressFormat, network)
113119
);
114120
} catch (e) {
115-
return this.node(`${name} ${addressFormat}`, 'undefined');
121+
return this.node(label, 'undefined');
116122
}
117123
})
118124
)
@@ -139,7 +145,7 @@ export class AddressParser extends Parser {
139145
}
140146

141147
parse(input: string): ParserNode {
142-
const networks = this.params.network ? [this.params.network] : utxolib.getNetworkList();
148+
const networks = this.params.network ? [this.params.network] : getNetworkList();
143149

144150
type Match = {
145151
network: utxolib.Network;
@@ -168,7 +174,7 @@ export class AddressParser extends Parser {
168174

169175
nodes.push(this.node('format', firstMatch.addressFormat));
170176
nodes.push(this.node('outputScript', firstMatch.buffer, this.parseOutputScript(firstMatch.buffer)));
171-
nodes.push(this.node('network', matches.map((m) => utxolib.getNetworkName(m.network)).join(', ')));
177+
nodes.push(this.node('network', matches.map((m) => getNetworkName(m.network)).join(', ')));
172178

173179
if (this.params.all || this.params.convert) {
174180
nodes.push(this.convert(firstMatch.buffer));

modules/utxo-bin/src/args/parseNetwork.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,59 @@
11
import * as utxolib from '@bitgo/utxo-lib';
2+
import { getMainnet } from '@bitgo/utxo-lib';
3+
4+
export const bitcoinRegtest: utxolib.Network = {
5+
...utxolib.networks.testnet,
6+
bech32: 'bcrt',
7+
};
28

39
export function getNetworkForName(name: string): utxolib.Network {
10+
if (name === 'bitcoinRegtest') {
11+
return bitcoinRegtest;
12+
}
413
const network = utxolib.networks[name as utxolib.NetworkName];
514
if (!network) {
615
throw new Error(`invalid network ${name}`);
716
}
817
return network;
918
}
1019

20+
export function getNetworkName(network: utxolib.Network): string {
21+
if (network === bitcoinRegtest) {
22+
return 'bitcoinRegtest';
23+
}
24+
return utxolib.getNetworkName(network) as string;
25+
}
26+
27+
const networkOrder = [
28+
utxolib.networks.bitcoin,
29+
utxolib.networks.bitcoincash,
30+
utxolib.networks.bitcoingold,
31+
utxolib.networks.bitcoinsv,
32+
utxolib.networks.dash,
33+
utxolib.networks.dogecoin,
34+
utxolib.networks.ecash,
35+
utxolib.networks.litecoin,
36+
utxolib.networks.zcash,
37+
];
38+
39+
function getNetworkOrderIndex(network: utxolib.Network): number {
40+
if (network === bitcoinRegtest) {
41+
network = utxolib.networks.bitcoin;
42+
}
43+
network = getMainnet(network);
44+
const index = networkOrder.indexOf(network);
45+
if (index === -1) {
46+
throw new Error(`unknown network ${network}`);
47+
}
48+
return index;
49+
}
50+
51+
export function getNetworkList(): utxolib.Network[] {
52+
return [...utxolib.getNetworkList(), bitcoinRegtest].sort(
53+
(a, b) => getNetworkOrderIndex(a) - getNetworkOrderIndex(b) || getNetworkName(a).localeCompare(getNetworkName(b))
54+
);
55+
}
56+
1157
export function getNetwork(argv: { network: string }): utxolib.Network {
1258
return getNetworkForName(argv.network);
1359
}

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2pkh_default_mziT9FmaG3XjV7tbaGYekqvBcVDvGnY3EZ.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ address: mziT9FmaG3XjV7tbaGYekqvBcVDvGnY3EZ
1111
│ ├── type: pubkeyhash
1212
│ ├── hex: 76a914d2970bfc64117e397dc5cb7a9afe9f8b22dca1f188ac
1313
│ └── asm: OP_DUP OP_HASH160 d2970bfc64117e397dc5cb7a9afe9f8b22dca1f1 OP_EQUALVERIFY OP_CHECKSIG
14-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, ecashTest, litecoinTest
14+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinRegtest, bitcoinTestnet4, testnet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, ecashTest, litecoinTest

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2shP2wsh_default_2MuQ6eusngGJY693CU1VUTADGWQtcBBk1di.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ address: 2MuQ6eusngGJY693CU1VUTADGWQtcBBk1di
1111
│ ├── type: scripthash
1212
│ ├── hex: a914179ddf848a574c6fdb30e6e564f2c8b8fc11354587
1313
│ └── asm: OP_HASH160 179ddf848a574c6fdb30e6e564f2c8b8fc113545 OP_EQUAL
14-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, dogecoinTest, ecashTest
14+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinRegtest, bitcoinTestnet4, testnet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, dogecoinTest, ecashTest

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2sh_default_2MwZgZu9ZT8uDqSkskSJSiDMeoFa4z4RtdW.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ address: 2MwZgZu9ZT8uDqSkskSJSiDMeoFa4z4RtdW
1111
│ ├── type: scripthash
1212
│ ├── hex: a9142f5e2d8f4ee34ccd1a4130975b9fe635ff65e02b87
1313
│ └── asm: OP_HASH160 2f5e2d8f4ee34ccd1a4130975b9fe635ff65e02b OP_EQUAL
14-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, dogecoinTest, ecashTest
14+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinRegtest, bitcoinTestnet4, testnet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, dogecoinTest, ecashTest

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2trMusig2_default_tb1p5xqvqger0zdpcq6s3jznfz352exhk356p9hml6c69matmggrve9s097l78.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ address: tb1p5xqvqger0zdpcq6s3jznfz352exhk356p9hml6c69matmggrve9s097l78
99
│ ├── type: taproot
1010
│ ├── hex: 5120a180c02323789a1c03508c85348a34564d7b469a096fbfeb1a2efabda103664b
1111
│ └── asm: OP_1 a180c02323789a1c03508c85348a34564d7b469a096fbfeb1a2efabda103664b
12-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet
12+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinTestnet4, testnet

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2tr_default_tb1p8gv9m64s539mycj4m29wu8thy5n3y6u36xs58wq3yezrmqygwfns9l2hrf.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ address: tb1p8gv9m64s539mycj4m29wu8thy5n3y6u36xs58wq3yezrmqygwfns9l2hrf
99
│ ├── type: taproot
1010
│ ├── hex: 51203a185deab0a44bb26255da8aee1d772527126b91d1a143b81126443d80887267
1111
│ └── asm: OP_1 3a185deab0a44bb26255da8aee1d772527126b91d1a143b81126443d80887267
12-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet
12+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinTestnet4, testnet

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2wkh_default_tb1q62tshlryz9lrjlw9edaf4l5l3v3deg03l64eut.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ address: tb1q62tshlryz9lrjlw9edaf4l5l3v3deg03l64eut
55
│ ├── type: witnesspubkeyhash
66
│ ├── hex: 0014d2970bfc64117e397dc5cb7a9afe9f8b22dca1f1
77
│ └── asm: OP_0 d2970bfc64117e397dc5cb7a9afe9f8b22dca1f1
8-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet
8+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinTestnet4, testnet

modules/utxo-bin/test/fixtures/formatAddress/bitcoinBitGoSignet_p2wsh_default_tb1qeu6xmet5wqslj2w0wac0hx5gj6dupmp869cajrkt3kw6ud3kkecs62l3du.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ address: tb1qeu6xmet5wqslj2w0wac0hx5gj6dupmp869cajrkt3kw6ud3kkecs62l3du
99
│ ├── type: witnessscripthash
1010
│ ├── hex: 0020cf346de5747021f929cf7770fb9a88969bc0ec27d171d90ecb8d9dae3636b671
1111
│ └── asm: OP_0 cf346de5747021f929cf7770fb9a88969bc0ec27d171d90ecb8d9dae3636b671
12-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet
12+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinTestnet4, testnet

modules/utxo-bin/test/fixtures/formatAddress/bitcoinPublicSignet_p2pkh_default_mziT9FmaG3XjV7tbaGYekqvBcVDvGnY3EZ.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ address: mziT9FmaG3XjV7tbaGYekqvBcVDvGnY3EZ
1111
│ ├── type: pubkeyhash
1212
│ ├── hex: 76a914d2970bfc64117e397dc5cb7a9afe9f8b22dca1f188ac
1313
│ └── asm: OP_DUP OP_HASH160 d2970bfc64117e397dc5cb7a9afe9f8b22dca1f1 OP_EQUALVERIFY OP_CHECKSIG
14-
└── network: testnet, bitcoinPublicSignet, bitcoinTestnet4, bitcoinBitGoSignet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, ecashTest, litecoinTest
14+
└── network: bitcoinBitGoSignet, bitcoinPublicSignet, bitcoinRegtest, bitcoinTestnet4, testnet, bitcoincashTestnet, bitcoingoldTestnet, bitcoinsvTestnet, ecashTest, litecoinTest

0 commit comments

Comments
 (0)