-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathnames.ts
More file actions
203 lines (186 loc) · 5.37 KB
/
names.ts
File metadata and controls
203 lines (186 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import * as utxolib from '@bitgo/utxo-lib';
export const utxoCoinsMainnet = ['btc', 'bch', 'bcha', 'bsv', 'btg', 'dash', 'doge', 'ltc', 'zec'] as const;
export const utxoCoinsTestnet = [
'tbtc',
'tbtc4',
'tbtcsig',
'tbtcbgsig',
'tbch',
'tbcha',
'tbsv',
'tbtg',
'tdash',
'tdoge',
'tltc',
'tzec',
] as const;
export type UtxoCoinNameMainnet = (typeof utxoCoinsMainnet)[number];
export type UtxoCoinNameTestnet = `t${UtxoCoinNameMainnet}` | 'tbtcsig' | 'tbtc4' | 'tbtcbgsig';
export type UtxoCoinName = UtxoCoinNameMainnet | UtxoCoinNameTestnet;
export function isUtxoCoinNameMainnet(coinName: string): coinName is UtxoCoinNameMainnet {
return utxoCoinsMainnet.includes(coinName as UtxoCoinNameMainnet);
}
export function isUtxoCoinNameTestnet(coinName: string): coinName is UtxoCoinNameTestnet {
return utxoCoinsTestnet.includes(coinName as UtxoCoinNameTestnet);
}
export function isUtxoCoinName(coinName: string): coinName is UtxoCoinName {
return isUtxoCoinNameMainnet(coinName) || isUtxoCoinNameTestnet(coinName);
}
export function getMainnetCoinName(coinName: UtxoCoinName): UtxoCoinNameMainnet {
if (isUtxoCoinNameMainnet(coinName)) {
return coinName;
}
switch (coinName) {
case 'tbtc4':
case 'tbtcsig':
case 'tbtcbgsig':
return 'btc';
default:
return coinName.slice(1) as UtxoCoinNameMainnet;
}
}
function getNetworkName(n: utxolib.Network): utxolib.NetworkName {
const name = utxolib.getNetworkName(n);
if (!name) {
throw new Error('Unknown network');
}
return name;
}
/**
* @deprecated - will be removed when we drop support for utxolib
* @param n
* @returns the family name for a network. Testnets and mainnets of the same coin share the same family name.
*/
export function getFamilyFromNetwork(n: utxolib.Network): UtxoCoinNameMainnet {
switch (getNetworkName(n)) {
case 'bitcoin':
case 'testnet':
case 'bitcoinPublicSignet':
case 'bitcoinTestnet4':
case 'bitcoinBitGoSignet':
return 'btc';
case 'bitcoincash':
case 'bitcoincashTestnet':
return 'bch';
case 'ecash':
case 'ecashTest':
return 'bcha';
case 'bitcoingold':
case 'bitcoingoldTestnet':
return 'btg';
case 'bitcoinsv':
case 'bitcoinsvTestnet':
return 'bsv';
case 'dash':
case 'dashTest':
return 'dash';
case 'dogecoin':
case 'dogecoinTest':
return 'doge';
case 'litecoin':
case 'litecoinTest':
return 'ltc';
case 'zcash':
case 'zcashTest':
return 'zec';
}
}
/**
* @deprecated - will be removed when we drop support for utxolib
* Get the chain name for a network.
* The chain is different for every network.
*/
export function getCoinName(n: utxolib.Network): UtxoCoinName {
switch (getNetworkName(n)) {
case 'bitcoinPublicSignet':
return 'tbtcsig';
case 'bitcoinTestnet4':
return 'tbtc4';
case 'bitcoinBitGoSignet':
return 'tbtcbgsig';
case 'bitcoin':
case 'testnet':
case 'bitcoincash':
case 'bitcoincashTestnet':
case 'ecash':
case 'ecashTest':
case 'bitcoingold':
case 'bitcoingoldTestnet':
case 'bitcoinsv':
case 'bitcoinsvTestnet':
case 'dash':
case 'dashTest':
case 'dogecoin':
case 'dogecoinTest':
case 'litecoin':
case 'litecoinTest':
case 'zcash':
case 'zcashTest':
const mainnetName = getFamilyFromNetwork(n);
return utxolib.isTestnet(n) ? `t${mainnetName}` : mainnetName;
}
}
/**
* @deprecated - will be removed when we drop support for utxolib
* @param coinName - the name of the coin (e.g. 'btc', 'bch', 'ltc'). Also called 'chain' in some contexts.
* @returns the network for a coin. This is the mainnet network for the coin.
*/
export function getNetworkFromCoinName(coinName: string): utxolib.Network {
for (const network of utxolib.getNetworkList()) {
if (getCoinName(network) === coinName) {
return network;
}
}
throw new Error(`Unknown coin name ${coinName}`);
}
/** @deprecated - use getNetworkFromCoinName instead */
export const getNetworkFromChain = getNetworkFromCoinName;
function getBaseNameFromMainnet(coinName: UtxoCoinNameMainnet): string {
switch (coinName) {
case 'btc':
return 'Bitcoin';
case 'bch':
return 'Bitcoin Cash';
case 'bcha':
return 'Bitcoin ABC';
case 'btg':
return 'Bitcoin Gold';
case 'bsv':
return 'Bitcoin SV';
case 'dash':
return 'Dash';
case 'doge':
return 'Dogecoin';
case 'ltc':
return 'Litecoin';
case 'zec':
return 'ZCash';
}
}
export function getFullNameFromCoinName(coinName: UtxoCoinName): string {
let prefix: string;
switch (coinName) {
case 'tbtc4':
prefix = 'Testnet4 ';
break;
case 'tbtcsig':
prefix = 'Public Signet ';
break;
case 'tbtcbgsig':
prefix = 'BitGo Signet ';
break;
default:
prefix = isUtxoCoinNameTestnet(coinName) ? 'Testnet ' : '';
}
return prefix + getBaseNameFromMainnet(getMainnetCoinName(coinName));
}
/** @deprecated - use getFullNameFromCoinName instead */
export function getFullNameFromNetwork(n: utxolib.Network): string {
return getFullNameFromCoinName(getCoinName(n));
}
export function isTestnetCoin(coinName: UtxoCoinName): boolean {
return isUtxoCoinNameTestnet(coinName);
}
export function isMainnetCoin(coinName: UtxoCoinName): boolean {
return isUtxoCoinNameMainnet(coinName);
}