@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
@@ -1,5 +1,5 @@
1
1
  import { proxy, ref } from 'valtio';
2
- import { derive } from 'valtio/utils';
2
+ import { derive } from 'derive-valtio';
3
3
  import {
4
4
  EVMAdapter,
5
5
  type AppKitNetwork,
@@ -7,23 +7,22 @@ import {
7
7
  type CaipAddress,
8
8
  type CaipNetworkId,
9
9
  type ChainNamespace,
10
- type GetBalanceResponse,
11
- type WalletInfo
10
+ type Balance,
11
+ type WalletInfo,
12
+ type ConnectionProperties,
13
+ type AccountType,
14
+ type Connection,
15
+ SolanaBaseAdapter,
16
+ BitcoinBaseAdapter,
17
+ type Identity
12
18
  } from '@reown/appkit-common-react-native';
13
19
  import { StorageUtil } from '../utils/StorageUtil';
20
+ import { BlockchainApiController } from './BlockchainApiController';
21
+ import { SnackController } from './SnackController';
22
+ import { LogController } from './LogController';
23
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
14
24
 
15
25
  // -- Types --------------------------------------------- //
16
- type Balance = GetBalanceResponse;
17
-
18
- //TODO: balance could be elsewhere
19
- interface Connection {
20
- accounts: CaipAddress[];
21
- balances: Record<CaipAddress, Balance>; //TODO: make this an array of balances
22
- adapter: BlockchainAdapter;
23
- caipNetwork: CaipNetworkId;
24
- wallet?: WalletInfo;
25
- }
26
-
27
26
  export interface ConnectionsControllerState {
28
27
  activeNamespace?: ChainNamespace;
29
28
  connections: Map<ChainNamespace, Connection>;
@@ -37,59 +36,117 @@ const baseState = proxy<ConnectionsControllerState>({
37
36
  networks: []
38
37
  });
39
38
 
39
+ // -- Helper Functions --------------------------------------------- //
40
+ const getActiveConnection = (snap: ConnectionsControllerState): Connection | undefined => {
41
+ if (!snap.activeNamespace) return undefined;
42
+
43
+ return snap.connections.get(snap.activeNamespace);
44
+ };
45
+
46
+ const hasValidAccounts = (connection: Connection): boolean => {
47
+ return connection?.accounts && connection.accounts.length > 0;
48
+ };
49
+
50
+ const findSmartAccountForNetwork = (connection: Connection): CaipAddress | undefined => {
51
+ return connection.properties?.smartAccounts?.find(account =>
52
+ account.startsWith(connection.caipNetwork)
53
+ );
54
+ };
55
+
56
+ const findEOAForNetwork = (connection: Connection): CaipAddress | undefined => {
57
+ const smartAccounts = connection.properties?.smartAccounts || [];
58
+
59
+ return connection.accounts.find(
60
+ account => account.startsWith(connection.caipNetwork) && !smartAccounts.includes(account)
61
+ );
62
+ };
63
+
64
+ const getActiveAddress = (connection: Connection): CaipAddress | undefined => {
65
+ if (!hasValidAccounts(connection)) {
66
+ return undefined;
67
+ }
68
+
69
+ // For smart accounts, prioritize smart account addresses
70
+ if (connection.type === 'smartAccount') {
71
+ const smartAccount = findSmartAccountForNetwork(connection);
72
+ if (smartAccount) {
73
+ return smartAccount;
74
+ }
75
+ }
76
+
77
+ // Fall back to EOA or any account that matches the network
78
+ return findEOAForNetwork(connection);
79
+ };
80
+
81
+ const updateConnection = (
82
+ namespace: ChainNamespace,
83
+ connection: Connection,
84
+ updates: Partial<Connection>
85
+ ): Connection => {
86
+ const newConnectionsMap = new Map(baseState.connections);
87
+ const newConnection = { ...connection, ...updates };
88
+ newConnectionsMap.set(namespace, newConnection);
89
+ baseState.connections = newConnectionsMap;
90
+
91
+ return newConnection;
92
+ };
93
+
94
+ const getActiveIdentity = (connection: Connection): Identity | undefined => {
95
+ const activeAddress = getActiveAddress(connection);
96
+ if (!activeAddress) return undefined;
97
+
98
+ return connection.identities?.get(activeAddress);
99
+ };
100
+
40
101
  const derivedState = derive(
41
102
  {
42
- activeAddress: (get): CaipAddress | undefined => {
103
+ isConnected: (get): boolean => {
43
104
  const snap = get(baseState);
44
105
 
45
- if (!snap.activeNamespace) {
46
- return undefined;
47
- }
48
-
49
- const connection = snap.connections.get(snap.activeNamespace);
50
-
51
- if (!connection || !connection.accounts || connection.accounts.length === 0) {
52
- return undefined;
53
- }
106
+ return !!snap.activeNamespace && !!snap.connections.size;
107
+ },
108
+ activeAddress: (get): CaipAddress | undefined => {
109
+ const snap = get(baseState);
110
+ const connection = getActiveConnection(snap);
54
111
 
55
- //TODO: what happens if there are several accounts on the same chain?
56
- const activeAccount = connection.accounts.find(account =>
57
- account.startsWith(connection.caipNetwork)
58
- );
112
+ return connection ? getActiveAddress(connection) : undefined;
113
+ },
114
+ identity: (get): Identity | undefined => {
115
+ const snap = get(baseState);
116
+ const connection = getActiveConnection(snap);
59
117
 
60
- return activeAccount;
118
+ return connection ? getActiveIdentity(connection) : undefined;
61
119
  },
62
120
  activeBalance: (get): Balance | undefined => {
63
121
  const snap = get(baseState);
122
+ const connection = getActiveConnection(snap);
64
123
 
65
- if (!snap.activeNamespace) return undefined;
66
- const connection = snap.connections.get(snap.activeNamespace);
67
-
68
- if (!connection || !connection.accounts || connection.accounts.length === 0) {
124
+ if (!connection) {
69
125
  return undefined;
70
126
  }
71
127
 
72
- const activeAccount = connection.accounts.find(account =>
73
- account.startsWith(connection.caipNetwork)
74
- );
128
+ const activeAddress = getActiveAddress(connection);
129
+ if (!activeAddress || !connection.balances || connection.balances.size === 0) {
130
+ return undefined;
131
+ }
75
132
 
76
- if (
77
- !connection ||
78
- !connection.balances ||
79
- !activeAccount ||
80
- Object.keys(connection.balances).length === 0
81
- ) {
133
+ const addressBalances = connection.balances.get(activeAddress);
134
+ if (!addressBalances || addressBalances.length === 0) {
82
135
  return undefined;
83
136
  }
84
137
 
85
- return connection.balances[activeAccount];
138
+ // Return the native token (first balance without address)
139
+ const nativeToken = addressBalances.find(balance => !balance.address);
140
+ if (nativeToken) {
141
+ return nativeToken;
142
+ }
143
+
144
+ // Fallback to first available balance
145
+ return addressBalances[0];
86
146
  },
87
147
  activeNetwork: (get): AppKitNetwork | undefined => {
88
148
  const snap = get(baseState);
89
-
90
- if (!snap.activeNamespace) return undefined;
91
-
92
- const connection = snap.connections.get(snap.activeNamespace);
149
+ const connection = getActiveConnection(snap);
93
150
 
94
151
  if (!connection) return undefined;
95
152
 
@@ -101,21 +158,46 @@ const derivedState = derive(
101
158
  },
102
159
  activeCaipNetworkId: (get): CaipNetworkId | undefined => {
103
160
  const snap = get(baseState);
161
+ const connection = getActiveConnection(snap);
104
162
 
105
- if (!snap.activeNamespace) return undefined;
163
+ return connection?.caipNetwork;
164
+ },
165
+ accountType: (get): AccountType | undefined => {
166
+ const snap = get(baseState);
167
+ const connection = getActiveConnection(snap);
106
168
 
107
- const connection = snap.connections.get(snap.activeNamespace);
169
+ return connection?.type;
170
+ },
171
+ connection: (get): Connection | undefined => {
172
+ const snap = get(baseState);
108
173
 
109
- if (!connection) return undefined;
174
+ return getActiveConnection(snap);
175
+ },
176
+ balances: (get): Balance[] | undefined => {
177
+ const snap = get(baseState);
178
+
179
+ const _connection = getActiveConnection(snap);
180
+
181
+ if (!_connection) {
182
+ return undefined;
183
+ }
110
184
 
111
- return connection.caipNetwork;
185
+ const _activeAddress = getActiveAddress(_connection);
186
+
187
+ if (!_activeAddress) return [];
188
+
189
+ return (
190
+ _connection?.balances
191
+ .get(_activeAddress)
192
+ // Filter out tokens with no quantity
193
+ ?.filter(balance => balance?.quantity?.numeric)
194
+ );
112
195
  },
113
196
  walletInfo: (get): WalletInfo | undefined => {
114
197
  const snap = get(baseState);
198
+ const connection = getActiveConnection(snap);
115
199
 
116
- if (!snap.activeNamespace) return undefined;
117
-
118
- return snap.connections.get(snap.activeNamespace)?.wallet;
200
+ return connection?.wallet;
119
201
  }
120
202
  },
121
203
  {
@@ -128,32 +210,43 @@ export const ConnectionsController = {
128
210
  state: derivedState,
129
211
 
130
212
  setActiveNamespace(namespace?: ChainNamespace) {
213
+ if (baseState.activeNamespace === namespace) {
214
+ return;
215
+ }
216
+
131
217
  baseState.activeNamespace = namespace;
132
218
  StorageUtil.setActiveNamespace(namespace);
133
219
  },
134
220
 
135
- storeConnection({
136
- namespace,
137
- adapter,
221
+ setConnection({
138
222
  accounts,
139
- chains,
140
- wallet,
141
- caipNetwork
223
+ adapter,
224
+ caipNetwork,
225
+ namespace,
226
+ properties,
227
+ wallet
142
228
  }: {
143
- namespace: ChainNamespace;
144
- adapter: BlockchainAdapter;
145
229
  accounts: CaipAddress[];
146
- chains: CaipNetworkId[];
230
+ adapter: BlockchainAdapter;
231
+ caipNetwork: CaipNetworkId;
232
+ namespace: ChainNamespace;
233
+ properties?: ConnectionProperties;
147
234
  wallet?: WalletInfo;
148
- caipNetwork?: CaipNetworkId;
149
235
  }) {
150
- const newConnectionEntry = {
151
- balances: {},
152
- caipNetwork: caipNetwork ?? chains[0]!,
236
+ const type: AccountType =
237
+ properties?.smartAccounts?.length &&
238
+ properties.smartAccounts.find(account => account.startsWith(caipNetwork))
239
+ ? 'smartAccount'
240
+ : 'eoa';
241
+
242
+ const newConnectionEntry: Connection = {
243
+ balances: new Map<CaipAddress, Balance[]>(),
244
+ caipNetwork,
153
245
  adapter: ref(adapter),
154
246
  accounts,
155
- chains,
156
- wallet
247
+ wallet,
248
+ properties,
249
+ type
157
250
  };
158
251
 
159
252
  // Create a new Map to ensure Valtio detects the change
@@ -162,29 +255,83 @@ export const ConnectionsController = {
162
255
  baseState.connections = newConnectionsMap;
163
256
  },
164
257
 
165
- updateAccounts(namespace: ChainNamespace, accounts: CaipAddress[]) {
258
+ updateAccounts(namespace: ChainNamespace, accounts: CaipAddress[]): boolean {
166
259
  const connection = baseState.connections.get(namespace);
167
260
  if (!connection) {
168
- return;
261
+ return false;
169
262
  }
170
263
 
171
- const newConnectionsMap = new Map(baseState.connections);
172
- const updatedConnection = { ...connection, accounts };
173
- newConnectionsMap.set(namespace, updatedConnection);
174
- baseState.connections = newConnectionsMap;
264
+ if (connection.accounts.toString() === accounts.toString()) {
265
+ return false;
266
+ }
267
+
268
+ updateConnection(namespace, connection, { accounts });
269
+
270
+ return true;
175
271
  },
176
272
 
177
273
  updateBalance(namespace: ChainNamespace, address: CaipAddress, balance: Balance) {
178
274
  const connection = baseState.connections.get(namespace);
179
275
  if (!connection) {
276
+ console.warn(`No connection found for namespace: ${namespace}`);
277
+
180
278
  return;
181
279
  }
280
+ const newBalances = new Map(connection.balances);
281
+ const existingBalances = connection.balances.get(address) || [];
282
+ // Check if this token already exists by contract address or symbol
283
+ const existingIndex = existingBalances.findIndex(existingBalance => {
284
+ if (balance.address && existingBalance.address) {
285
+ return (
286
+ CoreHelperUtil.getPlainAddress(existingBalance.address) ===
287
+ CoreHelperUtil.getPlainAddress(balance.address)
288
+ );
289
+ }
182
290
 
183
- const newBalances = { ...connection.balances, [address]: balance };
184
- const updatedConnection = { ...connection, balances: newBalances };
185
- const newConnectionsMap = new Map(baseState.connections);
186
- newConnectionsMap.set(namespace, updatedConnection);
187
- baseState.connections = newConnectionsMap;
291
+ return existingBalance.symbol === balance.symbol;
292
+ });
293
+ let updatedBalances: Balance[];
294
+ if (existingIndex >= 0) {
295
+ // Update existing token
296
+ updatedBalances = [...existingBalances];
297
+ updatedBalances[existingIndex] = {
298
+ ...updatedBalances[existingIndex],
299
+ ...balance
300
+ };
301
+ } else {
302
+ // Add new token
303
+ updatedBalances = [...existingBalances, balance];
304
+ }
305
+ newBalances.set(address, updatedBalances);
306
+ updateConnection(namespace, connection, { balances: newBalances });
307
+ },
308
+
309
+ updateIdentity(
310
+ namespace: ChainNamespace,
311
+ connection: Connection,
312
+ plainAddress: string,
313
+ identity: Identity
314
+ ) {
315
+ const accounts = connection.accounts.filter(
316
+ account => CoreHelperUtil.getPlainAddress(account) === plainAddress
317
+ );
318
+
319
+ if (accounts.length > 0) {
320
+ const newIdentities = new Map(connection.identities || []);
321
+ let hasChanges = false;
322
+
323
+ accounts.forEach(account => {
324
+ const existingIdentity = newIdentities.get(account);
325
+ if (!existingIdentity || existingIdentity.name !== identity.name) {
326
+ newIdentities.set(account, identity);
327
+ hasChanges = true;
328
+ }
329
+ });
330
+
331
+ if (hasChanges) {
332
+ updateConnection(namespace, connection, { identities: newIdentities });
333
+ }
334
+ }
188
335
  },
189
336
 
190
337
  setActiveNetwork(namespace: ChainNamespace, networkId: CaipNetworkId) {
@@ -194,10 +341,9 @@ export const ConnectionsController = {
194
341
  return;
195
342
  }
196
343
 
197
- baseState.connections.set(namespace, {
198
- ...connection,
199
- caipNetwork: networkId
200
- });
344
+ updateConnection(namespace, connection, { caipNetwork: networkId });
345
+
346
+ this.setActiveNamespace(namespace);
201
347
  },
202
348
 
203
349
  setNetworks(networks: AppKitNetwork[]) {
@@ -205,6 +351,11 @@ export const ConnectionsController = {
205
351
  },
206
352
 
207
353
  getConnectedNetworks() {
354
+ const connection = getActiveConnection(baseState);
355
+ if (!connection) {
356
+ return [];
357
+ }
358
+
208
359
  return baseState.networks.filter(
209
360
  network =>
210
361
  baseState.connections
@@ -213,6 +364,42 @@ export const ConnectionsController = {
213
364
  );
214
365
  },
215
366
 
367
+ getAvailableNetworks() {
368
+ const connection = getActiveConnection(baseState);
369
+
370
+ if (!connection) {
371
+ return baseState.networks;
372
+ }
373
+
374
+ const canAddEvmChain = connection.properties?.canAddEvmChain;
375
+
376
+ return baseState.networks.filter(
377
+ network =>
378
+ baseState.connections
379
+ .get(network.chainNamespace)
380
+ ?.accounts.some(account => account.startsWith(network.caipNetworkId)) ||
381
+ (canAddEvmChain && network.chainNamespace === 'eip155')
382
+ );
383
+ },
384
+
385
+ setAccountType(namespace: ChainNamespace, type: AccountType) {
386
+ const connection = baseState.connections.get(namespace);
387
+ if (!connection) return;
388
+
389
+ const newConnection = updateConnection(namespace, connection, { type });
390
+
391
+ return getActiveAddress(newConnection);
392
+ },
393
+
394
+ getActiveNetworkId(namespace?: ChainNamespace) {
395
+ const connection = namespace
396
+ ? baseState.connections.get(namespace)
397
+ : getActiveConnection(baseState);
398
+ if (!connection) return undefined;
399
+
400
+ return connection.caipNetwork;
401
+ },
402
+
216
403
  async disconnect(namespace: ChainNamespace, isInternal = true) {
217
404
  const connection = baseState.connections.get(namespace);
218
405
  if (!connection) return;
@@ -265,27 +452,124 @@ export const ConnectionsController = {
265
452
  ?.adapter.parseUnits(value, decimals);
266
453
  },
267
454
 
268
- async sendTransaction(args: any) {
455
+ async signMessage(address: CaipAddress, message: string) {
269
456
  if (!baseState.activeNamespace) return undefined;
270
457
 
458
+ const [namespace, chainId, plainAddress] = address.split(':');
459
+
460
+ if (!namespace || namespace !== baseState.activeNamespace || !chainId || !plainAddress) {
461
+ return undefined;
462
+ }
463
+
271
464
  const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
272
465
 
273
- if (adapter instanceof EVMAdapter) {
274
- return adapter.sendTransaction(args);
466
+ if (
467
+ (adapter instanceof EVMAdapter ||
468
+ adapter instanceof SolanaBaseAdapter ||
469
+ adapter instanceof BitcoinBaseAdapter) &&
470
+ plainAddress &&
471
+ chainId
472
+ ) {
473
+ return adapter.signMessage(plainAddress, message, chainId);
275
474
  }
276
475
 
277
476
  return undefined;
278
477
  },
279
478
 
280
- async estimateGas(args: any) {
479
+ async sendTransaction(args: any) {
281
480
  if (!baseState.activeNamespace) return undefined;
282
481
 
283
482
  const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
483
+ if (adapter instanceof EVMAdapter || adapter instanceof SolanaBaseAdapter) {
484
+ return adapter.sendTransaction(args);
485
+ }
486
+
487
+ return undefined;
488
+ },
284
489
 
490
+ async estimateGas(args: any) {
491
+ if (!baseState.activeNamespace || baseState.activeNamespace !== 'eip155') return undefined;
492
+
493
+ const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
285
494
  if (adapter instanceof EVMAdapter) {
286
495
  return adapter.estimateGas(args);
287
496
  }
288
497
 
289
498
  return undefined;
499
+ },
500
+
501
+ async writeContract(args: any) {
502
+ if (!baseState.activeNamespace) return undefined;
503
+
504
+ const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
505
+ if (adapter instanceof EVMAdapter) {
506
+ return adapter.writeContract(args);
507
+ }
508
+
509
+ return undefined;
510
+ },
511
+
512
+ async fetchBalance(forceUpdateAddresses?: CaipAddress[]) {
513
+ const connection = getActiveConnection(baseState);
514
+ if (!connection) {
515
+ throw new Error('No active connection found for balance fetch');
516
+ }
517
+
518
+ const chainId = connection.caipNetwork;
519
+ const address = getActiveAddress(connection);
520
+ const namespace = baseState.activeNamespace;
521
+ if (!namespace || !address || !chainId) {
522
+ throw new Error('Missing required data for balance fetch');
523
+ }
524
+
525
+ try {
526
+ const response = await BlockchainApiController.getBalance(address, forceUpdateAddresses);
527
+ if (!response) {
528
+ throw new Error('Failed to fetch token balance');
529
+ }
530
+ // Update balances for each token in the response
531
+ response.balances.forEach(balance => {
532
+ this.updateBalance(namespace, address, {
533
+ name: balance.name,
534
+ symbol: balance.symbol,
535
+ amount: balance.quantity.numeric,
536
+ address: balance.address,
537
+ quantity: balance.quantity,
538
+ price: balance.price,
539
+ value: balance.value,
540
+ iconUrl: balance.iconUrl
541
+ });
542
+ });
543
+ } catch (error) {
544
+ LogController.sendError(error, 'ConnectionsController.ts', 'fetchBalance', {
545
+ address,
546
+ namespace
547
+ });
548
+ SnackController.showError('Failed to get account balance');
549
+ }
550
+ },
551
+
552
+ getSmartAccountEnabledNetworks(): AppKitNetwork[] {
553
+ const activeConnection = getActiveConnection(baseState);
554
+ if (!activeConnection) {
555
+ return [];
556
+ }
557
+ if (!activeConnection.properties?.smartAccounts?.length) {
558
+ return [];
559
+ }
560
+ const smartAccountNetworks = new Set<CaipNetworkId>();
561
+ activeConnection.properties.smartAccounts.forEach(smartAccount => {
562
+ const parts = smartAccount.split(':');
563
+ if (parts.length >= 2) {
564
+ const networkId: CaipNetworkId = `${parts[0]}:${parts[1]}`;
565
+ smartAccountNetworks.add(networkId);
566
+ }
567
+ });
568
+
569
+ return baseState.networks.filter(network => {
570
+ const networkId: CaipNetworkId = `${network.chainNamespace}:${network.id}`;
571
+
572
+ return smartAccountNetworks.has(networkId);
573
+ });
290
574
  }
291
575
  };
@@ -1,7 +1,8 @@
1
- import { subscribeKey as subKey } from 'valtio/vanilla/utils';
2
- import { proxy, subscribe as sub } from 'valtio/vanilla';
1
+ import { subscribeKey as subKey } from 'valtio/utils';
2
+ import { proxy, subscribe as sub } from 'valtio';
3
3
  import { BlockchainApiController } from './BlockchainApiController';
4
- import type { BlockchainApiEnsError } from '../utils/TypeUtil';
4
+ import type { BlockchainApiEnsError } from '@reown/appkit-common-react-native';
5
+ import { LogController } from './LogController';
5
6
 
6
7
  // -- Types --------------------------------------------- //
7
8
 
@@ -32,6 +33,7 @@ export const EnsController = {
32
33
  try {
33
34
  return await BlockchainApiController.lookupEnsName(name);
34
35
  } catch (e) {
36
+ LogController.sendError(e, 'EnsController.ts', 'resolveName');
35
37
  const error = e as BlockchainApiEnsError;
36
38
  throw new Error(error?.reasons?.[0]?.description || 'Error resolving name');
37
39
  }