@reown/appkit-solana-react-native 2.0.0-alpha.2 → 2.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/lib/commonjs/adapter.js +4 -5
  2. package/lib/commonjs/adapter.js.map +1 -1
  3. package/lib/commonjs/connectors/DeeplinkConnector.js +271 -0
  4. package/lib/commonjs/connectors/DeeplinkConnector.js.map +1 -0
  5. package/lib/commonjs/connectors/PhantomConnector.js +15 -223
  6. package/lib/commonjs/connectors/PhantomConnector.js.map +1 -1
  7. package/lib/commonjs/connectors/SolflareConnector.js +36 -0
  8. package/lib/commonjs/connectors/SolflareConnector.js.map +1 -0
  9. package/lib/commonjs/index.js +7 -0
  10. package/lib/commonjs/index.js.map +1 -1
  11. package/lib/commonjs/providers/DeeplinkProvider.js +432 -0
  12. package/lib/commonjs/providers/DeeplinkProvider.js.map +1 -0
  13. package/lib/module/adapter.js +4 -5
  14. package/lib/module/adapter.js.map +1 -1
  15. package/lib/module/connectors/DeeplinkConnector.js +265 -0
  16. package/lib/module/connectors/DeeplinkConnector.js.map +1 -0
  17. package/lib/module/connectors/PhantomConnector.js +16 -223
  18. package/lib/module/connectors/PhantomConnector.js.map +1 -1
  19. package/lib/module/connectors/SolflareConnector.js +31 -0
  20. package/lib/module/connectors/SolflareConnector.js.map +1 -0
  21. package/lib/module/index.js +1 -0
  22. package/lib/module/index.js.map +1 -1
  23. package/lib/module/providers/DeeplinkProvider.js +426 -0
  24. package/lib/module/providers/DeeplinkProvider.js.map +1 -0
  25. package/lib/typescript/adapter.d.ts +1 -3
  26. package/lib/typescript/adapter.d.ts.map +1 -1
  27. package/lib/typescript/connectors/DeeplinkConnector.d.ts +30 -0
  28. package/lib/typescript/connectors/DeeplinkConnector.d.ts.map +1 -0
  29. package/lib/typescript/connectors/PhantomConnector.d.ts +8 -23
  30. package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -1
  31. package/lib/typescript/connectors/SolflareConnector.d.ts +12 -0
  32. package/lib/typescript/connectors/SolflareConnector.d.ts.map +1 -0
  33. package/lib/typescript/index.d.ts +2 -1
  34. package/lib/typescript/index.d.ts.map +1 -1
  35. package/lib/typescript/providers/DeeplinkProvider.d.ts +59 -0
  36. package/lib/typescript/providers/DeeplinkProvider.d.ts.map +1 -0
  37. package/lib/typescript/types.d.ts +27 -32
  38. package/lib/typescript/types.d.ts.map +1 -1
  39. package/package.json +2 -2
  40. package/src/adapter.ts +4 -5
  41. package/src/connectors/DeeplinkConnector.ts +353 -0
  42. package/src/connectors/PhantomConnector.ts +17 -313
  43. package/src/connectors/SolflareConnector.ts +33 -0
  44. package/src/index.ts +2 -1
  45. package/src/providers/DeeplinkProvider.ts +605 -0
  46. package/src/types.ts +29 -37
  47. package/lib/commonjs/providers/PhantomProvider.js +0 -391
  48. package/lib/commonjs/providers/PhantomProvider.js.map +0 -1
  49. package/lib/module/providers/PhantomProvider.js +0 -385
  50. package/lib/module/providers/PhantomProvider.js.map +0 -1
  51. package/lib/typescript/providers/PhantomProvider.d.ts +0 -37
  52. package/lib/typescript/providers/PhantomProvider.d.ts.map +0 -1
  53. package/src/providers/PhantomProvider.ts +0 -532
@@ -1,329 +1,33 @@
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
- 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 { DeeplinkConnector } from './DeeplinkConnector';
3
+ import type { PhantomConnectorConfig } from '../types';
34
4
 
5
+ const PHANTOM_BASE_URL = 'https://phantom.app/ul/v1';
35
6
  const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
36
- const DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
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';
7
+ const PHANTOM_DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
45
8
 
9
+ export class PhantomConnector extends DeeplinkConnector {
46
10
  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
- }
172
-
173
- return this.provider as PhantomProvider;
11
+ super({ type: 'phantom', cluster: config?.cluster });
174
12
  }
