@reown/appkit-solana-react-native 0.0.0-feat-multichain-20250604171123 → 0.0.0-feat-multichain-phantom-20250606183519

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 (37) hide show
  1. package/lib/commonjs/connectors/PhantomConnector.js +246 -0
  2. package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
  3. package/lib/commonjs/helpers.js +9 -0
  4. package/lib/commonjs/helpers.js.map +1 -1
  5. package/lib/commonjs/index.js +7 -0
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/commonjs/providers/PhantomProvider.js +396 -0
  8. package/lib/commonjs/providers/PhantomProvider.js.map +1 -0
  9. package/lib/commonjs/types.js +6 -0
  10. package/lib/commonjs/types.js.map +1 -0
  11. package/lib/module/connectors/PhantomConnector.js +238 -0
  12. package/lib/module/connectors/PhantomConnector.js.map +1 -0
  13. package/lib/module/helpers.js +9 -0
  14. package/lib/module/helpers.js.map +1 -1
  15. package/lib/module/index.js +7 -2
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/module/providers/PhantomProvider.js +388 -0
  18. package/lib/module/providers/PhantomProvider.js.map +1 -0
  19. package/lib/module/types.js +2 -0
  20. package/lib/module/types.js.map +1 -0
  21. package/lib/typescript/connectors/PhantomConnector.d.ts +26 -0
  22. package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -0
  23. package/lib/typescript/helpers.d.ts +8 -7
  24. package/lib/typescript/helpers.d.ts.map +1 -1
  25. package/lib/typescript/index.d.ts +3 -2
  26. package/lib/typescript/index.d.ts.map +1 -1
  27. package/lib/typescript/providers/PhantomProvider.d.ts +37 -0
  28. package/lib/typescript/providers/PhantomProvider.d.ts.map +1 -0
  29. package/lib/typescript/types.d.ts +96 -0
  30. package/lib/typescript/types.d.ts.map +1 -0
  31. package/package.json +7 -5
  32. package/src/connectors/PhantomConnector.ts +329 -0
  33. package/src/helpers.ts +9 -7
  34. package/src/index.ts +8 -0
  35. package/src/providers/PhantomProvider.ts +535 -0
  36. package/src/types.ts +131 -0
  37. package/src/index.tsx +0 -2
@@ -0,0 +1,96 @@
1
+ import type { CaipNetworkId, Namespaces, Storage, WalletInfo } from '@reown/appkit-common-react-native';
2
+ import type nacl from 'tweetnacl';
3
+ export interface TokenInfo {
4
+ address: string;
5
+ symbol: string;
6
+ name: string;
7
+ decimals: number;
8
+ logoURI?: string;
9
+ }
10
+ export type PhantomCluster = 'mainnet-beta' | 'testnet' | 'devnet';
11
+ export interface PhantomProviderConfig {
12
+ appScheme: string;
13
+ dappUrl: string;
14
+ storage: Storage;
15
+ dappEncryptionKeyPair: nacl.BoxKeyPair;
16
+ }
17
+ export type PhantomConnectResult = PhantomSession;
18
+ export interface PhantomSession {
19
+ sessionToken: string;
20
+ userPublicKey: string;
21
+ phantomEncryptionPublicKeyBs58: string;
22
+ cluster: PhantomCluster;
23
+ }
24
+ export interface SignTransactionRequestParams {
25
+ transaction: string;
26
+ }
27
+ export interface SignMessageRequestParams {
28
+ message: Uint8Array | string;
29
+ display?: 'utf8' | 'hex';
30
+ }
31
+ export interface SignAllTransactionsRequestParams {
32
+ transactions: string[];
33
+ }
34
+ export interface PhantomDeeplinkResponse {
35
+ phantom_encryption_public_key?: string;
36
+ nonce: string;
37
+ data: string;
38
+ }
39
+ export interface DecryptedConnectData {
40
+ public_key: string;
41
+ session: string;
42
+ }
43
+ export interface PhantomProviderConfig {
44
+ appScheme: string;
45
+ dappUrl: string;
46
+ storage: Storage;
47
+ dappEncryptionKeyPair: nacl.BoxKeyPair;
48
+ }
49
+ export interface PhantomSession {
50
+ sessionToken: string;
51
+ userPublicKey: string;
52
+ phantomEncryptionPublicKeyBs58: string;
53
+ cluster: PhantomCluster;
54
+ }
55
+ export type PhantomRpcMethod = 'connect' | 'disconnect' | 'signTransaction' | 'signAndSendTransaction' | 'signAllTransactions' | 'signMessage';
56
+ export interface PhantomSignTransactionParams {
57
+ dapp_encryption_public_key: string;
58
+ redirect_link: string;
59
+ payload: string;
60
+ nonce: string;
61
+ cluster?: PhantomCluster;
62
+ }
63
+ export interface PhantomSignAllTransactionsParams {
64
+ dapp_encryption_public_key: string;
65
+ redirect_link: string;
66
+ payload: string;
67
+ nonce: string;
68
+ cluster?: PhantomCluster;
69
+ }
70
+ export interface PhantomSignMessageParams {
71
+ dapp_encryption_public_key: string;
72
+ redirect_link: string;
73
+ payload: string;
74
+ nonce: string;
75
+ }
76
+ export interface PhantomConnectParams {
77
+ app_url: string;
78
+ dapp_encryption_public_key: string;
79
+ redirect_link: string;
80
+ cluster?: PhantomCluster;
81
+ }
82
+ export interface PhantomDisconnectParams {
83
+ dapp_encryption_public_key: string;
84
+ redirect_link: string;
85
+ payload: string;
86
+ nonce: string;
87
+ }
88
+ export interface PhantomConnectorConfig {
89
+ cluster?: PhantomCluster;
90
+ }
91
+ export interface PhantomConnectorSessionData {
92
+ namespaces: Namespaces;
93
+ wallet: WalletInfo;
94
+ currentCaipNetworkId: CaipNetworkId;
95
+ }
96
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"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"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@reown/appkit-solana-react-native",
3
- "version": "0.0.0-feat-multichain-20250604171123",
3
+ "version": "0.0.0-feat-multichain-phantom-20250606183519",
4
4
  "main": "lib/commonjs/index.js",
