Skip to content

Commit db8efb5

Browse files
Merge pull request #5177 from BitGo/BTC-1450.concrete-utxo-tests
refactor: make AbstractUtxoCoin more concrete
2 parents df692e8 + e456e04 commit db8efb5

File tree

23 files changed

+200
-266
lines changed

23 files changed

+200
-266
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ import {
8888
} from './transaction';
8989
import { assertDescriptorWalletAddress } from './descriptor/assertDescriptorWalletAddress';
9090

91+
import { getChainFromNetwork, getFamilyFromNetwork, getFullNameFromNetwork } from './names';
92+
9193
type UtxoCustomSigningFunction<TNumber extends number | bigint> = {
9294
(params: {
9395
coin: IBaseCoin;
@@ -391,6 +393,30 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
391393
return this._network;
392394
}
393395

396+
getChain() {
397+
return getChainFromNetwork(this.network);
398+
}
399+
400+
getFamily() {
401+
return getFamilyFromNetwork(this.network);
402+
}
403+
404+
getFullName() {
405+
return getFullNameFromNetwork(this.network);
406+
}
407+
408+
/** Indicates whether the coin supports a block target */
409+
supportsBlockTarget() {
410+
// FIXME: the SDK does not seem to use this anywhere so it is unclear what the purpose of this method is
411+
switch (getMainnet(this.network)) {
412+
case utxolib.networks.bitcoin:
413+
case utxolib.networks.dogecoin:
414+
return true;
415+
default:
416+
return false;
417+
}
418+
}
419+
394420
sweepWithSendMany(): boolean {
395421
return true;
396422
}
@@ -1132,14 +1158,6 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
11321158
return true;
11331159
}
11341160

1135-
/**
1136-
* Indicates whether coin supports a block target
1137-
* @returns {boolean}
1138-
*/
1139-
supportsBlockTarget() {
1140-
return true;
1141-
}
1142-
11431161
/**
11441162
* @param addressType
11451163
* @returns true iff coin supports spending from unspentType

modules/abstract-utxo/src/names.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import * as utxolib from '@bitgo/utxo-lib';
2+
3+
function getNetworkName(n: utxolib.Network): utxolib.NetworkName {
4+
const name = utxolib.getNetworkName(n);
5+
if (!name) {
6+
throw new Error('Unknown network');
7+
}
8+
return name;
9+
}
10+
11+
/**
12+
* @param n
13+
* @returns the family name for a network. Testnets and mainnets of the same coin share the same family name.
14+
*/
15+
export function getFamilyFromNetwork(n: utxolib.Network): string {
16+
switch (getNetworkName(n)) {
17+
case 'bitcoin':
18+
case 'testnet':
19+
case 'bitcoinPublicSignet':
20+
case 'bitcoinTestnet4':
21+
case 'bitcoinBitGoSignet':
22+
return 'btc';
23+
case 'bitcoincash':
24+
case 'bitcoincashTestnet':
25+
return 'bch';
26+
case 'ecash':
27+
case 'ecashTest':
28+
return 'bcha';
29+
case 'bitcoingold':
30+
case 'bitcoingoldTestnet':
31+
return 'btg';
32+
case 'bitcoinsv':
33+
case 'bitcoinsvTestnet':
34+
return 'bsv';
35+
case 'dash':
36+
case 'dashTest':
37+
return 'dash';
38+
case 'dogecoin':
39+
case 'dogecoinTest':
40+
return 'doge';
41+
case 'litecoin':
42+
case 'litecoinTest':
43+
return 'ltc';
44+
case 'zcash':
45+
case 'zcashTest':
46+
return 'zec';
47+
}
48+
}
49+
50+
/**
51+
* Get the chain name for a network.
52+
* The chain is different for every network.
53+
*/
54+
export function getChainFromNetwork(n: utxolib.Network): string {
55+
switch (getNetworkName(n)) {
56+
case 'bitcoinPublicSignet':
57+
return 'tbtcsig';
58+
case 'bitcoinTestnet4':
59+
return 'tbtc4';
60+
case 'bitcoinBitGoSignet':
61+
return 'tbtcbgsig';
62+
case 'bitcoin':
63+
case 'testnet':
64+
case 'bitcoincash':
65+
case 'bitcoincashTestnet':
66+
case 'ecash':
67+
case 'ecashTest':
68+
case 'bitcoingold':
69+
case 'bitcoingoldTestnet':
70+
case 'bitcoinsv':
71+
case 'bitcoinsvTestnet':
72+
case 'dash':
73+
case 'dashTest':
74+
case 'dogecoin':
75+
case 'dogecoinTest':
76+
case 'litecoin':
77+
case 'litecoinTest':
78+
case 'zcash':
79+
case 'zcashTest':
80+
const mainnetName = getFamilyFromNetwork(n);
81+
return utxolib.isTestnet(n) ? `t${mainnetName}` : mainnetName;
82+
}
83+
}
84+
85+
export function getFullNameFromNetwork(n: utxolib.Network): string {
86+
const name = getNetworkName(n);
87+
88+
let prefix: string;
89+
switch (name) {
90+
case 'bitcoinTestnet4':
91+
prefix = 'Testnet4 ';
92+
break;
93+
case 'bitcoinPublicSignet':
94+
prefix = 'Public Signet ';
95+
break;
96+
case 'bitcoinBitGoSignet':
97+
prefix = 'BitGo Signet ';
98+
break;
99+
default:
100+
if (utxolib.isTestnet(n)) {
101+
prefix = 'Testnet ';
102+
} else {
103+
prefix = '';
104+
}
105+
}
106+
107+
switch (name) {
108+
case 'bitcoin':
109+
case 'testnet':
110+
case 'bitcoinTestnet4':
111+
case 'bitcoinPublicSignet':
112+
case 'bitcoinBitGoSignet':
113+
return prefix + 'Bitcoin';
114+
case 'bitcoincash':
115+
case 'bitcoincashTestnet':
116+
return prefix + 'Bitcoin Cash';
117+
case 'ecash':
118+
case 'ecashTest':
119+
return prefix + 'Bitcoin ABC';
120+
case 'bitcoingold':
121+
case 'bitcoingoldTestnet':
122+
return prefix + 'Bitcoin Gold';
123+
case 'bitcoinsv':
124+
case 'bitcoinsvTestnet':
125+
return prefix + 'Bitcoin SV';
126+
case 'dash':
127+
case 'dashTest':
128+
return prefix + 'Dash';
129+
case 'dogecoin':
130+
case 'dogecoinTest':
131+
return prefix + 'Dogecoin';
132+
case 'litecoin':
133+
case 'litecoinTest':
134+
return prefix + 'Litecoin';
135+
case 'zcash':
136+
case 'zcashTest':
137+
return prefix + 'ZCash';
138+
default:
139+
throw new Error('Unknown network');
140+
}
141+
}
Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1-
/**
2-
* prettier
3-
*/
4-
import 'should';
1+
import * as assert from 'assert';
52
import * as utxolib from '@bitgo/utxo-lib';
3+
64
import { getUtxoCoinForNetwork, utxoCoins } from './util';
75

86
describe('utxoCoins', function () {
97
it('has expected chain/network values for items', function () {
10-
utxoCoins
11-
.map((c) => [c.getChain(), utxolib.getNetworkName(c.network)])
12-
.should.eql([
13-
['btc', 'bitcoin'],
14-
['tbtc', 'testnet'],
15-
['tbtcsig', 'bitcoinPublicSignet'],
16-
['tbtc4', 'bitcoinTestnet4'],
17-
['tbtcbgsig', 'bitcoinBitGoSignet'],
18-
['bch', 'bitcoincash'],
19-
['tbch', 'bitcoincashTestnet'],
20-
['btg', 'bitcoingold'],
21-
['bsv', 'bitcoinsv'],
22-
['tbsv', 'bitcoinsvTestnet'],
23-
['dash', 'dash'],
24-
['tdash', 'dashTest'],
25-
['doge', 'dogecoin'],
26-
['tdoge', 'dogecoinTest'],
27-
['bcha', 'ecash'],
28-
['tbcha', 'ecashTest'],
29-
['ltc', 'litecoin'],
30-
['tltc', 'litecoinTest'],
31-
['zec', 'zcash'],
32-
['tzec', 'zcashTest'],
33-
]);
8+
assert.deepStrictEqual(
9+
utxoCoins.map((c) => [c.getChain(), c.getFamily(), c.getFullName(), utxolib.getNetworkName(c.network)]),
10+
[
11+
['btc', 'btc', 'Bitcoin', 'bitcoin'],
12+
['tbtc', 'btc', 'Testnet Bitcoin', 'testnet'],
13+
['tbtcsig', 'btc', 'Public Signet Bitcoin', 'bitcoinPublicSignet'],
14+
['tbtc4', 'btc', 'Testnet4 Bitcoin', 'bitcoinTestnet4'],
15+
['tbtcbgsig', 'btc', 'BitGo Signet Bitcoin', 'bitcoinBitGoSignet'],
16+
['bch', 'bch', 'Bitcoin Cash', 'bitcoincash'],
17+
['tbch', 'bch', 'Testnet Bitcoin Cash', 'bitcoincashTestnet'],
18+
['btg', 'btg', 'Bitcoin Gold', 'bitcoingold'],
19+
['bsv', 'bsv', 'Bitcoin SV', 'bitcoinsv'],
20+
['tbsv', 'bsv', 'Testnet Bitcoin SV', 'bitcoinsvTestnet'],
21+
['dash', 'dash', 'Dash', 'dash'],
22+
['tdash', 'dash', 'Testnet Dash', 'dashTest'],
23+
['doge', 'doge', 'Dogecoin', 'dogecoin'],
24+
['tdoge', 'doge', 'Testnet Dogecoin', 'dogecoinTest'],
25+
['bcha', 'bcha', 'Bitcoin ABC', 'ecash'],
26+
['tbcha', 'bcha', 'Testnet Bitcoin ABC', 'ecashTest'],
27+
['ltc', 'ltc', 'Litecoin', 'litecoin'],
28+
['tltc', 'ltc', 'Testnet Litecoin', 'litecoinTest'],
29+
['zec', 'zec', 'ZCash', 'zcash'],
30+
['tzec', 'zec', 'Testnet ZCash', 'zcashTest'],
31+
]
32+
);
3433

35-
utxolib
36-
.getNetworkList()
37-
.map((network): [string | undefined, string | undefined] => {
34+
assert.deepStrictEqual(
35+
utxolib.getNetworkList().map((network): [string | undefined, string | undefined] => {
3836
let coin;
3937
try {
4038
coin = getUtxoCoinForNetwork(network);
@@ -43,8 +41,8 @@ describe('utxoCoins', function () {
4341
}
4442

4543
return [utxolib.getNetworkName(network), coin?.getChain()];
46-
})
47-
.should.eql([
44+
}),
45+
[
4846
['bitcoin', 'btc'],
4947
['testnet', 'tbtc'],
5048
['bitcoinPublicSignet', 'tbtcsig'],
@@ -66,6 +64,7 @@ describe('utxoCoins', function () {
6664
['litecoinTest', 'tltc'],
6765
['zcash', 'zec'],
6866
['zcashTest', 'tzec'],
69-
]);
67+
]
68+
);
7069
});
7170
});

modules/sdk-coin-bch/src/bch.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ export class Bch extends AbstractUtxoCoin {
1111
return new Bch(bitgo);
1212
}
1313

14-
getChain() {
15-
return 'bch';
16-
}
17-
18-
getFamily() {
19-
return 'bch';
20-
}
21-
22-
getFullName() {
23-
return 'Bitcoin Cash';
24-
}
25-
26-
supportsBlockTarget() {
27-
return false;
28-
}
29-
3014
/**
3115
* Canonicalize a Bitcoin Cash address for a specific version
3216
*

modules/sdk-coin-bch/src/tbch.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,4 @@ export class Tbch extends Bch {
1313
static createInstance(bitgo: BitGoBase): BaseCoin {
1414
return new Tbch(bitgo);
1515
}
16-
17-
getChain() {
18-
return 'tbch';
19-
}
20-
21-
getFullName() {
22-
return 'Testnet Bitcoin Cash';
23-
}
2416
}

modules/sdk-coin-bcha/src/bcha.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ export class Bcha extends Bch {
1212
return new Bcha(bitgo);
1313
}
1414

15-
getChain(): string {
16-
return 'bcha';
17-
}
18-
19-
getFamily(): string {
20-
return 'bcha';
21-
}
22-
23-
getFullName(): string {
24-
return 'Bitcoin ABC';
25-
}
26-
2715
canonicalAddress(address: string, version: unknown = 'base58'): string {
2816
if (version === 'base58') {
2917
return utxolib.addressFormat.toCanonicalFormat(address, this.network);

modules/sdk-coin-bcha/src/tbcha.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,4 @@ export class Tbcha extends Bcha {
1313
static createInstance(bitgo: BitGoBase): BaseCoin {
1414
return new Tbcha(bitgo);
1515
}
16-
17-
getChain(): string {
18-
return 'tbcha';
19-
}
20-
21-
getFullName(): string {
22-
return 'Testnet Bitcoin ABC';
23-
}
2416
}

modules/sdk-coin-bsv/src/bsv.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,4 @@ export class Bsv extends Bch {
1111
static createInstance(bitgo: BitGoBase): BaseCoin {
1212
return new Bsv(bitgo);
1313
}
14-
15-
getChain(): string {
16-
return 'bsv';
17-
}
18-
19-
getFamily(): string {
20-
return 'bsv';
21-
}
22-
23-
getFullName(): string {
24-
return 'Bitcoin SV';
25-
}
2614
}

modules/sdk-coin-bsv/src/tbsv.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,4 @@ export class Tbsv extends Bsv {
1313
static createInstance(bitgo: BitGoBase): BaseCoin {
1414
return new Tbsv(bitgo);
1515
}
16-
17-
getChain(): string {
18-
return 'tbsv';
19-
}
20-
21-
getFullName(): string {
22-
return 'Testnet Bitcoin SV';
23-
}
2416
}

0 commit comments

Comments
 (0)