Skip to content

Commit e456e04

Browse files
feat(abstract-utxo): make AbstractUtxoCoin less abstract
This commit now implements more methods of the BaseCoin class, making AbstractUtxoCoin easier to instantiate. We can now write tests more easily since we can instantiate AbstractUtxoCoin with just the network parameter. Also fixes a bug where Testnet Dash had the wrong family name (`tdash` instead of `dash`). Issue: BTC-1450
1 parent b7c422f commit e456e04

File tree

23 files changed

+168
-233
lines changed

23 files changed

+168
-233
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
}
@@ -1111,14 +1137,6 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
11111137
return true;
11121138
}
11131139

1114-
/**
1115-
* Indicates whether coin supports a block target
1116-
* @returns {boolean}
1117-
*/
1118-
supportsBlockTarget() {
1119-
return true;
1120-
}
1121-
11221140
/**
11231141
* @param addressType
11241142
* @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+
}

modules/bitgo/test/v2/unit/coins/utxo/coins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('utxoCoins', function () {
1919
['bsv', 'bsv', 'Bitcoin SV', 'bitcoinsv'],
2020
['tbsv', 'bsv', 'Testnet Bitcoin SV', 'bitcoinsvTestnet'],
2121
['dash', 'dash', 'Dash', 'dash'],
22-
['tdash', 'tdash', 'Testnet Dash', 'dashTest'],
22+
['tdash', 'dash', 'Testnet Dash', 'dashTest'],
2323
['doge', 'doge', 'Dogecoin', 'dogecoin'],
2424
['tdoge', 'doge', 'Testnet Dogecoin', 'dogecoinTest'],
2525
['bcha', 'bcha', 'Bitcoin ABC', 'ecash'],

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
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ export class Btc extends AbstractUtxoCoin {
2121
return new Btc(bitgo);
2222
}
2323

24-
getChain(): string {
25-
return 'btc';
26-
}
27-
28-
getFamily(): string {
29-
return 'btc';
30-
}
31-
32-
getFullName(): string {
33-
return 'Bitcoin';
34-
}
35-
36-
supportsBlockTarget(): boolean {
37-
return true;
38-
}
39-
4024
supportsLightning(): boolean {
4125
return true;
4226
}

0 commit comments

Comments
 (0)