@rango-dev/wallets-core 0.0.0-experimental-936229e8-20251208

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 (351) hide show
  1. package/CHANGELOG.md +334 -0
  2. package/dist/builders/action.d.ts +22 -0
  3. package/dist/builders/action.d.ts.map +1 -0
  4. package/dist/builders/mod.d.ts +5 -0
  5. package/dist/builders/mod.d.ts.map +1 -0
  6. package/dist/builders/namespace.d.ts +47 -0
  7. package/dist/builders/namespace.d.ts.map +1 -0
  8. package/dist/builders/provider.d.ts +13 -0
  9. package/dist/builders/provider.d.ts.map +1 -0
  10. package/dist/builders/types.d.ts +21 -0
  11. package/dist/builders/types.d.ts.map +1 -0
  12. package/dist/hub/helpers.d.ts +6 -0
  13. package/dist/hub/helpers.d.ts.map +1 -0
  14. package/dist/hub/hub.d.ts +29 -0
  15. package/dist/hub/hub.d.ts.map +1 -0
  16. package/dist/hub/mod.d.ts +9 -0
  17. package/dist/hub/mod.d.ts.map +1 -0
  18. package/dist/hub/namespaces/errors.d.ts +5 -0
  19. package/dist/hub/namespaces/errors.d.ts.map +1 -0
  20. package/dist/hub/namespaces/mod.d.ts +3 -0
  21. package/dist/hub/namespaces/mod.d.ts.map +1 -0
  22. package/dist/hub/namespaces/namespace.d.ts +148 -0
  23. package/dist/hub/namespaces/namespace.d.ts.map +1 -0
  24. package/dist/hub/namespaces/namespace.test.d.ts +11 -0
  25. package/dist/hub/namespaces/namespace.test.d.ts.map +1 -0
  26. package/dist/hub/namespaces/types.d.ts +36 -0
  27. package/dist/hub/namespaces/types.d.ts.map +1 -0
  28. package/dist/hub/provider/mod.d.ts +3 -0
  29. package/dist/hub/provider/mod.d.ts.map +1 -0
  30. package/dist/hub/provider/provider.d.ts +136 -0
  31. package/dist/hub/provider/provider.d.ts.map +1 -0
  32. package/dist/hub/provider/provider.test.d.ts +2 -0
  33. package/dist/hub/provider/provider.test.d.ts.map +1 -0
  34. package/dist/hub/provider/types.d.ts +37 -0
  35. package/dist/hub/provider/types.d.ts.map +1 -0
  36. package/dist/hub/store/events.d.ts +54 -0
  37. package/dist/hub/store/events.d.ts.map +1 -0
  38. package/dist/hub/store/extend.d.ts +14 -0
  39. package/dist/hub/store/extend.d.ts.map +1 -0
  40. package/dist/hub/store/hub.d.ts +11 -0
  41. package/dist/hub/store/hub.d.ts.map +1 -0
  42. package/dist/hub/store/mod.d.ts +8 -0
  43. package/dist/hub/store/mod.d.ts.map +1 -0
  44. package/dist/hub/store/mod.js +2 -0
  45. package/dist/hub/store/mod.js.map +7 -0
  46. package/dist/hub/store/namespaces.d.ts +38 -0
  47. package/dist/hub/store/namespaces.d.ts.map +1 -0
  48. package/dist/hub/store/providers.d.ts +77 -0
  49. package/dist/hub/store/providers.d.ts.map +1 -0
  50. package/dist/hub/store/selectors.d.ts +18 -0
  51. package/dist/hub/store/selectors.d.ts.map +1 -0
  52. package/dist/hub/store/store.d.ts +14 -0
  53. package/dist/hub/store/store.d.ts.map +1 -0
  54. package/dist/hub/store/store.test.d.ts +2 -0
  55. package/dist/hub/store/store.test.d.ts.map +1 -0
  56. package/dist/legacy/helpers.d.ts +12 -0
  57. package/dist/legacy/helpers.d.ts.map +1 -0
  58. package/dist/legacy/mod.d.ts +8 -0
  59. package/dist/legacy/mod.d.ts.map +1 -0
  60. package/dist/legacy/mod.js +2 -0
  61. package/dist/legacy/mod.js.map +7 -0
  62. package/dist/legacy/persistor.d.ts +11 -0
  63. package/dist/legacy/persistor.d.ts.map +1 -0
  64. package/dist/legacy/types.d.ts +247 -0
  65. package/dist/legacy/types.d.ts.map +1 -0
  66. package/dist/legacy/utils.d.ts +8 -0
  67. package/dist/legacy/utils.d.ts.map +1 -0
  68. package/dist/legacy/wallet.d.ts +65 -0
  69. package/dist/legacy/wallet.d.ts.map +1 -0
  70. package/dist/mod.d.ts +9 -0
  71. package/dist/mod.d.ts.map +1 -0
  72. package/dist/mod.js +2 -0
  73. package/dist/mod.js.map +7 -0
  74. package/dist/namespaces/common/actions.d.ts +4 -0
  75. package/dist/namespaces/common/actions.d.ts.map +1 -0
  76. package/dist/namespaces/common/after.d.ts +4 -0
  77. package/dist/namespaces/common/after.d.ts.map +1 -0
  78. package/dist/namespaces/common/and.d.ts +6 -0
  79. package/dist/namespaces/common/and.d.ts.map +1 -0
  80. package/dist/namespaces/common/before.d.ts +4 -0
  81. package/dist/namespaces/common/before.d.ts.map +1 -0
  82. package/dist/namespaces/common/builders.d.ts +5 -0
  83. package/dist/namespaces/common/builders.d.ts.map +1 -0
  84. package/dist/namespaces/common/helpers.d.ts +2 -0
  85. package/dist/namespaces/common/helpers.d.ts.map +1 -0
  86. package/dist/namespaces/common/hooks/changeAccountSubscriber.d.ts +90 -0
  87. package/dist/namespaces/common/hooks/changeAccountSubscriber.d.ts.map +1 -0
  88. package/dist/namespaces/common/hooks/changeAccountSubscriber.test.d.ts +2 -0
  89. package/dist/namespaces/common/hooks/changeAccountSubscriber.test.d.ts.map +1 -0
  90. package/dist/namespaces/common/mod.d.ts +11 -0
  91. package/dist/namespaces/common/mod.d.ts.map +1 -0
  92. package/dist/namespaces/common/mod.js +2 -0
  93. package/dist/namespaces/common/mod.js.map +7 -0
  94. package/dist/namespaces/common/or.d.ts +13 -0
  95. package/dist/namespaces/common/or.d.ts.map +1 -0
  96. package/dist/namespaces/common/types.d.ts +11 -0
  97. package/dist/namespaces/common/types.d.ts.map +1 -0
  98. package/dist/namespaces/common/utils.d.ts +10 -0
  99. package/dist/namespaces/common/utils.d.ts.map +1 -0
  100. package/dist/namespaces/cosmos/actions.d.ts +2 -0
  101. package/dist/namespaces/cosmos/actions.d.ts.map +1 -0
  102. package/dist/namespaces/cosmos/after.d.ts +2 -0
  103. package/dist/namespaces/cosmos/after.d.ts.map +1 -0
  104. package/dist/namespaces/cosmos/and.d.ts +3 -0
  105. package/dist/namespaces/cosmos/and.d.ts.map +1 -0
  106. package/dist/namespaces/cosmos/before.d.ts +2 -0
  107. package/dist/namespaces/cosmos/before.d.ts.map +1 -0
  108. package/dist/namespaces/cosmos/builders.d.ts +4 -0
  109. package/dist/namespaces/cosmos/builders.d.ts.map +1 -0
  110. package/dist/namespaces/cosmos/constants.d.ts +2 -0
  111. package/dist/namespaces/cosmos/constants.d.ts.map +1 -0
  112. package/dist/namespaces/cosmos/mod.d.ts +9 -0
  113. package/dist/namespaces/cosmos/mod.d.ts.map +1 -0
  114. package/dist/namespaces/cosmos/mod.js +2 -0
  115. package/dist/namespaces/cosmos/mod.js.map +7 -0
  116. package/dist/namespaces/cosmos/types.d.ts +19 -0
  117. package/dist/namespaces/cosmos/types.d.ts.map +1 -0
  118. package/dist/namespaces/cosmos/utils.d.ts +3 -0
  119. package/dist/namespaces/cosmos/utils.d.ts.map +1 -0
  120. package/dist/namespaces/evm/actions.d.ts +10 -0
  121. package/dist/namespaces/evm/actions.d.ts.map +1 -0
  122. package/dist/namespaces/evm/after.d.ts +2 -0
  123. package/dist/namespaces/evm/after.d.ts.map +1 -0
  124. package/dist/namespaces/evm/and.d.ts +3 -0
  125. package/dist/namespaces/evm/and.d.ts.map +1 -0
  126. package/dist/namespaces/evm/before.d.ts +2 -0
  127. package/dist/namespaces/evm/before.d.ts.map +1 -0
  128. package/dist/namespaces/evm/builders.d.ts +9 -0
  129. package/dist/namespaces/evm/builders.d.ts.map +1 -0
  130. package/dist/namespaces/evm/constants.d.ts +3 -0
  131. package/dist/namespaces/evm/constants.d.ts.map +1 -0
  132. package/dist/namespaces/evm/eip1193.d.ts +1229 -0
  133. package/dist/namespaces/evm/eip1193.d.ts.map +1 -0
  134. package/dist/namespaces/evm/hooks.d.ts +5 -0
  135. package/dist/namespaces/evm/hooks.d.ts.map +1 -0
  136. package/dist/namespaces/evm/mod.d.ts +10 -0
  137. package/dist/namespaces/evm/mod.d.ts.map +1 -0
  138. package/dist/namespaces/evm/mod.js +2 -0
  139. package/dist/namespaces/evm/mod.js.map +7 -0
  140. package/dist/namespaces/evm/types.d.ts +23 -0
  141. package/dist/namespaces/evm/types.d.ts.map +1 -0
  142. package/dist/namespaces/evm/utils.d.ts +13 -0
  143. package/dist/namespaces/evm/utils.d.ts.map +1 -0
  144. package/dist/namespaces/solana/actions.d.ts +7 -0
  145. package/dist/namespaces/solana/actions.d.ts.map +1 -0
  146. package/dist/namespaces/solana/after.d.ts +2 -0
  147. package/dist/namespaces/solana/after.d.ts.map +1 -0
  148. package/dist/namespaces/solana/and.d.ts +3 -0
  149. package/dist/namespaces/solana/and.d.ts.map +1 -0
  150. package/dist/namespaces/solana/before.d.ts +2 -0
  151. package/dist/namespaces/solana/before.d.ts.map +1 -0
  152. package/dist/namespaces/solana/builders.d.ts +6 -0
  153. package/dist/namespaces/solana/builders.d.ts.map +1 -0
  154. package/dist/namespaces/solana/constants.d.ts +3 -0
  155. package/dist/namespaces/solana/constants.d.ts.map +1 -0
  156. package/dist/namespaces/solana/hooks.d.ts +4 -0
  157. package/dist/namespaces/solana/hooks.d.ts.map +1 -0
  158. package/dist/namespaces/solana/mod.d.ts +10 -0
  159. package/dist/namespaces/solana/mod.d.ts.map +1 -0
  160. package/dist/namespaces/solana/mod.js +2 -0
  161. package/dist/namespaces/solana/mod.js.map +7 -0
  162. package/dist/namespaces/solana/types.d.ts +11 -0
  163. package/dist/namespaces/solana/types.d.ts.map +1 -0
  164. package/dist/namespaces/solana/utils.d.ts +8 -0
  165. package/dist/namespaces/solana/utils.d.ts.map +1 -0
  166. package/dist/namespaces/sui/actions.d.ts +8 -0
  167. package/dist/namespaces/sui/actions.d.ts.map +1 -0
  168. package/dist/namespaces/sui/builders.d.ts +14 -0
  169. package/dist/namespaces/sui/builders.d.ts.map +1 -0
  170. package/dist/namespaces/sui/constants.d.ts +3 -0
  171. package/dist/namespaces/sui/constants.d.ts.map +1 -0
  172. package/dist/namespaces/sui/hooks.d.ts +4 -0
  173. package/dist/namespaces/sui/hooks.d.ts.map +1 -0
  174. package/dist/namespaces/sui/mod.d.ts +7 -0
  175. package/dist/namespaces/sui/mod.d.ts.map +1 -0
  176. package/dist/namespaces/sui/mod.js +2 -0
  177. package/dist/namespaces/sui/mod.js.map +7 -0
  178. package/dist/namespaces/sui/types.d.ts +12 -0
  179. package/dist/namespaces/sui/types.d.ts.map +1 -0
  180. package/dist/namespaces/sui/utils.d.ts +15 -0
  181. package/dist/namespaces/sui/utils.d.ts.map +1 -0
  182. package/dist/namespaces/tron/actions.d.ts +2 -0
  183. package/dist/namespaces/tron/actions.d.ts.map +1 -0
  184. package/dist/namespaces/tron/after.d.ts +2 -0
  185. package/dist/namespaces/tron/after.d.ts.map +1 -0
  186. package/dist/namespaces/tron/and.d.ts +3 -0
  187. package/dist/namespaces/tron/and.d.ts.map +1 -0
  188. package/dist/namespaces/tron/before.d.ts +2 -0
  189. package/dist/namespaces/tron/before.d.ts.map +1 -0
  190. package/dist/namespaces/tron/builders.d.ts +5 -0
  191. package/dist/namespaces/tron/builders.d.ts.map +1 -0
  192. package/dist/namespaces/tron/constants.d.ts +3 -0
  193. package/dist/namespaces/tron/constants.d.ts.map +1 -0
  194. package/dist/namespaces/tron/mod.d.ts +9 -0
  195. package/dist/namespaces/tron/mod.d.ts.map +1 -0
  196. package/dist/namespaces/tron/mod.js +2 -0
  197. package/dist/namespaces/tron/mod.js.map +7 -0
  198. package/dist/namespaces/tron/types.d.ts +8 -0
  199. package/dist/namespaces/tron/types.d.ts.map +1 -0
  200. package/dist/namespaces/tron/utils.d.ts +2 -0
  201. package/dist/namespaces/tron/utils.d.ts.map +1 -0
  202. package/dist/namespaces/utxo/actions.d.ts +2 -0
  203. package/dist/namespaces/utxo/actions.d.ts.map +1 -0
  204. package/dist/namespaces/utxo/after.d.ts +2 -0
  205. package/dist/namespaces/utxo/after.d.ts.map +1 -0
  206. package/dist/namespaces/utxo/and.d.ts +3 -0
  207. package/dist/namespaces/utxo/and.d.ts.map +1 -0
  208. package/dist/namespaces/utxo/before.d.ts +2 -0
  209. package/dist/namespaces/utxo/before.d.ts.map +1 -0
  210. package/dist/namespaces/utxo/builders.d.ts +4 -0
  211. package/dist/namespaces/utxo/builders.d.ts.map +1 -0
  212. package/dist/namespaces/utxo/constants.d.ts +3 -0
  213. package/dist/namespaces/utxo/constants.d.ts.map +1 -0
  214. package/dist/namespaces/utxo/mod.d.ts +9 -0
  215. package/dist/namespaces/utxo/mod.d.ts.map +1 -0
  216. package/dist/namespaces/utxo/mod.js +2 -0
  217. package/dist/namespaces/utxo/mod.js.map +7 -0
  218. package/dist/namespaces/utxo/types.d.ts +8 -0
  219. package/dist/namespaces/utxo/types.d.ts.map +1 -0
  220. package/dist/namespaces/utxo/utils.d.ts +2 -0
  221. package/dist/namespaces/utxo/utils.d.ts.map +1 -0
  222. package/dist/test-utils/fixtures.d.ts +4 -0
  223. package/dist/test-utils/fixtures.d.ts.map +1 -0
  224. package/dist/types/accounts.d.ts +11 -0
  225. package/dist/types/accounts.d.ts.map +1 -0
  226. package/dist/types/actions.d.ts +5 -0
  227. package/dist/types/actions.d.ts.map +1 -0
  228. package/dist/types/mod.d.ts +2 -0
  229. package/dist/types/mod.d.ts.map +1 -0
  230. package/dist/types/utils.d.ts +7 -0
  231. package/dist/types/utils.d.ts.map +1 -0
  232. package/dist/utils/mod.d.ts +4 -0
  233. package/dist/utils/mod.d.ts.map +1 -0
  234. package/dist/utils/mod.js +2 -0
  235. package/dist/utils/mod.js.map +7 -0
  236. package/dist/utils/versions.d.ts +21 -0
  237. package/dist/utils/versions.d.ts.map +1 -0
  238. package/dist/utils/versions.test.d.ts +2 -0
  239. package/dist/utils/versions.test.d.ts.map +1 -0
  240. package/dist/wallets-core.build.json +1 -0
  241. package/legacy/package.json +8 -0
  242. package/package.json +84 -0
  243. package/readme.md +4 -0
  244. package/src/builders/action.ts +86 -0
  245. package/src/builders/mod.ts +5 -0
  246. package/src/builders/namespace.ts +258 -0
  247. package/src/builders/provider.ts +61 -0
  248. package/src/builders/types.ts +29 -0
  249. package/src/hub/helpers.ts +11 -0
  250. package/src/hub/hub.ts +128 -0
  251. package/src/hub/mod.ts +18 -0
  252. package/src/hub/namespaces/errors.ts +11 -0
  253. package/src/hub/namespaces/mod.ts +10 -0
  254. package/src/hub/namespaces/namespace.test.ts +443 -0
  255. package/src/hub/namespaces/namespace.ts +470 -0
  256. package/src/hub/namespaces/types.ts +58 -0
  257. package/src/hub/provider/mod.ts +10 -0
  258. package/src/hub/provider/provider.test.ts +232 -0
  259. package/src/hub/provider/provider.ts +335 -0
  260. package/src/hub/provider/types.ts +50 -0
  261. package/src/hub/store/events.ts +89 -0
  262. package/src/hub/store/extend.ts +125 -0
  263. package/src/hub/store/hub.ts +18 -0
  264. package/src/hub/store/mod.ts +25 -0
  265. package/src/hub/store/namespaces.ts +177 -0
  266. package/src/hub/store/providers.ts +169 -0
  267. package/src/hub/store/selectors.ts +59 -0
  268. package/src/hub/store/store.test.ts +50 -0
  269. package/src/hub/store/store.ts +30 -0
  270. package/src/legacy/helpers.ts +75 -0
  271. package/src/legacy/mod.ts +44 -0
  272. package/src/legacy/persistor.ts +19 -0
  273. package/src/legacy/types.ts +291 -0
  274. package/src/legacy/utils.ts +20 -0
  275. package/src/legacy/wallet.ts +552 -0
  276. package/src/mod.ts +44 -0
  277. package/src/namespaces/common/actions.ts +12 -0
  278. package/src/namespaces/common/after.ts +8 -0
  279. package/src/namespaces/common/and.ts +42 -0
  280. package/src/namespaces/common/before.ts +9 -0
  281. package/src/namespaces/common/builders.ts +14 -0
  282. package/src/namespaces/common/helpers.ts +10 -0
  283. package/src/namespaces/common/hooks/changeAccountSubscriber.test.ts +173 -0
  284. package/src/namespaces/common/hooks/changeAccountSubscriber.ts +236 -0
  285. package/src/namespaces/common/mod.ts +23 -0
  286. package/src/namespaces/common/or.ts +17 -0
  287. package/src/namespaces/common/types.ts +25 -0
  288. package/src/namespaces/common/utils.ts +43 -0
  289. package/src/namespaces/cosmos/actions.ts +3 -0
  290. package/src/namespaces/cosmos/after.ts +3 -0
  291. package/src/namespaces/cosmos/and.ts +5 -0
  292. package/src/namespaces/cosmos/before.ts +3 -0
  293. package/src/namespaces/cosmos/builders.ts +15 -0
  294. package/src/namespaces/cosmos/constants.ts +1 -0
  295. package/src/namespaces/cosmos/mod.ts +9 -0
  296. package/src/namespaces/cosmos/types.ts +26 -0
  297. package/src/namespaces/cosmos/utils.ts +21 -0
  298. package/src/namespaces/evm/actions.ts +119 -0
  299. package/src/namespaces/evm/after.ts +3 -0
  300. package/src/namespaces/evm/and.ts +5 -0
  301. package/src/namespaces/evm/before.ts +3 -0
  302. package/src/namespaces/evm/builders.ts +52 -0
  303. package/src/namespaces/evm/constants.ts +2 -0
  304. package/src/namespaces/evm/eip1193.ts +1415 -0
  305. package/src/namespaces/evm/hooks.ts +43 -0
  306. package/src/namespaces/evm/mod.ts +10 -0
  307. package/src/namespaces/evm/types.ts +33 -0
  308. package/src/namespaces/evm/utils.ts +106 -0
  309. package/src/namespaces/solana/actions.ts +44 -0
  310. package/src/namespaces/solana/after.ts +3 -0
  311. package/src/namespaces/solana/and.ts +5 -0
  312. package/src/namespaces/solana/before.ts +3 -0
  313. package/src/namespaces/solana/builders.ts +40 -0
  314. package/src/namespaces/solana/constants.ts +2 -0
  315. package/src/namespaces/solana/hooks.ts +10 -0
  316. package/src/namespaces/solana/mod.ts +10 -0
  317. package/src/namespaces/solana/types.ts +26 -0
  318. package/src/namespaces/solana/utils.ts +36 -0
  319. package/src/namespaces/sui/actions.ts +27 -0
  320. package/src/namespaces/sui/builders.ts +77 -0
  321. package/src/namespaces/sui/constants.ts +8 -0
  322. package/src/namespaces/sui/hooks.ts +10 -0
  323. package/src/namespaces/sui/mod.ts +8 -0
  324. package/src/namespaces/sui/types.ts +26 -0
  325. package/src/namespaces/sui/utils.ts +56 -0
  326. package/src/namespaces/tron/actions.ts +3 -0
  327. package/src/namespaces/tron/after.ts +3 -0
  328. package/src/namespaces/tron/and.ts +5 -0
  329. package/src/namespaces/tron/before.ts +3 -0
  330. package/src/namespaces/tron/builders.ts +15 -0
  331. package/src/namespaces/tron/constants.ts +8 -0
  332. package/src/namespaces/tron/mod.ts +8 -0
  333. package/src/namespaces/tron/types.ts +15 -0
  334. package/src/namespaces/tron/utils.ts +18 -0
  335. package/src/namespaces/utxo/actions.ts +3 -0
  336. package/src/namespaces/utxo/after.ts +3 -0
  337. package/src/namespaces/utxo/and.ts +5 -0
  338. package/src/namespaces/utxo/before.ts +3 -0
  339. package/src/namespaces/utxo/builders.ts +12 -0
  340. package/src/namespaces/utxo/constants.ts +2 -0
  341. package/src/namespaces/utxo/mod.ts +8 -0
  342. package/src/namespaces/utxo/types.ts +15 -0
  343. package/src/namespaces/utxo/utils.ts +18 -0
  344. package/src/test-utils/fixtures.ts +13 -0
  345. package/src/types/accounts.ts +12 -0
  346. package/src/types/actions.ts +11 -0
  347. package/src/types/mod.ts +1 -0
  348. package/src/types/utils.ts +7 -0
  349. package/src/utils/mod.ts +8 -0
  350. package/src/utils/versions.test.ts +22 -0
  351. package/src/utils/versions.ts +62 -0
