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