@reown/appkit-solana-react-native 2.0.0-alpha.3 → 2.0.0-alpha.5

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 (47) hide show
  1. package/lib/commonjs/adapter.js +3 -3
  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 -230
  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/{PhantomProvider.js → DeeplinkProvider.js} +155 -154
  12. package/lib/commonjs/providers/DeeplinkProvider.js.map +1 -0
  13. package/lib/module/adapter.js +3 -3
  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 -230
  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/{PhantomProvider.js → DeeplinkProvider.js} +153 -152
  24. package/lib/module/providers/DeeplinkProvider.js.map +1 -0
  25. package/lib/typescript/connectors/DeeplinkConnector.d.ts +30 -0
  26. package/lib/typescript/connectors/DeeplinkConnector.d.ts.map +1 -0
  27. package/lib/typescript/connectors/PhantomConnector.d.ts +8 -23
  28. package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -1
  29. package/lib/typescript/connectors/SolflareConnector.d.ts +12 -0
  30. package/lib/typescript/connectors/SolflareConnector.d.ts.map +1 -0
  31. package/lib/typescript/index.d.ts +2 -1
  32. package/lib/typescript/index.d.ts.map +1 -1
  33. package/lib/typescript/providers/{PhantomProvider.d.ts → DeeplinkProvider.d.ts} +16 -8
  34. package/lib/typescript/providers/DeeplinkProvider.d.ts.map +1 -0
  35. package/lib/typescript/types.d.ts +27 -32
  36. package/lib/typescript/types.d.ts.map +1 -1
  37. package/package.json +5 -4
  38. package/src/adapter.ts +3 -3
  39. package/src/connectors/DeeplinkConnector.ts +353 -0
  40. package/src/connectors/PhantomConnector.ts +17 -313
  41. package/src/connectors/SolflareConnector.ts +33 -0
  42. package/src/index.ts +2 -1
  43. package/src/providers/{PhantomProvider.ts → DeeplinkProvider.ts} +256 -233
  44. package/src/types.ts +29 -37
  45. package/lib/commonjs/providers/PhantomProvider.js.map +0 -1
  46. package/lib/module/providers/PhantomProvider.js.map +0 -1
  47. package/lib/typescript/providers/PhantomProvider.d.ts.map +0 -1
