@reown/appkit-solana-react-native 2.0.0-alpha.0 → 2.0.0
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 +0 -1
- package/lib/commonjs/adapter.js.map +1 -1
- package/lib/commonjs/connectors/PhantomConnector.js +247 -0
- package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
- package/lib/commonjs/helpers.js +9 -0
- package/lib/commonjs/helpers.js.map +1 -1
- package/lib/commonjs/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/providers/PhantomProvider.js +391 -0
- package/lib/commonjs/providers/PhantomProvider.js.map +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/adapter.js +0 -1
- package/lib/module/adapter.js.map +1 -1
- package/lib/module/connectors/PhantomConnector.js +239 -0
- package/lib/module/connectors/PhantomConnector.js.map +1 -0
- package/lib/module/helpers.js +9 -0
- package/lib/module/helpers.js.map +1 -1
- package/lib/module/index.js +7 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/PhantomProvider.js +383 -0
- package/lib/module/providers/PhantomProvider.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/adapter.d.ts.map +1 -1
- package/lib/typescript/connectors/PhantomConnector.d.ts +26 -0
- package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -0
- package/lib/typescript/helpers.d.ts +8 -7
- package/lib/typescript/helpers.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +3 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/providers/PhantomProvider.d.ts +37 -0
- package/lib/typescript/providers/PhantomProvider.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +96 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/package.json +7 -5
- package/src/adapter.ts +1 -5
- package/src/connectors/PhantomConnector.ts +328 -0
- package/src/helpers.ts +9 -7
- package/src/index.ts +8 -0
- package/src/providers/PhantomProvider.ts +530 -0
- package/src/types.ts +131 -0
- package/src/index.tsx +0 -2
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reown/appkit-solana-react-native",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"main": "lib/commonjs/index.js",
|
|
5
5
|
"types": "lib/typescript/index.d.ts",
|
|
6
6
|
"module": "lib/module/index.js",
|
|
7
|
-
"
|
|
7
|
+
"react-native": "src/index.ts",
|
|
8
|
+
"source": "src/index.ts",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"build": "bob build",
|
|
10
11
|
"clean": "rm -rf lib",
|
|
@@ -38,7 +39,8 @@
|
|
|
38
39
|
"access": "public"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@reown/appkit-common-react-native": "2.0.0
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
"@reown/appkit-common-react-native": "2.0.0",
|
|
43
|
+
"bs58": "6.0.0",
|
|
44
|
+
"tweetnacl": "1.0.3"
|
|
45
|
+
}
|
|
44
46
|
}
|
package/src/adapter.ts
CHANGED
|
@@ -56,11 +56,7 @@ export class SolanaAdapter extends SolanaBaseAdapter {
|
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
this.emit('balanceChanged', {
|
|
60
|
-
namespace: this.getSupportedNamespace(),
|
|
61
|
-
address: balanceAddress,
|
|
62
|
-
balance
|
|
63
|
-
});
|
|
59
|
+
this.emit('balanceChanged', { address: balanceAddress, balance });
|
|
64
60
|
|
|
65
61
|
return balance;
|
|
66
62
|
} catch (error) {
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WalletConnector,
|
|
3
|
+
type AppKitNetwork,
|
|
4
|
+
type CaipNetworkId,
|
|
5
|
+
type ChainNamespace,
|
|
6
|
+
type ConnectOptions,
|
|
7
|
+
type Namespaces,
|
|
8
|
+
type WalletInfo,
|
|
9
|
+
type CaipAddress,
|
|
10
|
+
type ConnectorInitOptions,
|
|
11
|
+
type Storage,
|
|
12
|
+
solana,
|
|
13
|
+
solanaDevnet,
|
|
14
|
+
solanaTestnet
|
|
15
|
+
} from '@reown/appkit-common-react-native';
|
|
16
|
+
import nacl from 'tweetnacl';
|
|
17
|
+
import bs58 from 'bs58';
|
|
18
|
+
|
|
19
|
+
import { PhantomProvider, SOLANA_SIGNING_METHODS } from '../providers/PhantomProvider';
|
|
20
|
+
import type {
|
|
21
|
+
PhantomCluster,
|
|
22
|
+
PhantomConnectorConfig,
|
|
23
|
+
PhantomConnectorSessionData,
|
|
24
|
+
PhantomProviderConfig
|
|
25
|
+
} from '../types';
|
|
26
|
+
|
|
27
|
+
const SOLANA_CLUSTER_TO_CHAIN_ID_PART: Record<PhantomCluster, string> = {
|
|
28
|
+
'mainnet-beta': solana.id as string,
|
|
29
|
+
'testnet': solanaTestnet.id as string,
|
|
30
|
+
'devnet': solanaDevnet.id as string
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
|
|
34
|
+
const DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
|
|
35
|
+
|
|
36
|
+
export class PhantomConnector extends WalletConnector {
|
|
37
|
+
private readonly config: PhantomConnectorConfig;
|
|
38
|
+
|
|
39
|
+
private currentCaipNetworkId: CaipNetworkId | null = null;
|
|
40
|
+
private dappEncryptionKeyPair?: nacl.BoxKeyPair;
|
|
41
|
+
|
|
42
|
+
private static readonly SUPPORTED_NAMESPACE: ChainNamespace = 'solana';
|
|
43
|
+
|
|
44
|
+
constructor(config?: PhantomConnectorConfig) {
|
|
45
|
+
super({ type: 'phantom' });
|
|
46
|
+
this.config = config ?? { cluster: 'mainnet-beta' };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
override async init(ops: ConnectorInitOptions) {
|
|
50
|
+
super.init(ops);
|
|
51
|
+
this.storage = ops.storage;
|
|
52
|
+
await this.initializeKeyPair();
|
|
53
|
+
|
|
54
|
+
const appScheme = ops.metadata.redirect?.universal;
|
|
55
|
+
if (!appScheme) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
'Phantom Connector: No universal link found in metadata. Please add redirect.universal to the metadata.'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const providerConfig: PhantomProviderConfig = {
|
|
62
|
+
appScheme,
|
|
63
|
+
dappUrl: ops.metadata.url,
|
|
64
|
+
storage: ops.storage,
|
|
65
|
+
dappEncryptionKeyPair: this.dappEncryptionKeyPair!
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
this.provider = new PhantomProvider(providerConfig);
|
|
69
|
+
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
|
+
}
|
|
193
|
+
|
|
194
|
+
override getChainId(namespace: ChainNamespace): CaipNetworkId | undefined {
|
|
195
|
+
if (namespace === PhantomConnector.SUPPORTED_NAMESPACE) {
|
|
196
|
+
return this.currentCaipNetworkId ?? undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
override getWalletInfo(): WalletInfo | undefined {
|
|
203
|
+
if (!this.isConnected()) {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return this.wallet;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
isConnected(): boolean {
|
|
211
|
+
// Rely solely on the provider as the source of truth for connection status.
|
|
212
|
+
return this.getProvider().isConnected() && !!this.getProvider().getUserPublicKey();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
override async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
216
|
+
const targetClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
|
|
217
|
+
key =>
|
|
218
|
+
SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART] ===
|
|
219
|
+
network.id
|
|
220
|
+
) as PhantomCluster | undefined;
|
|
221
|
+
|
|
222
|
+
if (!targetClusterName) {
|
|
223
|
+
throw new Error(`Cannot switch to unsupported network ID: ${network.id}`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const currentClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
|
|
227
|
+
key =>
|
|
228
|
+
`solana:${
|
|
229
|
+
SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART]
|
|
230
|
+
}` === this.currentCaipNetworkId
|
|
231
|
+
) as PhantomCluster | undefined;
|
|
232
|
+
|
|
233
|
+
if (targetClusterName === currentClusterName && this.isConnected()) {
|
|
234
|
+
return Promise.resolve();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// For deeplink wallets, switching network effectively means re-connecting to the new cluster.
|
|
238
|
+
// We can try to disconnect the current session and then initiate a new connection.
|
|
239
|
+
// console.log(`Attempting to switch network to: ${targetClusterName}`);
|
|
240
|
+
await this.disconnect(); // Clear current session
|
|
241
|
+
|
|
242
|
+
// Create a temporary options object to guide the new connection
|
|
243
|
+
const tempConnectOpts: ConnectOptions = {
|
|
244
|
+
defaultChain: `solana:${SOLANA_CLUSTER_TO_CHAIN_ID_PART[targetClusterName]}` as CaipNetworkId
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// Attempt to connect to the new cluster
|
|
248
|
+
// The connect method will use the defaultChain from opts to determine the cluster.
|
|
249
|
+
await this.connect(tempConnectOpts);
|
|
250
|
+
|
|
251
|
+
// Verify if the connection was successful and to the correct new network
|
|
252
|
+
if (
|
|
253
|
+
!this.isConnected() ||
|
|
254
|
+
this.getChainId(PhantomConnector.SUPPORTED_NAMESPACE) !== tempConnectOpts.defaultChain
|
|
255
|
+
) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
`Failed to switch network to ${targetClusterName}. Please try connecting manually.`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Orchestrates session restoration
|
|
263
|
+
public async restoreSession(): Promise<boolean> {
|
|
264
|
+
try {
|
|
265
|
+
const providerSession = await this.getProvider().restoreSession();
|
|
266
|
+
if (!providerSession) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// If provider session is restored, try to restore connector data
|
|
271
|
+
const 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
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Saves only connector-specific data
|
|
302
|
+
private async saveSession(): Promise<void> {
|
|
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
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Clears only connector-specific data from storage
|
|
321
|
+
private async clearSessionStorage(): Promise<void> {
|
|
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
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
package/src/helpers.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
address: string;
|
|
3
|
-
symbol: string;
|
|
4
|
-
name: string;
|
|
5
|
-
decimals: number;
|
|
6
|
-
logoURI?: string;
|
|
7
|
-
}
|
|
1
|
+
import type { TokenInfo } from './types';
|
|
8
2
|
|
|
9
3
|
/**
|
|
10
4
|
* Validates if the given string is a Solana address.
|
|
@@ -48,6 +42,7 @@ export async function getSolanaNativeBalance(rpcUrl: string, address: string): P
|
|
|
48
42
|
}
|
|
49
43
|
|
|
50
44
|
let tokenCache: Record<string, TokenInfo> = {};
|
|
45
|
+
|
|
51
46
|
/**
|
|
52
47
|
* Fetch metadata for a Solana SPL token using the Jupiter token list.
|
|
53
48
|
* @param mint - The token's mint address
|
|
@@ -71,6 +66,13 @@ export async function getSolanaTokenMetadata(mint: string): Promise<TokenInfo |
|
|
|
71
66
|
}
|
|
72
67
|
}
|
|
73
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Get the balance of a token for a given address
|
|
71
|
+
* @param rpcUrl - The RPC URL to use
|
|
72
|
+
* @param address - The address to get the balance for
|
|
73
|
+
* @param tokenAddress - The address of the token to get the balance for
|
|
74
|
+
* @returns The balance of the token for the given address
|
|
75
|
+
*/
|
|
74
76
|
export async function getSolanaTokenBalance(
|
|
75
77
|
rpcUrl: string,
|
|
76
78
|
address: string,
|