@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,73 @@
1
+ /**
2
+ * Tests for Result type utilities
3
+ */
4
+
5
+ import { ok, err, toResult, toResultSync } from '../../utils/result'
6
+
7
+ describe('result', () => {
8
+ describe('ok', () => {
9
+ it('should create a success result', () => {
10
+ const result = ok('test data')
11
+ expect(result.success).toBe(true)
12
+ expect(result.data).toBe('test data')
13
+ expect(result.error).toBeUndefined()
14
+ })
15
+ })
16
+
17
+ describe('err', () => {
18
+ it('should create an error result', () => {
19
+ const error = new Error('test error')
20
+ const result = err(error)
21
+ expect(result.success).toBe(false)
22
+ expect(result.error).toBe(error)
23
+ expect(result.data).toBeUndefined()
24
+ })
25
+ })
26
+
27
+ describe('toResult', () => {
28
+ it('should return ok result for successful async function', async () => {
29
+ const fn = async () => 'success'
30
+ const result = await toResult(fn)
31
+ expect(result.success).toBe(true)
32
+ if (result.success) {
33
+ expect(result.data).toBe('success')
34
+ }
35
+ })
36
+
37
+ it('should return err result for failed async function', async () => {
38
+ const fn = async () => {
39
+ throw new Error('async error')
40
+ }
41
+ const result = await toResult(fn)
42
+ expect(result.success).toBe(false)
43
+ if (!result.success) {
44
+ expect(result.error).toBeInstanceOf(Error)
45
+ expect((result.error as Error).message).toBe('async error')
46
+ }
47
+ })
48
+ })
49
+
50
+ describe('toResultSync', () => {
51
+ it('should return ok result for successful sync function', () => {
52
+ const fn = () => 'success'
53
+ const result = toResultSync(fn)
54
+ expect(result.success).toBe(true)
55
+ if (result.success) {
56
+ expect(result.data).toBe('success')
57
+ }
58
+ })
59
+
60
+ it('should return err result for failed sync function', () => {
61
+ const fn = () => {
62
+ throw new Error('sync error')
63
+ }
64
+ const result = toResultSync(fn)
65
+ expect(result.success).toBe(false)
66
+ if (!result.success) {
67
+ expect(result.error).toBeInstanceOf(Error)
68
+ expect((result.error as Error).message).toBe('sync error')
69
+ }
70
+ })
71
+ })
72
+ })
73
+
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Tests for Zod schemas
3
+ */
4
+
5
+ import {
6
+ workletResponseSchema,
7
+ balanceResponseSchema,
8
+ accountMethodResponseSchema,
9
+ } from '../../utils/schemas'
10
+
11
+ describe('schemas', () => {
12
+ describe('workletResponseSchema', () => {
13
+ it('should validate valid worklet response', () => {
14
+ const valid = {
15
+ result: '{"data": "test"}',
16
+ }
17
+ expect(() => workletResponseSchema.parse(valid)).not.toThrow()
18
+ })
19
+
20
+ it('should validate worklet response with error', () => {
21
+ const valid = {
22
+ result: '{"data": "test"}',
23
+ error: 'Some error',
24
+ }
25
+ expect(() => workletResponseSchema.parse(valid)).not.toThrow()
26
+ })
27
+
28
+ it('should reject invalid worklet response', () => {
29
+ const invalid = {
30
+ result: null,
31
+ }
32
+ expect(() => workletResponseSchema.parse(invalid)).toThrow()
33
+ })
34
+
35
+ it('should require result field', () => {
36
+ const invalid = {}
37
+ expect(() => workletResponseSchema.parse(invalid)).toThrow()
38
+ })
39
+ })
40
+
41
+ describe('balanceResponseSchema', () => {
42
+ it('should validate numeric string', () => {
43
+ expect(() => balanceResponseSchema.parse('1000000000000000000')).not.toThrow()
44
+ expect(() => balanceResponseSchema.parse('0')).not.toThrow()
45
+ expect(() => balanceResponseSchema.parse('123456789')).not.toThrow()
46
+ })
47
+
48
+ it('should reject non-numeric strings', () => {
49
+ expect(() => balanceResponseSchema.parse('abc')).toThrow()
50
+ expect(() => balanceResponseSchema.parse('123.45')).toThrow()
51
+ expect(() => balanceResponseSchema.parse('-123')).toThrow()
52
+ expect(() => balanceResponseSchema.parse('')).toThrow()
53
+ })
54
+
55
+ it('should reject non-string values', () => {
56
+ expect(() => balanceResponseSchema.parse(123)).toThrow()
57
+ expect(() => balanceResponseSchema.parse(null)).toThrow()
58
+ expect(() => balanceResponseSchema.parse(undefined)).toThrow()
59
+ })
60
+ })
61
+
62
+ describe('accountMethodResponseSchema', () => {
63
+ it('should validate balance response', () => {
64
+ expect(() => accountMethodResponseSchema.parse('1000000000000000000')).not.toThrow()
65
+ })
66
+
67
+ it('should validate string response', () => {
68
+ expect(() => accountMethodResponseSchema.parse('0x1234567890123456789012345678901234567890')).not.toThrow()
69
+ expect(() => accountMethodResponseSchema.parse('some string')).not.toThrow()
70
+ })
71
+
72
+ it('should validate object response', () => {
73
+ expect(() => accountMethodResponseSchema.parse({ data: 'test' })).not.toThrow()
74
+ expect(() => accountMethodResponseSchema.parse({ nested: { value: 123 } })).not.toThrow()
75
+ })
76
+
77
+ it('should validate empty object', () => {
78
+ expect(() => accountMethodResponseSchema.parse({})).not.toThrow()
79
+ })
80
+ })
81
+ })
82
+
@@ -0,0 +1,323 @@
1
+ /**
2
+ * Tests for store helper utilities
3
+ */
4
+
5
+ import {
6
+ requireInitialized,
7
+ requireExtendedHRPC,
8
+ isInitialized,
9
+ updateBalanceInState,
10
+ updateAddressInState,
11
+ } from '../../utils/storeHelpers'
12
+ import { getWorkletStore } from '../../store/workletStore'
13
+ import { asExtendedHRPC } from '../../types/hrpc'
14
+
15
+ // Mock stores
16
+ jest.mock('../../store/workletStore', () => ({
17
+ getWorkletStore: jest.fn(),
18
+ }))
19
+
20
+ jest.mock('../../types/hrpc', () => ({
21
+ asExtendedHRPC: jest.fn(),
22
+ }))
23
+
24
+ describe('storeHelpers', () => {
25
+ let mockWorkletStore: any
26
+ let mockHRPC: any
27
+ let mockExtendedHRPC: any
28
+
29
+ beforeEach(() => {
30
+ jest.clearAllMocks()
31
+
32
+ mockHRPC = {
33
+ callMethod: jest.fn(),
34
+ }
35
+
36
+ mockExtendedHRPC = {
37
+ callMethod: jest.fn(),
38
+ initializeWDK: jest.fn(),
39
+ }
40
+
41
+ mockWorkletStore = {
42
+ getState: jest.fn(() => ({
43
+ isInitialized: true,
44
+ hrpc: mockHRPC,
45
+ })),
46
+ }
47
+
48
+ ;(getWorkletStore as jest.Mock).mockReturnValue(mockWorkletStore)
49
+ ;(asExtendedHRPC as jest.Mock).mockReturnValue(mockExtendedHRPC)
50
+ })
51
+
52
+ describe('requireInitialized', () => {
53
+ it('should return HRPC when initialized', () => {
54
+ const hrpc = requireInitialized()
55
+ expect(hrpc).toBe(mockHRPC)
56
+ expect(getWorkletStore).toHaveBeenCalled()
57
+ })
58
+
59
+ it('should throw error when not initialized', () => {
60
+ mockWorkletStore.getState = jest.fn(() => ({
61
+ isInitialized: false,
62
+ hrpc: null,
63
+ }))
64
+
65
+ expect(() => requireInitialized()).toThrow('WDK not initialized')
66
+ })
67
+
68
+ it('should throw error when HRPC is null', () => {
69
+ mockWorkletStore.getState = jest.fn(() => ({
70
+ isInitialized: true,
71
+ hrpc: null,
72
+ }))
73
+
74
+ expect(() => requireInitialized()).toThrow('WDK not initialized')
75
+ })
76
+ })
77
+
78
+ describe('requireExtendedHRPC', () => {
79
+ it('should return extended HRPC when initialized', () => {
80
+ const hrpc = requireExtendedHRPC()
81
+ expect(hrpc).toBe(mockExtendedHRPC)
82
+ expect(asExtendedHRPC).toHaveBeenCalledWith(mockHRPC)
83
+ })
84
+
85
+ it('should throw error when not initialized', () => {
86
+ mockWorkletStore.getState = jest.fn(() => ({
87
+ isInitialized: false,
88
+ hrpc: null,
89
+ }))
90
+
91
+ expect(() => requireExtendedHRPC()).toThrow('WDK not initialized')
92
+ })
93
+ })
94
+
95
+ describe('isInitialized', () => {
96
+ it('should return true when initialized', () => {
97
+ expect(isInitialized()).toBe(true)
98
+ })
99
+
100
+ it('should return false when not initialized', () => {
101
+ mockWorkletStore.getState = jest.fn(() => ({
102
+ isInitialized: false,
103
+ hrpc: null,
104
+ }))
105
+
106
+ expect(isInitialized()).toBe(false)
107
+ })
108
+
109
+ it('should return false when HRPC is null', () => {
110
+ mockWorkletStore.getState = jest.fn(() => ({
111
+ isInitialized: true,
112
+ hrpc: null,
113
+ }))
114
+
115
+ expect(isInitialized()).toBe(false)
116
+ })
117
+ })
118
+
119
+ describe('updateBalanceInState', () => {
120
+ it('should update balance in empty state', () => {
121
+ const prev = {
122
+ balances: {},
123
+ } as any
124
+
125
+ const result = updateBalanceInState(prev, 'wallet-1', 'ethereum', 0, 'native', '100')
126
+
127
+ expect(result.balances).toEqual({
128
+ 'wallet-1': {
129
+ ethereum: {
130
+ 0: {
131
+ native: '100',
132
+ },
133
+ },
134
+ },
135
+ })
136
+ })
137
+
138
+ it('should update balance in existing state', () => {
139
+ const prev = {
140
+ balances: {
141
+ 'wallet-1': {
142
+ ethereum: {
143
+ 0: {
144
+ native: '50',
145
+ },
146
+ },
147
+ },
148
+ },
149
+ } as any
150
+
151
+ const result = updateBalanceInState(prev, 'wallet-1', 'ethereum', 0, 'native', '100')
152
+
153
+ expect(result.balances?.['wallet-1']?.ethereum?.[0]?.native).toBe('100')
154
+ })
155
+
156
+ it('should add new network balance', () => {
157
+ const prev = {
158
+ balances: {
159
+ 'wallet-1': {
160
+ polygon: {
161
+ 0: {
162
+ native: '50',
163
+ },
164
+ },
165
+ },
166
+ },
167
+ } as any
168
+
169
+ const result = updateBalanceInState(prev, 'wallet-1', 'ethereum', 0, 'native', '100')
170
+
171
+ expect(result.balances?.['wallet-1']?.ethereum?.[0]?.native).toBe('100')
172
+ expect(result.balances?.['wallet-1']?.polygon?.[0]?.native).toBe('50')
173
+ })
174
+
175
+ it('should add new account balance', () => {
176
+ const prev = {
177
+ balances: {
178
+ 'wallet-1': {
179
+ ethereum: {
180
+ 0: {
181
+ native: '50',
182
+ },
183
+ },
184
+ },
185
+ },
186
+ } as any
187
+
188
+ const result = updateBalanceInState(prev, 'wallet-1', 'ethereum', 1, 'native', '200')
189
+
190
+ expect(result.balances?.['wallet-1']?.ethereum?.[0]?.native).toBe('50')
191
+ expect(result.balances?.['wallet-1']?.ethereum?.[1]?.native).toBe('200')
192
+ })
193
+
194
+ it('should add token balance', () => {
195
+ const prev = {
196
+ balances: {
197
+ 'wallet-1': {
198
+ ethereum: {
199
+ 0: {
200
+ native: '50',
201
+ },
202
+ },
203
+ },
204
+ },
205
+ } as any
206
+
207
+ const result = updateBalanceInState(
208
+ prev,
209
+ 'wallet-1',
210
+ 'ethereum',
211
+ 0,
212
+ '0x123',
213
+ '1000'
214
+ )
215
+
216
+ expect(result.balances?.['wallet-1']?.ethereum?.[0]?.native).toBe('50')
217
+ expect(result.balances?.['wallet-1']?.ethereum?.[0]?.['0x123']).toBe('1000')
218
+ })
219
+ })
220
+
221
+ describe('updateAddressInState', () => {
222
+ it('should update address in empty state', () => {
223
+ const prev = {
224
+ addresses: {},
225
+ } as any
226
+
227
+ const result = updateAddressInState(
228
+ prev,
229
+ 'wallet-1',
230
+ 'ethereum',
231
+ 0,
232
+ '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
233
+ )
234
+
235
+ expect(result.addresses).toEqual({
236
+ 'wallet-1': {
237
+ ethereum: {
238
+ 0: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0',
239
+ },
240
+ },
241
+ })
242
+ })
243
+
244
+ it('should update address in existing state', () => {
245
+ const prev = {
246
+ addresses: {
247
+ 'wallet-1': {
248
+ ethereum: {
249
+ 0: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0',
250
+ },
251
+ },
252
+ },
253
+ } as any
254
+
255
+ const result = updateAddressInState(
256
+ prev,
257
+ 'wallet-1',
258
+ 'ethereum',
259
+ 0,
260
+ '0x842d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
261
+ )
262
+
263
+ expect(result.addresses?.['wallet-1']?.ethereum?.[0]).toBe(
264
+ '0x842d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
265
+ )
266
+ })
267
+
268
+ it('should add new network address', () => {
269
+ const prev = {
270
+ addresses: {
271
+ 'wallet-1': {
272
+ polygon: {
273
+ 0: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0',
274
+ },
275
+ },
276
+ },
277
+ } as any
278
+
279
+ const result = updateAddressInState(
280
+ prev,
281
+ 'wallet-1',
282
+ 'ethereum',
283
+ 0,
284
+ '0x842d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
285
+ )
286
+
287
+ expect(result.addresses?.['wallet-1']?.ethereum?.[0]).toBe(
288
+ '0x842d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
289
+ )
290
+ expect(result.addresses?.['wallet-1']?.polygon?.[0]).toBe(
291
+ '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
292
+ )
293
+ })
294
+
295
+ it('should add new account address', () => {
296
+ const prev = {
297
+ addresses: {
298
+ 'wallet-1': {
299
+ ethereum: {
300
+ 0: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0',
301
+ },
302
+ },
303
+ },
304
+ } as any
305
+
306
+ const result = updateAddressInState(
307
+ prev,
308
+ 'wallet-1',
309
+ 'ethereum',
310
+ 1,
311
+ '0x842d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
312
+ )
313
+
314
+ expect(result.addresses?.['wallet-1']?.ethereum?.[0]).toBe(
315
+ '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
316
+ )
317
+ expect(result.addresses?.['wallet-1']?.ethereum?.[1]).toBe(
318
+ '0x842d35Cc6634C0532925a3b844Bc9e7595f0bEb0'
319
+ )
320
+ })
321
+ })
322
+ })
323
+
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Tests for type guard utilities
3
+ */
4
+
5
+ import {
6
+ isNetworkConfig,
7
+ isNetworkConfigs,
8
+ isTokenConfig,
9
+ isTokenConfigs,
10
+ isWalletAddresses,
11
+ isWalletBalances,
12
+ isEthereumAddress,
13
+ isValidAccountIndex,
14
+ isValidNetworkName,
15
+ isValidBalanceString,
16
+ } from '../../utils/typeGuards'
17
+ import type { NetworkConfig, NetworkConfigs, TokenConfig, TokenConfigs } from '../../types'
18
+
19
+ describe('typeGuards', () => {
20
+ describe('isNetworkConfig', () => {
21
+ it('should return true for valid network config', () => {
22
+ const valid: NetworkConfig = {
23
+ chainId: 1,
24
+ blockchain: 'ethereum',
25
+ }
26
+ expect(isNetworkConfig(valid)).toBe(true)
27
+ })
28
+
29
+ it('should return false for invalid network config', () => {
30
+ expect(isNetworkConfig(null)).toBe(false)
31
+ expect(isNetworkConfig({})).toBe(false)
32
+ expect(isNetworkConfig({ chainId: '1', blockchain: 'ethereum' })).toBe(false)
33
+ expect(isNetworkConfig({ chainId: 1 })).toBe(false)
34
+ expect(isNetworkConfig({ blockchain: 'ethereum' })).toBe(false)
35
+ })
36
+ })
37
+
38
+ describe('isNetworkConfigs', () => {
39
+ it('should return true for valid network configs', () => {
40
+ const valid: NetworkConfigs = {
41
+ ethereum: {
42
+ chainId: 1,
43
+ blockchain: 'ethereum',
44
+ },
45
+ }
46
+ expect(isNetworkConfigs(valid)).toBe(true)
47
+ })
48
+
49
+ it('should return false for invalid network configs', () => {
50
+ expect(isNetworkConfigs(null)).toBe(false)
51
+ expect(isNetworkConfigs({})).toBe(false)
52
+ expect(isNetworkConfigs([])).toBe(false)
53
+ expect(isNetworkConfigs({ ethereum: null })).toBe(false)
54
+ })
55
+ })
56
+
57
+ describe('isTokenConfig', () => {
58
+ it('should return true for valid token config', () => {
59
+ const valid: TokenConfig = {
60
+ symbol: 'ETH',
61
+ name: 'Ethereum',
62
+ decimals: 18,
63
+ address: null,
64
+ }
65
+ expect(isTokenConfig(valid)).toBe(true)
66
+
67
+ const validWithAddress: TokenConfig = {
68
+ symbol: 'USDT',
69
+ name: 'Tether',
70
+ decimals: 6,
71
+ address: '0x1234567890123456789012345678901234567890',
72
+ }
73
+ expect(isTokenConfig(validWithAddress)).toBe(true)
74
+ })
75
+
76
+ it('should return false for invalid token config', () => {
77
+ expect(isTokenConfig(null)).toBe(false)
78
+ expect(isTokenConfig({})).toBe(false)
79
+ expect(isTokenConfig({ symbol: 'ETH' })).toBe(false)
80
+ expect(isTokenConfig({ symbol: 'ETH', name: 'Ethereum', decimals: '18' })).toBe(false)
81
+ })
82
+ })
83
+
84
+ describe('isTokenConfigs', () => {
85
+ it('should return true for valid token configs', () => {
86
+ const valid: TokenConfigs = {
87
+ ethereum: {
88
+ native: {
89
+ symbol: 'ETH',
90
+ name: 'Ethereum',
91
+ decimals: 18,
92
+ address: null,
93
+ },
94
+ tokens: [],
95
+ },
96
+ }
97
+ expect(isTokenConfigs(valid)).toBe(true)
98
+ })
99
+
100
+ it('should return false for invalid token configs', () => {
101
+ expect(isTokenConfigs(null)).toBe(false)
102
+ expect(isTokenConfigs({})).toBe(false)
103
+ expect(isTokenConfigs([])).toBe(false)
104
+ })
105
+ })
106
+
107
+ describe('isWalletAddresses', () => {
108
+ it('should return true for valid wallet addresses', () => {
109
+ const valid = {
110
+ ethereum: {
111
+ 0: '0x1234567890123456789012345678901234567890',
112
+ 1: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
113
+ },
114
+ }
115
+ expect(isWalletAddresses(valid)).toBe(true)
116
+ })
117
+
118
+ it('should return false for invalid wallet addresses', () => {
119
+ expect(isWalletAddresses(null)).toBe(false)
120
+ expect(isWalletAddresses({})).toBe(true) // Empty object is valid
121
+ expect(isWalletAddresses([])).toBe(false)
122
+ expect(isWalletAddresses({ ethereum: 'invalid' })).toBe(false)
123
+ expect(isWalletAddresses({ ethereum: { 0: 'invalid' } })).toBe(false)
124
+ })
125
+ })
126
+
127
+ describe('isWalletBalances', () => {
128
+ it('should return true for valid wallet balances', () => {
129
+ const valid = {
130
+ ethereum: {
131
+ 0: {
132
+ '0x0000000000000000000000000000000000000000': '1000000000000000000',
133
+ '0x1234567890123456789012345678901234567890': '2000000000000000000',
134
+ },
135
+ },
136
+ }
137
+ expect(isWalletBalances(valid)).toBe(true)
138
+ })
139
+
140
+ it('should return false for invalid wallet balances', () => {
141
+ expect(isWalletBalances(null)).toBe(false)
142
+ expect(isWalletBalances({})).toBe(true) // Empty object is valid
143
+ expect(isWalletBalances([])).toBe(false)
144
+ expect(isWalletBalances({ ethereum: { 0: 'invalid' } })).toBe(false)
145
+ })
146
+ })
147
+
148
+ describe('isEthereumAddress', () => {
149
+ it('should return true for valid Ethereum addresses', () => {
150
+ expect(isEthereumAddress('0x1234567890123456789012345678901234567890')).toBe(true)
151
+ expect(isEthereumAddress('0xABCDEFabcdef1234567890123456789012345678')).toBe(true)
152
+ })
153
+
154
+ it('should return false for invalid Ethereum addresses', () => {
155
+ expect(isEthereumAddress('')).toBe(false)
156
+ expect(isEthereumAddress('0x123')).toBe(false)
157
+ expect(isEthereumAddress('1234567890123456789012345678901234567890')).toBe(false)
158
+ expect(isEthereumAddress(null)).toBe(false)
159
+ expect(isEthereumAddress(123)).toBe(false)
160
+ })
161
+ })
162
+
163
+ describe('isValidAccountIndex', () => {
164
+ it('should return true for valid account indices', () => {
165
+ expect(isValidAccountIndex(0)).toBe(true)
166
+ expect(isValidAccountIndex(1)).toBe(true)
167
+ expect(isValidAccountIndex(100)).toBe(true)
168
+ })
169
+
170
+ it('should return false for invalid account indices', () => {
171
+ expect(isValidAccountIndex(-1)).toBe(false)
172
+ expect(isValidAccountIndex(1.5)).toBe(false)
173
+ expect(isValidAccountIndex(NaN)).toBe(false)
174
+ expect(isValidAccountIndex(Infinity)).toBe(false)
175
+ })
176
+ })
177
+
178
+ describe('isValidNetworkName', () => {
179
+ it('should return true for valid network names', () => {
180
+ expect(isValidNetworkName('ethereum')).toBe(true)
181
+ expect(isValidNetworkName('polygon-mainnet')).toBe(true)
182
+ expect(isValidNetworkName('network_1')).toBe(true)
183
+ })
184
+
185
+ it('should return false for invalid network names', () => {
186
+ expect(isValidNetworkName('')).toBe(false)
187
+ expect(isValidNetworkName(' ')).toBe(false)
188
+ expect(isValidNetworkName('network with spaces')).toBe(false)
189
+ expect(isValidNetworkName('network@invalid')).toBe(false)
190
+ })
191
+ })
192
+
193
+ describe('isValidBalanceString', () => {
194
+ it('should return true for valid balance strings', () => {
195
+ expect(isValidBalanceString('0')).toBe(true)
196
+ expect(isValidBalanceString('100')).toBe(true)
197
+ expect(isValidBalanceString('100.5')).toBe(true)
198
+ expect(isValidBalanceString('-100')).toBe(true)
199
+ })
200
+
201
+ it('should return false for invalid balance strings', () => {
202
+ expect(isValidBalanceString('')).toBe(false)
203
+ expect(isValidBalanceString('abc')).toBe(false)
204
+ expect(isValidBalanceString('100.5.5')).toBe(false)
205
+ expect(isValidBalanceString('100a')).toBe(false)
206
+ })
207
+ })
208
+ })
209
+