@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.
- package/lib/commonjs/controllers/ApiController.js +148 -82
- package/lib/commonjs/controllers/ApiController.js.map +1 -1
- package/lib/commonjs/controllers/AssetController.js +0 -4
- package/lib/commonjs/controllers/AssetController.js.map +1 -1
- package/lib/commonjs/controllers/BlockchainApiController.js +127 -134
- package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionsController.js +280 -60
- package/lib/commonjs/controllers/ConnectionsController.js.map +1 -1
- package/lib/commonjs/controllers/EnsController.js +6 -4
- package/lib/commonjs/controllers/EnsController.js.map +1 -1
- package/lib/commonjs/controllers/EventsController.js +55 -5
- package/lib/commonjs/controllers/EventsController.js.map +1 -1
- package/lib/commonjs/controllers/LogController.js +188 -0
- package/lib/commonjs/controllers/LogController.js.map +1 -0
- package/lib/commonjs/controllers/ModalController.js +9 -7
- package/lib/commonjs/controllers/ModalController.js.map +1 -1
- package/lib/commonjs/controllers/OnRampController.js +134 -53
- package/lib/commonjs/controllers/OnRampController.js.map +1 -1
- package/lib/commonjs/controllers/OptionsController.js +20 -7
- package/lib/commonjs/controllers/OptionsController.js.map +1 -1
- package/lib/commonjs/controllers/PublicStateController.js.map +1 -1
- package/lib/commonjs/controllers/RouterController.js +7 -22
- package/lib/commonjs/controllers/RouterController.js.map +1 -1
- package/lib/commonjs/controllers/SendController.js +115 -99
- package/lib/commonjs/controllers/SendController.js.map +1 -1
- package/lib/commonjs/controllers/SnackController.js +29 -5
- package/lib/commonjs/controllers/SnackController.js.map +1 -1
- package/lib/commonjs/controllers/SwapController.js +119 -119
- package/lib/commonjs/controllers/SwapController.js.map +1 -1
- package/lib/commonjs/controllers/ThemeController.js +5 -0
- package/lib/commonjs/controllers/ThemeController.js.map +1 -1
- package/lib/commonjs/controllers/TransactionsController.js +25 -19
- package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
- package/lib/commonjs/controllers/WcController.js +73 -0
- package/lib/commonjs/controllers/WcController.js.map +1 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +272 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
- package/lib/commonjs/features/reown-authentication/index.js +28 -0
- package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
- package/lib/commonjs/index.js +30 -37
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/utils/ApiUtil.js +1 -1
- package/lib/commonjs/utils/ApiUtil.js.map +1 -1
- package/lib/commonjs/utils/AssetUtil.js +8 -17
- package/lib/commonjs/utils/AssetUtil.js.map +1 -1
- package/lib/commonjs/utils/ConstantsUtil.js +14 -431
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/CoreHelperUtil.js +33 -10
- package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
- package/lib/commonjs/utils/EventUtil.js.map +1 -1
- package/lib/commonjs/utils/FetchUtil.js +41 -12
- package/lib/commonjs/utils/FetchUtil.js.map +1 -1
- package/lib/commonjs/utils/LogUtils.js +131 -0
- package/lib/commonjs/utils/LogUtils.js.map +1 -0
- package/lib/commonjs/utils/StorageUtil.js +119 -172
- package/lib/commonjs/utils/StorageUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapApiUtil.js +2 -17
- package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -1
- package/lib/commonjs/utils/WalletUtil.js +23 -0
- package/lib/commonjs/utils/WalletUtil.js.map +1 -0
- package/lib/module/controllers/ApiController.js +150 -82
- package/lib/module/controllers/ApiController.js.map +1 -1
- package/lib/module/controllers/AssetController.js +2 -4
- package/lib/module/controllers/AssetController.js.map +1 -1
- package/lib/module/controllers/BlockchainApiController.js +128 -133
- package/lib/module/controllers/BlockchainApiController.js.map +1 -1
- package/lib/module/controllers/ConnectionsController.js +282 -60
- package/lib/module/controllers/ConnectionsController.js.map +1 -1
- package/lib/module/controllers/EnsController.js +6 -2
- package/lib/module/controllers/EnsController.js.map +1 -1
- package/lib/module/controllers/EventsController.js +54 -2
- package/lib/module/controllers/EventsController.js.map +1 -1
- package/lib/module/controllers/LogController.js +185 -0
- package/lib/module/controllers/LogController.js.map +1 -0
- package/lib/module/controllers/ModalController.js +11 -7
- package/lib/module/controllers/ModalController.js.map +1 -1
- package/lib/module/controllers/OnRampController.js +135 -52
- package/lib/module/controllers/OnRampController.js.map +1 -1
- package/lib/module/controllers/OptionsController.js +22 -7
- package/lib/module/controllers/OptionsController.js.map +1 -1
- package/lib/module/controllers/PublicStateController.js +2 -0
- package/lib/module/controllers/PublicStateController.js.map +1 -1
- package/lib/module/controllers/RouterController.js +9 -22
- package/lib/module/controllers/RouterController.js.map +1 -1
- package/lib/module/controllers/SendController.js +114 -96
- package/lib/module/controllers/SendController.js.map +1 -1
- package/lib/module/controllers/SnackController.js +31 -5
- package/lib/module/controllers/SnackController.js.map +1 -1
- package/lib/module/controllers/SwapController.js +121 -119
- package/lib/module/controllers/SwapController.js.map +1 -1
- package/lib/module/controllers/ThemeController.js +7 -0
- package/lib/module/controllers/ThemeController.js.map +1 -1
- package/lib/module/controllers/TransactionsController.js +25 -17
- package/lib/module/controllers/TransactionsController.js.map +1 -1
- package/lib/module/controllers/WcController.js +70 -0
- package/lib/module/controllers/WcController.js.map +1 -0
- package/lib/module/features/reown-authentication/ReownAuthentication.js +268 -0
- package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
- package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
- package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
- package/lib/module/features/reown-authentication/index.js +5 -0
- package/lib/module/features/reown-authentication/index.js.map +1 -0
- package/lib/module/index.js +12 -7
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/ApiUtil.js +3 -1
- package/lib/module/utils/ApiUtil.js.map +1 -1
- package/lib/module/utils/AssetUtil.js +10 -17
- package/lib/module/utils/AssetUtil.js.map +1 -1
- package/lib/module/utils/ConstantsUtil.js +16 -431
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/CoreHelperUtil.js +35 -8
- package/lib/module/utils/CoreHelperUtil.js.map +1 -1
- package/lib/module/utils/EventUtil.js +2 -0
- package/lib/module/utils/EventUtil.js.map +1 -1
- package/lib/module/utils/FetchUtil.js +44 -12
- package/lib/module/utils/FetchUtil.js.map +1 -1
- package/lib/module/utils/LogUtils.js +121 -0
- package/lib/module/utils/LogUtils.js.map +1 -0
- package/lib/module/utils/StorageUtil.js +122 -172
- package/lib/module/utils/StorageUtil.js.map +1 -1
- package/lib/module/utils/SwapApiUtil.js +4 -17
- package/lib/module/utils/SwapApiUtil.js.map +1 -1
- package/lib/module/utils/SwapCalculationUtil.js +3 -0
- package/lib/module/utils/SwapCalculationUtil.js.map +1 -1
- package/lib/module/utils/WalletUtil.js +19 -0
- package/lib/module/utils/WalletUtil.js.map +1 -0
- package/lib/typescript/controllers/ApiController.d.ts +8 -7
- package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/AssetController.d.ts +0 -2
- package/lib/typescript/controllers/AssetController.d.ts.map +1 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts +21 -13
- package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionsController.d.ts +22 -18
- package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/EnsController.d.ts +1 -1
- package/lib/typescript/controllers/EnsController.d.ts.map +1 -1
- package/lib/typescript/controllers/EventsController.d.ts +13 -1
- package/lib/typescript/controllers/EventsController.d.ts.map +1 -1
- package/lib/typescript/controllers/LogController.d.ts +65 -0
- package/lib/typescript/controllers/LogController.d.ts.map +1 -0
- package/lib/typescript/controllers/ModalController.d.ts +1 -1
- package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
- package/lib/typescript/controllers/OnRampController.d.ts +4 -3
- package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
- package/lib/typescript/controllers/OptionsController.d.ts +8 -6
- package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/RouterController.d.ts +6 -21
- package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
- package/lib/typescript/controllers/SendController.d.ts +5 -7
- package/lib/typescript/controllers/SendController.d.ts.map +1 -1
- package/lib/typescript/controllers/SnackController.d.ts +3 -3
- package/lib/typescript/controllers/SnackController.d.ts.map +1 -1
- package/lib/typescript/controllers/SwapController.d.ts +11 -11
- package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
- package/lib/typescript/controllers/ThemeController.d.ts +2 -0
- package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
- package/lib/typescript/controllers/TransactionsController.d.ts +3 -3
- package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/WcController.d.ts +27 -0
- package/lib/typescript/controllers/WcController.d.ts.map +1 -0
- package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
- package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
- package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
- package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
- package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
- package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +6 -8
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/ApiUtil.d.ts +1 -1
- package/lib/typescript/utils/AssetUtil.d.ts +3 -4
- package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
- package/lib/typescript/utils/ConstantsUtil.d.ts +7 -416
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/CoreHelperUtil.d.ts +8 -6
- package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
- package/lib/typescript/utils/EventUtil.d.ts +1 -1
- package/lib/typescript/utils/EventUtil.d.ts.map +1 -1
- package/lib/typescript/utils/FetchUtil.d.ts +3 -3
- package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
- package/lib/typescript/utils/LogUtils.d.ts +15 -0
- package/lib/typescript/utils/LogUtils.d.ts.map +1 -0
- package/lib/typescript/utils/StorageUtil.d.ts +8 -19
- package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapApiUtil.d.ts +3 -4
- package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapCalculationUtil.d.ts +1 -1
- package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -1
- package/lib/typescript/utils/WalletUtil.d.ts +5 -0
- package/lib/typescript/utils/WalletUtil.d.ts.map +1 -0
- package/package.json +12 -25
- package/src/controllers/ApiController.ts +158 -80
- package/src/controllers/AssetController.ts +0 -6
- package/src/controllers/BlockchainApiController.ts +109 -144
- package/src/controllers/ConnectionsController.ts +372 -88
- package/src/controllers/EnsController.ts +5 -3
- package/src/controllers/EventsController.ts +74 -3
- package/src/controllers/LogController.ts +250 -0
- package/src/controllers/ModalController.ts +11 -9
- package/src/controllers/OnRampController.ts +195 -82
- package/src/controllers/OptionsController.ts +40 -13
- package/src/controllers/RouterController.ts +20 -58
- package/src/controllers/SendController.ts +148 -112
- package/src/controllers/SnackController.ts +31 -5
- package/src/controllers/SwapController.ts +140 -148
- package/src/controllers/ThemeController.ts +7 -0
- package/src/controllers/TransactionsController.ts +26 -18
- package/src/controllers/WcController.ts +93 -0
- package/src/features/reown-authentication/ReownAuthentication.ts +475 -0
- package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
- package/src/features/reown-authentication/index.ts +2 -0
- package/src/index.ts +15 -22
- package/src/utils/ApiUtil.ts +1 -1
- package/src/utils/AssetUtil.ts +9 -20
- package/src/utils/ConstantsUtil.ts +16 -435
- package/src/utils/CoreHelperUtil.ts +62 -11
- package/src/utils/EventUtil.ts +1 -1
- package/src/utils/FetchUtil.ts +50 -14
- package/src/utils/LogUtils.ts +179 -0
- package/src/utils/StorageUtil.ts +171 -199
- package/src/utils/SwapApiUtil.ts +18 -37
- package/src/utils/SwapCalculationUtil.ts +1 -2
- package/src/utils/WalletUtil.ts +14 -0
- package/lib/commonjs/controllers/AccountController.js +0 -93
- package/lib/commonjs/controllers/AccountController.js.map +0 -1
- package/lib/commonjs/controllers/ConnectionController.js +0 -132
- package/lib/commonjs/controllers/ConnectionController.js.map +0 -1
- package/lib/commonjs/controllers/ConnectorController.js +0 -50
- package/lib/commonjs/controllers/ConnectorController.js.map +0 -1
- package/lib/commonjs/controllers/NetworkController.js +0 -90
- package/lib/commonjs/controllers/NetworkController.js.map +0 -1
- package/lib/commonjs/controllers/WebviewController.js +0 -52
- package/lib/commonjs/controllers/WebviewController.js.map +0 -1
- package/lib/commonjs/utils/NetworkUtil.js +0 -46
- package/lib/commonjs/utils/NetworkUtil.js.map +0 -1
- package/lib/commonjs/utils/RouterUtil.js +0 -25
- package/lib/commonjs/utils/RouterUtil.js.map +0 -1
- package/lib/commonjs/utils/TypeUtil.js +0 -6
- package/lib/commonjs/utils/TypeUtil.js.map +0 -1
- package/lib/module/controllers/AccountController.js +0 -88
- package/lib/module/controllers/AccountController.js.map +0 -1
- package/lib/module/controllers/ConnectionController.js +0 -127
- package/lib/module/controllers/ConnectionController.js.map +0 -1
- package/lib/module/controllers/ConnectorController.js +0 -45
- package/lib/module/controllers/ConnectorController.js.map +0 -1
- package/lib/module/controllers/NetworkController.js +0 -85
- package/lib/module/controllers/NetworkController.js.map +0 -1
- package/lib/module/controllers/WebviewController.js +0 -47
- package/lib/module/controllers/WebviewController.js.map +0 -1
- package/lib/module/utils/NetworkUtil.js +0 -40
- package/lib/module/utils/NetworkUtil.js.map +0 -1
- package/lib/module/utils/RouterUtil.js +0 -19
- package/lib/module/utils/RouterUtil.js.map +0 -1
- package/lib/module/utils/TypeUtil.js +0 -2
- package/lib/module/utils/TypeUtil.js.map +0 -1
- package/lib/typescript/controllers/AccountController.d.ts +0 -33
- package/lib/typescript/controllers/AccountController.d.ts.map +0 -1
- package/lib/typescript/controllers/ConnectionController.d.ts +0 -68
- package/lib/typescript/controllers/ConnectionController.d.ts.map +0 -1
- package/lib/typescript/controllers/ConnectorController.d.ts +0 -18
- package/lib/typescript/controllers/ConnectorController.d.ts.map +0 -1
- package/lib/typescript/controllers/NetworkController.d.ts +0 -34
- package/lib/typescript/controllers/NetworkController.d.ts.map +0 -1
- package/lib/typescript/controllers/WebviewController.d.ts +0 -21
- package/lib/typescript/controllers/WebviewController.d.ts.map +0 -1
- package/lib/typescript/utils/NetworkUtil.d.ts +0 -8
- package/lib/typescript/utils/NetworkUtil.d.ts.map +0 -1
- package/lib/typescript/utils/RouterUtil.d.ts +0 -5
- package/lib/typescript/utils/RouterUtil.d.ts.map +0 -1
- package/lib/typescript/utils/TypeUtil.d.ts +0 -817
- package/lib/typescript/utils/TypeUtil.d.ts.map +0 -1
- package/src/controllers/AccountController.ts +0 -128
- package/src/controllers/ConnectionController.ts +0 -208
- package/src/controllers/ConnectorController.ts +0 -64
- package/src/controllers/NetworkController.ts +0 -120
- package/src/controllers/WebviewController.ts +0 -63
- package/src/utils/NetworkUtil.ts +0 -33
- package/src/utils/RouterUtil.ts +0 -21
- package/src/utils/TypeUtil.ts +0 -995
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { proxy, ref } from 'valtio';
|
|
2
|
-
import { derive } from 'valtio
|
|
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
|
|
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
|
-
|
|
103
|
+
isConnected: (get): boolean => {
|
|
43
104
|
const snap = get(baseState);
|
|
44
105
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const connection =
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
|
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 (!
|
|
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
|
|
73
|
-
|
|
74
|
-
|
|
128
|
+
const activeAddress = getActiveAddress(connection);
|
|
129
|
+
if (!activeAddress || !connection.balances || connection.balances.size === 0) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
75
132
|
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
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
|
-
|
|
163
|
+
return connection?.caipNetwork;
|
|
164
|
+
},
|
|
165
|
+
accountType: (get): AccountType | undefined => {
|
|
166
|
+
const snap = get(baseState);
|
|
167
|
+
const connection = getActiveConnection(snap);
|
|
106
168
|
|
|
107
|
-
|
|
169
|
+
return connection?.type;
|
|
170
|
+
},
|
|
171
|
+
connection: (get): Connection | undefined => {
|
|
172
|
+
const snap = get(baseState);
|
|
108
173
|
|
|
109
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
136
|
-
namespace,
|
|
137
|
-
adapter,
|
|
221
|
+
setConnection({
|
|
138
222
|
accounts,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
223
|
+
adapter,
|
|
224
|
+
caipNetwork,
|
|
225
|
+
namespace,
|
|
226
|
+
properties,
|
|
227
|
+
wallet
|
|
142
228
|
}: {
|
|
143
|
-
namespace: ChainNamespace;
|
|
144
|
-
adapter: BlockchainAdapter;
|
|
145
229
|
accounts: CaipAddress[];
|
|
146
|
-
|
|
230
|
+
adapter: BlockchainAdapter;
|
|
231
|
+
caipNetwork: CaipNetworkId;
|
|
232
|
+
namespace: ChainNamespace;
|
|
233
|
+
properties?: ConnectionProperties;
|
|
147
234
|
wallet?: WalletInfo;
|
|
148
|
-
caipNetwork?: CaipNetworkId;
|
|
149
235
|
}) {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
156
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
|
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 (
|
|
274
|
-
|
|
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
|
|
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/
|
|
2
|
-
import { proxy, subscribe as sub } from 'valtio
|
|
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 '
|
|
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
|
}
|