@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,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utilities for WDK provider props and inputs
|
|
3
|
+
*
|
|
4
|
+
* These functions throw errors for invalid inputs.
|
|
5
|
+
* Uses Zod schemas for validation with better error messages.
|
|
6
|
+
* For type guards (boolean returns), see typeGuards.ts
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { z } from 'zod'
|
|
10
|
+
import {
|
|
11
|
+
networkConfigsSchema,
|
|
12
|
+
tokenConfigsSchema,
|
|
13
|
+
accountIndexSchema,
|
|
14
|
+
networkNameSchema,
|
|
15
|
+
balanceStringSchema,
|
|
16
|
+
ethereumAddressSchema,
|
|
17
|
+
} from './schemas'
|
|
18
|
+
import type { NetworkConfigs, TokenConfigs } from '../types'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Extract error message from Zod error
|
|
22
|
+
*/
|
|
23
|
+
function getZodErrorMessage(error: unknown): string {
|
|
24
|
+
if (error instanceof z.ZodError) {
|
|
25
|
+
// Get the first error message for simplicity
|
|
26
|
+
const firstIssue = error.issues[0]
|
|
27
|
+
if (firstIssue) {
|
|
28
|
+
return firstIssue.message
|
|
29
|
+
}
|
|
30
|
+
return 'Validation failed'
|
|
31
|
+
}
|
|
32
|
+
if (error instanceof Error) {
|
|
33
|
+
return error.message
|
|
34
|
+
}
|
|
35
|
+
return String(error)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Validate network configuration
|
|
40
|
+
*/
|
|
41
|
+
export function validateNetworkConfigs(networkConfigs: NetworkConfigs): void {
|
|
42
|
+
try {
|
|
43
|
+
networkConfigsSchema.parse(networkConfigs)
|
|
44
|
+
} catch (error) {
|
|
45
|
+
throw new Error(`Invalid networkConfigs: ${getZodErrorMessage(error)}`)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Validate token configuration
|
|
51
|
+
*/
|
|
52
|
+
export function validateTokenConfigs(tokenConfigs: TokenConfigs): void {
|
|
53
|
+
try {
|
|
54
|
+
tokenConfigsSchema.parse(tokenConfigs)
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new Error(`Invalid tokenConfigs: ${getZodErrorMessage(error)}`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validate balance refresh interval
|
|
62
|
+
*/
|
|
63
|
+
export function validateBalanceRefreshInterval(interval: number | undefined): void {
|
|
64
|
+
if (interval !== undefined) {
|
|
65
|
+
if (typeof interval !== 'number') {
|
|
66
|
+
throw new Error('balanceRefreshInterval must be a number')
|
|
67
|
+
}
|
|
68
|
+
if (interval < 0) {
|
|
69
|
+
throw new Error('balanceRefreshInterval must be a non-negative number')
|
|
70
|
+
}
|
|
71
|
+
if (!Number.isFinite(interval)) {
|
|
72
|
+
throw new Error('balanceRefreshInterval must be a finite number')
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Validate that an object has required methods
|
|
79
|
+
*
|
|
80
|
+
* @param obj - Object to validate
|
|
81
|
+
* @param requiredMethods - Array of required method names
|
|
82
|
+
* @param objectName - Name of the object for error messages
|
|
83
|
+
*/
|
|
84
|
+
export function validateRequiredMethods(
|
|
85
|
+
obj: unknown,
|
|
86
|
+
requiredMethods: string[],
|
|
87
|
+
objectName: string
|
|
88
|
+
): void {
|
|
89
|
+
if (!obj || typeof obj !== 'object') {
|
|
90
|
+
throw new Error(`${objectName} must be an object`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const methodName of requiredMethods) {
|
|
94
|
+
if (typeof (obj as Record<string, unknown>)[methodName] !== 'function') {
|
|
95
|
+
throw new Error(`${objectName} must have a ${methodName} method`)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Validate account index
|
|
102
|
+
*/
|
|
103
|
+
export function validateAccountIndex(accountIndex: number): void {
|
|
104
|
+
try {
|
|
105
|
+
accountIndexSchema.parse(accountIndex)
|
|
106
|
+
} catch (error) {
|
|
107
|
+
const message = getZodErrorMessage(error)
|
|
108
|
+
throw new Error(`Invalid accountIndex: ${message}`)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Validate network name
|
|
114
|
+
*/
|
|
115
|
+
export function validateNetworkName(network: string): void {
|
|
116
|
+
try {
|
|
117
|
+
networkNameSchema.parse(network)
|
|
118
|
+
} catch (error) {
|
|
119
|
+
const message = getZodErrorMessage(error)
|
|
120
|
+
// Provide a fallback message if Zod error doesn't have a clear message
|
|
121
|
+
if (message === 'Validation failed' || !message) {
|
|
122
|
+
throw new Error('network must be a non-empty string containing only alphanumeric characters, hyphens, and underscores')
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`Invalid network name: ${message}`)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Validate token address (can be null for native tokens)
|
|
130
|
+
*/
|
|
131
|
+
export function validateTokenAddress(tokenAddress: string | null): void {
|
|
132
|
+
if (tokenAddress === null) {
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
ethereumAddressSchema.parse(tokenAddress)
|
|
137
|
+
} catch (error) {
|
|
138
|
+
const message = getZodErrorMessage(error)
|
|
139
|
+
throw new Error(`Invalid tokenAddress: ${message}`)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Validate balance string
|
|
145
|
+
*/
|
|
146
|
+
export function validateBalance(balance: string): void {
|
|
147
|
+
try {
|
|
148
|
+
balanceStringSchema.parse(balance)
|
|
149
|
+
} catch (error) {
|
|
150
|
+
const message = getZodErrorMessage(error)
|
|
151
|
+
throw new Error(`Invalid balance: ${message}`)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Validate wallet parameters (network, accountIndex, optional tokenAddress)
|
|
157
|
+
* Convenience function to validate common wallet operation parameters
|
|
158
|
+
*
|
|
159
|
+
* @param network - Network name
|
|
160
|
+
* @param accountIndex - Account index
|
|
161
|
+
* @param tokenAddress - Optional token address (null for native tokens)
|
|
162
|
+
*/
|
|
163
|
+
export function validateWalletParams(
|
|
164
|
+
network: string,
|
|
165
|
+
accountIndex: number,
|
|
166
|
+
tokenAddress?: string | null
|
|
167
|
+
): void {
|
|
168
|
+
validateNetworkName(network)
|
|
169
|
+
validateAccountIndex(accountIndex)
|
|
170
|
+
if (tokenAddress !== undefined) {
|
|
171
|
+
validateTokenAddress(tokenAddress)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet State Helper Functions
|
|
3
|
+
*
|
|
4
|
+
* Pure functions that determine wallet state transition decisions.
|
|
5
|
+
* These helpers are used by WdkAppProvider for wallet state synchronization logic.
|
|
6
|
+
*
|
|
7
|
+
* These functions are extracted for:
|
|
8
|
+
* - Readability: Complex state logic separated from component orchestration
|
|
9
|
+
* - Testability: Pure functions can be tested in isolation
|
|
10
|
+
* - Reusability: Can be used elsewhere if needed
|
|
11
|
+
*
|
|
12
|
+
* Note: The effect that uses these functions must remain consolidated to prevent race conditions.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { WalletLoadingState } from '../store/walletStore'
|
|
16
|
+
import { isWalletLoadingState } from '../store/walletStore'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Determine if wallet state should be reset to not_loaded
|
|
20
|
+
* This happens when activeWalletId is cleared (null)
|
|
21
|
+
*
|
|
22
|
+
* @param activeWalletId - Currently active wallet identifier (null if none)
|
|
23
|
+
* @param walletLoadingState - Current wallet loading state
|
|
24
|
+
* @returns true if state should be reset to not_loaded
|
|
25
|
+
*/
|
|
26
|
+
export function shouldResetToNotLoaded(
|
|
27
|
+
activeWalletId: string | null,
|
|
28
|
+
walletLoadingState: WalletLoadingState
|
|
29
|
+
): boolean {
|
|
30
|
+
return !activeWalletId && walletLoadingState.type !== 'not_loaded'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Determine wallet switch decision when activeWalletId changes
|
|
35
|
+
* Returns whether a switch should occur and whether the new wallet should be marked ready
|
|
36
|
+
*
|
|
37
|
+
* @param currentWalletId - Wallet ID currently tracked in loading state
|
|
38
|
+
* @param activeWalletId - New active wallet ID
|
|
39
|
+
* @param hasAddresses - Whether the new wallet has addresses available
|
|
40
|
+
* @returns Object with shouldSwitch and shouldMarkReady flags
|
|
41
|
+
*/
|
|
42
|
+
export function getWalletSwitchDecision(
|
|
43
|
+
currentWalletId: string | null,
|
|
44
|
+
activeWalletId: string | null,
|
|
45
|
+
hasAddresses: boolean
|
|
46
|
+
): { shouldSwitch: boolean; shouldMarkReady: boolean } {
|
|
47
|
+
if (currentWalletId !== activeWalletId) {
|
|
48
|
+
return { shouldSwitch: true, shouldMarkReady: hasAddresses }
|
|
49
|
+
}
|
|
50
|
+
return { shouldSwitch: false, shouldMarkReady: false }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Determine if wallet should be marked as ready
|
|
55
|
+
* This checks if wallet is in loading/checking state, addresses exist, and worklet is initialized with seed
|
|
56
|
+
*
|
|
57
|
+
* Note: If addresses exist but state is not_loaded, we need to go through loading first.
|
|
58
|
+
* This is handled by useWalletManager.initializeWallet() or useOnboarding.
|
|
59
|
+
*
|
|
60
|
+
* @param walletLoadingState - Current wallet loading state
|
|
61
|
+
* @param hasAddresses - Whether wallet has addresses available
|
|
62
|
+
* @param currentWalletId - Wallet ID currently tracked in loading state
|
|
63
|
+
* @param activeWalletId - Currently active wallet identifier
|
|
64
|
+
* @param isWorkletInitialized - Whether the worklet is initialized with wallet credentials (seed loaded)
|
|
65
|
+
* @returns true if wallet should be marked as ready
|
|
66
|
+
*/
|
|
67
|
+
export function shouldMarkWalletAsReady(
|
|
68
|
+
walletLoadingState: WalletLoadingState,
|
|
69
|
+
hasAddresses: boolean,
|
|
70
|
+
currentWalletId: string | null,
|
|
71
|
+
activeWalletId: string | null,
|
|
72
|
+
isWorkletInitialized: boolean
|
|
73
|
+
): boolean {
|
|
74
|
+
// Only allow ready transition from loading/checking states
|
|
75
|
+
// AND worklet must be initialized with seed (not just addresses cached)
|
|
76
|
+
// If addresses exist but state is not_loaded, we need to go through loading first
|
|
77
|
+
// This is handled by useWalletManager.initializeWallet() or useOnboarding
|
|
78
|
+
if (isWalletLoadingState(walletLoadingState) && currentWalletId === activeWalletId && hasAddresses && isWorkletInitialized) {
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Determine if an error should be handled for the current wallet
|
|
86
|
+
* Only handle errors if they're for the wallet we're currently tracking
|
|
87
|
+
*
|
|
88
|
+
* @param walletManagerError - Error message from wallet manager (null if no error)
|
|
89
|
+
* @param currentWalletId - Wallet ID currently tracked in loading state
|
|
90
|
+
* @param activeWalletId - Currently active wallet identifier
|
|
91
|
+
* @param walletLoadingState - Current wallet loading state
|
|
92
|
+
* @returns true if error should be handled for current wallet
|
|
93
|
+
*/
|
|
94
|
+
export function shouldHandleError(
|
|
95
|
+
walletManagerError: string | null,
|
|
96
|
+
currentWalletId: string | null,
|
|
97
|
+
activeWalletId: string | null,
|
|
98
|
+
walletLoadingState: WalletLoadingState
|
|
99
|
+
): boolean {
|
|
100
|
+
if (!walletManagerError) return false
|
|
101
|
+
// Only update error if we're tracking this wallet
|
|
102
|
+
return currentWalletId === activeWalletId || walletLoadingState.type === 'not_loaded'
|
|
103
|
+
}
|
|
104
|
+
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions for working with wallets and retrieving addresses from walletStore.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { AccountService } from '../services/accountService'
|
|
8
|
+
import { getWalletStore } from '../store/walletStore'
|
|
9
|
+
import { getWorkletStore } from '../store/workletStore'
|
|
10
|
+
import type { WalletStore as WalletStoreType } from '../store/walletStore'
|
|
11
|
+
import type { WalletStore } from '../types'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get addresses for a wallet from walletStore
|
|
15
|
+
*
|
|
16
|
+
* This helper function retrieves addresses for a specific accountIndex from the walletStore.
|
|
17
|
+
* Addresses are stored in walletStore as: { [network]: { [accountIndex]: address } }
|
|
18
|
+
* This converts them to: { [network]: address } for a specific accountIndex
|
|
19
|
+
*
|
|
20
|
+
* NOTE: walletStore is the ONLY place where addresses are actually stored.
|
|
21
|
+
* This function simply retrieves/looks up addresses from walletStore.
|
|
22
|
+
*
|
|
23
|
+
* @param walletStore - The wallet store instance (Zustand store) - the source of truth for addresses
|
|
24
|
+
* @param accountIndex - The account index to get addresses for
|
|
25
|
+
* @returns Record of network -> address for the given accountIndex
|
|
26
|
+
*/
|
|
27
|
+
export function getWalletAddresses(
|
|
28
|
+
walletStore: { getState: () => WalletStoreType },
|
|
29
|
+
accountIndex: number
|
|
30
|
+
): Record<string, string> {
|
|
31
|
+
const state = walletStore.getState()
|
|
32
|
+
const addresses: Record<string, string> = {}
|
|
33
|
+
|
|
34
|
+
// Get active wallet ID or use all wallets
|
|
35
|
+
const activeWalletId = state.activeWalletId
|
|
36
|
+
|
|
37
|
+
// Retrieve addresses for this accountIndex from walletStore
|
|
38
|
+
// state.addresses is WalletAddressesByWallet: { [walletId]: { [network]: { [accountIndex]: address } } }
|
|
39
|
+
if (activeWalletId && state.addresses[activeWalletId]) {
|
|
40
|
+
// Use active wallet if available
|
|
41
|
+
const walletAddresses = state.addresses[activeWalletId]
|
|
42
|
+
// walletAddresses is WalletAddresses: { [network]: { [accountIndex]: address } }
|
|
43
|
+
Object.entries(walletAddresses).forEach(([network, networkAddresses]) => {
|
|
44
|
+
if (networkAddresses && typeof networkAddresses === 'object') {
|
|
45
|
+
const address = networkAddresses[accountIndex]
|
|
46
|
+
if (address) {
|
|
47
|
+
addresses[network] = address
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
} else {
|
|
52
|
+
// Fallback: iterate over all wallets (for backward compatibility or when no active wallet)
|
|
53
|
+
Object.values(state.addresses).forEach((walletAddresses) => {
|
|
54
|
+
if (walletAddresses && typeof walletAddresses === 'object') {
|
|
55
|
+
// walletAddresses is WalletAddresses: { [network]: { [accountIndex]: address } }
|
|
56
|
+
Object.entries(walletAddresses).forEach(([network, networkAddresses]) => {
|
|
57
|
+
if (networkAddresses && typeof networkAddresses === 'object') {
|
|
58
|
+
const address = networkAddresses[accountIndex]
|
|
59
|
+
if (address) {
|
|
60
|
+
addresses[network] = address
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return addresses
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create a base wallet store that wraps the worklet and wallet stores
|
|
73
|
+
*
|
|
74
|
+
* This provides the worklet methods (callAccountMethod, isWalletInitialized)
|
|
75
|
+
* and helper functions for retrieving addresses from walletStore.
|
|
76
|
+
* Apps should extend this with their own wallet metadata and balance management.
|
|
77
|
+
*
|
|
78
|
+
* Always uses the default MMKV storage adapter.
|
|
79
|
+
*
|
|
80
|
+
* @returns Base wallet store implementation
|
|
81
|
+
*/
|
|
82
|
+
export function createBaseWalletStore(): Pick<WalletStore, 'callAccountMethod' | 'isWalletInitialized'> & {
|
|
83
|
+
getWalletAddresses: (accountIndex: number) => Record<string, string>
|
|
84
|
+
} {
|
|
85
|
+
const workletStore = getWorkletStore()
|
|
86
|
+
const walletStore = getWalletStore()
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
callAccountMethod: async <T = unknown>(
|
|
90
|
+
network: string,
|
|
91
|
+
accountIndex: number,
|
|
92
|
+
methodName: string,
|
|
93
|
+
args?: unknown
|
|
94
|
+
): Promise<T> => {
|
|
95
|
+
return AccountService.callAccountMethod<T>(network, accountIndex, methodName, args)
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
isWalletInitialized: () => {
|
|
99
|
+
return workletStore.getState().isInitialized
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
getWalletAddresses: (accountIndex: number) => {
|
|
103
|
+
return getWalletAddresses(walletStore, accountIndex)
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|