Skip to content

Commit fb605fb

Browse files
refactor(networks): simplify network symbol typing
1 parent 0935304 commit fb605fb

203 files changed

Lines changed: 1311 additions & 814 deletions

File tree

Some content is hidden

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

networks/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Every package name must start with `network-`, followed by the network name (for
1313
| Suite Common | `@trezor/network-<network>-suite-common` |
1414
| Suite Native | `@trezor/network-<network>-suite-native` |
1515

16+
`@trezor/network-module` contains layer-independent primitives shared by all technical layers.
17+
Its `NetworkSymbol` is intentionally open-ended; exhaustive symbol unions and mappings belong to
18+
individual network families.
19+
1620
Suite and Suite Native packages may depend on Suite Common. Suite Common must remain platform-independent and must not depend on Suite or Suite Native.
1721

1822
All the 3rd party dependencies related to a network should be defined inside that network's directory. Moreover, currently they're defined only inside general, no-suffix packages, e.g. `network-cardano` (previously coins packages) and dynamically exported.

networks/bitcoin/network-bitcoin-suite-common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"@noble/hashes": "^2.0.1",
1616
"@scure/base": "^2.0.0",
17+
"@trezor/network-module": "workspace:*",
1718
"@trezor/network-module-suite-common-types": "workspace:*",
1819
"@trezor/utils": "workspace:*"
1920
}
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
import type { SuiteCommonNetworkModule } from '@trezor/network-module-suite-common-types';
1+
import { type NetworkSymbol, asNetworkSymbol } from '@trezor/network-module';
2+
import type {
3+
AddressValidator,
4+
SuiteCommonNetworkModule,
5+
} from '@trezor/network-module-suite-common-types';
6+
import { isArrayMember } from '@trezor/utils';
27

38
import { bitcoinValidator } from './addressValidator/bitcoinAddressValidator';
49
import {
510
type BitcoinNetworkSymbol,
611
getSupportedNetworks,
7-
isSupportedNetwork,
12+
supportedNetworks,
813
} from './supportedNetworks';
914

10-
export type BitcoinNetworkSuiteCommonNetworkModule = SuiteCommonNetworkModule<BitcoinNetworkSymbol>;
15+
const isSupportedNetwork = (
16+
symbol: NetworkSymbol,
17+
): symbol is NetworkSymbol & BitcoinNetworkSymbol =>
18+
isArrayMember(symbol as string, supportedNetworks);
19+
20+
const toBitcoinNetworkSymbol = (symbol: NetworkSymbol): BitcoinNetworkSymbol => {
21+
if (!isSupportedNetwork(symbol)) {
22+
throw new Error(`Unsupported Bitcoin network symbol: ${symbol}`);
23+
}
24+
25+
return symbol;
26+
};
1127

12-
export const createBitcoinSuiteCommonNetworkModule =
13-
(): BitcoinNetworkSuiteCommonNetworkModule => ({
14-
addressValidator: bitcoinValidator,
15-
getSupportedNetworks,
16-
isSupportedNetwork,
17-
});
28+
const addressValidator: AddressValidator<NetworkSymbol> = {
29+
isAddressValid: (address, symbol) =>
30+
bitcoinValidator.isAddressValid(address, toBitcoinNetworkSymbol(symbol)),
31+
getAddressType: (address, symbol) =>
32+
bitcoinValidator.getAddressType(address, toBitcoinNetworkSymbol(symbol)),
33+
};
34+
35+
export const createBitcoinSuiteCommonNetworkModule = (): SuiteCommonNetworkModule => ({
36+
addressValidator,
37+
getSupportedNetworks: () => getSupportedNetworks().map(asNetworkSymbol),
38+
isSupportedNetwork,
39+
});

networks/bitcoin/network-bitcoin-suite-common/src/addressValidator/bitcoinAddressValidator.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ const BITCOIN_CURRENCIES: Record<BitcoinCurrencySymbol, BitcoinCurrency> = {
6363
},
6464
};
6565

