@spacesops/wdk-react-native-core 1.0.0-beta.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +551 -0
- package/dist/__mocks__/react.d.ts +40 -0
- package/dist/__mocks__/react.js +33 -0
- package/dist/__mocks__/react.js.map +1 -0
- package/dist/__mocks__/secureStorage.d.ts +26 -0
- package/dist/__mocks__/secureStorage.js +80 -0
- package/dist/__mocks__/secureStorage.js.map +1 -0
- package/dist/__tests__/hooks/useBalance.test.d.ts +6 -0
- package/dist/__tests__/hooks/useBalance.test.js +217 -0
- package/dist/__tests__/hooks/useBalance.test.js.map +1 -0
- package/dist/__tests__/hooks/useWallet.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWallet.test.js +105 -0
- package/dist/__tests__/hooks/useWallet.test.js.map +1 -0
- package/dist/__tests__/hooks/useWalletManager.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWalletManager.test.js +193 -0
- package/dist/__tests__/hooks/useWalletManager.test.js.map +1 -0
- package/dist/__tests__/hooks/useWdkApp.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWdkApp.test.js +55 -0
- package/dist/__tests__/hooks/useWdkApp.test.js.map +1 -0
- package/dist/__tests__/hooks/useWorklet.test.d.ts +6 -0
- package/dist/__tests__/hooks/useWorklet.test.js +168 -0
- package/dist/__tests__/hooks/useWorklet.test.js.map +1 -0
- package/dist/__tests__/provider/WdkAppProvider.test.d.ts +6 -0
- package/dist/__tests__/provider/WdkAppProvider.test.js +57 -0
- package/dist/__tests__/provider/WdkAppProvider.test.js.map +1 -0
- package/dist/__tests__/services/accountService.test.d.ts +6 -0
- package/dist/__tests__/services/accountService.test.js +216 -0
- package/dist/__tests__/services/accountService.test.js.map +1 -0
- package/dist/__tests__/services/addressService.test.d.ts +6 -0
- package/dist/__tests__/services/addressService.test.js +246 -0
- package/dist/__tests__/services/addressService.test.js.map +1 -0
- package/dist/__tests__/services/balanceService.test.d.ts +6 -0
- package/dist/__tests__/services/balanceService.test.js +333 -0
- package/dist/__tests__/services/balanceService.test.js.map +1 -0
- package/dist/__tests__/services/walletSetupService.test.d.ts +6 -0
- package/dist/__tests__/services/walletSetupService.test.js +304 -0
- package/dist/__tests__/services/walletSetupService.test.js.map +1 -0
- package/dist/__tests__/services/walletSwitchingService.test.d.ts +6 -0
- package/dist/__tests__/services/walletSwitchingService.test.js +209 -0
- package/dist/__tests__/services/walletSwitchingService.test.js.map +1 -0
- package/dist/__tests__/services/workletLifecycleService.test.d.ts +6 -0
- package/dist/__tests__/services/workletLifecycleService.test.js +451 -0
- package/dist/__tests__/services/workletLifecycleService.test.js.map +1 -0
- package/dist/__tests__/setup.d.ts +7 -0
- package/dist/__tests__/setup.js +49 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/__tests__/storage/mmkvStorage.test.d.ts +6 -0
- package/dist/__tests__/storage/mmkvStorage.test.js +213 -0
- package/dist/__tests__/storage/mmkvStorage.test.js.map +1 -0
- package/dist/__tests__/store/walletStore.test.d.ts +4 -0
- package/dist/__tests__/store/walletStore.test.js +86 -0
- package/dist/__tests__/store/walletStore.test.js.map +1 -0
- package/dist/__tests__/store/workletStore.test.d.ts +4 -0
- package/dist/__tests__/store/workletStore.test.js +255 -0
- package/dist/__tests__/store/workletStore.test.js.map +1 -0
- package/dist/__tests__/types/hrpc.test.d.ts +4 -0
- package/dist/__tests__/types/hrpc.test.js +99 -0
- package/dist/__tests__/types/hrpc.test.js.map +1 -0
- package/dist/__tests__/utils/balanceUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/balanceUtils.test.js +133 -0
- package/dist/__tests__/utils/balanceUtils.test.js.map +1 -0
- package/dist/__tests__/utils/errorHandling.test.d.ts +6 -0
- package/dist/__tests__/utils/errorHandling.test.js +119 -0
- package/dist/__tests__/utils/errorHandling.test.js.map +1 -0
- package/dist/__tests__/utils/errorUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/errorUtils.test.js +133 -0
- package/dist/__tests__/utils/errorUtils.test.js.map +1 -0
- package/dist/__tests__/utils/initializationState.test.d.ts +6 -0
- package/dist/__tests__/utils/initializationState.test.js +243 -0
- package/dist/__tests__/utils/initializationState.test.js.map +1 -0
- package/dist/__tests__/utils/jsonUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/jsonUtils.test.js +118 -0
- package/dist/__tests__/utils/jsonUtils.test.js.map +1 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.d.ts +6 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.js +158 -0
- package/dist/__tests__/utils/mmkvKeyManager.test.js.map +1 -0
- package/dist/__tests__/utils/mnemonicUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/mnemonicUtils.test.js +40 -0
- package/dist/__tests__/utils/mnemonicUtils.test.js.map +1 -0
- package/dist/__tests__/utils/raceConditions.test.d.ts +6 -0
- package/dist/__tests__/utils/raceConditions.test.js +239 -0
- package/dist/__tests__/utils/raceConditions.test.js.map +1 -0
- package/dist/__tests__/utils/result.test.d.ts +4 -0
- package/dist/__tests__/utils/result.test.js +66 -0
- package/dist/__tests__/utils/result.test.js.map +1 -0
- package/dist/__tests__/utils/schemas.test.d.ts +4 -0
- package/dist/__tests__/utils/schemas.test.js +66 -0
- package/dist/__tests__/utils/schemas.test.js.map +1 -0
- package/dist/__tests__/utils/storeHelpers.test.d.ts +4 -0
- package/dist/__tests__/utils/storeHelpers.test.js +227 -0
- package/dist/__tests__/utils/storeHelpers.test.js.map +1 -0
- package/dist/__tests__/utils/typeGuards.test.d.ts +4 -0
- package/dist/__tests__/utils/typeGuards.test.js +175 -0
- package/dist/__tests__/utils/typeGuards.test.js.map +1 -0
- package/dist/__tests__/utils/validation.test.d.ts +4 -0
- package/dist/__tests__/utils/validation.test.js +126 -0
- package/dist/__tests__/utils/validation.test.js.map +1 -0
- package/dist/__tests__/utils/walletUtils.test.d.ts +4 -0
- package/dist/__tests__/utils/walletUtils.test.js +156 -0
- package/dist/__tests__/utils/walletUtils.test.js.map +1 -0
- package/dist/hooks/useBalance.d.ts +170 -0
- package/dist/hooks/useBalance.js +442 -0
- package/dist/hooks/useBalance.js.map +1 -0
- package/dist/hooks/useWallet.d.ts +43 -0
- package/dist/hooks/useWallet.js +193 -0
- package/dist/hooks/useWallet.js.map +1 -0
- package/dist/hooks/useWalletManager.d.ts +100 -0
- package/dist/hooks/useWalletManager.js +526 -0
- package/dist/hooks/useWalletManager.js.map +1 -0
- package/dist/hooks/useWdkApp.d.ts +66 -0
- package/dist/hooks/useWdkApp.js +74 -0
- package/dist/hooks/useWdkApp.js.map +1 -0
- package/dist/hooks/useWorklet.d.ts +66 -0
- package/dist/hooks/useWorklet.js +42 -0
- package/dist/hooks/useWorklet.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/provider/WdkAppProvider.d.ts +112 -0
- package/dist/provider/WdkAppProvider.js +558 -0
- package/dist/provider/WdkAppProvider.js.map +1 -0
- package/dist/services/accountService.d.ts +52 -0
- package/dist/services/accountService.js +119 -0
- package/dist/services/accountService.js.map +1 -0
- package/dist/services/addressService.d.ts +22 -0
- package/dist/services/addressService.js +110 -0
- package/dist/services/addressService.js.map +1 -0
- package/dist/services/balanceService.d.ts +114 -0
- package/dist/services/balanceService.js +208 -0
- package/dist/services/balanceService.js.map +1 -0
- package/dist/services/walletSetupService.d.ts +217 -0
- package/dist/services/walletSetupService.js +535 -0
- package/dist/services/walletSetupService.js.map +1 -0
- package/dist/services/walletSwitchingService.d.ts +48 -0
- package/dist/services/walletSwitchingService.js +143 -0
- package/dist/services/walletSwitchingService.js.map +1 -0
- package/dist/services/workletLifecycleService.d.ts +117 -0
- package/dist/services/workletLifecycleService.js +433 -0
- package/dist/services/workletLifecycleService.js.map +1 -0
- package/dist/storage/mmkvStorage.d.ts +44 -0
- package/dist/storage/mmkvStorage.js +348 -0
- package/dist/storage/mmkvStorage.js.map +1 -0
- package/dist/store/walletStore.d.ts +130 -0
- package/dist/store/walletStore.js +204 -0
- package/dist/store/walletStore.js.map +1 -0
- package/dist/store/workletStore.d.ts +121 -0
- package/dist/store/workletStore.js +256 -0
- package/dist/store/workletStore.js.map +1 -0
- package/dist/types/hrpc.d.ts +60 -0
- package/dist/types/hrpc.js +26 -0
- package/dist/types/hrpc.js.map +1 -0
- package/dist/types.d.ts +168 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/balanceUtils.d.ts +30 -0
- package/dist/utils/balanceUtils.js +86 -0
- package/dist/utils/balanceUtils.js.map +1 -0
- package/dist/utils/constants.d.ts +66 -0
- package/dist/utils/constants.js +73 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/errorHandling.d.ts +47 -0
- package/dist/utils/errorHandling.js +54 -0
- package/dist/utils/errorHandling.js.map +1 -0
- package/dist/utils/errorUtils.d.ts +89 -0
- package/dist/utils/errorUtils.js +264 -0
- package/dist/utils/errorUtils.js.map +1 -0
- package/dist/utils/initializationState.d.ts +129 -0
- package/dist/utils/initializationState.js +220 -0
- package/dist/utils/initializationState.js.map +1 -0
- package/dist/utils/jsonUtils.d.ts +21 -0
- package/dist/utils/jsonUtils.js +67 -0
- package/dist/utils/jsonUtils.js.map +1 -0
- package/dist/utils/logger.d.ts +27 -0
- package/dist/utils/logger.js +50 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/mmkvKeyManager.d.ts +57 -0
- package/dist/utils/mmkvKeyManager.js +214 -0
- package/dist/utils/mmkvKeyManager.js.map +1 -0
- package/dist/utils/mnemonicUtils.d.ts +17 -0
- package/dist/utils/mnemonicUtils.js +28 -0
- package/dist/utils/mnemonicUtils.js.map +1 -0
- package/dist/utils/operationMutex.d.ts +69 -0
- package/dist/utils/operationMutex.js +150 -0
- package/dist/utils/operationMutex.js.map +1 -0
- package/dist/utils/result.d.ts +34 -0
- package/dist/utils/result.js +43 -0
- package/dist/utils/result.js.map +1 -0
- package/dist/utils/schemas.d.ts +365 -0
- package/dist/utils/schemas.js +152 -0
- package/dist/utils/schemas.js.map +1 -0
- package/dist/utils/storeHelpers.d.ts +112 -0
- package/dist/utils/storeHelpers.js +198 -0
- package/dist/utils/storeHelpers.js.map +1 -0
- package/dist/utils/typeGuards.d.ts +70 -0
- package/dist/utils/typeGuards.js +95 -0
- package/dist/utils/typeGuards.js.map +1 -0
- package/dist/utils/validation.d.ts +53 -0
- package/dist/utils/validation.js +152 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/walletStateHelpers.d.ts +62 -0
- package/dist/utils/walletStateHelpers.js +81 -0
- package/dist/utils/walletStateHelpers.js.map +1 -0
- package/dist/utils/walletUtils.d.ts +38 -0
- package/dist/utils/walletUtils.js +87 -0
- package/dist/utils/walletUtils.js.map +1 -0
- package/package.json +79 -0
- package/src/__mocks__/react.ts +34 -0
- package/src/__mocks__/secureStorage.ts +87 -0
- package/src/__tests__/hooks/useBalance.test.ts +286 -0
- package/src/__tests__/hooks/useWallet.test.ts +122 -0
- package/src/__tests__/hooks/useWalletManager.test.ts +271 -0
- package/src/__tests__/hooks/useWdkApp.test.tsx +59 -0
- package/src/__tests__/hooks/useWorklet.test.ts +195 -0
- package/src/__tests__/provider/WdkAppProvider.test.tsx +67 -0
- package/src/__tests__/services/accountService.test.ts +337 -0
- package/src/__tests__/services/addressService.test.ts +312 -0
- package/src/__tests__/services/balanceService.test.ts +411 -0
- package/src/__tests__/services/walletSetupService.test.ts +463 -0
- package/src/__tests__/services/walletSwitchingService.test.ts +258 -0
- package/src/__tests__/services/workletLifecycleService.test.ts +514 -0
- package/src/__tests__/setup.ts +54 -0
- package/src/__tests__/storage/mmkvStorage.test.ts +325 -0
- package/src/__tests__/store/walletStore.test.ts +101 -0
- package/src/__tests__/store/workletStore.test.ts +329 -0
- package/src/__tests__/types/hrpc.test.ts +125 -0
- package/src/__tests__/utils/balanceUtils.test.ts +151 -0
- package/src/__tests__/utils/errorHandling.test.ts +147 -0
- package/src/__tests__/utils/errorUtils.test.ts +170 -0
- package/src/__tests__/utils/initializationState.test.ts +337 -0
- package/src/__tests__/utils/jsonUtils.test.ts +131 -0
- package/src/__tests__/utils/mmkvKeyManager.test.ts +205 -0
- package/src/__tests__/utils/mnemonicUtils.test.ts +48 -0
- package/src/__tests__/utils/raceConditions.test.ts +292 -0
- package/src/__tests__/utils/result.test.ts +73 -0
- package/src/__tests__/utils/schemas.test.ts +82 -0
- package/src/__tests__/utils/storeHelpers.test.ts +323 -0
- package/src/__tests__/utils/typeGuards.test.ts +209 -0
- package/src/__tests__/utils/validation.test.ts +152 -0
- package/src/__tests__/utils/walletUtils.test.ts +195 -0
- package/src/hooks/useBalance.ts +597 -0
- package/src/hooks/useWallet.ts +288 -0
- package/src/hooks/useWalletManager.ts +654 -0
- package/src/hooks/useWdkApp.ts +78 -0
- package/src/hooks/useWorklet.ts +109 -0
- package/src/index.ts +112 -0
- package/src/provider/WdkAppProvider.tsx +764 -0
- package/src/services/accountService.ts +134 -0
- package/src/services/addressService.ts +125 -0
- package/src/services/balanceService.ts +291 -0
- package/src/services/walletSetupService.ts +695 -0
- package/src/services/walletSwitchingService.ts +157 -0
- package/src/services/workletLifecycleService.ts +561 -0
- package/src/storage/mmkvStorage.ts +417 -0
- package/src/store/walletStore.ts +301 -0
- package/src/store/workletStore.ts +340 -0
- package/src/types/hrpc.ts +78 -0
- package/src/types.ts +188 -0
- package/src/utils/balanceUtils.ts +96 -0
- package/src/utils/constants.ts +84 -0
- package/src/utils/errorHandling.md +163 -0
- package/src/utils/errorHandling.ts +61 -0
- package/src/utils/errorUtils.ts +300 -0
- package/src/utils/initializationState.ts +242 -0
- package/src/utils/jsonUtils.ts +75 -0
- package/src/utils/logger.ts +54 -0
- package/src/utils/mmkvKeyManager.ts +256 -0
- package/src/utils/mnemonicUtils.ts +35 -0
- package/src/utils/operationMutex.ts +181 -0
- package/src/utils/result.ts +57 -0
- package/src/utils/schemas.ts +190 -0
- package/src/utils/storeHelpers.ts +236 -0
- package/src/utils/typeGuards.ts +129 -0
- package/src/utils/validation.ts +174 -0
- package/src/utils/walletStateHelpers.ts +104 -0
- package/src/utils/walletUtils.ts +107 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hrpc.js","sourceRoot":"","sources":["../../src/types/hrpc.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkDH;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAAU;IACvC,OAAO,CACL,OAAQ,IAA2C,CAAC,aAAa,KAAK,UAAU;QAChF,OAAQ,IAA2C,CAAC,yBAAyB,KAAK,UAAU;QAC5F,OAAQ,IAA2C,CAAC,sBAAsB,KAAK,UAAU;QACzF,OAAQ,IAA2C,CAAC,6BAA6B,KAAK,UAAU,CACjG,CAAA;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAU;IACvC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAA;IAC1E,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* All network, token, and wallet type definitions for the WDK React Native Core library.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Network Configuration
|
|
8
|
+
*
|
|
9
|
+
* Defines the configuration for a blockchain network.
|
|
10
|
+
*/
|
|
11
|
+
export interface NetworkConfig {
|
|
12
|
+
/** Chain ID for the network */
|
|
13
|
+
chainId: number;
|
|
14
|
+
/** Blockchain name (e.g., "ethereum", "polygon") */
|
|
15
|
+
blockchain: string;
|
|
16
|
+
/** Optional RPC provider URL */
|
|
17
|
+
provider?: string;
|
|
18
|
+
/** Optional bundler URL for account abstraction */
|
|
19
|
+
bundlerUrl?: string;
|
|
20
|
+
/** Optional paymaster URL for account abstraction */
|
|
21
|
+
paymasterUrl?: string;
|
|
22
|
+
/** Optional paymaster contract address */
|
|
23
|
+
paymasterAddress?: string;
|
|
24
|
+
/** Optional entry point contract address */
|
|
25
|
+
entryPointAddress?: string;
|
|
26
|
+
/** Optional maximum fee for transfers */
|
|
27
|
+
transferMaxFee?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Network Configurations
|
|
31
|
+
*
|
|
32
|
+
* Maps network names to their configurations.
|
|
33
|
+
*/
|
|
34
|
+
export type NetworkConfigs = Record<string, NetworkConfig>;
|
|
35
|
+
/**
|
|
36
|
+
* Token Configuration
|
|
37
|
+
*
|
|
38
|
+
* Defines the configuration for a token (native or ERC20).
|
|
39
|
+
*/
|
|
40
|
+
export interface TokenConfig {
|
|
41
|
+
/** Token symbol (e.g., "ETH", "USDT") */
|
|
42
|
+
symbol: string;
|
|
43
|
+
/** Token name (e.g., "Ethereum", "Tether") */
|
|
44
|
+
name: string;
|
|
45
|
+
/** Number of decimals (0-18) */
|
|
46
|
+
decimals: number;
|
|
47
|
+
/** Token contract address (null for native tokens) */
|
|
48
|
+
address: string | null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Network Tokens
|
|
52
|
+
*
|
|
53
|
+
* Defines the tokens available for a network (native + ERC20 tokens).
|
|
54
|
+
*/
|
|
55
|
+
export interface NetworkTokens {
|
|
56
|
+
/** Native token configuration */
|
|
57
|
+
native: TokenConfig;
|
|
58
|
+
/** Array of ERC20 token configurations */
|
|
59
|
+
tokens: TokenConfig[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Token Configurations
|
|
63
|
+
*
|
|
64
|
+
* Maps network names to their token configurations.
|
|
65
|
+
*/
|
|
66
|
+
export type TokenConfigs = Record<string, NetworkTokens>;
|
|
67
|
+
/**
|
|
68
|
+
* Wallet
|
|
69
|
+
*
|
|
70
|
+
* Represents a wallet instance with metadata.
|
|
71
|
+
*/
|
|
72
|
+
export interface Wallet {
|
|
73
|
+
/** Account index (0-based) */
|
|
74
|
+
accountIndex: number;
|
|
75
|
+
/** Unique wallet identifier */
|
|
76
|
+
identifier: string;
|
|
77
|
+
/** Wallet display name */
|
|
78
|
+
name: string;
|
|
79
|
+
/** Timestamp when wallet was created */
|
|
80
|
+
createdAt: number;
|
|
81
|
+
/** Timestamp when wallet was last updated */
|
|
82
|
+
updatedAt: number;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Wallet Addresses
|
|
86
|
+
*
|
|
87
|
+
* Maps network -> accountIndex -> address
|
|
88
|
+
* Structure: { [network]: { [accountIndex]: address } }
|
|
89
|
+
*/
|
|
90
|
+
export type WalletAddresses = Record<string, Record<number, string>>;
|
|
91
|
+
/**
|
|
92
|
+
* Wallet Addresses by Wallet Identifier
|
|
93
|
+
*
|
|
94
|
+
* Maps walletId -> network -> accountIndex -> address
|
|
95
|
+
* Structure: { [walletId]: { [network]: { [accountIndex]: address } } }
|
|
96
|
+
*/
|
|
97
|
+
export type WalletAddressesByWallet = Record<string, WalletAddresses>;
|
|
98
|
+
/**
|
|
99
|
+
* Wallet Balances
|
|
100
|
+
*
|
|
101
|
+
* Maps network -> accountIndex -> tokenAddress -> balance
|
|
102
|
+
* Structure: { [network]: { [accountIndex]: { [tokenAddress]: balance } } }
|
|
103
|
+
* Note: balance is stored as a string to handle BigInt values
|
|
104
|
+
*/
|
|
105
|
+
export type WalletBalances = Record<string, Record<number, Record<string, string>>>;
|
|
106
|
+
/**
|
|
107
|
+
* Wallet Balances by Wallet Identifier
|
|
108
|
+
*
|
|
109
|
+
* Maps walletId -> network -> accountIndex -> tokenAddress -> balance
|
|
110
|
+
* Structure: { [walletId]: { [network]: { [accountIndex]: { [tokenAddress]: balance } } } }
|
|
111
|
+
*/
|
|
112
|
+
export type WalletBalancesByWallet = Record<string, WalletBalances>;
|
|
113
|
+
/**
|
|
114
|
+
* Balance Loading States
|
|
115
|
+
*
|
|
116
|
+
* Maps "network-accountIndex-tokenAddress" -> boolean
|
|
117
|
+
* Used to track which balances are currently being fetched.
|
|
118
|
+
*/
|
|
119
|
+
export type BalanceLoadingStates = Record<string, boolean>;
|
|
120
|
+
/**
|
|
121
|
+
* Balance Fetch Result
|
|
122
|
+
*
|
|
123
|
+
* Result of a balance fetch operation.
|
|
124
|
+
*/
|
|
125
|
+
export interface BalanceFetchResult {
|
|
126
|
+
/** Whether the fetch was successful */
|
|
127
|
+
success: boolean;
|
|
128
|
+
/** Network name */
|
|
129
|
+
network: string;
|
|
130
|
+
/** Account index */
|
|
131
|
+
accountIndex: number;
|
|
132
|
+
/** Token address (null for native tokens) */
|
|
133
|
+
tokenAddress: string | null;
|
|
134
|
+
/** Balance as a string (null if fetch failed) */
|
|
135
|
+
balance: string | null;
|
|
136
|
+
/** Error message (only present if success is false) */
|
|
137
|
+
error?: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Token Config Provider
|
|
141
|
+
*
|
|
142
|
+
* Either a TokenConfigs object or a function that returns TokenConfigs.
|
|
143
|
+
* Allows for dynamic token configuration.
|
|
144
|
+
*/
|
|
145
|
+
export type TokenConfigProvider = TokenConfigs | (() => TokenConfigs);
|
|
146
|
+
/**
|
|
147
|
+
* Token Helpers
|
|
148
|
+
*
|
|
149
|
+
* Helper functions for working with token configurations.
|
|
150
|
+
*/
|
|
151
|
+
export interface TokenHelpers {
|
|
152
|
+
/** Get all tokens (native + ERC20) for a network */
|
|
153
|
+
getTokensForNetwork: (network: string) => TokenConfig[];
|
|
154
|
+
/** Get all supported network names */
|
|
155
|
+
getSupportedNetworks: () => string[];
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Wallet Store Interface
|
|
159
|
+
*
|
|
160
|
+
* Interface for wallet store implementations that provide account methods
|
|
161
|
+
* and wallet initialization status.
|
|
162
|
+
*/
|
|
163
|
+
export interface WalletStore {
|
|
164
|
+
/** Call a method on a wallet account */
|
|
165
|
+
callAccountMethod: <T = unknown>(network: string, accountIndex: number, methodName: string, args?: unknown) => Promise<T>;
|
|
166
|
+
/** Check if the wallet is initialized */
|
|
167
|
+
isWalletInitialized: () => boolean;
|
|
168
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Balance utility functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Convert a value to string, handling BigInt values
|
|
6
|
+
*/
|
|
7
|
+
export declare function convertBalanceToString(value: unknown): string;
|
|
8
|
+
/**
|
|
9
|
+
* Recursively convert BigInt values to strings in objects/arrays
|
|
10
|
+
* Prevents serialization errors when BigInt values are present
|
|
11
|
+
*
|
|
12
|
+
* @param value - Value that may contain BigInt values
|
|
13
|
+
* @returns Value with all BigInt values converted to strings
|
|
14
|
+
*/
|
|
15
|
+
export declare function convertBigIntToString(value: unknown): unknown;
|
|
16
|
+
/**
|
|
17
|
+
* Format a balance from wei/smallest unit to readable format
|
|
18
|
+
*
|
|
19
|
+
* @param balance - Balance as string (from wei/smallest unit)
|
|
20
|
+
* @param decimals - Number of decimals for the token (e.g., 18 for ETH, 6 for USDT)
|
|
21
|
+
* @returns Formatted balance string (e.g., "1.5" or "1000")
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* formatBalance("1500000000000000000", 18) // "1.5"
|
|
26
|
+
* formatBalance("1000000", 6) // "1"
|
|
27
|
+
* formatBalance("1000001", 6) // "1.000001"
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function formatBalance(balance: string | null, decimals: number): string;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Balance utility functions
|
|
3
|
+
*/
|
|
4
|
+
import { logError } from './logger';
|
|
5
|
+
/**
|
|
6
|
+
* Convert a value to string, handling BigInt values
|
|
7
|
+
*/
|
|
8
|
+
export function convertBalanceToString(value) {
|
|
9
|
+
if (typeof value === 'bigint') {
|
|
10
|
+
return value.toString();
|
|
11
|
+
}
|
|
12
|
+
if (typeof value === 'string') {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === 'number') {
|
|
16
|
+
return value.toString();
|
|
17
|
+
}
|
|
18
|
+
// Fallback: try to convert to string
|
|
19
|
+
return String(value);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Recursively convert BigInt values to strings in objects/arrays
|
|
23
|
+
* Prevents serialization errors when BigInt values are present
|
|
24
|
+
*
|
|
25
|
+
* @param value - Value that may contain BigInt values
|
|
26
|
+
* @returns Value with all BigInt values converted to strings
|
|
27
|
+
*/
|
|
28
|
+
export function convertBigIntToString(value) {
|
|
29
|
+
if (typeof value === 'bigint') {
|
|
30
|
+
return value.toString();
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
return value.map(convertBigIntToString);
|
|
34
|
+
}
|
|
35
|
+
if (value && typeof value === 'object') {
|
|
36
|
+
return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, convertBigIntToString(val)]));
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Format a balance from wei/smallest unit to readable format
|
|
42
|
+
*
|
|
43
|
+
* @param balance - Balance as string (from wei/smallest unit)
|
|
44
|
+
* @param decimals - Number of decimals for the token (e.g., 18 for ETH, 6 for USDT)
|
|
45
|
+
* @returns Formatted balance string (e.g., "1.5" or "1000")
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* formatBalance("1500000000000000000", 18) // "1.5"
|
|
50
|
+
* formatBalance("1000000", 6) // "1"
|
|
51
|
+
* formatBalance("1000001", 6) // "1.000001"
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function formatBalance(balance, decimals) {
|
|
55
|
+
if (!balance || balance === '0' || balance === 'null') {
|
|
56
|
+
return '0';
|
|
57
|
+
}
|
|
58
|
+
// Validate that balance is a valid numeric string before attempting BigInt conversion
|
|
59
|
+
// BigInt accepts: digits only, or digits with optional leading minus sign
|
|
60
|
+
if (!/^-?\d+$/.test(balance.trim())) {
|
|
61
|
+
// Invalid format - return as-is (for backwards compatibility with test expectations)
|
|
62
|
+
return balance;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const balanceBigInt = BigInt(balance);
|
|
66
|
+
const divisor = BigInt(10 ** decimals);
|
|
67
|
+
const wholePart = balanceBigInt / divisor;
|
|
68
|
+
const fractionalPart = balanceBigInt % divisor;
|
|
69
|
+
if (fractionalPart === BigInt(0)) {
|
|
70
|
+
return wholePart.toString();
|
|
71
|
+
}
|
|
72
|
+
// Format fractional part with leading zeros if needed
|
|
73
|
+
const fractionalStr = fractionalPart.toString().padStart(decimals, '0');
|
|
74
|
+
// Remove trailing zeros
|
|
75
|
+
const fractionalTrimmed = fractionalStr.replace(/0+$/, '');
|
|
76
|
+
if (fractionalTrimmed === '') {
|
|
77
|
+
return wholePart.toString();
|
|
78
|
+
}
|
|
79
|
+
return `${wholePart}.${fractionalTrimmed}`;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
logError('Error formatting balance:', error);
|
|
83
|
+
return balance;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=balanceUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balanceUtils.js","sourceRoot":"","sources":["../../src/utils/balanceUtils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAc;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAA;IACzB,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAA;IACd,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAA;IACzB,CAAC;IACD,qCAAqC;IACrC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;AACtB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAc;IAClD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAA;IACzB,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAA;IACzC,CAAC;IACD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,CAC7E,CAAA;IACH,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAAC,OAAsB,EAAE,QAAgB;IACpE,IAAI,CAAC,OAAO,IAAI,OAAO,KAAK,GAAG,IAAI,OAAO,KAAK,MAAM,EAAE,CAAC;QACtD,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,sFAAsF;IACtF,0EAA0E;IAC1E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;QACpC,qFAAqF;QACrF,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,IAAI,CAAC;QACH,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,CAAA;QACrC,MAAM,OAAO,GAAG,MAAM,CAAC,EAAE,IAAI,QAAQ,CAAC,CAAA;QACtC,MAAM,SAAS,GAAG,aAAa,GAAG,OAAO,CAAA;QACzC,MAAM,cAAc,GAAG,aAAa,GAAG,OAAO,CAAA;QAE9C,IAAI,cAAc,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;YACjC,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAA;QAC7B,CAAC;QAED,sDAAsD;QACtD,MAAM,aAAa,GAAG,cAAc,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;QACvE,wBAAwB;QACxB,MAAM,iBAAiB,GAAG,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;QAE1D,IAAI,iBAAiB,KAAK,EAAE,EAAE,CAAC;YAC7B,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAA;QAC7B,CAAC;QAED,OAAO,GAAG,SAAS,IAAI,iBAAiB,EAAE,CAAA;IAC5C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,QAAQ,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAA;QAC5C,OAAO,OAAO,CAAA;IAChB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application constants
|
|
3
|
+
*
|
|
4
|
+
* Centralized location for magic numbers and configuration values
|
|
5
|
+
* to improve maintainability and documentation.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Default balance refresh interval in milliseconds
|
|
9
|
+
*
|
|
10
|
+
* How often to automatically refresh wallet balances when auto-fetch is enabled.
|
|
11
|
+
* 30 seconds provides a good balance between freshness and performance.
|
|
12
|
+
*/
|
|
13
|
+
export declare const DEFAULT_BALANCE_REFRESH_INTERVAL_MS = 30000;
|
|
14
|
+
/**
|
|
15
|
+
* Valid mnemonic word counts
|
|
16
|
+
*
|
|
17
|
+
* BIP-39 standard supports 12-word (128 bits) and 24-word (256 bits) mnemonics.
|
|
18
|
+
*/
|
|
19
|
+
export declare const MNEMONIC_WORD_COUNTS: {
|
|
20
|
+
/** 12-word mnemonic (128 bits of entropy) */
|
|
21
|
+
readonly TWELVE: 12;
|
|
22
|
+
/** 24-word mnemonic (256 bits of entropy) */
|
|
23
|
+
readonly TWENTY_FOUR: 24;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Default mnemonic word count
|
|
27
|
+
*
|
|
28
|
+
* 12 words is the most common choice, providing 128 bits of entropy
|
|
29
|
+
* which is sufficient for most use cases.
|
|
30
|
+
*/
|
|
31
|
+
export declare const DEFAULT_MNEMONIC_WORD_COUNT: 12;
|
|
32
|
+
/**
|
|
33
|
+
* Account method names for balance operations
|
|
34
|
+
*/
|
|
35
|
+
export declare const ACCOUNT_METHOD_GET_BALANCE = "getBalance";
|
|
36
|
+
export declare const ACCOUNT_METHOD_GET_TOKEN_BALANCE = "getTokenBalance";
|
|
37
|
+
/**
|
|
38
|
+
* Wallet identifier constants
|
|
39
|
+
*/
|
|
40
|
+
export declare const WALLET_IDENTIFIER_PREFIX = "wallet-";
|
|
41
|
+
export declare const MAIN_WALLET_NAME = "Main Wallet";
|
|
42
|
+
export declare const WALLET_NAME_PREFIX = "Wallet ";
|
|
43
|
+
/**
|
|
44
|
+
* Token key for native tokens in balance storage
|
|
45
|
+
*/
|
|
46
|
+
export declare const NATIVE_TOKEN_KEY = "native";
|
|
47
|
+
/**
|
|
48
|
+
* Default query stale time in milliseconds
|
|
49
|
+
*
|
|
50
|
+
* How long data is considered fresh before TanStack Query refetches it.
|
|
51
|
+
* 30 seconds provides a good balance between freshness and performance.
|
|
52
|
+
*/
|
|
53
|
+
export declare const DEFAULT_QUERY_STALE_TIME_MS: number;
|
|
54
|
+
/**
|
|
55
|
+
* Default query garbage collection time in milliseconds
|
|
56
|
+
*
|
|
57
|
+
* How long unused query data is kept in cache before being garbage collected.
|
|
58
|
+
* 5 minutes provides a good balance between cache efficiency and memory usage.
|
|
59
|
+
*/
|
|
60
|
+
export declare const DEFAULT_QUERY_GC_TIME_MS: number;
|
|
61
|
+
/**
|
|
62
|
+
* Allowed account methods whitelist
|
|
63
|
+
* Only these methods can be called through AccountService for security
|
|
64
|
+
*/
|
|
65
|
+
export declare const ALLOWED_ACCOUNT_METHODS: readonly ["getAddress", "getBalance", "getTokenBalance", "signMessage", "signTransaction", "sendTransaction"];
|
|
66
|
+
export type AllowedAccountMethod = typeof ALLOWED_ACCOUNT_METHODS[number];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application constants
|
|
3
|
+
*
|
|
4
|
+
* Centralized location for magic numbers and configuration values
|
|
5
|
+
* to improve maintainability and documentation.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Default balance refresh interval in milliseconds
|
|
9
|
+
*
|
|
10
|
+
* How often to automatically refresh wallet balances when auto-fetch is enabled.
|
|
11
|
+
* 30 seconds provides a good balance between freshness and performance.
|
|
12
|
+
*/
|
|
13
|
+
export const DEFAULT_BALANCE_REFRESH_INTERVAL_MS = 30000;
|
|
14
|
+
/**
|
|
15
|
+
* Valid mnemonic word counts
|
|
16
|
+
*
|
|
17
|
+
* BIP-39 standard supports 12-word (128 bits) and 24-word (256 bits) mnemonics.
|
|
18
|
+
*/
|
|
19
|
+
export const MNEMONIC_WORD_COUNTS = {
|
|
20
|
+
/** 12-word mnemonic (128 bits of entropy) */
|
|
21
|
+
TWELVE: 12,
|
|
22
|
+
/** 24-word mnemonic (256 bits of entropy) */
|
|
23
|
+
TWENTY_FOUR: 24,
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Default mnemonic word count
|
|
27
|
+
*
|
|
28
|
+
* 12 words is the most common choice, providing 128 bits of entropy
|
|
29
|
+
* which is sufficient for most use cases.
|
|
30
|
+
*/
|
|
31
|
+
export const DEFAULT_MNEMONIC_WORD_COUNT = MNEMONIC_WORD_COUNTS.TWELVE;
|
|
32
|
+
/**
|
|
33
|
+
* Account method names for balance operations
|
|
34
|
+
*/
|
|
35
|
+
export const ACCOUNT_METHOD_GET_BALANCE = 'getBalance';
|
|
36
|
+
export const ACCOUNT_METHOD_GET_TOKEN_BALANCE = 'getTokenBalance';
|
|
37
|
+
/**
|
|
38
|
+
* Wallet identifier constants
|
|
39
|
+
*/
|
|
40
|
+
export const WALLET_IDENTIFIER_PREFIX = 'wallet-';
|
|
41
|
+
export const MAIN_WALLET_NAME = 'Main Wallet';
|
|
42
|
+
export const WALLET_NAME_PREFIX = 'Wallet ';
|
|
43
|
+
/**
|
|
44
|
+
* Token key for native tokens in balance storage
|
|
45
|
+
*/
|
|
46
|
+
export const NATIVE_TOKEN_KEY = 'native';
|
|
47
|
+
/**
|
|
48
|
+
* Default query stale time in milliseconds
|
|
49
|
+
*
|
|
50
|
+
* How long data is considered fresh before TanStack Query refetches it.
|
|
51
|
+
* 30 seconds provides a good balance between freshness and performance.
|
|
52
|
+
*/
|
|
53
|
+
export const DEFAULT_QUERY_STALE_TIME_MS = 30 * 1000;
|
|
54
|
+
/**
|
|
55
|
+
* Default query garbage collection time in milliseconds
|
|
56
|
+
*
|
|
57
|
+
* How long unused query data is kept in cache before being garbage collected.
|
|
58
|
+
* 5 minutes provides a good balance between cache efficiency and memory usage.
|
|
59
|
+
*/
|
|
60
|
+
export const DEFAULT_QUERY_GC_TIME_MS = 5 * 60 * 1000;
|
|
61
|
+
/**
|
|
62
|
+
* Allowed account methods whitelist
|
|
63
|
+
* Only these methods can be called through AccountService for security
|
|
64
|
+
*/
|
|
65
|
+
export const ALLOWED_ACCOUNT_METHODS = [
|
|
66
|
+
'getAddress',
|
|
67
|
+
'getBalance',
|
|
68
|
+
'getTokenBalance',
|
|
69
|
+
'signMessage',
|
|
70
|
+
'signTransaction',
|
|
71
|
+
'sendTransaction',
|
|
72
|
+
];
|
|
73
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,KAAK,CAAA;AAExD;;;;GAIG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,6CAA6C;IAC7C,MAAM,EAAE,EAAE;IACV,6CAA6C;IAC7C,WAAW,EAAE,EAAE;CACP,CAAA;AAEV;;;;;GAKG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,oBAAoB,CAAC,MAAM,CAAA;AAEtE;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,YAAY,CAAA;AACtD,MAAM,CAAC,MAAM,gCAAgC,GAAG,iBAAiB,CAAA;AAEjE;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,SAAS,CAAA;AACjD,MAAM,CAAC,MAAM,gBAAgB,GAAG,aAAa,CAAA;AAC7C,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAA;AAE3C;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAQ,CAAA;AAExC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,GAAG,IAAI,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAA;AAErD;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,YAAY;IACZ,YAAY;IACZ,iBAAiB;IACjB,aAAa;IACb,iBAAiB;IACjB,iBAAiB;CACT,CAAA"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error handling utilities for services
|
|
3
|
+
*
|
|
4
|
+
* Provides consistent error handling patterns across all services
|
|
5
|
+
* to reduce code duplication and improve maintainability.
|
|
6
|
+
*
|
|
7
|
+
* ## Usage Guidelines
|
|
8
|
+
*
|
|
9
|
+
* **For Services**: Use `handleServiceError()` - this provides consistent normalization
|
|
10
|
+
* and logging for service-layer errors. Errors are NOT sanitized (sanitizeLevel: false)
|
|
11
|
+
* because services log internally and need full error details for debugging.
|
|
12
|
+
*
|
|
13
|
+
* **For Hooks/UI**: Use `normalizeError()` directly with appropriate sanitization level.
|
|
14
|
+
* Hooks should sanitize errors before exposing them to UI components.
|
|
15
|
+
*
|
|
16
|
+
* @example Service usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* try {
|
|
19
|
+
* await someOperation()
|
|
20
|
+
* } catch (error) {
|
|
21
|
+
* handleServiceError(error, 'AddressService', 'getAddress', { network, accountIndex })
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Hook/UI usage:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* try {
|
|
28
|
+
* await someOperation()
|
|
29
|
+
* } catch (error) {
|
|
30
|
+
* const normalized = normalizeError(error, true, { component: 'MyHook', operation: 'fetchData' })
|
|
31
|
+
* setError(normalized.message)
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Handle service errors with consistent normalization and logging
|
|
37
|
+
*
|
|
38
|
+
* **Use this in services** - provides consistent error handling with full error details
|
|
39
|
+
* (no sanitization) for internal logging and debugging.
|
|
40
|
+
*
|
|
41
|
+
* @param error - Error to handle
|
|
42
|
+
* @param component - Component/service name where error occurred
|
|
43
|
+
* @param operation - Operation name that failed
|
|
44
|
+
* @param context - Additional context for error
|
|
45
|
+
* @throws Normalized error
|
|
46
|
+
*/
|
|
47
|
+
export declare function handleServiceError(error: unknown, component: string, operation: string, context?: Record<string, unknown>): never;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error handling utilities for services
|
|
3
|
+
*
|
|
4
|
+
* Provides consistent error handling patterns across all services
|
|
5
|
+
* to reduce code duplication and improve maintainability.
|
|
6
|
+
*
|
|
7
|
+
* ## Usage Guidelines
|
|
8
|
+
*
|
|
9
|
+
* **For Services**: Use `handleServiceError()` - this provides consistent normalization
|
|
10
|
+
* and logging for service-layer errors. Errors are NOT sanitized (sanitizeLevel: false)
|
|
11
|
+
* because services log internally and need full error details for debugging.
|
|
12
|
+
*
|
|
13
|
+
* **For Hooks/UI**: Use `normalizeError()` directly with appropriate sanitization level.
|
|
14
|
+
* Hooks should sanitize errors before exposing them to UI components.
|
|
15
|
+
*
|
|
16
|
+
* @example Service usage:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* try {
|
|
19
|
+
* await someOperation()
|
|
20
|
+
* } catch (error) {
|
|
21
|
+
* handleServiceError(error, 'AddressService', 'getAddress', { network, accountIndex })
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Hook/UI usage:
|
|
26
|
+
* ```typescript
|
|
27
|
+
* try {
|
|
28
|
+
* await someOperation()
|
|
29
|
+
* } catch (error) {
|
|
30
|
+
* const normalized = normalizeError(error, true, { component: 'MyHook', operation: 'fetchData' })
|
|
31
|
+
* setError(normalized.message)
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import { normalizeError } from './errorUtils';
|
|
36
|
+
import { logError } from './logger';
|
|
37
|
+
/**
|
|
38
|
+
* Handle service errors with consistent normalization and logging
|
|
39
|
+
*
|
|
40
|
+
* **Use this in services** - provides consistent error handling with full error details
|
|
41
|
+
* (no sanitization) for internal logging and debugging.
|
|
42
|
+
*
|
|
43
|
+
* @param error - Error to handle
|
|
44
|
+
* @param component - Component/service name where error occurred
|
|
45
|
+
* @param operation - Operation name that failed
|
|
46
|
+
* @param context - Additional context for error
|
|
47
|
+
* @throws Normalized error
|
|
48
|
+
*/
|
|
49
|
+
export function handleServiceError(error, component, operation, context) {
|
|
50
|
+
const normalized = normalizeError(error, false, { component, operation, ...context });
|
|
51
|
+
logError(`[${component}] ${operation} failed:`, normalized);
|
|
52
|
+
throw normalized;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=errorHandling.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandling.js","sourceRoot":"","sources":["../../src/utils/errorHandling.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAc,EACd,SAAiB,EACjB,SAAiB,EACjB,OAAiC;IAEjC,MAAM,UAAU,GAAG,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA;IACrF,QAAQ,CAAC,IAAI,SAAS,KAAK,SAAS,UAAU,EAAE,UAAU,CAAC,CAAA;IAC3D,MAAM,UAAU,CAAA;AAClB,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error utility functions for consistent error handling
|
|
3
|
+
*
|
|
4
|
+
* ## Usage Guidelines
|
|
5
|
+
*
|
|
6
|
+
* **For Services**: Use `handleServiceError()` from `errorHandling.ts` - this provides
|
|
7
|
+
* consistent normalization and logging for service-layer errors. Errors are NOT sanitized
|
|
8
|
+
* (sanitizeLevel: false) because services log internally and need full error details.
|
|
9
|
+
*
|
|
10
|
+
* **For Hooks/UI**: Use `normalizeError()` directly with appropriate sanitization level.
|
|
11
|
+
* Hooks should sanitize errors before exposing them to UI components to prevent information leakage.
|
|
12
|
+
*
|
|
13
|
+
* @example Service usage (use handleServiceError instead):
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { handleServiceError } from './errorHandling'
|
|
16
|
+
* try {
|
|
17
|
+
* await someOperation()
|
|
18
|
+
* } catch (error) {
|
|
19
|
+
* handleServiceError(error, 'MyService', 'operationName', { context })
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example Hook/UI usage:
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { normalizeError } from './errorUtils'
|
|
26
|
+
* try {
|
|
27
|
+
* await someOperation()
|
|
28
|
+
* } catch (error) {
|
|
29
|
+
* const normalized = normalizeError(error, true, { component: 'MyHook', operation: 'fetchData' })
|
|
30
|
+
* setError(normalized.message)
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* Sanitization levels for error messages
|
|
36
|
+
*/
|
|
37
|
+
export declare enum SanitizationLevel {
|
|
38
|
+
NONE = "none",// No sanitization (internal debugging only)
|
|
39
|
+
DEVELOPMENT = "dev",// Mask sensitive strings but show structure
|
|
40
|
+
PRODUCTION = "prod"
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Sanitize error message to prevent information leakage
|
|
44
|
+
* Removes or masks sensitive information while preserving useful debugging info
|
|
45
|
+
*
|
|
46
|
+
* @param message - Error message to sanitize
|
|
47
|
+
* @param isDevelopment - Whether we're in development mode (less sanitization)
|
|
48
|
+
* @param context - Optional context about where the error occurred (for better sanitization)
|
|
49
|
+
* @returns Sanitized error message
|
|
50
|
+
*/
|
|
51
|
+
export declare function sanitizeErrorMessage(message: string, isDevelopment?: boolean, context?: {
|
|
52
|
+
operation?: string;
|
|
53
|
+
component?: string;
|
|
54
|
+
}): string;
|
|
55
|
+
/**
|
|
56
|
+
* Normalize error to Error instance
|
|
57
|
+
* Converts any error-like value to a proper Error object
|
|
58
|
+
* Always sanitizes the error message (with different levels) to prevent information leakage
|
|
59
|
+
*
|
|
60
|
+
* @param error - Error to normalize
|
|
61
|
+
* @param sanitizeLevel - Sanitization level or boolean (default: PRODUCTION in production, DEVELOPMENT in dev)
|
|
62
|
+
* @param context - Optional context about where the error occurred
|
|
63
|
+
* @returns Normalized Error instance
|
|
64
|
+
*/
|
|
65
|
+
export declare function normalizeError(error: unknown, sanitizeLevel?: SanitizationLevel | boolean, context?: {
|
|
66
|
+
operation?: string;
|
|
67
|
+
component?: string;
|
|
68
|
+
[key: string]: unknown;
|
|
69
|
+
}): Error;
|
|
70
|
+
/**
|
|
71
|
+
* Get error message from any error-like value
|
|
72
|
+
*/
|
|
73
|
+
export declare function getErrorMessage(error: unknown): string;
|
|
74
|
+
/**
|
|
75
|
+
* Check if error is a specific type
|
|
76
|
+
*/
|
|
77
|
+
export declare function isErrorType(error: unknown, typeName: string): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Create a standardized error with context
|
|
80
|
+
*/
|
|
81
|
+
export declare function createContextualError(message: string, context?: Record<string, unknown>): Error;
|
|
82
|
+
/**
|
|
83
|
+
* Check if an error is an authentication error
|
|
84
|
+
* Used to prevent automatic retries when authentication fails
|
|
85
|
+
*
|
|
86
|
+
* @param error - Error to check
|
|
87
|
+
* @returns true if the error is an authentication error
|
|
88
|
+
*/
|
|
89
|
+
export declare function isAuthenticationError(error: unknown): boolean;
|