@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,288 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState, useMemo } from 'react'
|
|
2
|
+
import { useShallow } from 'zustand/react/shallow'
|
|
3
|
+
|
|
4
|
+
import { AccountService } from '../services/accountService'
|
|
5
|
+
import { AddressService } from '../services/addressService'
|
|
6
|
+
import { WalletSetupService } from '../services/walletSetupService'
|
|
7
|
+
import { WalletSwitchingService } from '../services/walletSwitchingService'
|
|
8
|
+
import { getWalletStore } from '../store/walletStore'
|
|
9
|
+
import { getWorkletStore } from '../store/workletStore'
|
|
10
|
+
import { isOperationInProgress } from '../utils/operationMutex'
|
|
11
|
+
import { log, logError } from '../utils/logger'
|
|
12
|
+
import type { WalletStore } from '../store/walletStore'
|
|
13
|
+
import type { WorkletStore } from '../store/workletStore'
|
|
14
|
+
|
|
15
|
+
// Stable empty objects to prevent creating new objects on every render
|
|
16
|
+
const EMPTY_ADDRESSES = {} as Record<string, Record<number, string>>
|
|
17
|
+
const EMPTY_WALLET_LOADING = {} as Record<string, boolean>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Check if wallet switching should be skipped
|
|
21
|
+
*/
|
|
22
|
+
function shouldSkipWalletSwitch(
|
|
23
|
+
requestedWalletId: string | undefined,
|
|
24
|
+
activeWalletId: string | null,
|
|
25
|
+
isSwitchingWallet: boolean,
|
|
26
|
+
switchingToWalletId: string | null
|
|
27
|
+
): boolean {
|
|
28
|
+
// Skip if no walletId provided or walletId matches activeWalletId
|
|
29
|
+
if (!requestedWalletId || requestedWalletId === activeWalletId) {
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Skip if already switching to this wallet
|
|
34
|
+
if (isSwitchingWallet && switchingToWalletId === requestedWalletId) {
|
|
35
|
+
return true
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Skip if switching to a different wallet (wait for current switch to complete)
|
|
39
|
+
if (isSwitchingWallet && switchingToWalletId !== requestedWalletId) {
|
|
40
|
+
return true
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Check if the requested wallet is a temporary wallet
|
|
48
|
+
*/
|
|
49
|
+
function isTemporaryWalletId(walletId: string | undefined): boolean {
|
|
50
|
+
return walletId === '__temporary__'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Normalize error to Error instance
|
|
55
|
+
*/
|
|
56
|
+
function normalizeErrorToError(error: unknown): Error {
|
|
57
|
+
return error instanceof Error ? error : new Error(String(error))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Hook to interact with wallet data (addresses and account methods)
|
|
62
|
+
*
|
|
63
|
+
* PURPOSE: Use this hook for wallet operations AFTER the wallet has been initialized.
|
|
64
|
+
* This hook provides access to wallet addresses and account methods.
|
|
65
|
+
*
|
|
66
|
+
* When to use which hook:
|
|
67
|
+
* - **App initialization state**: Use `useWdkApp()` to check if app is ready
|
|
68
|
+
* - **Wallet lifecycle** (create, load, import, delete): Use `useWalletManager()`
|
|
69
|
+
* - **Wallet operations** (addresses, account methods): Use this hook (`useWallet()`)
|
|
70
|
+
* - **Balance fetching**: Use `useBalance()` hook with TanStack Query
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```tsx
|
|
74
|
+
* // First, check if app is ready
|
|
75
|
+
* const { isReady } = useWdkApp()
|
|
76
|
+
* if (!isReady) return <LoadingScreen />
|
|
77
|
+
*
|
|
78
|
+
* // Then use wallet operations
|
|
79
|
+
* const { addresses, getAddress, callAccountMethod } = useWallet()
|
|
80
|
+
*
|
|
81
|
+
* // Use specific wallet (automatically switches if needed)
|
|
82
|
+
* const { addresses, getAddress } = useWallet({ walletId: 'user@example.com' })
|
|
83
|
+
*
|
|
84
|
+
* // Note: For creating temporary wallets, use useWalletManager().createTemporaryWallet()
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export interface UseWalletResult {
|
|
88
|
+
// State (reactive)
|
|
89
|
+
addresses: Record<string, Record<number, string>> // network -> accountIndex -> address (for current wallet)
|
|
90
|
+
walletLoading: Record<string, boolean> // loading states for current wallet
|
|
91
|
+
isInitialized: boolean
|
|
92
|
+
// Switching state
|
|
93
|
+
isSwitchingWallet: boolean
|
|
94
|
+
switchingToWalletId: string | null
|
|
95
|
+
switchWalletError: Error | null
|
|
96
|
+
isTemporaryWallet: boolean
|
|
97
|
+
// Computed helpers
|
|
98
|
+
getNetworkAddresses: (network: string) => Record<number, string>
|
|
99
|
+
isLoadingAddress: (network: string, accountIndex?: number) => boolean
|
|
100
|
+
// Actions
|
|
101
|
+
getAddress: (network: string, accountIndex?: number) => Promise<string>
|
|
102
|
+
callAccountMethod: <T = unknown>(
|
|
103
|
+
network: string,
|
|
104
|
+
accountIndex: number,
|
|
105
|
+
methodName: string,
|
|
106
|
+
args?: unknown
|
|
107
|
+
) => Promise<T>
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function useWallet(options?: {
|
|
111
|
+
walletId?: string
|
|
112
|
+
}): UseWalletResult {
|
|
113
|
+
const workletStore = getWorkletStore()
|
|
114
|
+
const walletStore = getWalletStore()
|
|
115
|
+
|
|
116
|
+
// Switching state
|
|
117
|
+
const [isSwitchingWallet, setIsSwitchingWallet] = useState(false)
|
|
118
|
+
const [switchingToWalletId, setSwitchingToWalletId] = useState<string | null>(null)
|
|
119
|
+
const [switchWalletError, setSwitchWalletError] = useState<Error | null>(null)
|
|
120
|
+
const [isTemporaryWallet, setIsTemporaryWallet] = useState(false)
|
|
121
|
+
|
|
122
|
+
// Get activeWalletId from stores
|
|
123
|
+
const activeWalletId = walletStore((state: WalletStore) => state.activeWalletId)
|
|
124
|
+
|
|
125
|
+
// Determine target walletId
|
|
126
|
+
const targetWalletId = options?.walletId || activeWalletId
|
|
127
|
+
|
|
128
|
+
// Subscribe to wallet state for target wallet
|
|
129
|
+
// useShallow ensures stable references when content doesn't change
|
|
130
|
+
// We select the specific wallet's data directly from the store
|
|
131
|
+
// Use stable empty objects to prevent new object creation on every render
|
|
132
|
+
const walletState = walletStore(
|
|
133
|
+
useShallow((state: WalletStore) => {
|
|
134
|
+
const walletId = options?.walletId || state.activeWalletId
|
|
135
|
+
if (!walletId) {
|
|
136
|
+
return {
|
|
137
|
+
addresses: EMPTY_ADDRESSES,
|
|
138
|
+
walletLoading: EMPTY_WALLET_LOADING,
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const addresses = state.addresses[walletId]
|
|
142
|
+
const walletLoading = state.walletLoading[walletId]
|
|
143
|
+
return {
|
|
144
|
+
addresses: addresses || EMPTY_ADDRESSES,
|
|
145
|
+
walletLoading: walletLoading || EMPTY_WALLET_LOADING,
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
)
|
|
149
|
+
const isInitialized = workletStore((state: WorkletStore) => state.isInitialized)
|
|
150
|
+
|
|
151
|
+
// Automatic wallet switching logic
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
const requestedWalletId = options?.walletId
|
|
154
|
+
|
|
155
|
+
// Skip if switching should be skipped
|
|
156
|
+
if (shouldSkipWalletSwitch(requestedWalletId, activeWalletId, isSwitchingWallet, switchingToWalletId)) {
|
|
157
|
+
setIsTemporaryWallet(false)
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Skip until WdkAppProvider has registered SecureStorage (runs synchronously on mount)
|
|
162
|
+
if (!WalletSetupService.isSecureStorageInitialized()) {
|
|
163
|
+
log('[useWallet] SecureStorage not initialized yet, skipping wallet switch')
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Check if another operation is in progress (via mutex)
|
|
168
|
+
if (isOperationInProgress()) {
|
|
169
|
+
log('[useWallet] Operation in progress, skipping wallet switch')
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Handle temporary wallet identifier
|
|
174
|
+
if (isTemporaryWalletId(requestedWalletId)) {
|
|
175
|
+
setIsTemporaryWallet(true)
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let cancelled = false
|
|
180
|
+
|
|
181
|
+
const switchWallet = async () => {
|
|
182
|
+
setIsSwitchingWallet(true)
|
|
183
|
+
setSwitchingToWalletId(requestedWalletId!)
|
|
184
|
+
setSwitchWalletError(null)
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
// Use WalletSwitchingService for wallet switching logic (has mutex protection)
|
|
188
|
+
await WalletSwitchingService.switchToWallet(requestedWalletId!, {
|
|
189
|
+
autoStartWorklet: false,
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
if (!cancelled) {
|
|
193
|
+
setIsTemporaryWallet(false)
|
|
194
|
+
}
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (!cancelled) {
|
|
197
|
+
const err = normalizeErrorToError(error)
|
|
198
|
+
logError('[useWallet] Failed to switch wallet:', error)
|
|
199
|
+
setSwitchWalletError(err)
|
|
200
|
+
// Don't update activeWalletId if switch failed
|
|
201
|
+
}
|
|
202
|
+
} finally {
|
|
203
|
+
if (!cancelled) {
|
|
204
|
+
setIsSwitchingWallet(false)
|
|
205
|
+
setSwitchingToWalletId(null)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
switchWallet()
|
|
211
|
+
|
|
212
|
+
// Cleanup function to cancel in-flight operations
|
|
213
|
+
return () => {
|
|
214
|
+
cancelled = true
|
|
215
|
+
}
|
|
216
|
+
}, [options?.walletId, activeWalletId, isSwitchingWallet, switchingToWalletId])
|
|
217
|
+
|
|
218
|
+
// useShallow already provides stable references when content doesn't change
|
|
219
|
+
// We can use walletState.addresses and walletState.walletLoading directly
|
|
220
|
+
// No need to create new objects - useShallow handles reference stability
|
|
221
|
+
const addresses = walletState.addresses
|
|
222
|
+
const walletLoading = walletState.walletLoading
|
|
223
|
+
|
|
224
|
+
// Get all addresses for a specific network
|
|
225
|
+
// Use addresses directly from walletState (stable reference from useShallow)
|
|
226
|
+
const getNetworkAddresses = useCallback((network: string) => {
|
|
227
|
+
return addresses[network] || {}
|
|
228
|
+
}, [addresses])
|
|
229
|
+
|
|
230
|
+
// Check if an address is loading
|
|
231
|
+
// Use walletLoading directly from walletState (stable reference from useShallow)
|
|
232
|
+
const isLoadingAddress = useCallback((network: string, accountIndex: number = 0) => {
|
|
233
|
+
return walletLoading[`${network}-${accountIndex}`] || false
|
|
234
|
+
}, [walletLoading])
|
|
235
|
+
|
|
236
|
+
// Get a specific address (from cache or fetch)
|
|
237
|
+
const getAddress = useCallback(async (network: string, accountIndex: number = 0) => {
|
|
238
|
+
const walletId = targetWalletId || '__temporary__'
|
|
239
|
+
return AddressService.getAddress(network, accountIndex, walletId)
|
|
240
|
+
}, [targetWalletId])
|
|
241
|
+
|
|
242
|
+
// Call a method on a wallet account
|
|
243
|
+
const callAccountMethod = useCallback(async <T = unknown>(
|
|
244
|
+
network: string,
|
|
245
|
+
accountIndex: number,
|
|
246
|
+
methodName: string,
|
|
247
|
+
args?: unknown
|
|
248
|
+
): Promise<T> => {
|
|
249
|
+
const walletId = targetWalletId || '__temporary__'
|
|
250
|
+
return AccountService.callAccountMethod<T>(network, accountIndex, methodName, args, walletId)
|
|
251
|
+
}, [targetWalletId])
|
|
252
|
+
|
|
253
|
+
// Memoize the entire result object to ensure stable reference
|
|
254
|
+
// useShallow already provides stable references for addresses and walletLoading
|
|
255
|
+
// We memoize the result object to prevent creating new objects on every render
|
|
256
|
+
const result = useMemo(() => ({
|
|
257
|
+
// State (reactive) - useShallow ensures stable references
|
|
258
|
+
addresses,
|
|
259
|
+
walletLoading,
|
|
260
|
+
isInitialized,
|
|
261
|
+
// Switching state
|
|
262
|
+
isSwitchingWallet,
|
|
263
|
+
switchingToWalletId,
|
|
264
|
+
switchWalletError,
|
|
265
|
+
isTemporaryWallet,
|
|
266
|
+
// Computed helpers
|
|
267
|
+
getNetworkAddresses,
|
|
268
|
+
isLoadingAddress,
|
|
269
|
+
// Actions
|
|
270
|
+
getAddress,
|
|
271
|
+
callAccountMethod,
|
|
272
|
+
}), [
|
|
273
|
+
addresses,
|
|
274
|
+
walletLoading,
|
|
275
|
+
isInitialized,
|
|
276
|
+
isSwitchingWallet,
|
|
277
|
+
switchingToWalletId,
|
|
278
|
+
switchWalletError,
|
|
279
|
+
isTemporaryWallet,
|
|
280
|
+
getNetworkAddresses,
|
|
281
|
+
isLoadingAddress,
|
|
282
|
+
getAddress,
|
|
283
|
+
callAccountMethod,
|
|
284
|
+
]);
|
|
285
|
+
|
|
286
|
+
return result;
|
|
287
|
+
}
|
|
288
|
+
|