@reown/appkit-core-react-native 2.0.0 → 2.0.1

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 (282) hide show
  1. package/lib/commonjs/controllers/ApiController.js +148 -82
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -1
  3. package/lib/commonjs/controllers/AssetController.js +0 -4
  4. package/lib/commonjs/controllers/AssetController.js.map +1 -1
  5. package/lib/commonjs/controllers/BlockchainApiController.js +127 -134
  6. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
  7. package/lib/commonjs/controllers/ConnectionsController.js +280 -60
  8. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -1
  9. package/lib/commonjs/controllers/EnsController.js +6 -4
  10. package/lib/commonjs/controllers/EnsController.js.map +1 -1
  11. package/lib/commonjs/controllers/EventsController.js +55 -5
  12. package/lib/commonjs/controllers/EventsController.js.map +1 -1
  13. package/lib/commonjs/controllers/LogController.js +188 -0
  14. package/lib/commonjs/controllers/LogController.js.map +1 -0
  15. package/lib/commonjs/controllers/ModalController.js +9 -7
  16. package/lib/commonjs/controllers/ModalController.js.map +1 -1
  17. package/lib/commonjs/controllers/OnRampController.js +134 -53
  18. package/lib/commonjs/controllers/OnRampController.js.map +1 -1
  19. package/lib/commonjs/controllers/OptionsController.js +20 -7
  20. package/lib/commonjs/controllers/OptionsController.js.map +1 -1
  21. package/lib/commonjs/controllers/PublicStateController.js.map +1 -1
  22. package/lib/commonjs/controllers/RouterController.js +7 -22
  23. package/lib/commonjs/controllers/RouterController.js.map +1 -1
  24. package/lib/commonjs/controllers/SendController.js +115 -99
  25. package/lib/commonjs/controllers/SendController.js.map +1 -1
  26. package/lib/commonjs/controllers/SnackController.js +29 -5
  27. package/lib/commonjs/controllers/SnackController.js.map +1 -1
  28. package/lib/commonjs/controllers/SwapController.js +119 -119
  29. package/lib/commonjs/controllers/SwapController.js.map +1 -1
  30. package/lib/commonjs/controllers/ThemeController.js +5 -0
  31. package/lib/commonjs/controllers/ThemeController.js.map +1 -1
  32. package/lib/commonjs/controllers/TransactionsController.js +25 -19
  33. package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
  34. package/lib/commonjs/controllers/WcController.js +73 -0
  35. package/lib/commonjs/controllers/WcController.js.map +1 -0
  36. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +272 -0
  37. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
  38. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
  39. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  40. package/lib/commonjs/features/reown-authentication/index.js +28 -0
  41. package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
  42. package/lib/commonjs/index.js +30 -37
  43. package/lib/commonjs/index.js.map +1 -1
  44. package/lib/commonjs/package.json +1 -0
  45. package/lib/commonjs/utils/ApiUtil.js +1 -1
  46. package/lib/commonjs/utils/ApiUtil.js.map +1 -1
  47. package/lib/commonjs/utils/AssetUtil.js +8 -17
  48. package/lib/commonjs/utils/AssetUtil.js.map +1 -1
  49. package/lib/commonjs/utils/ConstantsUtil.js +14 -431
  50. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
  51. package/lib/commonjs/utils/CoreHelperUtil.js +33 -10
  52. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
  53. package/lib/commonjs/utils/EventUtil.js.map +1 -1
  54. package/lib/commonjs/utils/FetchUtil.js +41 -12
  55. package/lib/commonjs/utils/FetchUtil.js.map +1 -1
  56. package/lib/commonjs/utils/LogUtils.js +131 -0
  57. package/lib/commonjs/utils/LogUtils.js.map +1 -0
  58. package/lib/commonjs/utils/StorageUtil.js +119 -172
  59. package/lib/commonjs/utils/StorageUtil.js.map +1 -1
  60. package/lib/commonjs/utils/SwapApiUtil.js +2 -17
  61. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
  62. package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -1
  63. package/lib/commonjs/utils/WalletUtil.js +23 -0
  64. package/lib/commonjs/utils/WalletUtil.js.map +1 -0
  65. package/lib/module/controllers/ApiController.js +150 -82
  66. package/lib/module/controllers/ApiController.js.map +1 -1
  67. package/lib/module/controllers/AssetController.js +2 -4
  68. package/lib/module/controllers/AssetController.js.map +1 -1
  69. package/lib/module/controllers/BlockchainApiController.js +128 -133
  70. package/lib/module/controllers/BlockchainApiController.js.map +1 -1
  71. package/lib/module/controllers/ConnectionsController.js +282 -60
  72. package/lib/module/controllers/ConnectionsController.js.map +1 -1
  73. package/lib/module/controllers/EnsController.js +6 -2
  74. package/lib/module/controllers/EnsController.js.map +1 -1
  75. package/lib/module/controllers/EventsController.js +54 -2
  76. package/lib/module/controllers/EventsController.js.map +1 -1
  77. package/lib/module/controllers/LogController.js +185 -0
  78. package/lib/module/controllers/LogController.js.map +1 -0
  79. package/lib/module/controllers/ModalController.js +11 -7
  80. package/lib/module/controllers/ModalController.js.map +1 -1
  81. package/lib/module/controllers/OnRampController.js +135 -52
  82. package/lib/module/controllers/OnRampController.js.map +1 -1
  83. package/lib/module/controllers/OptionsController.js +22 -7
  84. package/lib/module/controllers/OptionsController.js.map +1 -1
  85. package/lib/module/controllers/PublicStateController.js +2 -0
  86. package/lib/module/controllers/PublicStateController.js.map +1 -1
  87. package/lib/module/controllers/RouterController.js +9 -22
  88. package/lib/module/controllers/RouterController.js.map +1 -1
  89. package/lib/module/controllers/SendController.js +114 -96
  90. package/lib/module/controllers/SendController.js.map +1 -1
  91. package/lib/module/controllers/SnackController.js +31 -5
  92. package/lib/module/controllers/SnackController.js.map +1 -1
  93. package/lib/module/controllers/SwapController.js +121 -119
  94. package/lib/module/controllers/SwapController.js.map +1 -1
  95. package/lib/module/controllers/ThemeController.js +7 -0
  96. package/lib/module/controllers/ThemeController.js.map +1 -1
  97. package/lib/module/controllers/TransactionsController.js +25 -17
  98. package/lib/module/controllers/TransactionsController.js.map +1 -1
  99. package/lib/module/controllers/WcController.js +70 -0
  100. package/lib/module/controllers/WcController.js.map +1 -0
  101. package/lib/module/features/reown-authentication/ReownAuthentication.js +268 -0
  102. package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
  103. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
  104. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  105. package/lib/module/features/reown-authentication/index.js +5 -0
  106. package/lib/module/features/reown-authentication/index.js.map +1 -0
  107. package/lib/module/index.js +12 -7
  108. package/lib/module/index.js.map +1 -1
  109. package/lib/module/utils/ApiUtil.js +3 -1
  110. package/lib/module/utils/ApiUtil.js.map +1 -1
  111. package/lib/module/utils/AssetUtil.js +10 -17
  112. package/lib/module/utils/AssetUtil.js.map +1 -1
  113. package/lib/module/utils/ConstantsUtil.js +16 -431
  114. package/lib/module/utils/ConstantsUtil.js.map +1 -1
  115. package/lib/module/utils/CoreHelperUtil.js +35 -8
  116. package/lib/module/utils/CoreHelperUtil.js.map +1 -1
  117. package/lib/module/utils/EventUtil.js +2 -0
  118. package/lib/module/utils/EventUtil.js.map +1 -1
  119. package/lib/module/utils/FetchUtil.js +44 -12
  120. package/lib/module/utils/FetchUtil.js.map +1 -1
  121. package/lib/module/utils/LogUtils.js +121 -0
  122. package/lib/module/utils/LogUtils.js.map +1 -0
  123. package/lib/module/utils/StorageUtil.js +122 -172
  124. package/lib/module/utils/StorageUtil.js.map +1 -1
  125. package/lib/module/utils/SwapApiUtil.js +4 -17
  126. package/lib/module/utils/SwapApiUtil.js.map +1 -1
  127. package/lib/module/utils/SwapCalculationUtil.js +3 -0
  128. package/lib/module/utils/SwapCalculationUtil.js.map +1 -1
  129. package/lib/module/utils/WalletUtil.js +19 -0
  130. package/lib/module/utils/WalletUtil.js.map +1 -0
  131. package/lib/typescript/controllers/ApiController.d.ts +8 -7
  132. package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
  133. package/lib/typescript/controllers/AssetController.d.ts +0 -2
  134. package/lib/typescript/controllers/AssetController.d.ts.map +1 -1
  135. package/lib/typescript/controllers/BlockchainApiController.d.ts +21 -13
  136. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
  137. package/lib/typescript/controllers/ConnectionsController.d.ts +22 -18
  138. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -1
  139. package/lib/typescript/controllers/EnsController.d.ts +1 -1
  140. package/lib/typescript/controllers/EnsController.d.ts.map +1 -1
  141. package/lib/typescript/controllers/EventsController.d.ts +13 -1
  142. package/lib/typescript/controllers/EventsController.d.ts.map +1 -1
  143. package/lib/typescript/controllers/LogController.d.ts +65 -0
  144. package/lib/typescript/controllers/LogController.d.ts.map +1 -0
  145. package/lib/typescript/controllers/ModalController.d.ts +1 -1
  146. package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
  147. package/lib/typescript/controllers/OnRampController.d.ts +4 -3
  148. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
  149. package/lib/typescript/controllers/OptionsController.d.ts +8 -6
  150. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
  151. package/lib/typescript/controllers/RouterController.d.ts +6 -21
  152. package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
  153. package/lib/typescript/controllers/SendController.d.ts +5 -7
  154. package/lib/typescript/controllers/SendController.d.ts.map +1 -1
  155. package/lib/typescript/controllers/SnackController.d.ts +3 -3
  156. package/lib/typescript/controllers/SnackController.d.ts.map +1 -1
  157. package/lib/typescript/controllers/SwapController.d.ts +11 -11
  158. package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
  159. package/lib/typescript/controllers/ThemeController.d.ts +2 -0
  160. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
  161. package/lib/typescript/controllers/TransactionsController.d.ts +3 -3
  162. package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -1
  163. package/lib/typescript/controllers/WcController.d.ts +27 -0
  164. package/lib/typescript/controllers/WcController.d.ts.map +1 -0
  165. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
  166. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
  167. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
  168. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
  169. package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
  170. package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
  171. package/lib/typescript/index.d.ts +6 -8
  172. package/lib/typescript/index.d.ts.map +1 -1
  173. package/lib/typescript/utils/ApiUtil.d.ts +1 -1
  174. package/lib/typescript/utils/AssetUtil.d.ts +3 -4
  175. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
  176. package/lib/typescript/utils/ConstantsUtil.d.ts +7 -416
  177. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
  178. package/lib/typescript/utils/CoreHelperUtil.d.ts +8 -6
  179. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
  180. package/lib/typescript/utils/EventUtil.d.ts +1 -1
  181. package/lib/typescript/utils/EventUtil.d.ts.map +1 -1
  182. package/lib/typescript/utils/FetchUtil.d.ts +3 -3
  183. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
  184. package/lib/typescript/utils/LogUtils.d.ts +15 -0
  185. package/lib/typescript/utils/LogUtils.d.ts.map +1 -0
  186. package/lib/typescript/utils/StorageUtil.d.ts +8 -19
  187. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
  188. package/lib/typescript/utils/SwapApiUtil.d.ts +3 -4
  189. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
  190. package/lib/typescript/utils/SwapCalculationUtil.d.ts +1 -1
  191. package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -1
  192. package/lib/typescript/utils/WalletUtil.d.ts +5 -0
  193. package/lib/typescript/utils/WalletUtil.d.ts.map +1 -0
  194. package/package.json +12 -25
  195. package/src/controllers/ApiController.ts +158 -80
  196. package/src/controllers/AssetController.ts +0 -6
  197. package/src/controllers/BlockchainApiController.ts +109 -144
  198. package/src/controllers/ConnectionsController.ts +372 -88
  199. package/src/controllers/EnsController.ts +5 -3
  200. package/src/controllers/EventsController.ts +74 -3
  201. package/src/controllers/LogController.ts +250 -0
  202. package/src/controllers/ModalController.ts +11 -9
  203. package/src/controllers/OnRampController.ts +195 -82
  204. package/src/controllers/OptionsController.ts +40 -13
  205. package/src/controllers/RouterController.ts +20 -58
  206. package/src/controllers/SendController.ts +148 -112
  207. package/src/controllers/SnackController.ts +31 -5
  208. package/src/controllers/SwapController.ts +140 -148
  209. package/src/controllers/ThemeController.ts +7 -0
  210. package/src/controllers/TransactionsController.ts +26 -18
  211. package/src/controllers/WcController.ts +93 -0
  212. package/src/features/reown-authentication/ReownAuthentication.ts +475 -0
  213. package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
  214. package/src/features/reown-authentication/index.ts +2 -0
  215. package/src/index.ts +15 -22
  216. package/src/utils/ApiUtil.ts +1 -1
  217. package/src/utils/AssetUtil.ts +9 -20
  218. package/src/utils/ConstantsUtil.ts +16 -435
  219. package/src/utils/CoreHelperUtil.ts +62 -11
  220. package/src/utils/EventUtil.ts +1 -1
  221. package/src/utils/FetchUtil.ts +50 -14
  222. package/src/utils/LogUtils.ts +179 -0
  223. package/src/utils/StorageUtil.ts +171 -199
  224. package/src/utils/SwapApiUtil.ts +18 -37
  225. package/src/utils/SwapCalculationUtil.ts +1 -2
  226. package/src/utils/WalletUtil.ts +14 -0
  227. package/lib/commonjs/controllers/AccountController.js +0 -93
  228. package/lib/commonjs/controllers/AccountController.js.map +0 -1
  229. package/lib/commonjs/controllers/ConnectionController.js +0 -132
  230. package/lib/commonjs/controllers/ConnectionController.js.map +0 -1
  231. package/lib/commonjs/controllers/ConnectorController.js +0 -50
  232. package/lib/commonjs/controllers/ConnectorController.js.map +0 -1
  233. package/lib/commonjs/controllers/NetworkController.js +0 -90
  234. package/lib/commonjs/controllers/NetworkController.js.map +0 -1
  235. package/lib/commonjs/controllers/WebviewController.js +0 -52
  236. package/lib/commonjs/controllers/WebviewController.js.map +0 -1
  237. package/lib/commonjs/utils/NetworkUtil.js +0 -46
  238. package/lib/commonjs/utils/NetworkUtil.js.map +0 -1
  239. package/lib/commonjs/utils/RouterUtil.js +0 -25
  240. package/lib/commonjs/utils/RouterUtil.js.map +0 -1
  241. package/lib/commonjs/utils/TypeUtil.js +0 -6
  242. package/lib/commonjs/utils/TypeUtil.js.map +0 -1
  243. package/lib/module/controllers/AccountController.js +0 -88
  244. package/lib/module/controllers/AccountController.js.map +0 -1
  245. package/lib/module/controllers/ConnectionController.js +0 -127
  246. package/lib/module/controllers/ConnectionController.js.map +0 -1
  247. package/lib/module/controllers/ConnectorController.js +0 -45
  248. package/lib/module/controllers/ConnectorController.js.map +0 -1
  249. package/lib/module/controllers/NetworkController.js +0 -85
  250. package/lib/module/controllers/NetworkController.js.map +0 -1
  251. package/lib/module/controllers/WebviewController.js +0 -47
  252. package/lib/module/controllers/WebviewController.js.map +0 -1
  253. package/lib/module/utils/NetworkUtil.js +0 -40
  254. package/lib/module/utils/NetworkUtil.js.map +0 -1
  255. package/lib/module/utils/RouterUtil.js +0 -19
  256. package/lib/module/utils/RouterUtil.js.map +0 -1
  257. package/lib/module/utils/TypeUtil.js +0 -2
  258. package/lib/module/utils/TypeUtil.js.map +0 -1
  259. package/lib/typescript/controllers/AccountController.d.ts +0 -33
  260. package/lib/typescript/controllers/AccountController.d.ts.map +0 -1
  261. package/lib/typescript/controllers/ConnectionController.d.ts +0 -68
  262. package/lib/typescript/controllers/ConnectionController.d.ts.map +0 -1
  263. package/lib/typescript/controllers/ConnectorController.d.ts +0 -18
  264. package/lib/typescript/controllers/ConnectorController.d.ts.map +0 -1
  265. package/lib/typescript/controllers/NetworkController.d.ts +0 -34
  266. package/lib/typescript/controllers/NetworkController.d.ts.map +0 -1
  267. package/lib/typescript/controllers/WebviewController.d.ts +0 -21
  268. package/lib/typescript/controllers/WebviewController.d.ts.map +0 -1
  269. package/lib/typescript/utils/NetworkUtil.d.ts +0 -8
  270. package/lib/typescript/utils/NetworkUtil.d.ts.map +0 -1
  271. package/lib/typescript/utils/RouterUtil.d.ts +0 -5
  272. package/lib/typescript/utils/RouterUtil.d.ts.map +0 -1
  273. package/lib/typescript/utils/TypeUtil.d.ts +0 -817
  274. package/lib/typescript/utils/TypeUtil.d.ts.map +0 -1
  275. package/src/controllers/AccountController.ts +0 -128
  276. package/src/controllers/ConnectionController.ts +0 -208
  277. package/src/controllers/ConnectorController.ts +0 -64
  278. package/src/controllers/NetworkController.ts +0 -120
  279. package/src/controllers/WebviewController.ts +0 -63
  280. package/src/utils/NetworkUtil.ts +0 -33
  281. package/src/utils/RouterUtil.ts +0 -21
  282. package/src/utils/TypeUtil.ts +0 -995
