Skip to content

Commit 365f620

Browse files
Merge pull request #103 from BitGo/BTC-2936.export-names
feat(wasm-utxo): enhance CoinName type with utility functions
2 parents a39a187 + 15c287b commit 365f620

1 file changed

Lines changed: 65 additions & 22 deletions

File tree

packages/wasm-utxo/js/coinName.ts

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
11
// BitGo coin names (from Network::from_coin_name in src/networks.rs)
2-
export type CoinName =
3-
| "btc"
4-
| "tbtc"
5-
| "tbtc4"
6-
| "tbtcsig"
7-
| "tbtcbgsig"
8-
| "bch"
9-
| "tbch"
10-
| "bcha"
11-
| "tbcha"
12-
| "btg"
13-
| "tbtg"
14-
| "bsv"
15-
| "tbsv"
16-
| "dash"
17-
| "tdash"
18-
| "doge"
19-
| "tdoge"
20-
| "ltc"
21-
| "tltc"
22-
| "zec"
23-
| "tzec";
2+
export const coinNames = [
3+
"btc",
4+
"tbtc",
5+
"tbtc4",
6+
"tbtcsig",
7+
"tbtcbgsig",
8+
"bch",
9+
"tbch",
10+
"bcha",
11+
"tbcha",
12+
"btg",
13+
"tbtg",
14+
"bsv",
15+
"tbsv",
16+
"dash",
17+
"tdash",
18+
"doge",
19+
"tdoge",
20+
"ltc",
21+
"tltc",
22+
"zec",
23+
"tzec",
24+
] as const;
25+
26+
export type CoinName = (typeof coinNames)[number];
27+
28+
export function getMainnet(name: CoinName): CoinName {
29+
switch (name) {
30+
case "tbtc":
31+
case "tbtc4":
32+
case "tbtcsig":
33+
case "tbtcbgsig":
34+
return "btc";
35+
case "tbch":
36+
return "bch";
37+
case "tbcha":
38+
return "bcha";
39+
case "tbtg":
40+
return "btg";
41+
case "tbsv":
42+
return "bsv";
43+
case "tdash":
44+
return "dash";
45+
case "tdoge":
46+
return "doge";
47+
case "tltc":
48+
return "ltc";
49+
case "tzec":
50+
return "zec";
51+
default:
52+
return name;
53+
}
54+
}
55+
56+
export function isMainnet(name: CoinName): boolean {
57+
return getMainnet(name) === name;
58+
}
59+
60+
export function isTestnet(name: CoinName): boolean {
61+
return getMainnet(name) !== name;
62+
}
63+
64+
export function isCoinName(v: string): v is CoinName {
65+
return (coinNames as readonly string[]).includes(v);
66+
}

0 commit comments

Comments
 (0)