@reown/appkit-solana-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/adapter.js +183 -4
- package/lib/commonjs/adapter.js.map +1 -1
- package/lib/commonjs/connectors/DeeplinkConnector.js +271 -0
- package/lib/commonjs/connectors/DeeplinkConnector.js.map +1 -0
- package/lib/commonjs/connectors/PhantomConnector.js +18 -226
- package/lib/commonjs/connectors/PhantomConnector.js.map +1 -1
- package/lib/commonjs/connectors/SolflareConnector.js +39 -0
- package/lib/commonjs/connectors/SolflareConnector.js.map +1 -0
- package/lib/commonjs/helpers.js +0 -1
- package/lib/commonjs/helpers.js.map +1 -1
- package/lib/commonjs/index.js +8 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/providers/DeeplinkProvider.js +432 -0
- package/lib/commonjs/providers/DeeplinkProvider.js.map +1 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/utils/createSPLTokenTransaction.js +96 -0
- package/lib/commonjs/utils/createSPLTokenTransaction.js.map +1 -0
- package/lib/commonjs/utils/createSendTransaction.js +30 -0
- package/lib/commonjs/utils/createSendTransaction.js.map +1 -0
- package/lib/module/adapter.js +184 -4
- package/lib/module/adapter.js.map +1 -1
- package/lib/module/connectors/DeeplinkConnector.js +265 -0
- package/lib/module/connectors/DeeplinkConnector.js.map +1 -0
- package/lib/module/connectors/PhantomConnector.js +21 -226
- package/lib/module/connectors/PhantomConnector.js.map +1 -1
- package/lib/module/connectors/SolflareConnector.js +34 -0
- package/lib/module/connectors/SolflareConnector.js.map +1 -0
- package/lib/module/helpers.js +2 -1
- package/lib/module/helpers.js.map +1 -1
- package/lib/module/index.js +7 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/DeeplinkProvider.js +426 -0
- package/lib/module/providers/DeeplinkProvider.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils/createSPLTokenTransaction.js +92 -0
- package/lib/module/utils/createSPLTokenTransaction.js.map +1 -0
- package/lib/module/utils/createSendTransaction.js +26 -0
- package/lib/module/utils/createSendTransaction.js.map +1 -0
- package/lib/typescript/adapter.d.ts +13 -3
- package/lib/typescript/adapter.d.ts.map +1 -1
- package/lib/typescript/connectors/DeeplinkConnector.d.ts +30 -0
- package/lib/typescript/connectors/DeeplinkConnector.d.ts.map +1 -0
- package/lib/typescript/connectors/PhantomConnector.d.ts +8 -22
- package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -1
- package/lib/typescript/connectors/SolflareConnector.d.ts +12 -0
- package/lib/typescript/connectors/SolflareConnector.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/providers/DeeplinkProvider.d.ts +59 -0
- package/lib/typescript/providers/DeeplinkProvider.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +35 -32
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/utils/createSPLTokenTransaction.d.ts +4 -0
- package/lib/typescript/utils/createSPLTokenTransaction.d.ts.map +1 -0
- package/lib/typescript/utils/createSendTransaction.d.ts +10 -0
- package/lib/typescript/utils/createSendTransaction.d.ts.map +1 -0
- package/package.json +13 -5
- package/src/adapter.ts +221 -4
- package/src/connectors/DeeplinkConnector.ts +353 -0
- package/src/connectors/PhantomConnector.ts +19 -311
- package/src/connectors/SolflareConnector.ts +36 -0
- package/src/index.ts +6 -5
- package/src/providers/DeeplinkProvider.ts +605 -0
- package/src/types.ts +38 -37
- package/src/utils/createSPLTokenTransaction.ts +152 -0
- package/src/utils/createSendTransaction.ts +41 -0
- package/lib/commonjs/providers/PhantomProvider.js +0 -391
- package/lib/commonjs/providers/PhantomProvider.js.map +0 -1
- package/lib/module/providers/PhantomProvider.js +0 -383
- package/lib/module/providers/PhantomProvider.js.map +0 -1
- package/lib/typescript/providers/PhantomProvider.d.ts +0 -37
- package/lib/typescript/providers/PhantomProvider.d.ts.map +0 -1
- package/src/providers/PhantomProvider.ts +0 -530
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WalletConnector,
|
|
3
|
+
type AppKitNetwork,
|
|
4
|
+
type CaipNetworkId,
|
|
5
|
+
type ChainNamespace,
|
|
6
|
+
type ConnectOptions,
|
|
7
|
+
type Namespaces,
|
|
8
|
+
type CaipAddress,
|
|
9
|
+
type ConnectorInitOptions,
|
|
10
|
+
type Storage,
|
|
11
|
+
solana,
|
|
12
|
+
solanaDevnet,
|
|
13
|
+
solanaTestnet,
|
|
14
|
+
type ConnectionProperties
|
|
15
|
+
} from '@reown/appkit-common-react-native';
|
|
16
|
+
import nacl from 'tweetnacl';
|
|
17
|
+
import bs58 from 'bs58';
|
|
18
|
+
|
|
19
|
+
import { DeeplinkProvider, SOLANA_SIGNING_METHODS } from '../providers/DeeplinkProvider';
|
|
20
|
+
import type {
|
|
21
|
+
Cluster,
|
|
22
|
+
DeeplinkConnectorConfig,
|
|
23
|
+
DeeplinkConnectorSessionData,
|
|
24
|
+
DeeplinkProviderConfig
|
|
25
|
+
} from '../types';
|
|
26
|
+
|
|
27
|
+
const SOLANA_CLUSTER_TO_NETWORK: Record<Cluster, AppKitNetwork> = {
|
|
28
|
+
'mainnet-beta': solana,
|
|
29
|
+
'testnet': solanaTestnet,
|
|
30
|
+
'devnet': solanaDevnet
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export abstract class DeeplinkConnector extends WalletConnector {
|
|
34
|
+
private readonly config: DeeplinkConnectorConfig;
|
|
35
|
+
private currentCaipNetworkId: CaipNetworkId | null = null;
|
|
36
|
+
private dappEncryptionKeyPair?: nacl.BoxKeyPair;
|
|
37
|
+
|
|
38
|
+
private static readonly SUPPORTED_NAMESPACE: ChainNamespace = 'solana';
|
|
39
|
+
|
|
40
|
+
constructor(config: DeeplinkConnectorConfig) {
|
|
41
|
+
super({ type: config.type });
|
|
42
|
+
this.config = config;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Abstract methods that wallet-specific connectors must implement
|
|
46
|
+
protected abstract getBaseUrl(): string;
|
|
47
|
+
protected abstract getStorageKey(): string;
|
|
48
|
+
protected abstract getDappKeypairStorageKey(): string;
|
|
49
|
+
protected abstract getEncryptionKeyFieldName(): string;
|
|
50
|
+
|
|
51
|
+
override async init(ops: ConnectorInitOptions) {
|
|
52
|
+
super.init(ops);
|
|
53
|
+
this.storage = ops.storage;
|
|
54
|
+
await this.initializeKeyPair();
|
|
55
|
+
|
|
56
|
+
const appScheme = ops.metadata.redirect?.universal ?? ops.metadata.redirect?.native;
|
|
57
|
+
if (!appScheme) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`${this.type} connector: No redirect link found in metadata. Please add redirect.universal or redirect.native to the metadata.`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const providerConfig: DeeplinkProviderConfig = {
|
|
64
|
+
appScheme,
|
|
65
|
+
dappUrl: ops.metadata.url,
|
|
66
|
+
storage: ops.storage,
|
|
67
|
+
type: this.type as 'phantom' | 'solflare',
|
|
68
|
+
cluster: this.config?.cluster ?? 'mainnet-beta',
|
|
69
|
+
dappEncryptionKeyPair: this.dappEncryptionKeyPair!,
|
|
70
|
+
baseUrl: this.getBaseUrl(),
|
|
71
|
+
encryptionKeyFieldName: this.getEncryptionKeyFieldName()
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
this.provider = new DeeplinkProvider(providerConfig);
|
|
75
|
+
await this.restoreSession();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private async initializeKeyPair(): Promise<void> {
|
|
79
|
+
try {
|
|
80
|
+
const secretKeyB58 = await this.getStorage().getItem(this.getDappKeypairStorageKey());
|
|
81
|
+
if (secretKeyB58) {
|
|
82
|
+
const secretKey = bs58.decode(secretKeyB58);
|
|
83
|
+
this.dappEncryptionKeyPair = nacl.box.keyPair.fromSecretKey(secretKey);
|
|
84
|
+
} else {
|
|
85
|
+
const newKeyPair = nacl.box.keyPair();
|
|
86
|
+
this.dappEncryptionKeyPair = newKeyPair;
|
|
87
|
+
await this.getStorage().setItem(
|
|
88
|
+
this.getDappKeypairStorageKey(),
|
|
89
|
+
bs58.encode(newKeyPair.secretKey)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
// disconnect and clear session
|
|
94
|
+
await this.disconnect();
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
override async connect(opts?: ConnectOptions): Promise<Namespaces | undefined> {
|
|
100
|
+
if (this.isConnected() && this.namespaces) {
|
|
101
|
+
return this.namespaces;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const defaultNetworkId: CaipNetworkId | undefined =
|
|
105
|
+
opts?.defaultNetwork?.caipNetworkId?.split(':')?.[0] === 'solana'
|
|
106
|
+
? opts?.defaultNetwork?.caipNetworkId
|
|
107
|
+
: opts?.namespaces?.['solana']?.chains?.[0];
|
|
108
|
+
|
|
109
|
+
const requestedCluster =
|
|
110
|
+
this.config?.cluster ??
|
|
111
|
+
(Object.keys(SOLANA_CLUSTER_TO_NETWORK).find(
|
|
112
|
+
key => SOLANA_CLUSTER_TO_NETWORK[key as Cluster]?.caipNetworkId === defaultNetworkId
|
|
113
|
+
) as Cluster | undefined);
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const connectResult = await this.getProvider().connect({ cluster: requestedCluster });
|
|
117
|
+
|
|
118
|
+
const solanaChainId = SOLANA_CLUSTER_TO_NETWORK[connectResult.cluster]?.caipNetworkId;
|
|
119
|
+
if (!solanaChainId) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`${this.type} Connect: Internal - Unknown cluster mapping for ${connectResult.cluster}`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
this.currentCaipNetworkId = solanaChainId;
|
|
125
|
+
|
|
126
|
+
this.wallet = {
|
|
127
|
+
name: this.getWalletInfo()?.name
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const userPublicKey = this.getProvider().getUserPublicKey();
|
|
131
|
+
if (!userPublicKey) {
|
|
132
|
+
throw new Error(`${this.type} Connect: Provider failed to return a user public key.`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const caipAddress = `${this.currentCaipNetworkId}:${userPublicKey}` as CaipAddress;
|
|
136
|
+
this.namespaces = {
|
|
137
|
+
[DeeplinkConnector.SUPPORTED_NAMESPACE]: {
|
|
138
|
+
accounts: [caipAddress],
|
|
139
|
+
methods: Object.values(SOLANA_SIGNING_METHODS),
|
|
140
|
+
events: [],
|
|
141
|
+
chains: [this.currentCaipNetworkId]
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
await this.saveSession(); // Save connector-specific session on successful connect
|
|
146
|
+
|
|
147
|
+
return this.namespaces;
|
|
148
|
+
} catch (error: any) {
|
|
149
|
+
this.clearSession();
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
override async disconnect(): Promise<void> {
|
|
155
|
+
try {
|
|
156
|
+
if (this.isConnected()) {
|
|
157
|
+
await super.disconnect();
|
|
158
|
+
}
|
|
159
|
+
} catch (error: any) {
|
|
160
|
+
console.warn(
|
|
161
|
+
`${this.type} Connector: Error during provider disconnect: ${error.message}. Proceeding with local clear.`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Cleanup provider resources
|
|
166
|
+
if (this.provider) {
|
|
167
|
+
(this.provider as DeeplinkProvider).destroy();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
await this.clearSession();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private async clearSession(): Promise<void> {
|
|
174
|
+
this.namespaces = undefined;
|
|
175
|
+
this.wallet = undefined;
|
|
176
|
+
this.currentCaipNetworkId = null;
|
|
177
|
+
await this.clearSessionStorage();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
override getProvider(): DeeplinkProvider {
|
|
181
|
+
if (!this.provider) {
|
|
182
|
+
throw new Error(`${this.type} Connector: Provider not initialized. Call init() first.`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return this.provider as DeeplinkProvider;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private getStorage(): Storage {
|
|
189
|
+
if (!this.storage) {
|
|
190
|
+
throw new Error(`${this.type} Connector: Storage not initialized. Call init() first.`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return this.storage;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
override getNamespaces(): Namespaces {
|
|
197
|
+
if (!this.namespaces) {
|
|
198
|
+
throw new Error(`${this.type} Connector: Not connected. Call connect() first.`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return this.namespaces;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
override getChainId(namespace: ChainNamespace): CaipNetworkId | undefined {
|
|
205
|
+
if (namespace === DeeplinkConnector.SUPPORTED_NAMESPACE) {
|
|
206
|
+
return this.currentCaipNetworkId ?? undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
override getProperties(): ConnectionProperties | undefined {
|
|
213
|
+
return this.properties;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
isConnected(): boolean {
|
|
217
|
+
// Rely solely on the provider as the source of truth for connection status.
|
|
218
|
+
const provider = this.getProvider();
|
|
219
|
+
|
|
220
|
+
return provider.isConnected() && !!provider.getUserPublicKey();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
override async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
224
|
+
const targetClusterName = Object.keys(SOLANA_CLUSTER_TO_NETWORK).find(
|
|
225
|
+
key => SOLANA_CLUSTER_TO_NETWORK[key as Cluster]?.caipNetworkId === network.caipNetworkId
|
|
226
|
+
) as Cluster | undefined;
|
|
227
|
+
|
|
228
|
+
if (!targetClusterName) {
|
|
229
|
+
throw new Error(
|
|
230
|
+
`${this.type} Connector: Cannot switch to unsupported network ID: ${network.id}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const currentClusterName = Object.keys(SOLANA_CLUSTER_TO_NETWORK).find(
|
|
235
|
+
key => SOLANA_CLUSTER_TO_NETWORK[key as Cluster]?.caipNetworkId === this.currentCaipNetworkId
|
|
236
|
+
) as Cluster | undefined;
|
|
237
|
+
|
|
238
|
+
if (targetClusterName === currentClusterName && this.isConnected()) {
|
|
239
|
+
return Promise.resolve();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Phantom/Solflare don't provide a way to switch network, so we need to disconnect and reconnect.
|
|
243
|
+
await this.disconnect(); // Clear current session
|
|
244
|
+
|
|
245
|
+
// Create a temporary options object to guide the new connection
|
|
246
|
+
const tempConnectOpts: ConnectOptions = {
|
|
247
|
+
defaultNetwork: SOLANA_CLUSTER_TO_NETWORK[targetClusterName]
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// Attempt to connect to the new cluster
|
|
251
|
+
// The connect method will use the defaultNetwork from opts to determine the cluster.
|
|
252
|
+
await this.connect(tempConnectOpts);
|
|
253
|
+
this.getProvider().emit('chainChanged', network.id);
|
|
254
|
+
|
|
255
|
+
// Verify if the connection was successful and to the correct new network
|
|
256
|
+
if (
|
|
257
|
+
!this.isConnected() ||
|
|
258
|
+
this.getChainId(DeeplinkConnector.SUPPORTED_NAMESPACE) !==
|
|
259
|
+
tempConnectOpts.defaultNetwork?.caipNetworkId
|
|
260
|
+
) {
|
|
261
|
+
throw new Error(
|
|
262
|
+
`${this.type} Connector: Failed to switch network to ${targetClusterName}. Please try connecting manually.`
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Orchestrates session restoration
|
|
268
|
+
override async restoreSession(): Promise<boolean> {
|
|
269
|
+
try {
|
|
270
|
+
const providerSession = await this.getProvider().restoreSession();
|
|
271
|
+
if (!providerSession) {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// If provider session is restored, try to restore connector data
|
|
276
|
+
const connectorData = await this.getStorage().getItem<DeeplinkConnectorSessionData>(
|
|
277
|
+
this.getStorageKey()
|
|
278
|
+
);
|
|
279
|
+
if (!connectorData) {
|
|
280
|
+
// Self-heal: reconstruct connector state from provider session
|
|
281
|
+
const userPublicKey = this.getProvider().getUserPublicKey();
|
|
282
|
+
const cluster = this.getProvider().getCurrentCluster();
|
|
283
|
+
const caipNetworkId = SOLANA_CLUSTER_TO_NETWORK[cluster]?.caipNetworkId;
|
|
284
|
+
if (userPublicKey && caipNetworkId) {
|
|
285
|
+
this.currentCaipNetworkId = caipNetworkId;
|
|
286
|
+
this.wallet = { name: this.getWalletInfo()?.name };
|
|
287
|
+
const caipAddress = `${caipNetworkId}:${userPublicKey}` as CaipAddress;
|
|
288
|
+
this.namespaces = {
|
|
289
|
+
[DeeplinkConnector.SUPPORTED_NAMESPACE]: {
|
|
290
|
+
accounts: [caipAddress],
|
|
291
|
+
methods: Object.values(SOLANA_SIGNING_METHODS),
|
|
292
|
+
events: [],
|
|
293
|
+
chains: [caipNetworkId]
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
await this.saveSession();
|
|
297
|
+
} else {
|
|
298
|
+
// Provider looks connected but we can't reconstruct state → clear everything
|
|
299
|
+
await this.disconnect();
|
|
300
|
+
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
this.namespaces = connectorData.namespaces;
|
|
305
|
+
this.wallet = connectorData.wallet;
|
|
306
|
+
this.currentCaipNetworkId = connectorData.currentCaipNetworkId;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Final validation
|
|
310
|
+
if (this.isConnected()) {
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// If validation fails, something is out of sync. Clear everything.
|
|
315
|
+
await this.disconnect();
|
|
316
|
+
|
|
317
|
+
return false;
|
|
318
|
+
} catch (error) {
|
|
319
|
+
// On any error, disconnect to ensure a clean state
|
|
320
|
+
await this.disconnect();
|
|
321
|
+
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Saves only connector-specific data
|
|
327
|
+
private async saveSession(): Promise<void> {
|
|
328
|
+
if (!this.namespaces || !this.wallet || !this.currentCaipNetworkId) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const connectorData: DeeplinkConnectorSessionData = {
|
|
333
|
+
namespaces: this.namespaces,
|
|
334
|
+
wallet: this.wallet,
|
|
335
|
+
currentCaipNetworkId: this.currentCaipNetworkId
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
await this.getStorage().setItem(this.getStorageKey(), connectorData);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
// console.error(`${this.type} Connector: Failed to save session.`, error);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Clears only connector-specific data from storage
|
|
346
|
+
private async clearSessionStorage(): Promise<void> {
|
|
347
|
+
try {
|
|
348
|
+
await this.getStorage().removeItem(this.getStorageKey());
|
|
349
|
+
} catch (error) {
|
|
350
|
+
// console.error(`${this.type} Connector: Failed to clear session from storage.`, error);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
@@ -1,328 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
};
|
|
1
|
+
import { ConstantsUtil, type WalletInfo } from '@reown/appkit-common-react-native';
|
|
2
|
+
import { DeeplinkConnector } from './DeeplinkConnector';
|
|
3
|
+
import type { PhantomConnectorConfig } from '../types';
|
|
32
4
|
|
|
5
|
+
const PHANTOM_BASE_URL = 'https://phantom.app/ul/v1';
|
|
33
6
|
const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
|
|
34
|
-
const
|
|
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';
|
|
7
|
+
const PHANTOM_DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
|
|
43
8
|
|
|
9
|
+
export class PhantomConnector extends DeeplinkConnector {
|
|
44
10
|
constructor(config?: PhantomConnectorConfig) {
|
|
45
|
-
super({ type: 'phantom' });
|
|
46
|
-
this.config = config ?? { cluster: 'mainnet-beta' };
|
|
11
|
+
super({ type: 'phantom', cluster: config?.cluster });
|
|
47
12
|
}
|
|
48
13
|
|
|
49
|
-
override
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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!
|
|
14
|
+
override getWalletInfo(): WalletInfo {
|
|
15
|
+
return {
|
|
16
|
+
name: ConstantsUtil.PHANTOM_CUSTOM_WALLET.name,
|
|
17
|
+
type: 'external'
|
|
66
18
|
};
|
|
67
|
-
|
|
68
|
-
this.provider = new PhantomProvider(providerConfig);
|
|
69
|
-
await 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
19
|
}
|
|
193
20
|
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
}
|
|
21
|
+
protected getBaseUrl(): string {
|
|
22
|
+
return PHANTOM_BASE_URL;
|
|
260
23
|
}
|
|
261
24
|
|
|
262
|
-
|
|
263
|
-
|
|
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 connectorData = await this.getStorage().getItem<PhantomConnectorSessionData>(
|
|
272
|
-
PHANTOM_CONNECTOR_STORAGE_KEY
|
|
273
|
-
);
|
|
274
|
-
if (!connectorData) {
|
|
275
|
-
return false; // Provider session exists but connector data is missing
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
this.namespaces = connectorData.namespaces;
|
|
279
|
-
this.wallet = connectorData.wallet;
|
|
280
|
-
this.currentCaipNetworkId = connectorData.currentCaipNetworkId;
|
|
281
|
-
|
|
282
|
-
// await this.initializeKeyPair();
|
|
283
|
-
|
|
284
|
-
// Final validation
|
|
285
|
-
if (this.isConnected()) {
|
|
286
|
-
return true;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// If validation fails, something is out of sync. Clear everything.
|
|
290
|
-
await this.disconnect();
|
|
291
|
-
|
|
292
|
-
return false;
|
|
293
|
-
} catch (error) {
|
|
294
|
-
// On any error, disconnect to ensure a clean state
|
|
295
|
-
await this.disconnect();
|
|
296
|
-
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
25
|
+
protected getStorageKey(): string {
|
|
26
|
+
return PHANTOM_CONNECTOR_STORAGE_KEY;
|
|
299
27
|
}
|
|
300
28
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (!this.namespaces || !this.wallet || !this.currentCaipNetworkId) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const connectorData: PhantomConnectorSessionData = {
|
|
308
|
-
namespaces: this.namespaces,
|
|
309
|
-
wallet: this.wallet,
|
|
310
|
-
currentCaipNetworkId: this.currentCaipNetworkId
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
try {
|
|
314
|
-
await this.getStorage().setItem(PHANTOM_CONNECTOR_STORAGE_KEY, connectorData);
|
|
315
|
-
} catch (error) {
|
|
316
|
-
// console.error('PhantomConnector: Failed to save session.', error);
|
|
317
|
-
}
|
|
29
|
+
protected getDappKeypairStorageKey(): string {
|
|
30
|
+
return PHANTOM_DAPP_KEYPAIR_STORAGE_KEY;
|
|
318
31
|
}
|
|
319
32
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
try {
|
|
323
|
-
await this.getStorage().removeItem(PHANTOM_CONNECTOR_STORAGE_KEY);
|
|
324
|
-
} catch (error) {
|
|
325
|
-
// console.error('PhantomConnector: Failed to clear session from storage.', error);
|
|
326
|
-
}
|
|
33
|
+
protected getEncryptionKeyFieldName(): string {
|
|
34
|
+
return 'phantom_encryption_public_key';
|
|
327
35
|
}
|
|
328
36
|
}
|