@@ -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: Record<PhantomCluster, CaipNetworkId> = {
30
- 'mainnet-beta': solana.caipNetworkId,
31
- 'testnet': solanaTestnet.caipNetworkId,
32
- 'devnet': solanaDevnet.caipNetworkId
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
- private currentCaipNetworkId: CaipNetworkId | null = null;
41
- private dappEncryptionKeyPair?: nacl.BoxKeyPair;
42
-
43
- private static readonly SUPPORTED_NAMESPACE: ChainNamespace = 'solana';
7
+ const PHANTOM_DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
44
8
 
9
+ export class PhantomConnector extends DeeplinkConnector {
45
10
  constructor(config?: PhantomConnectorConfig) {
46
- super({ type: 'phantom' });
47
- this.config = config ?? { cluster: 'mainnet-beta' };
11
+ super({ type: 'phantom', cluster: config?.cluster });
48
12
  }
49
13
 
50
- override async init(ops: ConnectorInitOptions) {
51
- super.init(ops);
52
- this.storage = ops.storage;
53
- await this.initializeKeyPair();
54
-
55
- const appScheme = ops.metadata.redirect?.universal ?? ops.metadata.redirect?.native;
56
- if (!appScheme) {
57
- throw new Error(
58
- 'Phantom Connector: No redirect link found in metadata. Please add redirect.universal or redirect.native to the metadata.'
59
- );
60
- }
61
-
62
- const providerConfig: PhantomProviderConfig = {
63
- appScheme,
64
- dappUrl: ops.metadata.url,
65
- storage: ops.storage,
66
- dappEncryptionKeyPair: this.dappEncryptionKeyPair!
67
- };
68
-
69
- this.provider = new PhantomProvider(providerConfig);
70
- await this.restoreSession();
14
+ override getWalletInfo(): WalletInfo {
15
+ return ConstantsUtil.PHANTOM_CUSTOM_WALLET;
71
16
  }
72
17
 
73
- private async initializeKeyPair(): Promise<void> {
74
- try {
75
- const secretKeyB58 = await this.getStorage().getItem(DAPP_KEYPAIR_STORAGE_KEY);
76
- if (secretKeyB58) {
77
- const secretKey = bs58.decode(secretKeyB58);
78
- this.dappEncryptionKeyPair = nacl.box.keyPair.fromSecretKey(secretKey);
79
- } else {
80
- const newKeyPair = nacl.box.keyPair();
81
- this.dappEncryptionKeyPair = newKeyPair;
82
- await this.getStorage().setItem(
83
- DAPP_KEYPAIR_STORAGE_KEY,
84
- bs58.encode(newKeyPair.secretKey)
85
- );
86
- }
87
- } catch (error) {
88
- // disconnect and clear session
89
- await this.disconnect();
90
- throw error;
91
- }
18
+ protected getBaseUrl(): string {
19
+ return PHANTOM_BASE_URL;
92
20
  }
93
21
 
94
- override async connect(opts?: ConnectOptions): Promise<Namespaces | undefined> {
95
- if (this.isConnected()) {
96
- return this.namespaces;
97
- }
98
-
99
- const defaultChain: CaipNetworkId | undefined =
100
- opts?.defaultChain?.split(':')?.[0] === 'solana'
101
- ? opts?.defaultChain
102
- : opts?.namespaces?.['solana']?.chains?.[0];
103
-
104
- const requestedCluster =
105
- this.config.cluster ??
106
- (Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID).find(
107
- key => SOLANA_CLUSTER_TO_CHAIN_ID[key as PhantomCluster] === defaultChain
108
- ) as PhantomCluster | undefined);
109
-
110
- try {
111
- const connectResult = await this.getProvider().connect({ cluster: requestedCluster });
112
-
113
- const solanaChainId = SOLANA_CLUSTER_TO_CHAIN_ID[connectResult.cluster];
114
- if (!solanaChainId) {
115
- throw new Error(
116
- `Phantom Connect: Internal - Unknown cluster mapping for ${connectResult.cluster}`
117
- );
118
- }
119
- this.currentCaipNetworkId = solanaChainId;
120
-
121
- this.wallet = {
122
- name: ConstantsUtil.PHANTOM_CUSTOM_WALLET.name
123
- };
124
-
125
- const userPublicKey = this.getProvider().getUserPublicKey();
126
- if (!userPublicKey) {
127
- throw new Error('Phantom Connect: Provider failed to return a user public key.');
128
- }
129
-
130
- const caipAddress = `${this.currentCaipNetworkId}:${userPublicKey}` as CaipAddress;
131
- this.namespaces = {
132
- [PhantomConnector.SUPPORTED_NAMESPACE]: {
133
- accounts: [caipAddress],
134
- methods: Object.values(SOLANA_SIGNING_METHODS),
135
- events: [],
136
- chains: [this.currentCaipNetworkId]
137
- }
138
- };
139
-
140
- await this.saveSession(); // Save connector-specific session on successful connect
141
-
142
- return this.namespaces;
143
- } catch (error: any) {
144
- this.clearSession();
145
- throw error;
146
- }
147
- }
148
-
149
- override async disconnect(): Promise<void> {
150
- try {
151
- if (this.isConnected()) {
152
- await super.disconnect();
153
- }
154
- } catch (error: any) {
155
- // console.warn(`PhantomConnector: Error during provider disconnect: ${error.message}. Proceeding with local clear.`);
156
- }
157
-
158
- // Cleanup provider resources
159
- if (this.provider) {
160
- (this.provider as PhantomProvider).destroy();
161
- }
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(): PhantomProvider {
174
- if (!this.provider) {
175
- throw new Error('Phantom Connector: Provider not initialized. Call init() first.');
176
- }
177
-
178
- return this.provider as PhantomProvider;
179
- }
180
-
181
- private getStorage(): Storage {
182
- if (!this.storage) {
183
- throw new Error('Phantom Connector: Storage not initialized. Call init() first.');
184
- }
185
-
186
- return this.storage;
187
- }
188
-
189
- override getNamespaces(): Namespaces {
190
- if (!this.namespaces) {
191
- throw new Error('Phantom Connector: Not connected. Call connect() first.');
192
- }
193
-
194
- return this.namespaces;
22
+ protected getStorageKey(): string {
23
+ return PHANTOM_CONNECTOR_STORAGE_KEY;
195
24
  }
196
25
 
197
- override getChainId(namespace: ChainNamespace): CaipNetworkId | undefined {
198
- if (namespace === PhantomConnector.SUPPORTED_NAMESPACE) {
199
- return this.currentCaipNetworkId ?? undefined;
200
- }
201
-
202
- return undefined;
203
- }
204
-
205
- override getProperties(): ConnectionProperties | undefined {
206
- return this.properties;
207
- }
208
-
209
- override getWalletInfo(): WalletInfo | undefined {
210
- if (!this.isConnected()) {
211
- return undefined;
212
- }
213
-
214
- return this.wallet;
215
- }
216
-
217
- isConnected(): boolean {
218
- // Rely solely on the provider as the source of truth for connection status.
219
- return this.getProvider().isConnected() && !!this.getProvider().getUserPublicKey();
220
- }
221
-
222
- override async switchNetwork(network: AppKitNetwork): Promise<void> {
223
- const targetClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID).find(
224
- key => SOLANA_CLUSTER_TO_CHAIN_ID[key as PhantomCluster] === network.caipNetworkId
225
- ) as PhantomCluster | undefined;
226
-
227
- if (!targetClusterName) {
228
- throw new Error(`Cannot switch to unsupported network ID: ${network.id}`);
229
- }
230
-
231
- const currentClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID).find(
232
- key => SOLANA_CLUSTER_TO_CHAIN_ID[key as PhantomCluster] === 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_CLUSTER_TO_CHAIN_ID[targetClusterName]
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
- }
261
- }
262
-
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
- }
300
- }
301
-
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';