5
5
  "types": "lib/typescript/index.d.ts",
6
6
  "module": "lib/module/index.js",
7
- "source": "src/index.tsx",
7
+ "react-native": "src/index.ts",
8
+ "source": "src/index.ts",
8
9
  "scripts": {
9
10
  "build": "bob build",
10
11
  "clean": "rm -rf lib",
@@ -38,7 +39,8 @@
38
39
  "access": "public"
39
40
  },
40
41
  "dependencies": {
41
- "@reown/appkit-common-react-native": "0.0.0-feat-multichain-20250604171123"
42
- },
43
- "react-native": "src/index.tsx"
42
+ "@reown/appkit-common-react-native": "0.0.0-feat-multichain-phantom-20250606183519",
43
+ "bs58": "6.0.0",
44
+ "tweetnacl": "1.0.3"
45
+ }
44
46
  }
@@ -0,0 +1,329 @@
1
+ import {
2
+ WalletConnector,
3
+ type AppKitNetwork,
4
+ type CaipNetworkId,
5
+ type ChainNamespace,
6
+ type ConnectOptions,
7
+ type Namespaces,
8
+ type WalletInfo,
9
+ type CaipAddress,
10
+ type ConnectorInitOptions,
11
+ type Storage,
12
+ solana,
13
+ solanaDevnet,
14
+ solanaTestnet
15
+ } from '@reown/appkit-common-react-native';
16
+ import nacl from 'tweetnacl';
17
+ import bs58 from 'bs58';
18
+
19
+ import { PhantomProvider, SOLANA_SIGNING_METHODS } from '../providers/PhantomProvider';
20
+ import type {
21
+ PhantomCluster,
22
+ PhantomConnectorConfig,
23
+ PhantomConnectorSessionData,
24
+ PhantomProviderConfig
25
+ } from '../types';
26
+
27
+ const SOLANA_CLUSTER_TO_CHAIN_ID_PART: Record<PhantomCluster, string> = {
28
+ 'mainnet-beta': solana.id as string,
29
+ 'testnet': solanaTestnet.id as string,
30
+ 'devnet': solanaDevnet.id as string
31
+ };
32
+
33
+ const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
34
+ const DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
35
+
36
+ export class PhantomConnector extends WalletConnector {
37
+ private readonly config: PhantomConnectorConfig;
38
+
39
+ private currentCaipNetworkId: CaipNetworkId | null = null;
40
+ private dappEncryptionKeyPair?: nacl.BoxKeyPair;
41
+
42
+ private static readonly SUPPORTED_NAMESPACE: ChainNamespace = 'solana';
43
+
44
+ constructor(config: PhantomConnectorConfig) {
45
+ super({ type: 'phantom' });
46
+ this.config = config;
47
+ }
48
+
49
+ override async init(ops: ConnectorInitOptions) {
50
+ super.init(ops);
51
+ this.storage = ops.storage;
52
+ await this.initializeKeyPair();
53
+
54
+ const appScheme = ops.metadata.redirect?.universal;
55
+ if (!appScheme) {
56
+ throw new Error(
57
+ 'Phantom Connector: No universal link found in metadata. Please add redirect.universal to the metadata.'
58
+ );
59
+ }
60
+
61
+ const providerConfig: PhantomProviderConfig = {
62
+ appScheme,
63
+ dappUrl: ops.metadata.url,
64
+ storage: ops.storage,
65
+ dappEncryptionKeyPair: this.dappEncryptionKeyPair!
66
+ };
67
+
68
+ this.provider = new PhantomProvider(providerConfig);
69
+ this.restoreSession();
70
+ }
71
+
72
+ private async initializeKeyPair(): Promise<void> {
73
+ try {
74
+ const secretKeyB58 = await this.getStorage().getItem(DAPP_KEYPAIR_STORAGE_KEY);
75
+ if (secretKeyB58) {
76
+ const secretKey = bs58.decode(secretKeyB58);
77
+ this.dappEncryptionKeyPair = nacl.box.keyPair.fromSecretKey(secretKey);
78
+ } else {
79
+ const newKeyPair = nacl.box.keyPair();
80
+ this.dappEncryptionKeyPair = newKeyPair;
81
+ await this.getStorage().setItem(
82
+ DAPP_KEYPAIR_STORAGE_KEY,
83
+ bs58.encode(newKeyPair.secretKey)
84
+ );
85
+ }
86
+ } catch (error) {
87
+ // disconnect and clear session
88
+ await this.disconnect();
89
+ throw error;
90
+ }
91
+ }
92
+
93
+ override async connect(opts?: ConnectOptions): Promise<Namespaces | undefined> {
94
+ if (this.isConnected()) {
95
+ return this.namespaces;
96
+ }
97
+
98
+ const defaultChain =
99
+ opts?.defaultChain?.split(':')?.[0] === 'solana'
100
+ ? opts?.defaultChain?.split(':')[1]
101
+ : opts?.namespaces?.['solana']?.chains?.[0]?.split(':')[1];
102
+
103
+ const requestedCluster =
104
+ this.config.cluster ??
105
+ (Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
106
+ key =>
107
+ SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART] ===
108
+ defaultChain
109
+ ) as PhantomCluster | undefined);
110
+
111
+ try {
112
+ const connectResult = await this.getProvider().connect({ cluster: requestedCluster });
113
+
114
+ const solanaChainIdPart = SOLANA_CLUSTER_TO_CHAIN_ID_PART[connectResult.cluster];
115
+ if (!solanaChainIdPart) {
116
+ throw new Error(
117
+ `Phantom Connect: Internal - Unknown cluster mapping for ${connectResult.cluster}`
118
+ );
119
+ }
120
+ this.currentCaipNetworkId = `solana:${solanaChainIdPart}` as CaipNetworkId;
121
+
122
+ this.wallet = {
123
+ name: 'Phantom Wallet',
124
+ id: 'phantom-wallet'
125
+ };
126
+
127
+ const userPublicKey = this.getProvider().getUserPublicKey();
128
+ if (!userPublicKey) {
129
+ throw new Error('Phantom Connect: Provider failed to return a user public key.');
130
+ }
131
+
132
+ const caipAddress = `${this.currentCaipNetworkId}:${userPublicKey}` as CaipAddress;
133
+ this.namespaces = {
134
+ [PhantomConnector.SUPPORTED_NAMESPACE]: {
135
+ accounts: [caipAddress],
136
+ methods: Object.values(SOLANA_SIGNING_METHODS),
137
+ events: [],
138
+ chains: [this.currentCaipNetworkId]
139
+ }
140
+ };
141
+
142
+ await this.saveSession(); // Save connector-specific session on successful connect
143
+
144
+ return this.namespaces;
145
+ } catch (error: any) {
146
+ this.clearSession();
147
+ throw error;
148
+ }
149
+ }
150
+
151
+ override async disconnect(): Promise<void> {
152
+ if (!this.isConnected()) {
153
+ return Promise.resolve();
154
+ }
155
+ try {
156
+ await this.getProvider().disconnect();
157
+ } catch (error: any) {
158
+ // console.warn(`PhantomConnector: Error during provider disconnect: ${error.message}. Proceeding with local clear.`);
159
+ }
160
+ await this.clearSession();
161
+ }
162
+
163
+ private async clearSession(): Promise<void> {
164
+ this.namespaces = undefined;
165
+ this.wallet = undefined;
166
+ this.currentCaipNetworkId = null;
167
+ await this.clearSessionStorage();
168
+ }
169
+
170
+ override getProvider(): PhantomProvider {
171
+ if (!this.provider) {
172
+ throw new Error('Phantom Connector: Provider not initialized. Call init() first.');
173
+ }
174
+
175
+ return this.provider as PhantomProvider;
176
+ }
177
+
178
+ private getStorage(): Storage {
179
+ if (!this.storage) {
180
+ throw new Error('Phantom Connector: Storage not initialized. Call init() first.');
181
+ }
182
+
183
+ return this.storage;
184
+ }
185
+
186
+ override getNamespaces(): Namespaces {
187
+ if (!this.namespaces) {
188
+ throw new Error('Phantom Connector: Not connected. Call connect() first.');
189
+ }
190
+
191
+ return this.namespaces;
192
+ }
193
+
194
+ override getChainId(namespace: ChainNamespace): CaipNetworkId | undefined {
195
+ if (namespace === PhantomConnector.SUPPORTED_NAMESPACE) {
196
+ return this.currentCaipNetworkId ?? undefined;
197
+ }
198
+
199
+ return undefined;
200
+ }
201
+
202
+ override getWalletInfo(): WalletInfo | undefined {
203
+ if (!this.isConnected()) {
204
+ return undefined;
205
+ }
206
+
207
+ return this.wallet;
208
+ }
209
+
210
+ isConnected(): boolean {
211
+ // Rely solely on the provider as the source of truth for connection status.
212
+ return this.getProvider().isConnected() && !!this.getProvider().getUserPublicKey();
213
+ }
214
+
215
+ override async switchNetwork(network: AppKitNetwork): Promise<void> {
216
+ const targetClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
217
+ key =>
218
+ SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART] ===
219
+ network.id
220
+ ) as PhantomCluster | undefined;
221
+
222
+ if (!targetClusterName) {
223
+ throw new Error(`Cannot switch to unsupported network ID: ${network.id}`);
224
+ }
225
+
226
+ const currentClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
227
+ key =>
228
+ `solana:${
229
+ SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART]
230
+ }` === this.currentCaipNetworkId
231
+ ) as PhantomCluster | undefined;
232
+
233
+ if (targetClusterName === currentClusterName && this.isConnected()) {
234
+ return Promise.resolve();
235
+ }
236
+
237
+ // For deeplink wallets, switching network effectively means re-connecting to the new cluster.
238
+ // We can try to disconnect the current session and then initiate a new connection.
239
+ // console.log(`Attempting to switch network to: ${targetClusterName}`);
240
+ await this.disconnect(); // Clear current session
241
+
242
+ // Create a temporary options object to guide the new connection
243
+ const tempConnectOpts: ConnectOptions = {
244
+ defaultChain: `solana:${SOLANA_CLUSTER_TO_CHAIN_ID_PART[targetClusterName]}` as CaipNetworkId
245
+ };
246
+
247
+ // Attempt to connect to the new cluster
248
+ // The connect method will use the defaultChain from opts to determine the cluster.
249
+ await this.connect(tempConnectOpts);
250
+
251
+ // Verify if the connection was successful and to the correct new network
252
+ if (
253
+ !this.isConnected() ||
254
+ this.getChainId(PhantomConnector.SUPPORTED_NAMESPACE) !== tempConnectOpts.defaultChain
255
+ ) {
256
+ throw new Error(
257
+ `Failed to switch network to ${targetClusterName}. Please try connecting manually.`
258
+ );
259
+ }
260
+ }
261
+
262
+ // Orchestrates session restoration
263
+ public async restoreSession(): Promise<boolean> {
264
+ try {
265
+ const providerSession = await this.getProvider().restoreSession();
266
+ if (!providerSession) {
267
+ return false;
268
+ }
269
+
270
+ // If provider session is restored, try to restore connector data
271
+ const storedConnectorDataJson = await this.getStorage().getItem(
272
+ PHANTOM_CONNECTOR_STORAGE_KEY
273
+ );
274
+ if (!storedConnectorDataJson) {
275
+ return false; // Provider session exists but connector data is missing
276
+ }
277
+
278
+ const connectorData: PhantomConnectorSessionData = JSON.parse(storedConnectorDataJson);
279
+ this.namespaces = connectorData.namespaces;
280
+ this.wallet = connectorData.wallet;
281
+ this.currentCaipNetworkId = connectorData.currentCaipNetworkId;
282
+
283
+ // await this.initializeKeyPair();
284
+
285
+ // Final validation
286
+ if (this.isConnected()) {
287
+ return true;
288
+ }
289
+
290
+ // If validation fails, something is out of sync. Clear everything.
291
+ await this.disconnect();
292
+
293
+ return false;
294
+ } catch (error) {
295
+ // On any error, disconnect to ensure a clean state
296
+ await this.disconnect();
297
+
298
+ return false;
299
+ }
300
+ }
301
+
302
+ // Saves only connector-specific data
303
+ private async saveSession(): Promise<void> {
304
+ if (!this.namespaces || !this.wallet || !this.currentCaipNetworkId) {
305
+ return;
306
+ }
307
+
308
+ const connectorData: PhantomConnectorSessionData = {
309
+ namespaces: this.namespaces,
310
+ wallet: this.wallet,
311
+ currentCaipNetworkId: this.currentCaipNetworkId
312
+ };
313
+
314
+ try {
315
+ await this.getStorage().setItem(PHANTOM_CONNECTOR_STORAGE_KEY, JSON.stringify(connectorData));
316
+ } catch (error) {
317
+ // console.error('PhantomConnector: Failed to save session.', error);
318
+ }
319
+ }
320
+
321
+ // Clears only connector-specific data from storage
322
+ private async clearSessionStorage(): Promise<void> {
323
+ try {
324
+ await this.getStorage().removeItem(PHANTOM_CONNECTOR_STORAGE_KEY);
325
+ } catch (error) {
326
+ // console.error('PhantomConnector: Failed to clear session from storage.', error);
327
+ }
328
+ }
329
+ }
package/src/helpers.ts CHANGED
@@ -1,10 +1,4 @@
1
- export interface TokenInfo {
2
- address: string;
3
- symbol: string;
4
- name: string;
5
- decimals: number;
6
- logoURI?: string;
7
- }
1
+ import type { TokenInfo } from './types';
8
2
 