@@ -0,0 +1,125 @@
1
+ import type {
2
+ Event,
3
+ ProviderConnectedEvent,
4
+ ProviderConnectingEvent,
5
+ ProviderDisconnectedEvent,
6
+ } from './events.js';
7
+ import type { RawStore, State } from './store.js';
8
+
9
+ import { guessProviderStateSelector } from './selectors.js';
10
+
11
+ export interface Store extends Omit<RawStore, 'subscribe'> {
12
+ subscribe(listener: (event: Event, state: State, prevState: State) => void): {
13
+ flushEvents(): void;
14
+ };
15
+ }
16
+
17
+ /**
18
+ * Zustand's store provides a set of built-in functionalities,
19
+ * but it may not meet all our requirements.
20
+ * This function modifies the interface and adds or updates the available methods.
21
+ */
22
+ export function extend(store: RawStore): Store {
23
+ const extendedSubscribe: (store: RawStore) => Store['subscribe'] =
24
+ (store) => (listener) => {
25
+ const executeListener = (
26
+ events: Event[],
27
+ state: State,
28
+ prevState: State
29
+ ) => {
30
+ for (const ev of events) {
31
+ listener(ev, state, prevState);
32
+ }
33
+ };
34
+
35
+ /*
36
+ * only known changes will fire event for lib users
37
+ * so all the changes in store won't trigger a user-face event
38
+ */
39
+ store.subscribe((state, prevState) => {
40
+ const eventsLike = getEventsLike(state, prevState);
41
+ const events = tryConsumeEvents(state);
42
+ const allQueuedEvents = [...events, ...eventsLike];
43
+ executeListener(allQueuedEvents, state, prevState);
44
+ });
45
+
46
+ return {
47
+ /**
48
+ * Manually run pending events.
49
+ * This useful when use `subscribe` method after sometime and when store initialized.
50
+ *
51
+ * Note: Please consider both current and previous state will be same and we don't have access to previous state in a such scenario.
52
+ */
53
+ flushEvents: () => {
54
+ const state = store.getState();
55
+ const events = tryConsumeEvents(state);
56
+ executeListener(events, state, state);
57
+ },
58
+ };
59
+ };
60
+
61
+ return new Proxy(store, {
62
+ get: function (target, prop, receiver) {
63
+ if (prop === 'subscribe') {
64
+ return extendedSubscribe(target);
65
+ }
66
+ return Reflect.get(target, prop, receiver);
67
+ },
68
+ }) as unknown as Store;
69
+ }
70
+
71
+ /**
72
+ * Retrieves the ConsumableEvent from the store and returns its values.
73
+ * The values will be consumed by iterating over the field.
74
+ */
75
+ function tryConsumeEvents(state: State): Event[] {
76
+ return [...state.providers.events, ...state.namespaces.events];
77
+ }
78
+
79
+ /**
80
+ * In certain cases, adding events to the store is not possible (e.g., namespace or provider layer).
81
+ * In these cases, we observe store changes and treat specific patterns as events when detected.
82
+ *
83
+ * Note: This approach should be avoided in most cases. It is used here to maintain backward compatibility
84
+ * with the provider's legacy interface.
85
+ */
86
+ function getEventsLike(state: State, prevState: State): Event[] {
87
+ const events: Event[] = [];
88
+
89
+ for (const providerId of Object.keys(state.providers.list)) {
90
+ const currentProviderState = guessProviderStateSelector(state, providerId);
91
+ const previousProviderState = guessProviderStateSelector(
92
+ prevState,
93
+ providerId
94
+ );
95
+
96
+ if (previousProviderState.connecting !== currentProviderState.connecting) {
97
+ const ev: ProviderConnectingEvent = {
98
+ type: 'provider_connecting',
99
+ provider: providerId,
100
+ value: currentProviderState.connecting,
101
+ };
102
+ events.push(ev);
103
+ }
104
+
105
+ if (!previousProviderState.connected && currentProviderState.connected) {
106
+ const ev: ProviderConnectedEvent = {
107
+ type: 'provider_connected',
108
+ provider: providerId,
109
+ };
110
+
111
+ events.push(ev);
112
+ }
113
+
114
+ if (previousProviderState.connected && !currentProviderState.connected) {
115
+ const ev: ProviderDisconnectedEvent = {
116
+ type: 'provider_disconnected',
117
+ provider: providerId,
118
+ };
119
+
120
+ events.push(ev);
121
+ }
122
+ }
123
+
124
+ return events;
125
+ }
@@ -0,0 +1,18 @@
1
+ /************ Hub ************/
2
+
3
+ import type { State } from './mod.js';
4
+ import type { StateCreator } from 'zustand';
5
+
6
+ type HubConfig = object;
7
+
8
+ export interface HubStore {
9
+ config: HubConfig;
10
+ }
11
+
12
+ type HubStateCreator = StateCreator<State, [], [], HubStore>;
13
+
14
+ const hubStore: HubStateCreator = () => ({
15
+ config: {},
16
+ });
17
+
18
+ export { hubStore };
@@ -0,0 +1,25 @@
1
+ export {
2
+ guessProviderStateSelector,
3
+ namespaceStateSelector,
4
+ } from './selectors.js';
5
+ export type { Store } from './extend.js';
6
+ export type { State } from './store.js';
7
+ export type {
8
+ Event,
9
+ // Providers
10
+ ProviderDetectedEvent,
11
+ ProviderConnectingEvent,
12
+ ProviderConnectedEvent,
13
+ ProviderDisconnectedEvent,
14
+ // Namespaces
15
+ NamespaceDisconnectedEvent,
16
+ NamespaceConnectedEvent,
17
+ NamespaceSwitchedAccountEvent,
18
+ } from './events.js';
19
+ export type {
20
+ ProviderMetadata,
21
+ ProviderConfig,
22
+ ProviderInfo,
23
+ } from './providers.js';
24
+ export type { NamespaceConfig, NamespaceData } from './namespaces.js';
25
+ export { createStore } from './store.js';
@@ -0,0 +1,177 @@
1
+ /************ Namespace ************/
2
+
3
+ import type { StateCreator } from 'zustand';
4
+
5
+ import { produce } from 'immer';
6
+
7
+ import {
8
+ ConsumableEvents,
9
+ type NamespaceConnectedEvent,
10
+ type NamespaceDisconnectedEvent,
11
+ type NamespaceSwitchedAccountEvent,
12
+ type NamespaceSwitchedNetworkEvent,
13
+ } from './events.js';
14
+ import { namespaceStateSelector, type State } from './mod.js';
15
+
16
+ // Currently, namespace doesn't has any config.
17
+ export type NamespaceConfig = object;
18
+
19
+ export interface NamespaceData {
20
+ accounts: null | string[];
21
+ network: null | string;
22
+ connected: boolean;
23
+ connecting: boolean;
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ connectArgs: Record<string, any> | null;
26
+ }
27
+
28
+ interface NamespaceInfo {
29
+ providerId: string;
30
+ namespaceId: string;
31
+ }
32
+
33
+ interface NamespaceItem {
34
+ info: NamespaceInfo;
35
+ data: NamespaceData;
36
+ error: unknown;
37
+ }
38
+
39
+ type NamespaceState = {
40
+ events: InstanceType<typeof ConsumableEvents>;
41
+ list: Record<string, NamespaceItem>;
42
+ };
43
+
44
+ interface NamespaceActions {
45
+ addNamespace: (id: string, config: NamespaceInfo) => void;
46
+ updateStatus: <K extends keyof NamespaceData>(
47
+ id: string,
48
+ key: K,
49
+ value: NamespaceData[K]
50
+ ) => void;
51
+
52
+ _produceEventsWhenUpdatingStatus: <K extends keyof NamespaceData>(
53
+ namespace: NamespaceItem,
54
+ id: string,
55
+ key: K,
56
+ value: NamespaceData[K]
57
+ ) => void;
58
+ }
59
+ interface NamespaceSelectors {
60
+ getNamespaceData(storeId: string): NamespaceData;
61
+ }
62
+
63
+ export type NamespaceStore = NamespaceState &
64
+ NamespaceActions &
65
+ NamespaceSelectors;
66
+ type NamespaceStateCreator = StateCreator<State, [], [], NamespaceStore>;
67
+
68
+ const namespacesStore: NamespaceStateCreator = (set, get) => ({
69
+ events: new ConsumableEvents(),
70
+
71
+ list: {},
72
+ addNamespace: (id, info) => {
73
+ const data: NamespaceData = {
74
+ accounts: null,
75
+ network: null,
76
+ connected: false,
77
+ connecting: false,
78
+ connectArgs: null,
79
+ };
80
+
81
+ const item = {
82
+ data,
83
+ error: '',
84
+ info,
85
+ };
86
+
87
+ set(
88
+ produce((state: State) => {
89
+ state.namespaces.list[id] = item;
90
+ })
91
+ );
92
+ },
93
+ updateStatus: (id, key, value) => {
94
+ const ns = get().namespaces.list[id];
95
+ if (!ns) {
96
+ throw new Error(`No namespace with '${id}' found.`);
97
+ }
98
+
99
+ get().namespaces._produceEventsWhenUpdatingStatus(ns, id, key, value);
100
+
101
+ // Updating state
102
+ set(
103
+ produce((state: State) => {
104
+ state.namespaces.list[id].data[key] = value;
105
+ })
106
+ );
107
+ },
108
+ getNamespaceData(storeId) {
109
+ return namespaceStateSelector(get(), storeId);
110
+ },
111
+
112
+ _produceEventsWhenUpdatingStatus: (namespace, id, key, value) => {
113
+ if (key === 'accounts') {
114
+ // check for both null and empty array
115
+ const isAccountsEmpty =
116
+ Object.is(value, null) || (Array.isArray(value) && value.length === 0);
117
+
118
+ if (isAccountsEmpty) {
119
+ const currentConnectedStatus = get().namespaces.list[id].data.connected;
120
+ if (currentConnectedStatus) {
121
+ const event: NamespaceDisconnectedEvent = {
122
+ type: 'namespace_disconnected',
123
+ provider: namespace.info.providerId,
124
+ namespace: namespace.info.namespaceId,
125
+ };
126
+
127
+ get().namespaces.events.push(event);
128
+ }
129
+ // Skip emitting disconnect event, if the `connected` is false
130
+ } else {
131
+ const currentAccounts = get().namespaces.list[id].data.accounts;
132
+
133
+ if (!currentAccounts) {
134
+ const event: NamespaceConnectedEvent = {
135
+ type: 'namespace_connected',
136
+ provider: namespace.info.providerId,
137
+ namespace: namespace.info.namespaceId,
138
+ accounts: value as string[],
139
+ };
140
+
141
+ get().namespaces.events.push(event);
142
+ } else {
143
+ const areSameAccounts =
144
+ // Clone the object from the Zustand store, as it's immutable, to avoid errors during sorting.
145
+ [...currentAccounts].sort().toString() ===
146
+ (value as string[]).sort().toString();
147
+
148
+ if (!areSameAccounts) {
149
+ const event: NamespaceSwitchedAccountEvent = {
150
+ type: 'namespace_account_switched',
151
+ provider: namespace.info.providerId,
152
+ namespace: namespace.info.namespaceId,
153
+ previousAccounts: currentAccounts,
154
+ accounts: value as string[],
155
+ };
156
+
157
+ get().namespaces.events.push(event);
158
+ }
159
+ }
160
+ }
161
+ } else if (key === 'network') {
162
+ const currentNetwork = get().namespaces.list[id].data.network;
163
+
164
+ const event: NamespaceSwitchedNetworkEvent = {
165
+ type: 'namespace_network_switched',
166
+ provider: namespace.info.providerId,
167
+ namespace: namespace.info.namespaceId,
168
+ network: value as string,
169
+ previousNetwork: currentNetwork,
170
+ };
171
+
172
+ get().namespaces.events.push(event);
173
+ }
174
+ },
175
+ });
176
+
177
+ export { namespacesStore };
@@ -0,0 +1,169 @@
1
+ import type { Namespace } from '../../namespaces/common/types.js';
2
+ import type { State as InternalProviderState } from '../provider/mod.js';
3
+ import type { BlockchainMeta, SignerFactory } from 'rango-types';
4
+ import type { StateCreator } from 'zustand';
5
+
6
+ import { produce } from 'immer';
7
+
8
+ import { ConsumableEvents, type ProviderDetectedEvent } from './events.js';
9
+ import { guessProviderStateSelector, type State } from './mod.js';
10
+
11
+ type Browsers = 'firefox' | 'chrome' | 'edge' | 'brave' | 'homepage';
12
+ type Property<N extends string, V> = { name: N; value: V };
13
+
14
+ type NamespacesProperty = Property<
15
+ 'namespaces',
16
+ {
17
+ selection: 'single' | 'multiple';
18
+ data: {
19
+ label: string;
20
+ id: string;
21
+ value: Namespace;
22
+ unsupported?: boolean;
23
+ getSupportedChains: (chains: BlockchainMeta[]) => BlockchainMeta[];
24
+ }[];
25
+ }
26
+ >;
27
+ type DerivationPathProperty = Property<
28
+ 'derivationPath',
29
+ {
30
+ data: {
31
+ id: string;
32
+ label: string;
33
+ namespace: Namespace;
34
+ generateDerivationPath: (index: string) => string;
35
+ }[];
36
+ }
37
+ >;
38
+ type DetailsProperty = Property<
39
+ 'details',
40
+ {
41
+ mobileWallet?: boolean;
42
+ showOnMobile?: boolean;
43
+ isContractWallet?: boolean;
44
+ }
45
+ >;
46
+ type SignersProperty = Property<
47
+ 'signers',
48
+ {
49
+ getSigners: () => Promise<SignerFactory>;
50
+ }
51
+ >;
52
+
53
+ export type ProviderMetadata = {
54
+ name: string;
55
+ icon: string;
56
+ extensions: Partial<Record<Browsers, string>>;
57
+ properties?: Array<
58
+ | NamespacesProperty
59
+ | DerivationPathProperty
60
+ | DetailsProperty
61
+ | SignersProperty
62
+ >;
63
+ };
64
+
65
+ export interface ProviderConfig {
66
+ metadata: ProviderMetadata;
67
+ }
68
+
69
+ export type ProviderInfo = ProviderConfig;
70
+
71
+ interface ProviderData {
72
+ installed: boolean;
73
+ }
74
+
75
+ interface ProviderItem {
76
+ config: ProviderConfig;
77
+ data: ProviderData;
78
+ error: unknown;
79
+ }
80
+
81
+ type ProviderState = {
82
+ events: ConsumableEvents;
83
+ list: Record<string, ProviderItem>;
84
+ };
85
+ interface ProviderActions {
86
+ addProvider: (id: string, config: ProviderConfig) => void;
87
+ removeProvider: (id: string) => void;
88
+ updateStatus: <K extends keyof ProviderData>(
89
+ id: string,
90
+ key: K,
91
+ value: ProviderData[K]
92
+ ) => void;
93
+
94
+ _produceEventsWhenUpdatingStatus: <K extends keyof ProviderData>(
95
+ provider: ProviderItem,
96
+ id: string,
97
+ key: K,
98
+ value: ProviderData[K]
99
+ ) => void;
100
+ }
101
+
102
+ interface ProviderSelectors {
103
+ /**
104
+ * Provider has a limited state to itself, to be compatible with legacy, we try to produce same object as legacy
105
+ * which includes namespace state as well.
106
+ */
107
+ guessNamespacesState: (id: string) => InternalProviderState;
108
+ }
109
+
110
+ export type ProviderStore = ProviderState & ProviderActions & ProviderSelectors;
111
+ type ProvidersStateCreator = StateCreator<State, [], [], ProviderStore>;
112
+
113
+ const providersStore: ProvidersStateCreator = (set, get) => ({
114
+ events: new ConsumableEvents(),
115
+
116
+ list: {},
117
+ addProvider: (id, config) => {
118
+ const item = {
119
+ data: {
120
+ installed: false,
121
+ },
122
+ error: '',
123
+ config,
124
+ };
125
+
126
+ set(
127
+ produce((state: State) => {
128
+ state.providers.list[id] = item;
129
+ })
130
+ );
131
+ },
132
+ removeProvider: (id) => {
133
+ set(
134
+ produce((state: State) => {
135
+ delete state.providers.list[id];
136
+ })
137
+ );
138
+ },
139
+ updateStatus: (id, key, value) => {
140
+ const provider = get().providers.list[id];
141
+ if (!provider) {
142
+ throw new Error(`No namespace namespace with '${id}' found.`);
143
+ }
144
+
145
+ get().providers._produceEventsWhenUpdatingStatus(provider, id, key, value);
146
+
147
+ set(
148
+ produce((state: State) => {
149
+ state.providers.list[id].data[key] = value;
150
+ })
151
+ );
152
+ },
153
+ guessNamespacesState: (providerId: string): InternalProviderState => {
154
+ return guessProviderStateSelector(get(), providerId);
155
+ },
156
+
157
+ _produceEventsWhenUpdatingStatus: (_provider, id, key, _value) => {
158
+ if (key === 'installed') {
159
+ const event: ProviderDetectedEvent = {
160
+ type: 'provider_detected',
161
+ provider: id,
162
+ };
163
+
164
+ get().providers.events.push(event);
165
+ }
166
+ },
167
+ });
168
+
169
+ export { providersStore };
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Note: Zustand has some difficulty when you are trying to `previous` state on `subscribe`.
3
+ * If these selectors define inside store directly and use `get()` for accessing state, it will get the latest state
4
+ * instead of previous state which desired.
5
+ * So make them a helper will let us to reuse them both in store and outside of store in a `subscribe`.
6
+ */
7
+ import type { State } from '../mod.js';
8
+ import type { State as InternalProviderState } from '../provider/mod.js';
9
+
10
+ /**
11
+ * Legacy provider state includes `connecting` and `connected` values. It hadn't a separation layer for `namespaces`.
12
+ * For compatibility reasons, we should produce these two values somehow.
13
+ *
14
+ * Currently, We return `true` if any of namespaces return true. We are missing failed namespace here.
15
+ * But if we want to solve that, we should migrate our client code to use `namespace` state directly and not from `provider`
16
+ */
17
+ export function guessProviderStateSelector(
18
+ state: State,
19
+ providerId: string
20
+ ): InternalProviderState {
21
+ /*
22
+ * We keep namespaces in a separate branch than providers.
23
+ * We should look at all of them and find all the namespaces that for our current proivder.
24
+ */
25
+ const allNamespaces = state.namespaces.list;
26
+ const currentProviderNamespaces = Object.keys(allNamespaces).filter(
27
+ (key) => allNamespaces[key].info.providerId === providerId
28
+ );
29
+
30
+ // Returning provider state value directly.
31
+ const installed = !!state.providers.list[providerId]?.data.installed;
32
+
33
+ /*
34
+ * If any namespaces returns `true`, we consider the whole provider for this field to be `true`.
35
+ * it has a downside regarding errors which explained on top of the function.
36
+ */
37
+ const connected =
38
+ currentProviderNamespaces.length > 0
39
+ ? currentProviderNamespaces.some(
40
+ (key) => allNamespaces[key].data.connected
41
+ )
42
+ : false;
43
+ const connecting =
44
+ currentProviderNamespaces.length > 0
45
+ ? currentProviderNamespaces.some(
46
+ (key) => allNamespaces[key].data.connecting
47
+ )
48
+ : false;
49
+
50
+ return {
51
+ installed,
52
+ connected,
53
+ connecting,
54
+ };
55
+ }
56
+
57
+ export function namespaceStateSelector(state: State, storeId: string) {
58
+ return state.namespaces.list[storeId].data;
59
+ }
@@ -0,0 +1,50 @@
1
+ import type { Store } from './mod.js';
2
+
3
+ import { beforeEach, describe, expect, test } from 'vitest';
4
+
5
+ import { createStore } from './store.js';
6
+
7
+ describe('checking store', () => {
8
+ let hubStore: Store;
9
+
10
+ beforeEach(() => {
11
+ hubStore = createStore();
12
+ });
13
+
14
+ test('new providers can be added to store', () => {
15
+ const id = 'sol-or-something';
16
+ const info = {
17
+ metadata: {
18
+ name: 'sol grabage wallet',
19
+ icon: 'http://somewhere.world',
20
+ extensions: {
21
+ homepage: 'http://somewhere.world',
22
+ },
23
+ },
24
+ };
25
+
26
+ const { getState } = hubStore;
27
+ getState().providers.addProvider(id, info);
28
+
29
+ expect(getState().providers.list[id]).toBeDefined();
30
+ expect(Object.keys(getState().providers.list).length).toBe(1);
31
+ });
32
+ test('provider can be removed from store', () => {
33
+ const id = 'sol-or-something';
34
+ const metadata = {
35
+ name: 'sol grabage wallet',
36
+ icon: 'http://somewhere.world',
37
+ extensions: {
38
+ homepage: 'http://somewhere.world',
39
+ },
40
+ };
41
+
42
+ const { getState } = hubStore;
43
+ getState().providers.addProvider(id, { metadata });
44
+ expect(getState().providers.list[id]).toBeDefined();
45
+ expect(Object.keys(getState().providers.list).length).toBe(1);
46
+ getState().providers.removeProvider(id);
47
+ expect(getState().providers.list[id]).toBeUndefined();
48
+ expect(Object.keys(getState().providers.list).length).toBe(0);
49
+ });
50
+ });
@@ -0,0 +1,30 @@
1
+ import type { StoreApi } from 'zustand/vanilla';
2
+
3
+ import { createStore as createZustandStore } from 'zustand/vanilla';
4
+
5
+ import { extend, type Store } from './extend.js';
6
+ import { hubStore, type HubStore } from './hub.js';
7
+ import { namespacesStore, type NamespaceStore } from './namespaces.js';
8
+ import { providersStore, type ProviderStore } from './providers.js';
9
+
10
+ /************ State ************/
11
+
12
+ export interface State {
13
+ hub: HubStore;
14
+ providers: ProviderStore;
15
+ namespaces: NamespaceStore;
16
+ }
17
+
18
+ export type RawStore = StoreApi<State>;
19
+
20
+ export const createStore = (): Store => {
21
+ const store = createZustandStore<State>((...api) => {
22
+ return {
23
+ hub: hubStore(...api),
24
+ providers: providersStore(...api),
25
+ namespaces: namespacesStore(...api),
26
+ };
27
+ });
28
+
29
+ return extend(store);
30
+ };