@reown/appkit-solana-react-native 2.0.0-alpha.0 → 2.0.0-alpha.2
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 +120 -2
- package/lib/commonjs/adapter.js.map +1 -1
- package/lib/commonjs/connectors/PhantomConnector.js +244 -0
- package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
- package/lib/commonjs/helpers.js +9 -1
- 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/package.json +1 -0
- 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/commonjs/utils/createSendTransaction.js +44 -0
- package/lib/commonjs/utils/createSendTransaction.js.map +1 -0
- package/lib/module/adapter.js +121 -2
- package/lib/module/adapter.js.map +1 -1
- package/lib/module/connectors/PhantomConnector.js +238 -0
- package/lib/module/connectors/PhantomConnector.js.map +1 -0
- package/lib/module/helpers.js +11 -1
- package/lib/module/helpers.js.map +1 -1
- package/lib/module/index.js +9 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/PhantomProvider.js +385 -0
- package/lib/module/providers/PhantomProvider.js.map +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/createSendTransaction.js +41 -0
- package/lib/module/utils/createSendTransaction.js.map +1 -0
- package/lib/typescript/adapter.d.ts +10 -0
- package/lib/typescript/adapter.d.ts.map +1 -1
- package/lib/typescript/connectors/PhantomConnector.d.ts +27 -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/lib/typescript/utils/createSendTransaction.d.ts +10 -0
- package/lib/typescript/utils/createSendTransaction.d.ts.map +1 -0
- package/package.json +13 -5
- package/src/adapter.ts +152 -6
- package/src/connectors/PhantomConnector.ts +329 -0
- package/src/helpers.ts +9 -7
- package/src/index.ts +8 -0
- package/src/providers/PhantomProvider.ts +532 -0
- package/src/types.ts +131 -0
- package/src/utils/createSendTransaction.ts +57 -0
- package/src/index.tsx +0 -2
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { CaipNetworkId, Namespaces, Storage, WalletInfo } from '@reown/appkit-common-react-native';
|
|
2
|
+
import type nacl from 'tweetnacl';
|
|
3
|
+
export interface TokenInfo {
|
|
4
|
+
address: string;
|
|
5
|
+
symbol: string;
|
|
6
|
+
name: string;
|
|
7
|
+
decimals: number;
|
|
8
|
+
logoURI?: string;
|
|
9
|
+
}
|
|
10
|
+
export type PhantomCluster = 'mainnet-beta' | 'testnet' | 'devnet';
|
|
11
|
+
export interface PhantomProviderConfig {
|
|
12
|
+
appScheme: string;
|
|
13
|
+
dappUrl: string;
|
|
14
|
+
storage: Storage;
|
|
15
|
+
dappEncryptionKeyPair: nacl.BoxKeyPair;
|
|
16
|
+
}
|
|
17
|
+
export type PhantomConnectResult = PhantomSession;
|
|
18
|
+
export interface PhantomSession {
|
|
19
|
+
sessionToken: string;
|
|
20
|
+
userPublicKey: string;
|
|
21
|
+
phantomEncryptionPublicKeyBs58: string;
|
|
22
|
+
cluster: PhantomCluster;
|
|
23
|
+
}
|
|
24
|
+
export interface SignTransactionRequestParams {
|
|
25
|
+
transaction: string;
|
|
26
|
+
}
|
|
27
|
+
export interface SignMessageRequestParams {
|
|
28
|
+
message: Uint8Array | string;
|
|
29
|
+
display?: 'utf8' | 'hex';
|
|
30
|
+
}
|
|
31
|
+
export interface SignAllTransactionsRequestParams {
|
|
32
|
+
transactions: string[];
|
|
33
|
+
}
|
|
34
|
+
export interface PhantomDeeplinkResponse {
|
|
35
|
+
phantom_encryption_public_key?: string;
|
|
36
|
+
nonce: string;
|
|
37
|
+
data: string;
|
|
38
|
+
}
|
|
39
|
+
export interface DecryptedConnectData {
|
|
40
|
+
public_key: string;
|
|
41
|
+
session: string;
|
|
42
|
+
}
|
|
43
|
+
export interface PhantomProviderConfig {
|
|
44
|
+
appScheme: string;
|
|
45
|
+
dappUrl: string;
|
|
46
|
+
storage: Storage;
|
|
47
|
+
dappEncryptionKeyPair: nacl.BoxKeyPair;
|
|
48
|
+
}
|
|
49
|
+
export interface PhantomSession {
|
|
50
|
+
sessionToken: string;
|
|
51
|
+
userPublicKey: string;
|
|
52
|
+
phantomEncryptionPublicKeyBs58: string;
|
|
53
|
+
cluster: PhantomCluster;
|
|
54
|
+
}
|
|
55
|
+
export type PhantomRpcMethod = 'connect' | 'disconnect' | 'signTransaction' | 'signAndSendTransaction' | 'signAllTransactions' | 'signMessage';
|
|
56
|
+
export interface PhantomSignTransactionParams {
|
|
57
|
+
dapp_encryption_public_key: string;
|
|
58
|
+
redirect_link: string;
|
|
59
|
+
payload: string;
|
|
60
|
+
nonce: string;
|
|
61
|
+
cluster?: PhantomCluster;
|
|
62
|
+
}
|
|
63
|
+
export interface PhantomSignAllTransactionsParams {
|
|
64
|
+
dapp_encryption_public_key: string;
|
|
65
|
+
redirect_link: string;
|
|
66
|
+
payload: string;
|
|
67
|
+
nonce: string;
|
|
68
|
+
cluster?: PhantomCluster;
|
|
69
|
+
}
|
|
70
|
+
export interface PhantomSignMessageParams {
|
|
71
|
+
dapp_encryption_public_key: string;
|
|
72
|
+
redirect_link: string;
|
|
73
|
+
payload: string;
|
|
74
|
+
nonce: string;
|
|
75
|
+
}
|
|
76
|
+
export interface PhantomConnectParams {
|
|
77
|
+
app_url: string;
|
|
78
|
+
dapp_encryption_public_key: string;
|
|
79
|
+
redirect_link: string;
|
|
80
|
+
cluster?: PhantomCluster;
|
|
81
|
+
}
|
|
82
|
+
export interface PhantomDisconnectParams {
|
|
83
|
+
dapp_encryption_public_key: string;
|
|
84
|
+
redirect_link: string;
|
|
85
|
+
payload: string;
|
|
86
|
+
nonce: string;
|
|
87
|
+
}
|
|
88
|
+
export interface PhantomConnectorConfig {
|
|
89
|
+
cluster?: PhantomCluster;
|
|
90
|
+
}
|
|
91
|
+
export interface PhantomConnectorSessionData {
|
|
92
|
+
namespaces: Namespaces;
|
|
93
|
+
wallet: WalletInfo;
|
|
94
|
+
currentCaipNetworkId: CaipNetworkId;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Connection, Transaction } from '@solana/web3.js';
|
|
2
|
+
type SendTransactionArgs = {
|
|
3
|
+
connection: Connection;
|
|
4
|
+
fromAddress: string;
|
|
5
|
+
toAddress: string;
|
|
6
|
+
value: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function createSendTransaction({ fromAddress, toAddress, value, connection }: SendTransactionArgs): Promise<Transaction>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=createSendTransaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createSendTransaction.d.ts","sourceRoot":"","sources":["../../../src/utils/createSendTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EAIf,WAAW,EACZ,MAAM,iBAAiB,CAAC;AAIzB,KAAK,mBAAmB,GAAG;IACzB,UAAU,EAAE,UAAU,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAWF,wBAAsB,qBAAqB,CAAC,EAC1C,WAAW,EACX,SAAS,EACT,KAAK,EACL,UAAU,EACX,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,CAAC,CAwB5C"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reown/appkit-solana-react-native",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
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",
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
"react-native"
|
|
28
29
|
],
|
|
29
30
|
"repository": "https://github.com/reown-com/appkit-react-native",
|
|
30
|
-
"author": "Reown
|
|
31
|
+
"author": "Reown (https://discord.gg/reown)",
|
|
31
32
|
"homepage": "https://reown.com/appkit",
|
|
32
33
|
"license": "Apache-2.0",
|
|
33
34
|
"bugs": {
|
|
@@ -38,7 +39,14 @@
|
|
|
38
39
|
"access": "public"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@reown/appkit-common-react-native": "2.0.0-alpha.
|
|
42
|
+
"@reown/appkit-common-react-native": "2.0.0-alpha.2",
|
|
43
|
+
"@solana/web3.js": "1.98.2",
|
|
44
|
+
"bs58": "6.0.0",
|
|
45
|
+
"tweetnacl": "1.0.3"
|
|
42
46
|
},
|
|
43
|
-
"
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@walletconnect/react-native-compat": ">=2.16.1",
|
|
49
|
+
"react": ">=18",
|
|
50
|
+
"react-native": ">=0.72"
|
|
51
|
+
}
|
|
44
52
|
}
|
package/src/adapter.ts
CHANGED
|
@@ -7,6 +7,17 @@ import {
|
|
|
7
7
|
type GetBalanceResponse
|
|
8
8
|
} from '@reown/appkit-common-react-native';
|
|
9
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
|
+
|
|
14
|
+
export interface SolanaTransactionData {
|
|
15
|
+
fromAddress: string;
|
|
16
|
+
toAddress: string;
|
|
17
|
+
amount: number;
|
|
18
|
+
network?: AppKitNetwork;
|
|
19
|
+
rpcUrl?: string;
|
|
20
|
+
}
|
|
10
21
|
|
|
11
22
|
export class SolanaAdapter extends SolanaBaseAdapter {
|
|
12
23
|
private static supportedNamespace: ChainNamespace = 'solana';
|
|
@@ -14,7 +25,8 @@ export class SolanaAdapter extends SolanaBaseAdapter {
|
|
|
14
25
|
constructor(configParams: { projectId: string }) {
|
|
15
26
|
super({
|
|
16
27
|
projectId: configParams.projectId,
|
|
17
|
-
supportedNamespace: SolanaAdapter.supportedNamespace
|
|
28
|
+
supportedNamespace: SolanaAdapter.supportedNamespace,
|
|
29
|
+
adapterType: 'solana'
|
|
18
30
|
});
|
|
19
31
|
}
|
|
20
32
|
|
|
@@ -56,11 +68,7 @@ export class SolanaAdapter extends SolanaBaseAdapter {
|
|
|
56
68
|
};
|
|
57
69
|
}
|
|
58
70
|
|
|
59
|
-
this.emit('balanceChanged', {
|
|
60
|
-
namespace: this.getSupportedNamespace(),
|
|
61
|
-
address: balanceAddress,
|
|
62
|
-
balance
|
|
63
|
-
});
|
|
71
|
+
this.emit('balanceChanged', { address: balanceAddress, balance });
|
|
64
72
|
|
|
65
73
|
return balance;
|
|
66
74
|
} catch (error) {
|
|
@@ -68,6 +76,144 @@ export class SolanaAdapter extends SolanaBaseAdapter {
|
|
|
68
76
|
}
|
|
69
77
|
}
|
|
70
78
|
|
|
79
|
+
async signTransaction<T extends Transaction | VersionedTransaction>(
|
|
80
|
+
transaction: T,
|
|
81
|
+
network?: AppKitNetwork
|
|
82
|
+
): Promise<T> {
|
|
83
|
+
if (!this.connector) {
|
|
84
|
+
throw new Error('SolanaAdapter:signTransaction - no active connector');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!network) {
|
|
88
|
+
throw new Error('SolanaAdapter:signTransaction - network is undefined');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const provider = this.connector.getProvider();
|
|
92
|
+
if (!provider) {
|
|
93
|
+
throw new Error('SolanaAdapter:signTransaction - provider is undefined');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
// Serialize transaction to base64 (following WalletConnect standard)
|
|
98
|
+
const serializedTransaction = Buffer.from(
|
|
99
|
+
new Uint8Array(transaction.serialize({ verifySignatures: false }))
|
|
100
|
+
).toString('base64');
|
|
101
|
+
|
|
102
|
+
const result = (await provider.request(
|
|
103
|
+
{
|
|
104
|
+
method: 'solana_signTransaction',
|
|
105
|
+
params: {
|
|
106
|
+
transaction: serializedTransaction,
|
|
107
|
+
pubkey: this.getAccounts()?.[0]?.split(':')[2] || ''
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
network.caipNetworkId
|
|
111
|
+
)) as { signature?: string; transaction?: string };
|
|
112
|
+
|
|
113
|
+
// Handle different response formats
|
|
114
|
+
if ('signature' in result && result.signature) {
|
|
115
|
+
// Old RPC response format - add signature to transaction
|
|
116
|
+
const decoded = base58.decode(result.signature);
|
|
117
|
+
if (transaction instanceof Transaction && transaction.feePayer) {
|
|
118
|
+
transaction.addSignature(
|
|
119
|
+
transaction.feePayer,
|
|
120
|
+
Buffer.from(decoded) as Buffer & Uint8Array
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return transaction;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if ('transaction' in result && result.transaction) {
|
|
128
|
+
// New response format - deserialize the signed transaction
|
|
129
|
+
const decodedTransaction = Buffer.from(result.transaction, 'base64');
|
|
130
|
+
|
|
131
|
+
if (transaction instanceof VersionedTransaction) {
|
|
132
|
+
return VersionedTransaction.deserialize(new Uint8Array(decodedTransaction)) as T;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return Transaction.from(decodedTransaction) as T;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
throw new Error('SolanaAdapter:signTransaction - invalid response format');
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (error instanceof Error) {
|
|
141
|
+
throw new Error(`SolanaAdapter:signTransaction - ${error.message}`);
|
|
142
|
+
}
|
|
143
|
+
throw new Error('SolanaAdapter:signTransaction - unknown error occurred');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async sendTransaction(data: SolanaTransactionData): Promise<string | null> {
|
|
148
|
+
const { fromAddress, toAddress, amount, network, rpcUrl } = data;
|
|
149
|
+
|
|
150
|
+
if (!this.connector) {
|
|
151
|
+
throw new Error('SolanaAdapter:sendTransaction - no active connector');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const provider = this.connector.getProvider();
|
|
155
|
+
if (!provider) {
|
|
156
|
+
throw new Error('SolanaAdapter:sendTransaction - provider is undefined');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!network) {
|
|
160
|
+
throw new Error('SolanaAdapter:sendTransaction - network is undefined');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!fromAddress) {
|
|
164
|
+
throw new Error('SolanaAdapter:sendTransaction - fromAddress is undefined');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!toAddress) {
|
|
168
|
+
throw new Error('SolanaAdapter:sendTransaction - toAddress is undefined');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!amount || amount <= 0) {
|
|
172
|
+
throw new Error('SolanaAdapter:sendTransaction - amount must be greater than 0');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
// Determine RPC URL
|
|
177
|
+
let connectionRpcUrl = rpcUrl;
|
|
178
|
+
if (!connectionRpcUrl && network) {
|
|
179
|
+
connectionRpcUrl = network.rpcUrls?.default?.http?.[0];
|
|
180
|
+
}
|
|
181
|
+
if (!connectionRpcUrl) {
|
|
182
|
+
throw new Error('SolanaAdapter:sendTransaction - no RPC URL available');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Create connection
|
|
186
|
+
const connection = new Connection(connectionRpcUrl, 'confirmed');
|
|
187
|
+
|
|
188
|
+
const transaction = await createSendTransaction({
|
|
189
|
+
connection,
|
|
190
|
+
fromAddress,
|
|
191
|
+
toAddress,
|
|
192
|
+
value: amount
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Sign the transaction
|
|
196
|
+
const signedTransaction = await this.signTransaction(transaction, network);
|
|
197
|
+
|
|
198
|
+
// Send the signed transaction
|
|
199
|
+
const signature = await connection.sendRawTransaction(signedTransaction.serialize(), {
|
|
200
|
+
skipPreflight: false,
|
|
201
|
+
preflightCommitment: 'confirmed'
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
if (!signature) {
|
|
205
|
+
throw new Error('SolanaAdapter:sendTransaction - no signature returned');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return signature;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (error instanceof Error) {
|
|
211
|
+
throw new Error(`SolanaAdapter:sendTransaction - ${error.message}`);
|
|
212
|
+
}
|
|
213
|
+
throw new Error('SolanaAdapter:sendTransaction - unknown error occurred');
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
71
217
|
async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
72
218
|
if (!this.connector) throw new Error('No active connector');
|
|
73
219
|
|
|
@@ -0,0 +1,329 @@
|
|
|
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
|
+
};
|
|
34
|
+
|
|
35
|
+
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';
|
|
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
|
+
}
|
|
172
|
+
|
|
173
|
+
return this.provider as PhantomProvider;
|
|
174
|
+
}
|
|
175
|
+
|
|
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;
|
|
190
|
+
}
|
|
191
|
+
|
|
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
|
+
}
|
|
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
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
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
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|