@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.
Files changed (275) hide show
  1. package/README.md +551 -0
  2. package/dist/__mocks__/react.d.ts +40 -0
  3. package/dist/__mocks__/react.js +33 -0
  4. package/dist/__mocks__/react.js.map +1 -0
  5. package/dist/__mocks__/secureStorage.d.ts +26 -0
  6. package/dist/__mocks__/secureStorage.js +80 -0
  7. package/dist/__mocks__/secureStorage.js.map +1 -0
  8. package/dist/__tests__/hooks/useBalance.test.d.ts +6 -0
  9. package/dist/__tests__/hooks/useBalance.test.js +217 -0
  10. package/dist/__tests__/hooks/useBalance.test.js.map +1 -0
  11. package/dist/__tests__/hooks/useWallet.test.d.ts +6 -0
  12. package/dist/__tests__/hooks/useWallet.test.js +105 -0
  13. package/dist/__tests__/hooks/useWallet.test.js.map +1 -0
  14. package/dist/__tests__/hooks/useWalletManager.test.d.ts +6 -0
  15. package/dist/__tests__/hooks/useWalletManager.test.js +193 -0
  16. package/dist/__tests__/hooks/useWalletManager.test.js.map +1 -0
  17. package/dist/__tests__/hooks/useWdkApp.test.d.ts +6 -0
  18. package/dist/__tests__/hooks/useWdkApp.test.js +55 -0
  19. package/dist/__tests__/hooks/useWdkApp.test.js.map +1 -0
  20. package/dist/__tests__/hooks/useWorklet.test.d.ts +6 -0
  21. package/dist/__tests__/hooks/useWorklet.test.js +168 -0
  22. package/dist/__tests__/hooks/useWorklet.test.js.map +1 -0
  23. package/dist/__tests__/provider/WdkAppProvider.test.d.ts +6 -0
  24. package/dist/__tests__/provider/WdkAppProvider.test.js +57 -0
  25. package/dist/__tests__/provider/WdkAppProvider.test.js.map +1 -0
  26. package/dist/__tests__/services/accountService.test.d.ts +6 -0
  27. package/dist/__tests__/services/accountService.test.js +216 -0
  28. package/dist/__tests__/services/accountService.test.js.map +1 -0
  29. package/dist/__tests__/services/addressService.test.d.ts +6 -0
  30. package/dist/__tests__/services/addressService.test.js +246 -0
  31. package/dist/__tests__/services/addressService.test.js.map +1 -0
  32. package/dist/__tests__/services/balanceService.test.d.ts +6 -0
  33. package/dist/__tests__/services/balanceService.test.js +333 -0
  34. package/dist/__tests__/services/balanceService.test.js.map +1 -0
  35. package/dist/__tests__/services/walletSetupService.test.d.ts +6 -0
  36. package/dist/__tests__/services/walletSetupService.test.js +304 -0
  37. package/dist/__tests__/services/walletSetupService.test.js.map +1 -0
  38. package/dist/__tests__/services/walletSwitchingService.test.d.ts +6 -0
  39. package/dist/__tests__/services/walletSwitchingService.test.js +209 -0
  40. package/dist/__tests__/services/walletSwitchingService.test.js.map +1 -0
  41. package/dist/__tests__/services/workletLifecycleService.test.d.ts +6 -0
  42. package/dist/__tests__/services/workletLifecycleService.test.js +451 -0
  43. package/dist/__tests__/services/workletLifecycleService.test.js.map +1 -0
  44. package/dist/__tests__/setup.d.ts +7 -0
  45. package/dist/__tests__/setup.js +49 -0
  46. package/dist/__tests__/setup.js.map +1 -0
  47. package/dist/__tests__/storage/mmkvStorage.test.d.ts +6 -0
  48. package/dist/__tests__/storage/mmkvStorage.test.js +213 -0
  49. package/dist/__tests__/storage/mmkvStorage.test.js.map +1 -0
  50. package/dist/__tests__/store/walletStore.test.d.ts +4 -0
  51. package/dist/__tests__/store/walletStore.test.js +86 -0
  52. package/dist/__tests__/store/walletStore.test.js.map +1 -0
  53. package/dist/__tests__/store/workletStore.test.d.ts +4 -0
  54. package/dist/__tests__/store/workletStore.test.js +255 -0
  55. package/dist/__tests__/store/workletStore.test.js.map +1 -0
  56. package/dist/__tests__/types/hrpc.test.d.ts +4 -0
  57. package/dist/__tests__/types/hrpc.test.js +99 -0
  58. package/dist/__tests__/types/hrpc.test.js.map +1 -0
  59. package/dist/__tests__/utils/balanceUtils.test.d.ts +4 -0
  60. package/dist/__tests__/utils/balanceUtils.test.js +133 -0
  61. package/dist/__tests__/utils/balanceUtils.test.js.map +1 -0
  62. package/dist/__tests__/utils/errorHandling.test.d.ts +6 -0
  63. package/dist/__tests__/utils/errorHandling.test.js +119 -0
  64. package/dist/__tests__/utils/errorHandling.test.js.map +1 -0
  65. package/dist/__tests__/utils/errorUtils.test.d.ts +4 -0
  66. package/dist/__tests__/utils/errorUtils.test.js +133 -0
  67. package/dist/__tests__/utils/errorUtils.test.js.map +1 -0
  68. package/dist/__tests__/utils/initializationState.test.d.ts +6 -0
  69. package/dist/__tests__/utils/initializationState.test.js +243 -0
  70. package/dist/__tests__/utils/initializationState.test.js.map +1 -0
  71. package/dist/__tests__/utils/jsonUtils.test.d.ts +4 -0
  72. package/dist/__tests__/utils/jsonUtils.test.js +118 -0
  73. package/dist/__tests__/utils/jsonUtils.test.js.map +1 -0
  74. package/dist/__tests__/utils/mmkvKeyManager.test.d.ts +6 -0
  75. package/dist/__tests__/utils/mmkvKeyManager.test.js +158 -0
  76. package/dist/__tests__/utils/mmkvKeyManager.test.js.map +1 -0
  77. package/dist/__tests__/utils/mnemonicUtils.test.d.ts +4 -0
  78. package/dist/__tests__/utils/mnemonicUtils.test.js +40 -0
  79. package/dist/__tests__/utils/mnemonicUtils.test.js.map +1 -0
  80. package/dist/__tests__/utils/raceConditions.test.d.ts +6 -0
  81. package/dist/__tests__/utils/raceConditions.test.js +239 -0
  82. package/dist/__tests__/utils/raceConditions.test.js.map +1 -0
  83. package/dist/__tests__/utils/result.test.d.ts +4 -0
  84. package/dist/__tests__/utils/result.test.js +66 -0
  85. package/dist/__tests__/utils/result.test.js.map +1 -0
  86. package/dist/__tests__/utils/schemas.test.d.ts +4 -0
  87. package/dist/__tests__/utils/schemas.test.js +66 -0
  88. package/dist/__tests__/utils/schemas.test.js.map +1 -0
  89. package/dist/__tests__/utils/storeHelpers.test.d.ts +4 -0
  90. package/dist/__tests__/utils/storeHelpers.test.js +227 -0
  91. package/dist/__tests__/utils/storeHelpers.test.js.map +1 -0
  92. package/dist/__tests__/utils/typeGuards.test.d.ts +4 -0
  93. package/dist/__tests__/utils/typeGuards.test.js +175 -0
  94. package/dist/__tests__/utils/typeGuards.test.js.map +1 -0
  95. package/dist/__tests__/utils/validation.test.d.ts +4 -0
  96. package/dist/__tests__/utils/validation.test.js +126 -0
  97. package/dist/__tests__/utils/validation.test.js.map +1 -0
  98. package/dist/__tests__/utils/walletUtils.test.d.ts +4 -0
  99. package/dist/__tests__/utils/walletUtils.test.js +156 -0
  100. package/dist/__tests__/utils/walletUtils.test.js.map +1 -0
  101. package/dist/hooks/useBalance.d.ts +170 -0
  102. package/dist/hooks/useBalance.js +442 -0
  103. package/dist/hooks/useBalance.js.map +1 -0
  104. package/dist/hooks/useWallet.d.ts +43 -0
  105. package/dist/hooks/useWallet.js +193 -0
  106. package/dist/hooks/useWallet.js.map +1 -0
  107. package/dist/hooks/useWalletManager.d.ts +100 -0
  108. package/dist/hooks/useWalletManager.js +526 -0
  109. package/dist/hooks/useWalletManager.js.map +1 -0
  110. package/dist/hooks/useWdkApp.d.ts +66 -0
  111. package/dist/hooks/useWdkApp.js +74 -0
  112. package/dist/hooks/useWdkApp.js.map +1 -0
  113. package/dist/hooks/useWorklet.d.ts +66 -0
  114. package/dist/hooks/useWorklet.js +42 -0
  115. package/dist/hooks/useWorklet.js.map +1 -0
  116. package/dist/index.d.ts +33 -0
  117. package/dist/index.js +36 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/provider/WdkAppProvider.d.ts +112 -0
  120. package/dist/provider/WdkAppProvider.js +558 -0
  121. package/dist/provider/WdkAppProvider.js.map +1 -0
  122. package/dist/services/accountService.d.ts +52 -0
  123. package/dist/services/accountService.js +119 -0
  124. package/dist/services/accountService.js.map +1 -0
  125. package/dist/services/addressService.d.ts +22 -0
  126. package/dist/services/addressService.js +110 -0
  127. package/dist/services/addressService.js.map +1 -0
  128. package/dist/services/balanceService.d.ts +114 -0
  129. package/dist/services/balanceService.js +208 -0
  130. package/dist/services/balanceService.js.map +1 -0
  131. package/dist/services/walletSetupService.d.ts +217 -0
  132. package/dist/services/walletSetupService.js +535 -0
  133. package/dist/services/walletSetupService.js.map +1 -0
  134. package/dist/services/walletSwitchingService.d.ts +48 -0
  135. package/dist/services/walletSwitchingService.js +143 -0
  136. package/dist/services/walletSwitchingService.js.map +1 -0
  137. package/dist/services/workletLifecycleService.d.ts +117 -0
  138. package/dist/services/workletLifecycleService.js +433 -0
  139. package/dist/services/workletLifecycleService.js.map +1 -0
  140. package/dist/storage/mmkvStorage.d.ts +44 -0
  141. package/dist/storage/mmkvStorage.js +348 -0
  142. package/dist/storage/mmkvStorage.js.map +1 -0
  143. package/dist/store/walletStore.d.ts +130 -0
  144. package/dist/store/walletStore.js +204 -0
  145. package/dist/store/walletStore.js.map +1 -0
  146. package/dist/store/workletStore.d.ts +121 -0
  147. package/dist/store/workletStore.js +256 -0
  148. package/dist/store/workletStore.js.map +1 -0
  149. package/dist/types/hrpc.d.ts +60 -0
  150. package/dist/types/hrpc.js +26 -0
  151. package/dist/types/hrpc.js.map +1 -0
  152. package/dist/types.d.ts +168 -0
  153. package/dist/types.js +7 -0
  154. package/dist/types.js.map +1 -0
  155. package/dist/utils/balanceUtils.d.ts +30 -0
  156. package/dist/utils/balanceUtils.js +86 -0
  157. package/dist/utils/balanceUtils.js.map +1 -0
  158. package/dist/utils/constants.d.ts +66 -0
  159. package/dist/utils/constants.js +73 -0
  160. package/dist/utils/constants.js.map +1 -0
  161. package/dist/utils/errorHandling.d.ts +47 -0
  162. package/dist/utils/errorHandling.js +54 -0
  163. package/dist/utils/errorHandling.js.map +1 -0
  164. package/dist/utils/errorUtils.d.ts +89 -0
  165. package/dist/utils/errorUtils.js +264 -0
  166. package/dist/utils/errorUtils.js.map +1 -0
  167. package/dist/utils/initializationState.d.ts +129 -0
  168. package/dist/utils/initializationState.js +220 -0
  169. package/dist/utils/initializationState.js.map +1 -0
  170. package/dist/utils/jsonUtils.d.ts +21 -0
  171. package/dist/utils/jsonUtils.js +67 -0
  172. package/dist/utils/jsonUtils.js.map +1 -0
  173. package/dist/utils/logger.d.ts +27 -0
  174. package/dist/utils/logger.js +50 -0
  175. package/dist/utils/logger.js.map +1 -0
  176. package/dist/utils/mmkvKeyManager.d.ts +57 -0
  177. package/dist/utils/mmkvKeyManager.js +214 -0
  178. package/dist/utils/mmkvKeyManager.js.map +1 -0
  179. package/dist/utils/mnemonicUtils.d.ts +17 -0
  180. package/dist/utils/mnemonicUtils.js +28 -0
  181. package/dist/utils/mnemonicUtils.js.map +1 -0
  182. package/dist/utils/operationMutex.d.ts +69 -0
  183. package/dist/utils/operationMutex.js +150 -0
  184. package/dist/utils/operationMutex.js.map +1 -0
  185. package/dist/utils/result.d.ts +34 -0
  186. package/dist/utils/result.js +43 -0
  187. package/dist/utils/result.js.map +1 -0
  188. package/dist/utils/schemas.d.ts +365 -0
  189. package/dist/utils/schemas.js +152 -0
  190. package/dist/utils/schemas.js.map +1 -0
  191. package/dist/utils/storeHelpers.d.ts +112 -0
  192. package/dist/utils/storeHelpers.js +198 -0
  193. package/dist/utils/storeHelpers.js.map +1 -0
  194. package/dist/utils/typeGuards.d.ts +70 -0
  195. package/dist/utils/typeGuards.js +95 -0
  196. package/dist/utils/typeGuards.js.map +1 -0
  197. package/dist/utils/validation.d.ts +53 -0
  198. package/dist/utils/validation.js +152 -0
  199. package/dist/utils/validation.js.map +1 -0
  200. package/dist/utils/walletStateHelpers.d.ts +62 -0
  201. package/dist/utils/walletStateHelpers.js +81 -0
  202. package/dist/utils/walletStateHelpers.js.map +1 -0
  203. package/dist/utils/walletUtils.d.ts +38 -0
  204. package/dist/utils/walletUtils.js +87 -0
  205. package/dist/utils/walletUtils.js.map +1 -0
  206. package/package.json +79 -0
  207. package/src/__mocks__/react.ts +34 -0
  208. package/src/__mocks__/secureStorage.ts +87 -0
  209. package/src/__tests__/hooks/useBalance.test.ts +286 -0
  210. package/src/__tests__/hooks/useWallet.test.ts +122 -0
  211. package/src/__tests__/hooks/useWalletManager.test.ts +271 -0
  212. package/src/__tests__/hooks/useWdkApp.test.tsx +59 -0
  213. package/src/__tests__/hooks/useWorklet.test.ts +195 -0
  214. package/src/__tests__/provider/WdkAppProvider.test.tsx +67 -0
  215. package/src/__tests__/services/accountService.test.ts +337 -0
  216. package/src/__tests__/services/addressService.test.ts +312 -0
  217. package/src/__tests__/services/balanceService.test.ts +411 -0
  218. package/src/__tests__/services/walletSetupService.test.ts +463 -0
  219. package/src/__tests__/services/walletSwitchingService.test.ts +258 -0
  220. package/src/__tests__/services/workletLifecycleService.test.ts +514 -0
  221. package/src/__tests__/setup.ts +54 -0
  222. package/src/__tests__/storage/mmkvStorage.test.ts +325 -0
  223. package/src/__tests__/store/walletStore.test.ts +101 -0
  224. package/src/__tests__/store/workletStore.test.ts +329 -0
  225. package/src/__tests__/types/hrpc.test.ts +125 -0
  226. package/src/__tests__/utils/balanceUtils.test.ts +151 -0
  227. package/src/__tests__/utils/errorHandling.test.ts +147 -0
  228. package/src/__tests__/utils/errorUtils.test.ts +170 -0
  229. package/src/__tests__/utils/initializationState.test.ts +337 -0
  230. package/src/__tests__/utils/jsonUtils.test.ts +131 -0
  231. package/src/__tests__/utils/mmkvKeyManager.test.ts +205 -0
  232. package/src/__tests__/utils/mnemonicUtils.test.ts +48 -0
  233. package/src/__tests__/utils/raceConditions.test.ts +292 -0
  234. package/src/__tests__/utils/result.test.ts +73 -0
  235. package/src/__tests__/utils/schemas.test.ts +82 -0
  236. package/src/__tests__/utils/storeHelpers.test.ts +323 -0
  237. package/src/__tests__/utils/typeGuards.test.ts +209 -0
  238. package/src/__tests__/utils/validation.test.ts +152 -0
  239. package/src/__tests__/utils/walletUtils.test.ts +195 -0
  240. package/src/hooks/useBalance.ts +597 -0
  241. package/src/hooks/useWallet.ts +288 -0
  242. package/src/hooks/useWalletManager.ts +654 -0
  243. package/src/hooks/useWdkApp.ts +78 -0
  244. package/src/hooks/useWorklet.ts +109 -0
  245. package/src/index.ts +112 -0
  246. package/src/provider/WdkAppProvider.tsx +764 -0
  247. package/src/services/accountService.ts +134 -0
  248. package/src/services/addressService.ts +125 -0
  249. package/src/services/balanceService.ts +291 -0
  250. package/src/services/walletSetupService.ts +695 -0
  251. package/src/services/walletSwitchingService.ts +157 -0
  252. package/src/services/workletLifecycleService.ts +561 -0
  253. package/src/storage/mmkvStorage.ts +417 -0
  254. package/src/store/walletStore.ts +301 -0
  255. package/src/store/workletStore.ts +340 -0
  256. package/src/types/hrpc.ts +78 -0
  257. package/src/types.ts +188 -0
  258. package/src/utils/balanceUtils.ts +96 -0
  259. package/src/utils/constants.ts +84 -0
  260. package/src/utils/errorHandling.md +163 -0
  261. package/src/utils/errorHandling.ts +61 -0
  262. package/src/utils/errorUtils.ts +300 -0
  263. package/src/utils/initializationState.ts +242 -0
  264. package/src/utils/jsonUtils.ts +75 -0
  265. package/src/utils/logger.ts +54 -0
  266. package/src/utils/mmkvKeyManager.ts +256 -0
  267. package/src/utils/mnemonicUtils.ts +35 -0
  268. package/src/utils/operationMutex.ts +181 -0
  269. package/src/utils/result.ts +57 -0
  270. package/src/utils/schemas.ts +190 -0
  271. package/src/utils/storeHelpers.ts +236 -0
  272. package/src/utils/typeGuards.ts +129 -0
  273. package/src/utils/validation.ts +174 -0
  274. package/src/utils/walletStateHelpers.ts +104 -0
  275. package/src/utils/walletUtils.ts +107 -0
