@reown/appkit-solana-react-native 0.0.0-canary-20251008180350
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 +271 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/DeeplinkConnector.js +271 -0
- package/lib/commonjs/connectors/DeeplinkConnector.js.map +1 -0
- package/lib/commonjs/connectors/PhantomConnector.js +39 -0
- package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
- package/lib/commonjs/connectors/SolflareConnector.js +39 -0
- package/lib/commonjs/connectors/SolflareConnector.js.map +1 -0
- package/lib/commonjs/helpers.js +102 -0
- package/lib/commonjs/helpers.js.map +1 -0
- package/lib/commonjs/index.js +27 -0
- package/lib/commonjs/index.js.map +1 -0
- 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 +6 -0
- package/lib/commonjs/types.js.map +1 -0
- 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 +265 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/DeeplinkConnector.js +265 -0
- package/lib/module/connectors/DeeplinkConnector.js.map +1 -0
- package/lib/module/connectors/PhantomConnector.js +34 -0
- package/lib/module/connectors/PhantomConnector.js.map +1 -0
- package/lib/module/connectors/SolflareConnector.js +34 -0
- package/lib/module/connectors/SolflareConnector.js.map +1 -0
- package/lib/module/helpers.js +95 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +11 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/providers/DeeplinkProvider.js +426 -0
- package/lib/module/providers/DeeplinkProvider.js.map +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- 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 +23 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- 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 +12 -0
- package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -0
- package/lib/typescript/connectors/SolflareConnector.d.ts +12 -0
- package/lib/typescript/connectors/SolflareConnector.d.ts.map +1 -0
- package/lib/typescript/helpers.d.ts +31 -0
- package/lib/typescript/helpers.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -0
- 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 +99 -0
- package/lib/typescript/types.d.ts.map +1 -0
- 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 +54 -0
- package/readme.md +9 -0
- package/src/adapter.ts +315 -0
- package/src/connectors/DeeplinkConnector.ts +353 -0
- package/src/connectors/PhantomConnector.ts +36 -0
- package/src/connectors/SolflareConnector.ts +36 -0
- package/src/helpers.ts +102 -0
- package/src/index.ts +9 -0
- package/src/providers/DeeplinkProvider.ts +605 -0
- package/src/types.ts +132 -0
- package/src/utils/createSPLTokenTransaction.ts +152 -0
- package/src/utils/createSendTransaction.ts +41 -0
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SolanaBaseAdapter,
|
|
3
|
+
type AppKitNetwork,
|
|
4
|
+
type CaipAddress,
|
|
5
|
+
type ChainNamespace,
|
|
6
|
+
type GetBalanceParams,
|
|
7
|
+
type GetBalanceResponse
|
|
8
|
+
} from '@reown/appkit-common-react-native';
|
|
9
|
+
import { getSolanaNativeBalance, getSolanaTokenBalance } from './helpers';
|
|
10
|
+
import { Connection, Transaction, VersionedTransaction } from '@solana/web3.js';
|
|
11
|
+
import base58 from 'bs58';
|
|
12
|
+
import { createSendTransaction } from './utils/createSendTransaction';
|
|
13
|
+
import { createSPLTokenTransaction } from './utils/createSPLTokenTransaction';
|
|
14
|
+
|
|
15
|
+
export interface SolanaTransactionData {
|
|
16
|
+
fromAddress: string;
|
|
17
|
+
toAddress: string;
|
|
18
|
+
amount: number;
|
|
19
|
+
network?: AppKitNetwork;
|
|
20
|
+
rpcUrl?: string;
|
|
21
|
+
tokenMint?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class SolanaAdapter extends SolanaBaseAdapter {
|
|
25
|
+
private static supportedNamespace: ChainNamespace = 'solana';
|
|
26
|
+
|
|
27
|
+
constructor() {
|
|
28
|
+
super({
|
|
29
|
+
supportedNamespace: SolanaAdapter.supportedNamespace,
|
|
30
|
+
adapterType: 'solana'
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async getBalance(params: GetBalanceParams): Promise<GetBalanceResponse> {
|
|
35
|
+
const { network, address, tokens } = params;
|
|
36
|
+
|
|
37
|
+
if (!this.connector) throw new Error('No active connector');
|
|
38
|
+
if (!network) throw new Error('No network provided');
|
|
39
|
+
|
|
40
|
+
const balanceAddress =
|
|
41
|
+
address || this.getAccounts()?.find(account => account.includes(network.id.toString()));
|
|
42
|
+
|
|
43
|
+
if (!balanceAddress) {
|
|
44
|
+
return { amount: '0.00', symbol: 'SOL' };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const rpcUrl = network.rpcUrls?.default?.http?.[0];
|
|
49
|
+
if (!rpcUrl) throw new Error('No RPC URL available');
|
|
50
|
+
|
|
51
|
+
const base58Address = balanceAddress.split(':')[2];
|
|
52
|
+
|
|
53
|
+
if (!base58Address) throw new Error('Invalid balance address');
|
|
54
|
+
|
|
55
|
+
const token = network?.caipNetworkId && tokens?.[network.caipNetworkId]?.address;
|
|
56
|
+
let balance;
|
|
57
|
+
|
|
58
|
+
if (token) {
|
|
59
|
+
const { amount, symbol } = await getSolanaTokenBalance(rpcUrl, base58Address, token);
|
|
60
|
+
balance = {
|
|
61
|
+
amount,
|
|
62
|
+
symbol
|
|
63
|
+
};
|
|
64
|
+
} else {
|
|
65
|
+
const amount = await getSolanaNativeBalance(rpcUrl, base58Address);
|
|
66
|
+
balance = {
|
|
67
|
+
amount: amount.toString(),
|
|
68
|
+
symbol: 'SOL'
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.emit('balanceChanged', { address: balanceAddress, balance });
|
|
73
|
+
|
|
74
|
+
return balance;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return { amount: '0.00', symbol: 'SOL' };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async signTransaction<T extends Transaction | VersionedTransaction>(
|
|
81
|
+
transaction: T,
|
|
82
|
+
network?: AppKitNetwork
|
|
83
|
+
): Promise<T> {
|
|
84
|
+
if (!this.connector) {
|
|
85
|
+
throw new Error('SolanaAdapter:signTransaction - no active connector');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!network) {
|
|
89
|
+
throw new Error('SolanaAdapter:signTransaction - network is undefined');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const provider = this.connector.getProvider('solana');
|
|
93
|
+
if (!provider) {
|
|
94
|
+
throw new Error('SolanaAdapter:signTransaction - provider is undefined');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
// Check if this is a deeplink provider (Phantom/Solflare)
|
|
99
|
+
const isDeeplinkProvider =
|
|
100
|
+
this.connector.type === 'phantom' || this.connector.type === 'solflare';
|
|
101
|
+
|
|
102
|
+
// Serialize transaction based on provider type
|
|
103
|
+
let serializedTransaction: string;
|
|
104
|
+
if (isDeeplinkProvider) {
|
|
105
|
+
// Deeplink providers (Phantom/Solflare) expect base58
|
|
106
|
+
const transactionBytes = new Uint8Array(transaction.serialize({ verifySignatures: false }));
|
|
107
|
+
serializedTransaction = base58.encode(transactionBytes);
|
|
108
|
+
} else {
|
|
109
|
+
// WalletConnect providers expect base64 (following WalletConnect standard)
|
|
110
|
+
serializedTransaction = Buffer.from(
|
|
111
|
+
new Uint8Array(transaction.serialize({ verifySignatures: false }))
|
|
112
|
+
).toString('base64');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const result = (await provider.request(
|
|
116
|
+
{
|
|
117
|
+
method: 'solana_signTransaction',
|
|
118
|
+
params: {
|
|
119
|
+
transaction: serializedTransaction,
|
|
120
|
+
pubkey: this.getAccounts()?.[0]?.split(':')[2] || ''
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
network.caipNetworkId
|
|
124
|
+
)) as { signature?: string; transaction?: string };
|
|
125
|
+
|
|
126
|
+
// Handle different response formats
|
|
127
|
+
if ('signature' in result && result.signature) {
|
|
128
|
+
// Old RPC response format - add signature to transaction
|
|
129
|
+
const decoded = base58.decode(result.signature);
|
|
130
|
+
if (transaction instanceof Transaction && transaction.feePayer) {
|
|
131
|
+
transaction.addSignature(
|
|
132
|
+
transaction.feePayer,
|
|
133
|
+
Buffer.from(decoded) as Buffer & Uint8Array
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return transaction;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if ('transaction' in result && result.transaction) {
|
|
141
|
+
// New response format - deserialize the signed transaction
|
|
142
|
+
let decodedTransaction: Buffer;
|
|
143
|
+
|
|
144
|
+
if (isDeeplinkProvider) {
|
|
145
|
+
// Deeplink providers return base58 encoded transactions
|
|
146
|
+
try {
|
|
147
|
+
const decodedBytes = base58.decode(result.transaction);
|
|
148
|
+
decodedTransaction = Buffer.from(decodedBytes);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
throw new Error('Failed to decode base58 transaction from deeplink provider');
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
// WalletConnect providers return base64 encoded transactions
|
|
154
|
+
decodedTransaction = Buffer.from(result.transaction, 'base64');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (transaction instanceof VersionedTransaction) {
|
|
158
|
+
return VersionedTransaction.deserialize(new Uint8Array(decodedTransaction)) as T;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return Transaction.from(decodedTransaction) as T;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
throw new Error('SolanaAdapter:signTransaction - invalid response format');
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (error instanceof Error) {
|
|
167
|
+
throw new Error(`SolanaAdapter:signTransaction - ${error.message}`);
|
|
168
|
+
}
|
|
169
|
+
throw new Error('SolanaAdapter:signTransaction - unknown error occurred');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async sendTransaction(data: SolanaTransactionData): Promise<string | null> {
|
|
174
|
+
const { fromAddress, toAddress, amount, network, rpcUrl, tokenMint } = data;
|
|
175
|
+
|
|
176
|
+
if (!this.connector) {
|
|
177
|
+
throw new Error('SolanaAdapter:sendTransaction - no active connector');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const provider = this.connector.getProvider('solana');
|
|
181
|
+
if (!provider) {
|
|
182
|
+
throw new Error('SolanaAdapter:sendTransaction - provider is undefined');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (!network) {
|
|
186
|
+
throw new Error('SolanaAdapter:sendTransaction - network is undefined');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (!fromAddress) {
|
|
190
|
+
throw new Error('SolanaAdapter:sendTransaction - fromAddress is undefined');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!toAddress) {
|
|
194
|
+
throw new Error('SolanaAdapter:sendTransaction - toAddress is undefined');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!amount || amount <= 0) {
|
|
198
|
+
throw new Error('SolanaAdapter:sendTransaction - amount must be greater than 0');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
// Determine RPC URL
|
|
203
|
+
let connectionRpcUrl = rpcUrl;
|
|
204
|
+
if (!connectionRpcUrl && network) {
|
|
205
|
+
connectionRpcUrl = network.rpcUrls?.default?.http?.[0];
|
|
206
|
+
}
|
|
207
|
+
if (!connectionRpcUrl) {
|
|
208
|
+
throw new Error('SolanaAdapter:sendTransaction - no RPC URL available');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Create connection
|
|
212
|
+
const connection = new Connection(connectionRpcUrl, 'confirmed');
|
|
213
|
+
|
|
214
|
+
const transaction = tokenMint
|
|
215
|
+
? await createSPLTokenTransaction({
|
|
216
|
+
connection,
|
|
217
|
+
fromAddress,
|
|
218
|
+
toAddress,
|
|
219
|
+
amount,
|
|
220
|
+
tokenMint
|
|
221
|
+
})
|
|
222
|
+
: await createSendTransaction({
|
|
223
|
+
connection,
|
|
224
|
+
fromAddress,
|
|
225
|
+
toAddress,
|
|
226
|
+
amount
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Sign the transaction
|
|
230
|
+
const signedTransaction = await this.signTransaction(transaction, network);
|
|
231
|
+
|
|
232
|
+
// Send the signed transaction
|
|
233
|
+
const signature = await connection.sendRawTransaction(signedTransaction.serialize(), {
|
|
234
|
+
skipPreflight: false,
|
|
235
|
+
preflightCommitment: 'confirmed'
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
if (!signature) {
|
|
239
|
+
throw new Error('SolanaAdapter:sendTransaction - no signature returned');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return signature;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (error instanceof Error) {
|
|
245
|
+
throw new Error(`SolanaAdapter:sendTransaction - ${error.message}`);
|
|
246
|
+
}
|
|
247
|
+
throw new Error('SolanaAdapter:sendTransaction - unknown error occurred');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
override async signMessage(address: string, message: string, chainId?: string): Promise<string> {
|
|
252
|
+
try {
|
|
253
|
+
if (!this.connector) {
|
|
254
|
+
throw new Error('SolanaAdapter:signMessage - no active connector');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const provider = this.connector.getProvider('solana');
|
|
258
|
+
if (!provider) {
|
|
259
|
+
throw new Error('SolanaAdapter:signMessage - provider is undefined');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const chain = chainId ? `${this.getSupportedNamespace()}:${chainId}` : undefined;
|
|
263
|
+
|
|
264
|
+
const encodedMessage = new TextEncoder().encode(message);
|
|
265
|
+
const params = {
|
|
266
|
+
message: base58.encode(encodedMessage),
|
|
267
|
+
pubkey: address
|
|
268
|
+
// For Phantom, pubkey is not part of signMessage params directly with session
|
|
269
|
+
// For other wallets, it might be needed if they don't infer from session
|
|
270
|
+
};
|
|
271
|
+
const { signature } = (await provider.request(
|
|
272
|
+
{ method: 'solana_signMessage', params },
|
|
273
|
+
chain
|
|
274
|
+
)) as {
|
|
275
|
+
signature: string;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
return signature;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
285
|
+
if (!this.connector) throw new Error('No active connector');
|
|
286
|
+
|
|
287
|
+
const provider = this.connector.getProvider('solana');
|
|
288
|
+
if (!provider) throw new Error('No active provider');
|
|
289
|
+
|
|
290
|
+
try {
|
|
291
|
+
await this.connector.switchNetwork(network);
|
|
292
|
+
|
|
293
|
+
return;
|
|
294
|
+
} catch (switchError: any) {
|
|
295
|
+
throw switchError;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
getAccounts(): CaipAddress[] | undefined {
|
|
300
|
+
if (!this.connector) throw new Error('No active connector');
|
|
301
|
+
const namespaces = this.connector.getNamespaces();
|
|
302
|
+
|
|
303
|
+
return namespaces[this.getSupportedNamespace()]?.accounts;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
disconnect(): Promise<void> {
|
|
307
|
+
if (!this.connector) throw new Error('No active connector');
|
|
308
|
+
|
|
309
|
+
return this.connector.disconnect();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
getSupportedNamespace(): ChainNamespace {
|
|
313
|
+
return SolanaAdapter.supportedNamespace;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ConstantsUtil, type WalletInfo } from '@reown/appkit-common-react-native';
|
|
2
|
+
import { DeeplinkConnector } from './DeeplinkConnector';
|
|
3
|
+
import type { PhantomConnectorConfig } from '../types';
|
|
4
|
+
|
|
5
|
+
const PHANTOM_BASE_URL = 'https://phantom.app/ul/v1';
|
|
6
|
+
const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
|
|
7
|
+
const PHANTOM_DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
|
|
8
|
+
|
|
9
|
+
export class PhantomConnector extends DeeplinkConnector {
|
|
10
|
+
constructor(config?: PhantomConnectorConfig) {
|
|
11
|
+
super({ type: 'phantom', cluster: config?.cluster });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
override getWalletInfo(): WalletInfo {
|
|
15
|
+
return {
|
|
16
|
+
name: ConstantsUtil.PHANTOM_CUSTOM_WALLET.name,
|
|
17
|
+
type: 'external'
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected getBaseUrl(): string {
|
|
22
|
+
return PHANTOM_BASE_URL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected getStorageKey(): string {
|
|
26
|
+
return PHANTOM_CONNECTOR_STORAGE_KEY;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protected getDappKeypairStorageKey(): string {
|
|
30
|
+
return PHANTOM_DAPP_KEYPAIR_STORAGE_KEY;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected getEncryptionKeyFieldName(): string {
|
|
34
|
+
return 'phantom_encryption_public_key';
|
|
35
|
+
}
|
|
36
|
+
}
|