175
13
 
176
- private getStorage(): Storage {
177
- if (!this.storage) {
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;
14
+ override getWalletInfo(): WalletInfo {
15
+ return ConstantsUtil.PHANTOM_CUSTOM_WALLET;
190
16
  }
191
17
 
192
- override getChainId(namespace: ChainNamespace): CaipNetworkId | undefined {
193
- if (namespace === PhantomConnector.SUPPORTED_NAMESPACE) {
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
- }
18
+ protected getBaseUrl(): string {
19
+ return PHANTOM_BASE_URL;
261
20
  }
262
21
 
263
- // Orchestrates session restoration
264
- override async restoreSession(): Promise<boolean> {
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
- }
22
+ protected getStorageKey(): string {
23
+ return PHANTOM_CONNECTOR_STORAGE_KEY;
300
24
  }
301
25
 
302
- // Saves only connector-specific data
303
- private async saveSession(): Promise<void> {
304
- if (!this.namespaces || !this.wallet || !this.currentCaipNetworkId) {
305
- return;
306
- }
307
-
308
- const connectorData: PhantomConnectorSessionData = {
309
- namespaces: this.namespaces,
310
- wallet: this.wallet,
311
- currentCaipNetworkId: this.currentCaipNetworkId
312
- };
313
-
314
- try {
315
- await this.getStorage().setItem(PHANTOM_CONNECTOR_STORAGE_KEY, connectorData);
316
- } catch (error) {
317
- // console.error('PhantomConnector: Failed to save session.', error);
318
- }
26
+ protected getDappKeypairStorageKey(): string {
27
+ return PHANTOM_DAPP_KEYPAIR_STORAGE_KEY;
319
28
  }
320
29
 
321
- // Clears only connector-specific data from storage
322
- private async clearSessionStorage(): Promise<void> {
323
- try {
324
- await this.getStorage().removeItem(PHANTOM_CONNECTOR_STORAGE_KEY);
325
- } catch (error) {
326
- // console.error('PhantomConnector: Failed to clear session from storage.', error);
327
- }
30
+ protected getEncryptionKeyFieldName(): string {
31
+ return 'phantom_encryption_public_key';
328
32
  }
329
33
  }
@@ -0,0 +1,33 @@
1
+ import { ConstantsUtil, type WalletInfo } from '@reown/appkit-common-react-native';
2
+ import { DeeplinkConnector } from './DeeplinkConnector';
3
+ import type { SolflareConnectorConfig } from '../types';
4
+
5
+ const SOLFLARE_BASE_URL = 'https://solflare.com/ul/v1';
6
+ const SOLFLARE_CONNECTOR_STORAGE_KEY = '@appkit/solflare-connector-data';
7
+ const SOLFLARE_DAPP_KEYPAIR_STORAGE_KEY = '@appkit/solflare-dapp-secret-key';
8
+
9
+ export class SolflareConnector extends DeeplinkConnector {
10
+ constructor(config?: SolflareConnectorConfig) {
11
+ super({ type: 'solflare', cluster: config?.cluster });
12
+ }
13
+
14
+ override getWalletInfo(): WalletInfo {
15
+ return ConstantsUtil.SOLFLARE_CUSTOM_WALLET;
16
+ }
17
+
18
+ protected getBaseUrl(): string {
19
+ return SOLFLARE_BASE_URL;
20
+ }
21
+
22
+ protected getStorageKey(): string {
23
+ return SOLFLARE_CONNECTOR_STORAGE_KEY;
24
+ }
25
+
26
+ protected getDappKeypairStorageKey(): string {
27
+ return SOLFLARE_DAPP_KEYPAIR_STORAGE_KEY;
28
+ }
29
+
30
+ protected getEncryptionKeyFieldName(): string {
31
+ return 'solflare_encryption_public_key';
32
+ }
33
+ }
package/src/index.ts CHANGED
@@ -2,7 +2,8 @@
2
2
  export { SolanaAdapter } from './adapter';
3
3
 
4
4
  // Types
5
- export type { PhantomConnectorConfig } from './types';
5
+ export type { PhantomConnectorConfig, SolflareConnectorConfig } from './types';
6
6
 
7
7
  // Connectors
8
8
  export { PhantomConnector } from './connectors/PhantomConnector';
9
+ export { SolflareConnector } from './connectors/SolflareConnector';