66-
const getCurrency = (symbol: BitcoinCurrencySymbol): BitcoinCurrency => {
67-
const currency = BITCOIN_CURRENCIES[symbol];
68-
69-
if (!currency) {
70-
throw new Error(`Unsupported bitcoin network symbol: ${symbol}`);
71-
}
72-
73-
return currency;
74-
};
75-
7666
const getNetworkEnvironment = (symbol: BitcoinNetworkSymbol): NetworkEnvironment => {
7767
switch (symbol) {
7868
case 'test':
@@ -328,7 +318,7 @@ export const getAddressType = (address: string, symbol: BitcoinNetworkSymbol) =>
328318
return bchValidator.getAddressType(address, symbol);
329319
}
330320

331-
const currency = getCurrency(symbol);
321+
const currency = BITCOIN_CURRENCIES[symbol];
332322
const networkEnvironments = getNetworkEnvironments(symbol, currency);
333323

334324
for (const networkEnvironment of networkEnvironments) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { createBitcoinSuiteCommonNetworkModule } from './BitcoinNetworkSuiteCommonNetworkModule';
2-
export type { BitcoinNetworkSuiteCommonNetworkModule } from './BitcoinNetworkSuiteCommonNetworkModule';
2+
export type { BitcoinNetworkSymbol } from './supportedNetworks';
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { isArrayMember } from '@trezor/utils';
2-
31
export const supportedNetworks = ['btc', 'test', 'regtest', 'ltc', 'doge', 'zec', 'bch'] as const;
42

53
export type BitcoinNetworkSymbol = (typeof supportedNetworks)[number];
64

75
export const getSupportedNetworks = (): readonly BitcoinNetworkSymbol[] => supportedNetworks;
8-
9-
export const isSupportedNetwork = (symbol: string): symbol is BitcoinNetworkSymbol =>
10-
isArrayMember(symbol, supportedNetworks);

networks/bitcoin/network-bitcoin-suite-common/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"extends": "../../../tsconfig.base.json",
33
"compilerOptions": { "outDir": "libDev" },
44
"references": [
5+
{
6+
"path": "../../network-module/network-module"
7+
},
58
{
69
"path": "../../network-module/network-module-suite-common-types"
710
},

networks/bitcoin/network-bitcoin-suite-common/tsconfig.lib.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"compilerOptions": { "outDir": "./lib" },
44
"include": ["./src"],
55
"references": [
6+
{
7+
"path": "../../network-module/network-module/tsconfig.lib.json"
8+
},
69
{
710
"path": "../../network-module/network-module-suite-common-types"
811
},

networks/cardano/network-cardano-suite-common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@scure/base": "^2.0.0",
16+
"@trezor/network-module": "workspace:*",
1617
"@trezor/network-module-suite-common-types": "workspace:*",
1718
"@trezor/utils": "workspace:*",
1819
"cbor": "^10.0.12",
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
import type { SuiteCommonNetworkModule } from '@trezor/network-module-suite-common-types';
1+
import { type NetworkSymbol, asNetworkSymbol } from '@trezor/network-module';
2+
import type {
3+
AddressValidator,
4+
SuiteCommonNetworkModule,
5+
} from '@trezor/network-module-suite-common-types';
6+
import { isArrayMember } from '@trezor/utils';
27

38
import { adaValidator } from './addressValidator/cardanoAddressValidator';
49
import {
510
type CardanoNetworkSymbol,
611
getSupportedNetworks,
7-
isSupportedNetwork,
12+
supportedNetworks,
813
} from './supportedNetworks';
914

10-
export type CardanoNetworkSuiteCommonNetworkModule = SuiteCommonNetworkModule<CardanoNetworkSymbol>;
15+
const isSupportedNetwork = (
16+
symbol: NetworkSymbol,
17+
): symbol is NetworkSymbol & CardanoNetworkSymbol =>
18+
isArrayMember(symbol as string, supportedNetworks);
19+
20+
const toCardanoNetworkSymbol = (symbol: NetworkSymbol): CardanoNetworkSymbol => {
21+
if (!isSupportedNetwork(symbol)) {
22+
throw new Error(`Unsupported Cardano network symbol: ${symbol}`);
23+
}
24+
25+
return symbol;
26+
};
1127

12-
export const createCardanoSuiteCommonNetworkModule =
13-
(): CardanoNetworkSuiteCommonNetworkModule => ({
14-
addressValidator: adaValidator,
15-
getSupportedNetworks,
16-
isSupportedNetwork,
17-
});
28+
const addressValidator: AddressValidator<NetworkSymbol> = {
29+
isAddressValid: (address, symbol) =>
30+
adaValidator.isAddressValid(address, toCardanoNetworkSymbol(symbol)),
31+
getAddressType: (address, symbol) =>
32+
adaValidator.getAddressType(address, toCardanoNetworkSymbol(symbol)),
33+
};
34+
35+
export const createCardanoSuiteCommonNetworkModule = (): SuiteCommonNetworkModule => ({
36+
addressValidator,
37+
getSupportedNetworks: () => getSupportedNetworks().map(asNetworkSymbol),
38+
isSupportedNetwork,
39+
});

0 commit comments

Comments
 (0)