@@ -0,0 +1,93 @@
1
+ import { proxy } from 'valtio';
2
+ import { subscribeKey as subKey } from 'valtio/utils';
3
+ import type { WalletDeepLink, WcWallet } from '@reown/appkit-common-react-native';
4
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
5
+ import { StorageUtil } from '../utils/StorageUtil';
6
+
7
+ // -- Types --------------------------------------------- //
8
+ export interface WcControllerState {
9
+ wcUri?: string;
10
+ wcPromise?: Promise<void>;
11
+ wcPairingExpiry?: number;
12
+ wcLinking?: WalletDeepLink;
13
+ wcError?: boolean;
14
+ pressedWallet?: WcWallet;
15
+ recentWallets?: WcWallet[];
16
+ }
17
+
18
+ type StateKey = keyof WcControllerState;
19
+
20
+ // -- State --------------------------------------------- //
21
+ const state = proxy<WcControllerState>({
22
+ wcError: false
23
+ });
24
+
25
+ // -- Controller ---------------------------------------- //
26
+ export const WcController = {
27
+ state,
28
+
29
+ subscribeKey<K extends StateKey>(key: K, callback: (value: WcControllerState[K]) => void) {
30
+ return subKey(state, key, callback);
31
+ },
32
+
33
+ setWcLinking(wcLinking: WcControllerState['wcLinking']) {
34
+ state.wcLinking = wcLinking;
35
+ },
36
+
37
+ removeWcLinking() {
38
+ state.wcLinking = undefined;
39
+ },
40
+
41
+ setWcError(wcError: WcControllerState['wcError']) {
42
+ state.wcError = wcError;
43
+ },
44
+
45
+ setPressedWallet(wallet: WcControllerState['pressedWallet']) {
46
+ state.pressedWallet = wallet;
47
+ },
48
+
49
+ removePressedWallet() {
50
+ state.pressedWallet = undefined;
51
+ },
52
+
53
+ setWcPromise(wcPromise: WcControllerState['wcPromise']) {
54
+ state.wcPromise = wcPromise;
55
+ },
56
+
57
+ setWcUri(wcUri: WcControllerState['wcUri']) {
58
+ state.wcUri = wcUri;
59
+ state.wcPairingExpiry = CoreHelperUtil.getPairingExpiry();
60
+ },
61
+
62
+ setRecentWallets(wallets: WcControllerState['recentWallets']) {
63
+ state.recentWallets = wallets;
64
+ },
65
+
66
+ async addRecentWallet(wallet: WcWallet) {
67
+ const recentWallets = await StorageUtil.addRecentWallet(wallet);
68
+ if (recentWallets) {
69
+ WcController.setRecentWallets(recentWallets);
70
+ }
71
+ },
72
+
73
+ setConnectedWallet: async (wcLinking: WalletDeepLink, pressedWallet?: WcWallet) => {
74
+ StorageUtil.setWalletConnectDeepLink(wcLinking);
75
+
76
+ if (pressedWallet) {
77
+ WcController.addRecentWallet(pressedWallet);
78
+ }
79
+ },
80
+
81
+ clearUri() {
82
+ state.wcUri = undefined;
83
+ state.wcPairingExpiry = undefined;
84
+ state.wcPromise = undefined;
85
+ state.wcLinking = undefined;
86
+ },
87
+
88
+ resetState() {
89
+ this.clearUri();
90
+ state.pressedWallet = undefined;
91
+ StorageUtil.removeWalletConnectDeepLink();
92
+ }
93
+ };
@@ -0,0 +1,475 @@
1
+ import {
2
+ type CaipNetworkId,
3
+ type ChainNamespace,
4
+ type SafeStorageItems,
5
+ SafeStorageKeys,
6
+ type SIWXConfig,
7
+ type SIWXMessage,
8
+ type SIWXSession
9
+ } from '@reown/appkit-common-react-native';
10
+
11
+ import { ApiController } from '../../controllers/ApiController';
12
+ import { BlockchainApiController } from '../../controllers/BlockchainApiController';
13
+
14
+ import { ReownAuthenticationMessenger } from './ReownAuthenticationMessenger';
15
+ import { ConnectionsController } from '../../controllers/ConnectionsController';
16
+ import { CoreHelperUtil } from '../../utils/CoreHelperUtil';
17
+ import { OptionsController } from '../../controllers/OptionsController';
18
+ import { FetchUtil } from '../../utils/FetchUtil';
19
+ import { ApiUtil } from '../../utils/ApiUtil';
20
+ import { LogController } from '../../controllers/LogController';
21
+
22
+ /**
23
+ * This is the configuration for using SIWX with Reown Authentication service.
24
+ * It allows you to authenticate and capture user sessions through the Reown Dashboard.
25
+ */
26
+ export class ReownAuthentication implements SIWXConfig {
27
+ private readonly localAuthStorageKey: keyof SafeStorageItems;
28
+ private readonly localNonceStorageKey: keyof SafeStorageItems;
29
+ private readonly messenger: ReownAuthenticationMessenger;
30
+ private readonly fetchUtil = new FetchUtil({ baseUrl: CoreHelperUtil.getApiUrl() });
31
+ public readonly signOutOnDisconnect: boolean;
32
+
33
+ private required: boolean;
34
+
35
+ private listeners: ReownAuthentication.EventListeners = {
36
+ sessionChanged: []
37
+ };
38
+
39
+ constructor(params: ReownAuthentication.ConstructorParams = {}) {
40
+ this.localAuthStorageKey =
41
+ (params.localAuthStorageKey as keyof SafeStorageItems) || SafeStorageKeys.SIWX_AUTH_TOKEN;
42
+ this.localNonceStorageKey =
43
+ (params.localNonceStorageKey as keyof SafeStorageItems) || SafeStorageKeys.SIWX_NONCE_TOKEN;
44
+ this.required = params.required ?? true;
45
+
46
+ this.messenger = new ReownAuthenticationMessenger({
47
+ getNonce: this.getNonce.bind(this)
48
+ });
49
+
50
+ this.signOutOnDisconnect = params.signOutOnDisconnect ?? true;
51
+ }
52
+
53
+ async createMessage(input: SIWXMessage.Input): Promise<SIWXMessage> {
54
+ return this.messenger.createMessage(input);
55
+ }
56
+
57
+ async addSession(session: SIWXSession): Promise<void> {
58
+ const response = await this.request({
59
+ method: 'POST',
60
+ key: 'authenticate',
61
+ body: {
62
+ data: session.cacao ? undefined : session.data,
63
+ message: session.message,
64
+ signature: session.signature,
65
+ clientId: this.getClientId(),
66
+ walletInfo: this.getWalletInfo()
67
+ },
68
+ headers: ['nonce']
69
+ });
70
+
71
+ this.setStorageToken(response.token, this.localAuthStorageKey);
72
+
73
+ this.emit('sessionChanged', session);
74
+ }
75
+
76
+ async getSessions(chainId: CaipNetworkId, address: string): Promise<SIWXSession[]> {
77
+ try {
78
+ const sessions = await this.getStorageToken(this.localAuthStorageKey);
79
+ if (!sessions) {
80
+ return [];
81
+ }
82
+
83
+ const account = await this.request({
84
+ method: 'GET',
85
+ key: 'me',
86
+ query: {},
87
+ headers: ['auth']
88
+ });
89
+
90
+ if (!account) {
91
+ return [];
92
+ }
93
+
94
+ const isSameAddress = account.address.toLowerCase() === address.toLowerCase();
95
+ const isSameNetwork = account.caip2Network === chainId;
96
+
97
+ if (!isSameAddress || !isSameNetwork) {
98
+ return [];
99
+ }
100
+
101
+ const session: SIWXSession = {
102
+ data: {
103
+ accountAddress: account.address,
104
+ chainId: account.caip2Network
105
+ } as SIWXMessage.Data,
106
+ message: '',
107
+ signature: ''
108
+ };
109
+
110
+ this.emit('sessionChanged', session);
111
+
112
+ return [session];
113
+ } catch (error) {
114
+ LogController.sendError(error, 'ReownAuthentication.ts', 'getSessions');
115
+
116
+ return [];
117
+ }
118
+ }
119
+
120
+ async revokeSession(_chainId: CaipNetworkId, _address: string): Promise<void> {
121
+ return Promise.resolve(this.clearStorageTokens());
122
+ }
123
+
124
+ async setSessions(sessions: SIWXSession[]): Promise<void> {
125
+ if (sessions.length === 0) {
126
+ this.clearStorageTokens();
127
+ } else {
128
+ const session = (sessions.find(
129
+ s => s.data.chainId === ConnectionsController.state.activeCaipNetworkId
130
+ ) || sessions[0]) as SIWXSession;
131
+
132
+ await this.addSession(session);
133
+ }
134
+ }
135
+
136
+ getRequired() {
137
+ return this.required;
138
+ }
139
+
140
+ async getSessionAccount() {
141
+ const sessions = await this.getStorageToken(this.localAuthStorageKey);
142
+ if (!sessions) {
143
+ throw new Error('Not authenticated');
144
+ }
145
+
146
+ return this.request({
147
+ method: 'GET',
148
+ key: 'me',
149
+ body: undefined,
150
+ query: {
151
+ includeAppKitAccount: true
152
+ },
153
+ headers: ['auth']
154
+ });
155
+ }
156
+
157
+ async setSessionAccountMetadata(metadata: object | null = null) {
158
+ const sessions = await this.getStorageToken(this.localAuthStorageKey);
159
+ if (!sessions) {
160
+ throw new Error('Not authenticated');
161
+ }
162
+
163
+ return this.request({
164
+ method: 'PUT',
165
+ key: 'account-metadata',
166
+ body: { metadata },
167
+ headers: ['auth']
168
+ });
169
+ }
170
+
171
+ on<Event extends keyof ReownAuthentication.Events>(
172
+ event: Event,
173
+ callback: ReownAuthentication.Listener<Event>
174
+ ) {
175
+ this.listeners[event].push(callback);
176
+
177
+ return () => {
178
+ this.listeners[event] = this.listeners[event].filter(
179
+ cb => cb !== callback
180
+ ) as ReownAuthentication.EventListeners[Event];
181
+ };
182
+ }
183
+
184
+ removeAllListeners() {
185
+ const keys = Object.keys(this.listeners) as (keyof ReownAuthentication.Events)[];
186
+ keys.forEach(key => {
187
+ this.listeners[key] = [];
188
+ });
189
+ }
190
+
191
+ private async request<
192
+ Method extends ReownAuthentication.Methods,
193
+ Key extends ReownAuthentication.RequestKeys<Method>
194
+ >({
195
+ method,
196
+ key,
197
+ query,
198
+ body,
199
+ headers
200
+ }: ReownAuthentication.RequestParams<Key, Method>): Promise<
201
+ ReownAuthentication.RequestResponse<Method, Key>
202
+ > {
203
+ const { projectId, st, sv, domain } = this.getSDKProperties();
204
+
205
+ const url = this.fetchUtil.createUrl({
206
+ path: `/auth/v1/${String(key)}`,
207
+ params: { projectId, st, sv, domain, ...query }
208
+ });
209
+
210
+ const nonceJwt = await this.getStorageToken(this.localNonceStorageKey);
211
+ const auth = await this.getStorageToken(this.localAuthStorageKey);
212
+
213
+ const response = await fetch(url, {
214
+ method,
215
+ body: body ? JSON.stringify(body) : undefined,
216
+ headers: {
217
+ 'Content-Type': 'application/json',
218
+ ...(Array.isArray(headers)
219
+ ? headers.reduce((acc, header) => {
220
+ switch (header) {
221
+ case 'nonce':
222
+ acc['x-nonce-jwt'] = `Bearer ${nonceJwt}`;
223
+ break;
224
+ case 'auth':
225
+ acc['Authorization'] = `Bearer ${auth}`;
226
+ break;
227
+ default:
228
+ break;
229
+ }
230
+
231
+ return acc;
232
+ }, {})
233
+ : {})
234
+ }
235
+ });
236
+
237
+ if (!response.ok) {
238
+ throw new Error(await response.text());
239
+ }
240
+
241
+ if (response.headers.get('content-type')?.includes('application/json')) {
242
+ return response.json();
243
+ }
244
+
245
+ return null as ReownAuthentication.RequestResponse<Method, Key>;
246
+ }
247
+
248
+ private async getStorageToken(key: keyof SafeStorageItems): Promise<string | undefined> {
249
+ return OptionsController.getStorage().getItem(key);
250
+ }
251
+
252
+ private setStorageToken(token: string, key: keyof SafeStorageItems): void {
253
+ OptionsController.getStorage().setItem(key, token);
254
+ }
255
+
256
+ private clearStorageTokens(): void {
257
+ OptionsController.getStorage().removeItem(this.localAuthStorageKey);
258
+ OptionsController.getStorage().removeItem(this.localNonceStorageKey);
259
+ this.emit('sessionChanged', undefined);
260
+ }
261
+
262
+ private async getNonce(): Promise<string> {
263
+ const { nonce, token } = await this.request({
264
+ method: 'GET',
265
+ key: 'nonce'
266
+ });
267
+
268
+ this.setStorageToken(token, this.localNonceStorageKey);
269
+
270
+ return nonce;
271
+ }
272
+
273
+ private getClientId(): string | null {
274
+ return BlockchainApiController.state.clientId;
275
+ }
276
+
277
+ private getWalletInfo(): ReownAuthentication.WalletInfo | undefined {
278
+ const connectedWalletInfo = ConnectionsController.state.walletInfo;
279
+ const connectionProperties = ConnectionsController.state.connection?.properties;
280
+
281
+ if (!connectedWalletInfo || !connectionProperties) {
282
+ return undefined;
283
+ }
284
+
285
+ if (connectionProperties?.provider) {
286
+ const social = connectionProperties.provider;
287
+ const identifier = connectionProperties.email || 'unknown';
288
+
289
+ return { type: 'social', social, identifier };
290
+ }
291
+
292
+ let { name, icon, type } = connectedWalletInfo;
293
+
294
+ if (!type || type !== 'walletconnect') {
295
+ type = 'unknown';
296
+ }
297
+
298
+ return {
299
+ type,
300
+ name,
301
+ icon
302
+ };
303
+ }
304
+
305
+ private getSDKProperties(): { projectId: string; st: string; sv: string; domain: string } {
306
+ const { projectId, st, sv } = ApiController._getApiParams();
307
+ const domain = ApiUtil.getOrigin();
308
+
309
+ return {
310
+ projectId,
311
+ st,
312
+ sv,
313
+ domain
314
+ };
315
+ }
316
+
317
+ private emit<Event extends keyof ReownAuthentication.Events>(
318
+ event: Event,
319
+ data: ReownAuthentication.Events[Event]
320
+ ) {
321
+ this.listeners[event].forEach(listener => listener(data));
322
+ }
323
+ }
324
+
325
+ export namespace ReownAuthentication {
326
+ export type ConstructorParams = {
327
+ /**
328
+ * The key to use for storing the session token in local storage.
329
+ * @default '@appkit/siwx-auth-token'
330
+ */
331
+ localAuthStorageKey?: string;
332
+ /**
333
+ * The key to use for storing the nonce token in local storage.
334
+ * @default '@appkit/siwx-nonce-token'
335
+ */
336
+ localNonceStorageKey?: string;
337
+ /**
338
+ * If false the wallet stays connected when user denies the signature request.
339
+ * @default true
340
+ */
341
+ required?: boolean;
342
+ /**
343
+ * This flag determines whether the session should be cleared when the user disconnects.
344
+ *
345
+ * @default true
346
+ */
347
+ signOutOnDisconnect?: boolean;
348
+ };
349
+
350
+ export type AvailableRequestHeaders = {
351
+ nonce: {
352
+ 'x-nonce-jwt': string;
353
+ };
354
+ auth: {
355
+ Authorization: string;
356
+ };
357
+ origin: {
358
+ origin: string;
359
+ };
360
+ };
361
+
362
+ export type RequestParams<Key extends keyof Requests[Method], Method extends Methods> = {
363
+ method: Method;
364
+ key: Key;
365
+ // @ts-expect-error - This is matching correctly already
366
+ } & Pick<Requests[Method][Key], 'query' | 'body' | 'headers'>;
367
+
368
+ export type RequestResponse<
369
+ Method extends Methods,
370
+ Key extends RequestKeys<Method>
371
+ // @ts-expect-error - This is matching correctly already
372
+ > = Requests[Method][Key]['response'];
373
+
374
+ export type Request<
375
+ Body,
376
+ Response,
377
+ Query extends Record<string, unknown> | undefined = undefined,
378
+ Headers extends (keyof AvailableRequestHeaders)[] | undefined = undefined
379
+ > = (Response extends undefined
380
+ ? {
381
+ response?: never;
382
+ }
383
+ : {
384
+ response: Response;
385
+ }) &
386
+ (Body extends undefined ? { body?: never } : { body: Body }) &
387
+ (Query extends undefined ? { query?: never } : { query: Query }) &
388
+ (Headers extends undefined ? { headers?: never } : { headers: Headers });
389
+
390
+ export type Requests = {
391
+ GET: {
392
+ nonce: Request<undefined, { nonce: string; token: string }>;
393
+ me: Request<
394
+ undefined,
395
+ Omit<SessionAccount, 'appKitAccount'>,
396
+ { includeAppKitAccount?: boolean },
397
+ ['auth']
398
+ >;
399
+ };
400
+ POST: {
401
+ 'authenticate': Request<
402
+ {
403
+ data?: SIWXMessage.Data;
404
+ message: string;
405
+ signature: string;
406
+ clientId?: string | null;
407
+ walletInfo?: WalletInfo;
408
+ },
409
+ {
410
+ token: string;
411
+ },
412
+ undefined,
413
+ ['nonce']
414
+ >;
415
+ 'sign-out': Request<undefined, never, never, ['auth']>;
416
+ };
417
+ PUT: {
418
+ 'account-metadata': Request<{ metadata: object | null }, unknown, undefined, ['auth']>;
419
+ };
420
+ };
421
+
422
+ export type Methods = 'GET' | 'POST' | 'PUT';
423
+
424
+ export type RequestKeys<Method extends Methods> = keyof Requests[Method];
425
+
426
+ export type WalletInfo =
427
+ | {
428
+ type: 'walletconnect' | 'external' | 'unknown';
429
+ name: string | undefined;
430
+ icon: string | undefined;
431
+ }
432
+ | { type: 'social'; social: string; identifier: string };
433
+
434
+ export type Events = {
435
+ sessionChanged: SIWXSession | undefined;
436
+ };
437
+
438
+ export type Listener<Event extends keyof Events> = (event: Events[Event]) => void;
439
+
440
+ export type EventListeners = {
441
+ [Key in keyof Events]: Listener<Key>[];
442
+ };
443
+
444
+ export type SessionAccount = {
445
+ aud: string;
446
+ iss: string;
447
+ exp: number;
448
+ projectIdKey: string;
449
+ sub: string;
450
+ address: string;
451
+ chainId: number | string;
452
+ chainNamespace: ChainNamespace;
453
+ caip2Network: string;
454
+ uri: string;
455
+ domain: string;
456
+ projectUuid: string;
457
+ profileUuid: string;
458
+ nonce: string;
459
+ email?: string;
460
+ appKitAccount?: {
461
+ uuid: string;
462
+ caip2_chain: string;
463
+ address: string;
464
+ profile_uuid: string;
465
+ created_at: string;
466
+ is_main_account: boolean;
467
+ verification_status: null;
468
+ connection_method: object | null;
469
+ metadata: object;
470
+ last_signed_in_at: string;
471
+ signed_up_at: string;
472
+ updated_at: string;
473
+ };
474
+ };
475
+ }
@@ -0,0 +1,80 @@
1
+ import type { SIWXMessage, CaipNetworkId } from '@reown/appkit-common-react-native';
2
+ import { ConnectionsController } from '../../controllers/ConnectionsController';
3
+ import { OptionsController } from '../../controllers/OptionsController';
4
+ import { CoreHelperUtil } from '../../utils/CoreHelperUtil';
5
+
6
+ export class ReownAuthenticationMessenger {
7
+ public resources?: SIWXMessage['resources'];
8
+
9
+ protected getNonce: (params: SIWXMessage.Input) => Promise<SIWXMessage['nonce']>;
10
+
11
+ constructor(params: ReownAuthenticationMessenger.ConstructorParams) {
12
+ this.getNonce = params.getNonce;
13
+ }
14
+
15
+ async createMessage(input: SIWXMessage.Input): Promise<SIWXMessage> {
16
+ const metadata = OptionsController.state.metadata;
17
+ const domain = CoreHelperUtil.getBundleId();
18
+ const params = {
19
+ accountAddress: input.accountAddress,
20
+ chainId: input.chainId,
21
+ version: '1',
22
+ domain: domain ?? 'Unknown Domain',
23
+ uri: metadata?.redirect?.universal ?? metadata?.redirect?.native ?? 'Unknown URI',
24
+ resources: this.resources,
25
+ nonce: await this.getNonce(input),
26
+ issuedAt: this.stringifyDate(new Date()),
27
+ statement: undefined,
28
+ expirationTime: undefined,
29
+ notBefore: undefined
30
+ };
31
+
32
+ const methods = {
33
+ toString: () => this.stringify(params)
34
+ } satisfies SIWXMessage.Methods;
35
+
36
+ return Object.assign(params, methods);
37
+ }
38
+
39
+ private stringify(params: SIWXMessage.Data): string {
40
+ const networkName = this.getNetworkName(params.chainId);
41
+
42
+ return [
43
+ `${params.domain} wants you to sign in with your ${networkName} account:`,
44
+ params.accountAddress,
45
+ params.statement ? `\n${params.statement}\n` : '',
46
+ `URI: ${params.uri}`,
47
+ `Version: ${params.version}`,
48
+ `Chain ID: ${params.chainId}`,
49
+ `Nonce: ${params.nonce}`,
50
+ params.issuedAt && `Issued At: ${params.issuedAt}`,
51
+ params.expirationTime && `Expiration Time: ${params.expirationTime}`,
52
+ params.notBefore && `Not Before: ${params.notBefore}`,
53
+ params.requestId && `Request ID: ${params.requestId}`,
54
+ params.resources?.length &&
55
+ params.resources.reduce((acc, resource) => `${acc}\n- ${resource}`, 'Resources:')
56
+ ]
57
+ .filter(line => typeof line === 'string')
58
+ .join('\n')
59
+ .trim();
60
+ }
61
+
62
+ private getNetworkName(chainId: CaipNetworkId): string | undefined {
63
+ const requestedNetworks = ConnectionsController.state.networks;
64
+
65
+ return (
66
+ requestedNetworks.find(network => network.caipNetworkId === chainId)?.name ||
67
+ 'Unknown Network'
68
+ );
69
+ }
70
+
71
+ private stringifyDate(date: Date): string {
72
+ return date.toISOString();
73
+ }
74
+ }
75
+
76
+ export namespace ReownAuthenticationMessenger {
77
+ export interface ConstructorParams {
78
+ getNonce: (params: SIWXMessage.Input) => Promise<SIWXMessage['nonce']>;
79
+ }
80
+ }
@@ -0,0 +1,2 @@
1
+ export * from './ReownAuthentication';
2
+ export * from './ReownAuthenticationMessenger';