Skip to content

Commit f089222

Browse files
refactor(networks): simplify network symbol typing
1 parent d0b14d1 commit f089222

259 files changed

Lines changed: 1781 additions & 1002 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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ 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+
20+
`@trezor/network-<network>` is the layer-independent base package for one network family. It owns
21+
the supported-network tuple, the exhaustive symbol union inferred from that tuple, and conversions
22+
between the family symbol and `NetworkSymbol`. Every technical layer may depend on this base package.
23+
1624
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.
1725

1826
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.
@@ -23,13 +31,15 @@ The complete structure for Bitcoin illustrates all four layers alongside optiona
2331
networks/
2432
├── README.md
2533
├── bitcoin/
34+
│ ├── network-bitcoin/ → @trezor/network-bitcoin (layer-independent base)
2635
│ ├── network-bitcoin-connect/ → @trezor/network-bitcoin-connect
2736
│ ├── network-bitcoin-suite/ → @trezor/network-bitcoin-suite
2837
│ ├── network-bitcoin-suite-common/ → @trezor/network-bitcoin-suite-common
2938
│ ├── network-bitcoin-suite-native/ → @trezor/network-bitcoin-suite-native
3039
│ ├── network-bitcoin-bip32/ → @trezor/network-bitcoin-bip32 (custom/internal)
3140
│ └── network-bitcoin-coinjoin/ → @trezor/network-bitcoin-coinjoin (custom/internal)
3241
└── <network>/
42+
├── network-<network>/
3343
├── network-<network>-connect/
3444
├── network-<network>-suite/
3545
├── network-<network>-suite-common/
@@ -43,8 +53,10 @@ Types, runtime constants (independent of dependencies) and runtime functions sho
4353
what overhead is expected. Utilities using 3rd party code in runtime should always be exported in `runtime/exports.ts` and dynamically
4454
reexported in `runtime/index.ts` so this code is loaded on-demand, for security and performance reasons. From the top-level index file,
4555
`runtime/exports.ts` is directly exported instead, but importing from the package root is restricted by eslint rule in this monorepo.
56+
The lightweight `supported<Network>Networks`, `isSupported<Network>Network`, and
57+
`to<Network>NetworkSymbol` APIs belong to the `constants` entrypoint.
4658

47-
Each general network package follows a consistent three-entrypoint layout:
59+
Each general network package uses the relevant entrypoints from this layout:
4860