9
3
  /**
10
4
  * Validates if the given string is a Solana address.
@@ -48,6 +42,7 @@ export async function getSolanaNativeBalance(rpcUrl: string, address: string): P
48
42
  }
49
43
 
50
44
  let tokenCache: Record<string, TokenInfo> = {};
45
+
51
46
  /**
52
47
  * Fetch metadata for a Solana SPL token using the Jupiter token list.
53
48
  * @param mint - The token's mint address
@@ -71,6 +66,13 @@ export async function getSolanaTokenMetadata(mint: string): Promise<TokenInfo |
71
66
  }
72
67
  }
73
68
 
69
+ /**
70
+ * Get the balance of a token for a given address
71
+ * @param rpcUrl - The RPC URL to use
72
+ * @param address - The address to get the balance for
73
+ * @param tokenAddress - The address of the token to get the balance for
74
+ * @returns The balance of the token for the given address
75
+ */
74
76
  export async function getSolanaTokenBalance(
75
77
  rpcUrl: string,
76
78
  address: string,
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ // Connectors
2
+ export { PhantomConnector } from './connectors/PhantomConnector';
3
+
4
+ // Types
5
+ export type { PhantomConnectorConfig } from './types';
6
+
7
+ // Adapter
8
+ export { SolanaAdapter } from './adapter';