@@ -0,0 +1,329 @@
1
+ /**
2
+ * Tests for workletStore
3
+ */
4
+
5
+ import {
6
+ createWorkletStore,
7
+ getWorkletStore,
8
+ resetWorkletStore,
9
+ getCachedCredentials,
10
+ setCachedCredentials,
11
+ clearCredentialsCache,
12
+ clearAllSensitiveData
13
+ } from '../../store/workletStore'
14
+
15
+ describe('workletStore', () => {
16
+ beforeEach(() => {
17
+ resetWorkletStore()
18
+ jest.clearAllMocks()
19
+ jest.useFakeTimers()
20
+ })
21
+
22
+ afterEach(() => {
23
+ resetWorkletStore()
24
+ jest.useRealTimers()
25
+ })
26
+
27
+ describe('createWorkletStore', () => {
28
+ it('should create a worklet store instance', () => {
29
+ const store = createWorkletStore()
30
+ expect(store).toBeDefined()
31
+ expect(typeof store.getState).toBe('function')
32
+ })
33
+
34
+ it('should return the same instance on subsequent calls', () => {
35
+ const store1 = createWorkletStore()
36
+ const store2 = createWorkletStore()
37
+ expect(store1).toBe(store2)
38
+ })
39
+
40
+ it('should initialize with default state', () => {
41
+ const store = createWorkletStore()
42
+ const state = store.getState()
43
+
44
+ expect(state.worklet).toBe(null)
45
+ expect(state.hrpc).toBe(null)
46
+ expect(state.ipc).toBe(null)
47
+ expect(state.isWorkletStarted).toBe(false)
48
+ expect(state.isInitialized).toBe(false)
49
+ expect(state.isLoading).toBe(false)
50
+ expect(state.error).toBe(null)
51
+ expect(state.encryptedSeed).toBe(null)
52
+ expect(state.encryptionKey).toBe(null)
53
+ expect(state.networkConfigs).toBe(null)
54
+ expect(state.workletStartResult).toBe(null)
55
+ expect(state.wdkInitResult).toBe(null)
56
+ expect(state.credentialsCache).toEqual({})
57
+ expect(state.credentialsCacheTTL).toBe(5 * 60 * 1000)
58
+ })
59
+ })
60
+
61
+ describe('getWorkletStore', () => {
62
+ it('should return a worklet store instance', () => {
63
+ const store = getWorkletStore()
64
+ expect(store).toBeDefined()
65
+ expect(typeof store.getState).toBe('function')
66
+ })
67
+
68
+ it('should return the same instance as createWorkletStore', () => {
69
+ const store1 = createWorkletStore()
70
+ const store2 = getWorkletStore()
71
+ expect(store1).toBe(store2)
72
+ })
73
+ })
74
+
75
+ describe('resetWorkletStore', () => {
76
+ it('should reset the store instance', () => {
77
+ const store1 = createWorkletStore()
78
+ resetWorkletStore()
79
+ const store2 = createWorkletStore()
80
+
81
+ // After reset, a new instance should be created
82
+ expect(store1).not.toBe(store2)
83
+ })
84
+ })
85
+
86
+ describe('clearSensitiveData', () => {
87
+ it('should clear encrypted seed and encryption key', () => {
88
+ const store = createWorkletStore()
89
+
90
+ store.setState({
91
+ encryptedSeed: 'encrypted-seed',
92
+ encryptionKey: 'encryption-key',
93
+ })
94
+
95
+ clearAllSensitiveData()
96
+
97
+ const state = store.getState()
98
+ expect(state.encryptedSeed).toBe(null)
99
+ expect(state.encryptionKey).toBe(null)
100
+ })
101
+
102
+ it('should not affect other state', () => {
103
+ const store = createWorkletStore()
104
+
105
+ store.setState({
106
+ isWorkletStarted: true,
107
+ encryptedSeed: 'encrypted-seed',
108
+ encryptionKey: 'encryption-key',
109
+ })
110
+
111
+ clearAllSensitiveData()
112
+
113
+ const state = store.getState()
114
+ expect(state.isWorkletStarted).toBe(true)
115
+ expect(state.encryptedSeed).toBe(null)
116
+ expect(state.encryptionKey).toBe(null)
117
+ })
118
+ })
119
+
120
+ describe('store state management', () => {
121
+ it('should allow state updates', () => {
122
+ const store = createWorkletStore()
123
+
124
+ store.setState({
125
+ isWorkletStarted: true,
126
+ isLoading: true,
127
+ })
128
+
129
+ const state = store.getState()
130
+ expect(state.isWorkletStarted).toBe(true)
131
+ expect(state.isLoading).toBe(true)
132
+ })
133
+ })
134
+
135
+ describe('getCachedCredentials', () => {
136
+ it('should return null for non-existent identifier', () => {
137
+ const result = getCachedCredentials('non-existent')
138
+ expect(result).toBe(null)
139
+ })
140
+
141
+ it('should return cached credentials when valid', () => {
142
+ const identifier = 'test-wallet'
143
+ const credentials = {
144
+ encryptionKey: 'key-123',
145
+ encryptedSeed: 'seed-123',
146
+ expiresAt: Date.now() + 10000
147
+ }
148
+
149
+ setCachedCredentials(identifier, credentials)
150
+ const result = getCachedCredentials(identifier)
151
+
152
+ expect(result).not.toBe(null)
153
+ expect(result?.encryptionKey).toBe('key-123')
154
+ expect(result?.encryptedSeed).toBe('seed-123')
155
+ })
156
+
157
+ it('should return null for expired credentials', () => {
158
+ const identifier = 'expired-wallet'
159
+ const store = getWorkletStore()
160
+ const currentTime = 1000000
161
+ jest.setSystemTime(currentTime)
162
+
163
+ // Set credentials first
164
+ setCachedCredentials(identifier, {
165
+ encryptionKey: 'key-123',
166
+ })
167
+
168
+ // Manually set expired time in the store (bypassing setCachedCredentials which always sets future time)
169
+ store.setState({
170
+ credentialsCache: {
171
+ ...store.getState().credentialsCache,
172
+ [identifier]: {
173
+ encryptionKey: 'key-123',
174
+ expiresAt: currentTime - 1000, // Expired (1 second in the past)
175
+ },
176
+ },
177
+ })
178
+
179
+ const result = getCachedCredentials(identifier)
180
+
181
+ expect(result).toBe(null)
182
+ })
183
+
184
+ it('should remove expired credentials from cache', () => {
185
+ const identifier = 'expired-wallet'
186
+ const store = getWorkletStore()
187
+ const currentTime = 1000000
188
+ jest.setSystemTime(currentTime)
189
+
190
+ // Set credentials first
191
+ setCachedCredentials(identifier, {
192
+ encryptionKey: 'key-123',
193
+ })
194
+
195
+ // Manually set expired time in the store (bypassing setCachedCredentials which always sets future time)
196
+ store.setState({
197
+ credentialsCache: {
198
+ ...store.getState().credentialsCache,
199
+ [identifier]: {
200
+ encryptionKey: 'key-123',
201
+ expiresAt: currentTime - 1000, // Expired (1 second in the past)
202
+ },
203
+ },
204
+ })
205
+
206
+ getCachedCredentials(identifier)
207
+
208
+ const state = store.getState()
209
+ expect(state.credentialsCache[identifier]).toBeUndefined()
210
+ })
211
+ })
212
+
213
+ describe('setCachedCredentials', () => {
214
+ it('should set credentials with expiration', () => {
215
+ const identifier = 'test-wallet'
216
+ const credentials = {
217
+ encryptionKey: 'key-123',
218
+ encryptedSeed: 'seed-123'
219
+ }
220
+
221
+ setCachedCredentials(identifier, credentials)
222
+ const result = getCachedCredentials(identifier)
223
+
224
+ expect(result).not.toBe(null)
225
+ expect(result?.encryptionKey).toBe('key-123')
226
+ expect(result?.encryptedSeed).toBe('seed-123')
227
+ expect(result?.expiresAt).toBeGreaterThan(Date.now())
228
+ })
229
+
230
+ it('should merge with existing credentials', () => {
231
+ const identifier = 'test-wallet'
232
+
233
+ setCachedCredentials(identifier, { encryptionKey: 'key-123' })
234
+ setCachedCredentials(identifier, { encryptedSeed: 'seed-123' })
235
+
236
+ const result = getCachedCredentials(identifier)
237
+ expect(result?.encryptionKey).toBe('key-123')
238
+ expect(result?.encryptedSeed).toBe('seed-123')
239
+ })
240
+
241
+ it('should update expiration on each set', () => {
242
+ const identifier = 'test-wallet'
243
+
244
+ setCachedCredentials(identifier, { encryptionKey: 'key-123' })
245
+ const firstExpiry = getCachedCredentials(identifier)?.expiresAt
246
+
247
+ // Advance time
248
+ jest.advanceTimersByTime(100)
249
+
250
+ setCachedCredentials(identifier, { encryptedSeed: 'seed-123' })
251
+ const secondExpiry = getCachedCredentials(identifier)?.expiresAt
252
+
253
+ expect(secondExpiry).toBeGreaterThan(firstExpiry!)
254
+ })
255
+ })
256
+
257
+ describe('clearCredentialsCache', () => {
258
+ it('should clear specific wallet credentials', () => {
259
+ const identifier1 = 'wallet-1'
260
+ const identifier2 = 'wallet-2'
261
+
262
+ setCachedCredentials(identifier1, { encryptionKey: 'key-1' })
263
+ setCachedCredentials(identifier2, { encryptionKey: 'key-2' })
264
+
265
+ clearCredentialsCache(identifier1)
266
+
267
+ expect(getCachedCredentials(identifier1)).toBe(null)
268
+ expect(getCachedCredentials(identifier2)).not.toBe(null)
269
+ })
270
+
271
+ it('should clear all credentials when no identifier provided', () => {
272
+ const identifier1 = 'wallet-1'
273
+ const identifier2 = 'wallet-2'
274
+
275
+ setCachedCredentials(identifier1, { encryptionKey: 'key-1' })
276
+ setCachedCredentials(identifier2, { encryptionKey: 'key-2' })
277
+
278
+ clearCredentialsCache()
279
+
280
+ expect(getCachedCredentials(identifier1)).toBe(null)
281
+ expect(getCachedCredentials(identifier2)).toBe(null)
282
+ })
283
+ })
284
+
285
+ describe('clearAllSensitiveData', () => {
286
+ it('should clear encrypted seed and encryption key', () => {
287
+ const store = createWorkletStore()
288
+
289
+ store.setState({
290
+ encryptedSeed: 'encrypted-seed',
291
+ encryptionKey: 'encryption-key',
292
+ })
293
+
294
+ clearAllSensitiveData()
295
+
296
+ const state = store.getState()
297
+ expect(state.encryptedSeed).toBe(null)
298
+ expect(state.encryptionKey).toBe(null)
299
+ })
300
+
301
+ it('should clear credentials cache', () => {
302
+ const identifier = 'test-wallet'
303
+
304
+ setCachedCredentials(identifier, { encryptionKey: 'key-123' })
305
+ clearAllSensitiveData()
306
+
307
+ expect(getCachedCredentials(identifier)).toBe(null)
308
+ })
309
+
310
+ it('should clear both active credentials and cache', () => {
311
+ const store = createWorkletStore()
312
+ const identifier = 'test-wallet'
313
+
314
+ store.setState({
315
+ encryptedSeed: 'encrypted-seed',
316
+ encryptionKey: 'encryption-key',
317
+ })
318
+ setCachedCredentials(identifier, { encryptionKey: 'key-123' })
319
+
320
+ clearAllSensitiveData()
321
+
322
+ const state = store.getState()
323
+ expect(state.encryptedSeed).toBe(null)
324
+ expect(state.encryptionKey).toBe(null)
325
+ expect(getCachedCredentials(identifier)).toBe(null)
326
+ })
327
+ })
328
+ })
329
+
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Tests for HRPC type guards
3
+ */
4
+
5
+ import { isExtendedHRPC, asExtendedHRPC } from '../../types/hrpc'
6
+ import type { HRPC } from '@spacesops/pear-wrk-wdk'
7
+
8
+ describe('hrpc', () => {
9
+ describe('isExtendedHRPC', () => {
10
+ it('should return true for HRPC with all extended methods', () => {
11
+ const hrpc = {
12
+ callMethod: jest.fn(),
13
+ initializeWDK: jest.fn(),
14
+ generateEntropyAndEncrypt: jest.fn(),
15
+ getMnemonicFromEntropy: jest.fn(),
16
+ getSeedAndEntropyFromMnemonic: jest.fn(),
17
+ } as unknown as HRPC
18
+
19
+ expect(isExtendedHRPC(hrpc)).toBe(true)
20
+ })
21
+
22
+ it('should return false for HRPC without initializeWDK', () => {
23
+ const hrpc = {
24
+ callMethod: jest.fn(),
25
+ generateEntropyAndEncrypt: jest.fn(),
26
+ getMnemonicFromEntropy: jest.fn(),
27
+ getSeedAndEntropyFromMnemonic: jest.fn(),
28
+ } as unknown as HRPC
29
+
30
+ expect(isExtendedHRPC(hrpc)).toBe(false)
31
+ })
32
+
33
+ it('should return false for HRPC without generateEntropyAndEncrypt', () => {
34
+ const hrpc = {
35
+ callMethod: jest.fn(),
36
+ initializeWDK: jest.fn(),
37
+ getMnemonicFromEntropy: jest.fn(),
38
+ getSeedAndEntropyFromMnemonic: jest.fn(),
39
+ } as unknown as HRPC
40
+
41
+ expect(isExtendedHRPC(hrpc)).toBe(false)
42
+ })
43
+
44
+ it('should return false for HRPC without getMnemonicFromEntropy', () => {
45
+ const hrpc = {
46
+ callMethod: jest.fn(),
47
+ initializeWDK: jest.fn(),
48
+ generateEntropyAndEncrypt: jest.fn(),
49
+ getSeedAndEntropyFromMnemonic: jest.fn(),
50
+ } as unknown as HRPC
51
+
52
+ expect(isExtendedHRPC(hrpc)).toBe(false)
53
+ })
54
+
55
+ it('should return false for HRPC without getSeedAndEntropyFromMnemonic', () => {
56
+ const hrpc = {
57
+ callMethod: jest.fn(),
58
+ initializeWDK: jest.fn(),
59
+ generateEntropyAndEncrypt: jest.fn(),
60
+ getMnemonicFromEntropy: jest.fn(),
61
+ } as unknown as HRPC
62
+
63
+ expect(isExtendedHRPC(hrpc)).toBe(false)
64
+ })
65
+
66
+ it('should return false for HRPC with non-function methods', () => {
67
+ const hrpc = {
68
+ callMethod: jest.fn(),
69
+ initializeWDK: 'not a function',
70
+ generateEntropyAndEncrypt: jest.fn(),
71
+ getMnemonicFromEntropy: jest.fn(),
72
+ getSeedAndEntropyFromMnemonic: jest.fn(),
73
+ } as unknown as HRPC
74
+
75
+ expect(isExtendedHRPC(hrpc)).toBe(false)
76
+ })
77
+
78
+ it('should return false for basic HRPC', () => {
79
+ const hrpc = {
80
+ callMethod: jest.fn(),
81
+ } as unknown as HRPC
82
+
83
+ expect(isExtendedHRPC(hrpc)).toBe(false)
84
+ })
85
+ })
86
+
87
+ describe('asExtendedHRPC', () => {
88
+ it('should return HRPC when it has all extended methods', () => {
89
+ const hrpc = {
90
+ callMethod: jest.fn(),
91
+ initializeWDK: jest.fn(),
92
+ generateEntropyAndEncrypt: jest.fn(),
93
+ getMnemonicFromEntropy: jest.fn(),
94
+ getSeedAndEntropyFromMnemonic: jest.fn(),
95
+ } as unknown as HRPC
96
+
97
+ const result = asExtendedHRPC(hrpc)
98
+ expect(result).toBe(hrpc)
99
+ })
100
+
101
+ it('should throw error when HRPC does not have required methods', () => {
102
+ const hrpc = {
103
+ callMethod: jest.fn(),
104
+ } as unknown as HRPC
105
+
106
+ expect(() => asExtendedHRPC(hrpc)).toThrow(
107
+ 'HRPC instance does not have required extended methods'
108
+ )
109
+ })
110
+
111
+ it('should throw error when HRPC is missing initializeWDK', () => {
112
+ const hrpc = {
113
+ callMethod: jest.fn(),
114
+ generateEntropyAndEncrypt: jest.fn(),
115
+ getMnemonicFromEntropy: jest.fn(),
116
+ getSeedAndEntropyFromMnemonic: jest.fn(),
117
+ } as unknown as HRPC
118
+
119
+ expect(() => asExtendedHRPC(hrpc)).toThrow(
120
+ 'HRPC instance does not have required extended methods'
121
+ )
122
+ })
123
+ })
124
+ })
125
+
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Tests for balance utility functions
3
+ */
4
+
5
+ import { convertBalanceToString, formatBalance, convertBigIntToString } from '../../utils/balanceUtils'
6
+
7
+ describe('balanceUtils', () => {
8
+ describe('convertBalanceToString', () => {
9
+ it('should convert BigInt to string', () => {
10
+ expect(convertBalanceToString(BigInt(100))).toBe('100')
11
+ expect(convertBalanceToString(BigInt('1000000000000000000'))).toBe('1000000000000000000')
12
+ })
13
+
14
+ it('should return string as-is', () => {
15
+ expect(convertBalanceToString('100')).toBe('100')
16
+ expect(convertBalanceToString('0')).toBe('0')
17
+ })
18
+
19
+ it('should convert number to string', () => {
20
+ expect(convertBalanceToString(100)).toBe('100')
21
+ expect(convertBalanceToString(0)).toBe('0')
22
+ })
23
+
24
+ it('should convert other types to string', () => {
25
+ expect(convertBalanceToString(null)).toBe('null')
26
+ expect(convertBalanceToString(undefined)).toBe('undefined')
27
+ expect(convertBalanceToString(true)).toBe('true')
28
+ })
29
+ })
30
+
31
+ describe('formatBalance', () => {
32
+ it('should format balance with 18 decimals', () => {
33
+ expect(formatBalance('1000000000000000000', 18)).toBe('1')
34
+ expect(formatBalance('1500000000000000000', 18)).toBe('1.5')
35
+ expect(formatBalance('100000000000000000', 18)).toBe('0.1')
36
+ expect(formatBalance('123456789000000000', 18)).toBe('0.123456789')
37
+ })
38
+
39
+ it('should format balance with 6 decimals', () => {
40
+ expect(formatBalance('1000000', 6)).toBe('1')
41
+ expect(formatBalance('1500000', 6)).toBe('1.5')
42
+ expect(formatBalance('1000001', 6)).toBe('1.000001')
43
+ })
44
+
45
+ it('should handle zero balance', () => {
46
+ expect(formatBalance('0', 18)).toBe('0')
47
+ expect(formatBalance(null, 18)).toBe('0')
48
+ expect(formatBalance('null', 18)).toBe('0')
49
+ })
50
+
51
+ it('should remove trailing zeros', () => {
52
+ expect(formatBalance('1000000000000000000', 18)).toBe('1')
53
+ expect(formatBalance('10000000000000000000', 18)).toBe('10')
54
+ })
55
+
56
+ it('should handle large balances', () => {
57
+ expect(formatBalance('1000000000000000000000', 18)).toBe('1000')
58
+ expect(formatBalance('1000000000000000000000000', 18)).toBe('1000000')
59
+ })
60
+
61
+ it('should handle invalid balance gracefully', () => {
62
+ expect(formatBalance('invalid', 18)).toBe('invalid')
63
+ expect(formatBalance('', 18)).toBe('0')
64
+ })
65
+ })
66
+
67
+ describe('convertBigIntToString', () => {
68
+ it('should convert BigInt to string', () => {
69
+ expect(convertBigIntToString(BigInt(100))).toBe('100')
70
+ expect(convertBigIntToString(BigInt('1000000000000000000'))).toBe('1000000000000000000')
71
+ })
72
+
73
+ it('should convert BigInt in arrays', () => {
74
+ const input = [BigInt(100), BigInt(200), 'string', 42]
75
+ const result = convertBigIntToString(input)
76
+ expect(result).toEqual(['100', '200', 'string', 42])
77
+ })
78
+
79
+ it('should convert BigInt in nested arrays', () => {
80
+ const input = [[BigInt(100)], [BigInt(200), 'test']]
81
+ const result = convertBigIntToString(input)
82
+ expect(result).toEqual([['100'], ['200', 'test']])
83
+ })
84
+
85
+ it('should convert BigInt in objects', () => {
86
+ const input = {
87
+ balance: BigInt(100),
88
+ amount: BigInt(200),
89
+ name: 'test',
90
+ count: 42,
91
+ }
92
+ const result = convertBigIntToString(input)
93
+ expect(result).toEqual({
94
+ balance: '100',
95
+ amount: '200',
96
+ name: 'test',
97
+ count: 42,
98
+ })
99
+ })
100
+
101
+ it('should convert BigInt in nested objects', () => {
102
+ const input = {
103
+ data: {
104
+ balance: BigInt(100),
105
+ nested: {
106
+ amount: BigInt(200),
107
+ },
108
+ },
109
+ }
110
+ const result = convertBigIntToString(input)
111
+ expect(result).toEqual({
112
+ data: {
113
+ balance: '100',
114
+ nested: {
115
+ amount: '200',
116
+ },
117
+ },
118
+ })
119
+ })
120
+
121
+ it('should convert BigInt in mixed arrays and objects', () => {
122
+ const input = {
123
+ balances: [BigInt(100), BigInt(200)],
124
+ data: {
125
+ amounts: [BigInt(300), BigInt(400)],
126
+ },
127
+ }
128
+ const result = convertBigIntToString(input)
129
+ expect(result).toEqual({
130
+ balances: ['100', '200'],
131
+ data: {
132
+ amounts: ['300', '400'],
133
+ },
134
+ })
135
+ })
136
+
137
+ it('should return primitives as-is', () => {
138
+ expect(convertBigIntToString('string')).toBe('string')
139
+ expect(convertBigIntToString(42)).toBe(42)
140
+ expect(convertBigIntToString(null)).toBe(null)
141
+ expect(convertBigIntToString(undefined)).toBe(undefined)
142
+ expect(convertBigIntToString(true)).toBe(true)
143
+ expect(convertBigIntToString(false)).toBe(false)
144
+ })
145
+
146
+ it('should handle empty arrays and objects', () => {
147
+ expect(convertBigIntToString([])).toEqual([])
148
+ expect(convertBigIntToString({})).toEqual({})
149
+ })
150
+ })
151
+ })