4961
```
5062
networks/<network>/network-<network>/src/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"dependencies": {
1515
"@noble/hashes": "^2.0.1",
1616
"@scure/base": "^2.0.0",
17+
"@trezor/network-bitcoin": "workspace:*",
18+
"@trezor/network-module": "workspace:*",
1719
"@trezor/network-module-suite-common-types": "workspace:*",
1820
"@trezor/utils": "workspace:*"
1921
}
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
import type { SuiteCommonNetworkModule } from '@trezor/network-module-suite-common-types';
1+
import {
2+
isSupportedBitcoinNetwork,
3+
supportedBitcoinNetworks,
4+
toBitcoinNetworkSymbol,
5+
} from '@trezor/network-bitcoin/constants';
6+
import { type NetworkSymbol, asNetworkSymbols } from '@trezor/network-module';
7+
import type {
8+
AddressValidator,
9+
SuiteCommonNetworkModule,
10+
} from '@trezor/network-module-suite-common-types';
211

312
import { bitcoinValidator } from './addressValidator/bitcoinAddressValidator';
4-
import { getNetworkConfig } from './networkConfig';
5-
import {
6-
type BitcoinNetworkSymbol,
7-
getSupportedNetworks,
8-
isSupportedNetwork,
9-
} from './supportedNetworks';
13+
import { getNetworkConfig as getBitcoinNetworkConfig } from './networkConfig';
1014

11-
export type BitcoinNetworkSuiteCommonNetworkModule = SuiteCommonNetworkModule<BitcoinNetworkSymbol>;
15+
const addressValidator: AddressValidator<NetworkSymbol> = {
16+
isAddressValid: (address, symbol) =>
17+
bitcoinValidator.isAddressValid(address, toBitcoinNetworkSymbol(symbol)),
18+
getAddressType: (address, symbol) =>
19+
bitcoinValidator.getAddressType(address, toBitcoinNetworkSymbol(symbol)),
20+
};
1221

13-
export const createBitcoinSuiteCommonNetworkModule =
14-
(): BitcoinNetworkSuiteCommonNetworkModule => ({
15-
addressValidator: bitcoinValidator,
16-
getSupportedNetworks,
17-
isSupportedNetwork,
18-
getNetworkConfig,
19-
});
22+
export const createBitcoinSuiteCommonNetworkModule = (): SuiteCommonNetworkModule => ({
23+
addressValidator,
24+
getSupportedNetworks: () => asNetworkSymbols(supportedBitcoinNetworks),
25+
isSupportedNetwork: isSupportedBitcoinNetwork,
26+
getNetworkConfig: symbol => getBitcoinNetworkConfig(toBitcoinNetworkSymbol(symbol)),
27+
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// CashAddr address format spec:
22
// https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md
33

4+
import type { BitcoinNetworkSymbol } from '@trezor/network-bitcoin/constants';
45
import { type AddressValidator, addressType } from '@trezor/network-module-suite-common-types';
56

6-
import type { BitcoinNetworkSymbol } from '../supportedNetworks';
7-
87
type BitcoinCashNetworkSymbol = Extract<BitcoinNetworkSymbol, 'bch'>;
98

109
const CASHADDR_REGEXP = /^[qQpP]{1}[0-9a-zA-Z]{41}$/;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import type { BitcoinNetworkSymbol } from '@trezor/network-bitcoin/constants';
12
import { type AddressType, addressType } from '@trezor/network-module-suite-common-types';
23

3-
import type { BitcoinNetworkSymbol } from '../supportedNetworks';
44
import { bitcoinValidator } from './bitcoinAddressValidator';
55

66
type BitcoinIsAddressValidCase = {

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { keccak_256 } from '@noble/hashes/sha3.js';
55
import { bytesToHex, hexToBytes } from '@noble/hashes/utils.js';
66
import { base58 } from '@scure/base';
77

8+
import type { BitcoinNetworkSymbol } from '@trezor/network-bitcoin/constants';
89
import { type AddressValidator, addressType } from '@trezor/network-module-suite-common-types';
910
import { typedObjectKeys } from '@trezor/utils';
1011

1112
import { bchValidator } from './bchAddressValidator';
1213
import * as bech32 from './bech32';
13-
import type { BitcoinNetworkSymbol } from '../supportedNetworks';
1414

1515
type NetworkEnvironment = 'prod' | 'testnet' | 'regtest' | 'stake';
1616
type HashFunction = 'sha256' | 'blake256' | 'blake256keccak256' | 'keccak256';
@@ -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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { createBitcoinSuiteCommonNetworkModule } from './BitcoinNetworkSuiteCommonNetworkModule';
2-
export type { BitcoinNetworkSuiteCommonNetworkModule } from './BitcoinNetworkSuiteCommonNetworkModule';

networks/bitcoin/network-bitcoin-suite-common/src/networkConfig.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import type { BitcoinNetworkSymbol } from '@trezor/network-bitcoin/constants';
12
import {
23
type SuiteCommonNetworkConfig,
34
asProtocol,
45
} from '@trezor/network-module-suite-common-types';
56

6-
import type { BitcoinNetworkSymbol } from './supportedNetworks';
7-
87
const networkConfigBySymbol: Readonly<Record<BitcoinNetworkSymbol, SuiteCommonNetworkConfig>> = {
98
btc: { color: '#f29937', protocols: [asProtocol('bitcoin'), asProtocol('btc')] },
109
test: { color: '#e75f5f', protocols: [asProtocol('test')] },

networks/bitcoin/network-bitcoin-suite-common/src/supportedNetworks.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"extends": "../../../tsconfig.base.json",
33
"compilerOptions": { "outDir": "libDev" },
44
"references": [
5+
{ "path": "../network-bitcoin" },
6+
{
7+
"path": "../../network-module/network-module"
8+
},
59
{
610
"path": "../../network-module/network-module-suite-common-types"
711
},

0 commit comments

Comments
 (0)