@reown/appkit-solana-react-native 0.0.0-chore-bump-builder-20250728194329
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 +211 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/PhantomConnector.js +244 -0
- package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
- package/lib/commonjs/helpers.js +102 -0
- package/lib/commonjs/helpers.js.map +1 -0
- package/lib/commonjs/index.js +20 -0
- package/lib/commonjs/index.js.map +1 -0
- 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 +205 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/PhantomConnector.js +238 -0
- package/lib/module/connectors/PhantomConnector.js.map +1 -0
- package/lib/module/helpers.js +95 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +10 -0
- package/lib/module/index.js.map +1 -0
- 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 +23 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- 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 +31 -0
- package/lib/typescript/helpers.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +4 -0
- package/lib/typescript/index.d.ts.map +1 -0
- 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 +52 -0
- package/readme.md +9 -0
- package/src/adapter.ts +248 -0
- package/src/connectors/PhantomConnector.ts +329 -0
- package/src/helpers.ts +102 -0
- 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
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { SolanaBaseAdapter } from '@reown/appkit-common-react-native';
|
|
4
|
+
import { getSolanaNativeBalance, getSolanaTokenBalance } from './helpers';
|
|
5
|
+
import { Connection, Transaction, VersionedTransaction } from '@solana/web3.js';
|
|
6
|
+
import base58 from 'bs58';
|
|
7
|
+
import { createSendTransaction } from './utils/createSendTransaction';
|
|
8
|
+
export class SolanaAdapter extends SolanaBaseAdapter {
|
|
9
|
+
static supportedNamespace = 'solana';
|
|
10
|
+
constructor(configParams) {
|
|
11
|
+
super({
|
|
12
|
+
projectId: configParams.projectId,
|
|
13
|
+
supportedNamespace: SolanaAdapter.supportedNamespace,
|
|
14
|
+
adapterType: 'solana'
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
async getBalance(params) {
|
|
18
|
+
const {
|
|
19
|
+
network,
|
|
20
|
+
address,
|
|
21
|
+
tokens
|
|
22
|
+
} = params;
|
|
23
|
+
if (!this.connector) throw new Error('No active connector');
|
|
24
|
+
if (!network) throw new Error('No network provided');
|
|
25
|
+
const balanceAddress = address || this.getAccounts()?.find(account => account.includes(network.id.toString()));
|
|
26
|
+
if (!balanceAddress) {
|
|
27
|
+
return {
|
|
28
|
+
amount: '0.00',
|
|
29
|
+
symbol: 'SOL'
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const rpcUrl = network.rpcUrls?.default?.http?.[0];
|
|
34
|
+
if (!rpcUrl) throw new Error('No RPC URL available');
|
|
35
|
+
const base58Address = balanceAddress.split(':')[2];
|
|
36
|
+
if (!base58Address) throw new Error('Invalid balance address');
|
|
37
|
+
const token = network?.caipNetworkId && tokens?.[network.caipNetworkId]?.address;
|
|
38
|
+
let balance;
|
|
39
|
+
if (token) {
|
|
40
|
+
const {
|
|
41
|
+
amount,
|
|
42
|
+
symbol
|
|
43
|
+
} = await getSolanaTokenBalance(rpcUrl, base58Address, token);
|
|
44
|
+
balance = {
|
|
45
|
+
amount,
|
|
46
|
+
symbol
|
|
47
|
+
};
|
|
48
|
+
} else {
|
|
49
|
+
const amount = await getSolanaNativeBalance(rpcUrl, base58Address);
|
|
50
|
+
balance = {
|
|
51
|
+
amount: amount.toString(),
|
|
52
|
+
symbol: 'SOL'
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
this.emit('balanceChanged', {
|
|
56
|
+
address: balanceAddress,
|
|
57
|
+
balance
|
|
58
|
+
});
|
|
59
|
+
return balance;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return {
|
|
62
|
+
amount: '0.00',
|
|
63
|
+
symbol: 'SOL'
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async signTransaction(transaction, network) {
|
|
68
|
+
if (!this.connector) {
|
|
69
|
+
throw new Error('SolanaAdapter:signTransaction - no active connector');
|
|
70
|
+
}
|
|
71
|
+
if (!network) {
|
|
72
|
+
throw new Error('SolanaAdapter:signTransaction - network is undefined');
|
|
73
|
+
}
|
|
74
|
+
const provider = this.connector.getProvider();
|
|
75
|
+
if (!provider) {
|
|
76
|
+
throw new Error('SolanaAdapter:signTransaction - provider is undefined');
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
// Serialize transaction to base64 (following WalletConnect standard)
|
|
80
|
+
const serializedTransaction = Buffer.from(new Uint8Array(transaction.serialize({
|
|
81
|
+
verifySignatures: false
|
|
82
|
+
}))).toString('base64');
|
|
83
|
+
const result = await provider.request({
|
|
84
|
+
method: 'solana_signTransaction',
|
|
85
|
+
params: {
|
|
86
|
+
transaction: serializedTransaction,
|
|
87
|
+
pubkey: this.getAccounts()?.[0]?.split(':')[2] || ''
|
|
88
|
+
}
|
|
89
|
+
}, network.caipNetworkId);
|
|
90
|
+
|
|
91
|
+
// Handle different response formats
|
|
92
|
+
if ('signature' in result && result.signature) {
|
|
93
|
+
// Old RPC response format - add signature to transaction
|
|
94
|
+
const decoded = base58.decode(result.signature);
|
|
95
|
+
if (transaction instanceof Transaction && transaction.feePayer) {
|
|
96
|
+
transaction.addSignature(transaction.feePayer, Buffer.from(decoded));
|
|
97
|
+
}
|
|
98
|
+
return transaction;
|
|
99
|
+
}
|
|
100
|
+
if ('transaction' in result && result.transaction) {
|
|
101
|
+
// New response format - deserialize the signed transaction
|
|
102
|
+
const decodedTransaction = Buffer.from(result.transaction, 'base64');
|
|
103
|
+
if (transaction instanceof VersionedTransaction) {
|
|
104
|
+
return VersionedTransaction.deserialize(new Uint8Array(decodedTransaction));
|
|
105
|
+
}
|
|
106
|
+
return Transaction.from(decodedTransaction);
|
|
107
|
+
}
|
|
108
|
+
throw new Error('SolanaAdapter:signTransaction - invalid response format');
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (error instanceof Error) {
|
|
111
|
+
throw new Error(`SolanaAdapter:signTransaction - ${error.message}`);
|
|
112
|
+
}
|
|
113
|
+
throw new Error('SolanaAdapter:signTransaction - unknown error occurred');
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async sendTransaction(data) {
|
|
117
|
+
const {
|
|
118
|
+
fromAddress,
|
|
119
|
+
toAddress,
|
|
120
|
+
amount,
|
|
121
|
+
network,
|
|
122
|
+
rpcUrl
|
|
123
|
+
} = data;
|
|
124
|
+
if (!this.connector) {
|
|
125
|
+
throw new Error('SolanaAdapter:sendTransaction - no active connector');
|
|
126
|
+
}
|
|
127
|
+
const provider = this.connector.getProvider();
|
|
128
|
+
if (!provider) {
|
|
129
|
+
throw new Error('SolanaAdapter:sendTransaction - provider is undefined');
|
|
130
|
+
}
|
|
131
|
+
if (!network) {
|
|
132
|
+
throw new Error('SolanaAdapter:sendTransaction - network is undefined');
|
|
133
|
+
}
|
|
134
|
+
if (!fromAddress) {
|
|
135
|
+
throw new Error('SolanaAdapter:sendTransaction - fromAddress is undefined');
|
|
136
|
+
}
|
|
137
|
+
if (!toAddress) {
|
|
138
|
+
throw new Error('SolanaAdapter:sendTransaction - toAddress is undefined');
|
|
139
|
+
}
|
|
140
|
+
if (!amount || amount <= 0) {
|
|
141
|
+
throw new Error('SolanaAdapter:sendTransaction - amount must be greater than 0');
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
// Determine RPC URL
|
|
145
|
+
let connectionRpcUrl = rpcUrl;
|
|
146
|
+
if (!connectionRpcUrl && network) {
|
|
147
|
+
connectionRpcUrl = network.rpcUrls?.default?.http?.[0];
|
|
148
|
+
}
|
|
149
|
+
if (!connectionRpcUrl) {
|
|
150
|
+
throw new Error('SolanaAdapter:sendTransaction - no RPC URL available');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Create connection
|
|
154
|
+
const connection = new Connection(connectionRpcUrl, 'confirmed');
|
|
155
|
+
const transaction = await createSendTransaction({
|
|
156
|
+
connection,
|
|
157
|
+
fromAddress,
|
|
158
|
+
toAddress,
|
|
159
|
+
value: amount
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Sign the transaction
|
|
163
|
+
const signedTransaction = await this.signTransaction(transaction, network);
|
|
164
|
+
|
|
165
|
+
// Send the signed transaction
|
|
166
|
+
const signature = await connection.sendRawTransaction(signedTransaction.serialize(), {
|
|
167
|
+
skipPreflight: false,
|
|
168
|
+
preflightCommitment: 'confirmed'
|
|
169
|
+
});
|
|
170
|
+
if (!signature) {
|
|
171
|
+
throw new Error('SolanaAdapter:sendTransaction - no signature returned');
|
|
172
|
+
}
|
|
173
|
+
return signature;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (error instanceof Error) {
|
|
176
|
+
throw new Error(`SolanaAdapter:sendTransaction - ${error.message}`);
|
|
177
|
+
}
|
|
178
|
+
throw new Error('SolanaAdapter:sendTransaction - unknown error occurred');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async switchNetwork(network) {
|
|
182
|
+
if (!this.connector) throw new Error('No active connector');
|
|
183
|
+
const provider = this.connector.getProvider();
|
|
184
|
+
if (!provider) throw new Error('No active provider');
|
|
185
|
+
try {
|
|
186
|
+
await this.connector.switchNetwork(network);
|
|
187
|
+
return;
|
|
188
|
+
} catch (switchError) {
|
|
189
|
+
throw switchError;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
getAccounts() {
|
|
193
|
+
if (!this.connector) throw new Error('No active connector');
|
|
194
|
+
const namespaces = this.connector.getNamespaces();
|
|
195
|
+
return namespaces[this.getSupportedNamespace()]?.accounts;
|
|
196
|
+
}
|
|
197
|
+
disconnect() {
|
|
198
|
+
if (!this.connector) throw new Error('No active connector');
|
|
199
|
+
return this.connector.disconnect();
|
|
200
|
+
}
|
|
201
|
+
getSupportedNamespace() {
|
|
202
|
+
return SolanaAdapter.supportedNamespace;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["SolanaBaseAdapter","getSolanaNativeBalance","getSolanaTokenBalance","Connection","Transaction","VersionedTransaction","base58","createSendTransaction","SolanaAdapter","supportedNamespace","constructor","configParams","projectId","adapterType","getBalance","params","network","address","tokens","connector","Error","balanceAddress","getAccounts","find","account","includes","id","toString","amount","symbol","rpcUrl","rpcUrls","default","http","base58Address","split","token","caipNetworkId","balance","emit","error","signTransaction","transaction","provider","getProvider","serializedTransaction","Buffer","from","Uint8Array","serialize","verifySignatures","result","request","method","pubkey","signature","decoded","decode","feePayer","addSignature","decodedTransaction","deserialize","message","sendTransaction","data","fromAddress","toAddress","connectionRpcUrl","connection","value","signedTransaction","sendRawTransaction","skipPreflight","preflightCommitment","switchNetwork","switchError","namespaces","getNamespaces","getSupportedNamespace","accounts","disconnect"],"sourceRoot":"../../src","sources":["adapter.ts"],"mappings":";;AAAA,SACEA,iBAAiB,QAMZ,mCAAmC;AAC1C,SAASC,sBAAsB,EAAEC,qBAAqB,QAAQ,WAAW;AACzE,SAASC,UAAU,EAAEC,WAAW,EAAEC,oBAAoB,QAAQ,iBAAiB;AAC/E,OAAOC,MAAM,MAAM,MAAM;AACzB,SAASC,qBAAqB,QAAQ,+BAA+B;AAUrE,OAAO,MAAMC,aAAa,SAASR,iBAAiB,CAAC;EACnD,OAAeS,kBAAkB,GAAmB,QAAQ;EAE5DC,WAAWA,CAACC,YAAmC,EAAE;IAC/C,KAAK,CAAC;MACJC,SAAS,EAAED,YAAY,CAACC,SAAS;MACjCH,kBAAkB,EAAED,aAAa,CAACC,kBAAkB;MACpDI,WAAW,EAAE;IACf,CAAC,CAAC;EACJ;EAEA,MAAMC,UAAUA,CAACC,MAAwB,EAA+B;IACtE,MAAM;MAAEC,OAAO;MAAEC,OAAO;MAAEC;IAAO,CAAC,GAAGH,MAAM;IAE3C,IAAI,CAAC,IAAI,CAACI,SAAS,EAAE,MAAM,IAAIC,KAAK,CAAC,qBAAqB,CAAC;IAC3D,IAAI,CAACJ,OAAO,EAAE,MAAM,IAAII,KAAK,CAAC,qBAAqB,CAAC;IAEpD,MAAMC,cAAc,GAClBJ,OAAO,IAAI,IAAI,CAACK,WAAW,CAAC,CAAC,EAAEC,IAAI,CAACC,OAAO,IAAIA,OAAO,CAACC,QAAQ,CAACT,OAAO,CAACU,EAAE,CAACC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEzF,IAAI,CAACN,cAAc,EAAE;MACnB,OAAO;QAAEO,MAAM,EAAE,MAAM;QAAEC,MA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AC;IAE3D,MAAMuB,QAAQ,GAAG,IAAI,CAACxB,SAAS,CAACyB,WAAW,CAAC,CAAC;IAC7C,IAAI,CAACD,QAAQ,EAAE,MAAM,IAAIvB,KAAK,CAAC,oBAAoB,CAAC;IAEpD,IAAI;MACF,MAAM,IAAI,CAACD,SAAS,CAACuD,aAAa,CAAC1D,OAAO,CAAC;MAE3C;IACF,CAAC,CAAC,OAAO2D,WAAgB,EAAE;MACzB,MAAMA,WAAW;IACnB;EACF;EAEArD,WAAWA,CAAA,EAA8B;IACvC,IAAI,CAAC,IAAI,CAACH,SAAS,EAAE,MAAM,IAAIC,KAAK,CAAC,qBAAqB,CAAC;IAC3D,MAAMwD,UAAU,GAAG,IAAI,CAACzD,SAAS,CAAC0D,aAAa,CAAC,CAAC;IAEjD,OAAOD,UAAU,CAAC,IAAI,CAACE,qBAAqB,CAAC,CAAC,CAAC,EAAEC,QAAQ;EAC3D;EAEAC,UAAUA,CAAA,EAAkB;IAC1B,IAAI,CAAC,IAAI,CAAC7D,SAAS,EAAE,MAAM,IAAIC,KAAK,CAAC,qBAAqB,CAAC;IAE3D,OAAO,IAAI,CAACD,SAAS,CAAC6D,UAAU,CAAC,CAAC;EACpC;EAEAF,qBAAqBA,CAAA,EAAmB;IACtC,OAAOtE,aAAa,CAACC,kBAAkB;EACzC;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { WalletConnector, solana, solanaDevnet, solanaTestnet, ConstantsUtil } from '@reown/appkit-common-react-native';
|
|
4
|
+
import nacl from 'tweetnacl';
|
|
5
|
+
import bs58 from 'bs58';
|
|
6
|
+
import { PhantomProvider, SOLANA_SIGNING_METHODS } from '../providers/PhantomProvider';
|
|
7
|
+
const SOLANA_CLUSTER_TO_CHAIN_ID_PART = {
|
|
8
|
+
'mainnet-beta': solana.id,
|
|
9
|
+
'testnet': solanaTestnet.id,
|
|
10
|
+
'devnet': solanaDevnet.id
|
|
11
|
+
};
|
|
12
|
+
const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
|
|
13
|
+
const DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
|
|
14
|
+
export class PhantomConnector extends WalletConnector {
|
|
15
|
+
currentCaipNetworkId = null;
|
|
16
|
+
static SUPPORTED_NAMESPACE = 'solana';
|
|
17
|
+
constructor(config) {
|
|
18
|
+
super({
|
|
19
|
+
type: 'phantom'
|
|
20
|
+
});
|
|
21
|
+
this.config = config ?? {
|
|
22
|
+
cluster: 'mainnet-beta'
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
async init(ops) {
|
|
26
|
+
super.init(ops);
|
|
27
|
+
this.storage = ops.storage;
|
|
28
|
+
await this.initializeKeyPair();
|
|
29
|
+
const appScheme = ops.metadata.redirect?.universal ?? ops.metadata.redirect?.native;
|
|
30
|
+
if (!appScheme) {
|
|
31
|
+
throw new Error('Phantom Connector: No redirect link found in metadata. Please add redirect.universal or redirect.native to the metadata.');
|
|
32
|
+
}
|
|
33
|
+
const providerConfig = {
|
|
34
|
+
appScheme,
|
|
35
|
+
dappUrl: ops.metadata.url,
|
|
36
|
+
storage: ops.storage,
|
|
37
|
+
dappEncryptionKeyPair: this.dappEncryptionKeyPair
|
|
38
|
+
};
|
|
39
|
+
this.provider = new PhantomProvider(providerConfig);
|
|
40
|
+
await this.restoreSession();
|
|
41
|
+
}
|
|
42
|
+
async initializeKeyPair() {
|
|
43
|
+
try {
|
|
44
|
+
const secretKeyB58 = await this.getStorage().getItem(DAPP_KEYPAIR_STORAGE_KEY);
|
|
45
|
+
if (secretKeyB58) {
|
|
46
|
+
const secretKey = bs58.decode(secretKeyB58);
|
|
47
|
+
this.dappEncryptionKeyPair = nacl.box.keyPair.fromSecretKey(secretKey);
|
|
48
|
+
} else {
|
|
49
|
+
const newKeyPair = nacl.box.keyPair();
|
|
50
|
+
this.dappEncryptionKeyPair = newKeyPair;
|
|
51
|
+
await this.getStorage().setItem(DAPP_KEYPAIR_STORAGE_KEY, bs58.encode(newKeyPair.secretKey));
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
// disconnect and clear session
|
|
55
|
+
await this.disconnect();
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async connect(opts) {
|
|
60
|
+
if (this.isConnected()) {
|
|
61
|
+
return this.namespaces;
|
|
62
|
+
}
|
|
63
|
+
const defaultChain = opts?.defaultChain?.split(':')?.[0] === 'solana' ? opts?.defaultChain?.split(':')[1] : opts?.namespaces?.['solana']?.chains?.[0]?.split(':')[1];
|
|
64
|
+
const requestedCluster = this.config.cluster ?? Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(key => SOLANA_CLUSTER_TO_CHAIN_ID_PART[key] === defaultChain);
|
|
65
|
+
try {
|
|
66
|
+
const connectResult = await this.getProvider().connect({
|
|
67
|
+
cluster: requestedCluster
|
|
68
|
+
});
|
|
69
|
+
const solanaChainIdPart = SOLANA_CLUSTER_TO_CHAIN_ID_PART[connectResult.cluster];
|
|
70
|
+
if (!solanaChainIdPart) {
|
|
71
|
+
throw new Error(`Phantom Connect: Internal - Unknown cluster mapping for ${connectResult.cluster}`);
|
|
72
|
+
}
|
|
73
|
+
this.currentCaipNetworkId = `solana:${solanaChainIdPart}`;
|
|
74
|
+
this.wallet = ConstantsUtil.PHANTOM_CUSTOM_WALLET;
|
|
75
|
+
const userPublicKey = this.getProvider().getUserPublicKey();
|
|
76
|
+
if (!userPublicKey) {
|
|
77
|
+
throw new Error('Phantom Connect: Provider failed to return a user public key.');
|
|
78
|
+
}
|
|
79
|
+
const caipAddress = `${this.currentCaipNetworkId}:${userPublicKey}`;
|
|
80
|
+
this.namespaces = {
|
|
81
|
+
[PhantomConnector.SUPPORTED_NAMESPACE]: {
|
|
82
|
+
accounts: [caipAddress],
|
|
83
|
+
methods: Object.values(SOLANA_SIGNING_METHODS),
|
|
84
|
+
events: [],
|
|
85
|
+
chains: [this.currentCaipNetworkId]
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
await this.saveSession(); // Save connector-specific session on successful connect
|
|
89
|
+
|
|
90
|
+
return this.namespaces;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
this.clearSession();
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async disconnect() {
|
|
97
|
+
try {
|
|
98
|
+
if (this.isConnected()) {
|
|
99
|
+
await super.disconnect();
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
// console.warn(`PhantomConnector: Error during provider disconnect: ${error.message}. Proceeding with local clear.`);
|
|
103
|
+
}
|
|
104
|
+
await this.clearSession();
|
|
105
|
+
}
|
|
106
|
+
async clearSession() {
|
|
107
|
+
this.namespaces = undefined;
|
|
108
|
+
this.wallet = undefined;
|
|
109
|
+
this.currentCaipNetworkId = null;
|
|
110
|
+
await this.clearSessionStorage();
|
|
111
|
+
}
|
|
112
|
+
getProvider() {
|
|
113
|
+
if (!this.provider) {
|
|
114
|
+
throw new Error('Phantom Connector: Provider not initialized. Call init() first.');
|
|
115
|
+
}
|
|
116
|
+
return this.provider;
|
|
117
|
+
}
|
|
118
|
+
getStorage() {
|
|
119
|
+
if (!this.storage) {
|
|
120
|
+
throw new Error('Phantom Connector: Storage not initialized. Call init() first.');
|
|
121
|
+
}
|
|
122
|
+
return this.storage;
|
|
123
|
+
}
|
|
124
|
+
getNamespaces() {
|
|
125
|
+
if (!this.namespaces) {
|
|
126
|
+
throw new Error('Phantom Connector: Not connected. Call connect() first.');
|
|
127
|
+
}
|
|
128
|
+
return this.namespaces;
|
|
129
|
+
}
|
|
130
|
+
getChainId(namespace) {
|
|
131
|
+
if (namespace === PhantomConnector.SUPPORTED_NAMESPACE) {
|
|
132
|
+
return this.currentCaipNetworkId ?? undefined;
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
getProperties() {
|
|
137
|
+
return this.properties;
|
|
138
|
+
}
|
|
139
|
+
getWalletInfo() {
|
|
140
|
+
if (!this.isConnected()) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
return this.wallet;
|
|
144
|
+
}
|
|
145
|
+
isConnected() {
|
|
146
|
+
// Rely solely on the provider as the source of truth for connection status.
|
|
147
|
+
return this.getProvider().isConnected() && !!this.getProvider().getUserPublicKey();
|
|
148
|
+
}
|
|
149
|
+
async switchNetwork(network) {
|
|
150
|
+
const targetClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(key => SOLANA_CLUSTER_TO_CHAIN_ID_PART[key] === network.id);
|
|
151
|
+
if (!targetClusterName) {
|
|
152
|
+
throw new Error(`Cannot switch to unsupported network ID: ${network.id}`);
|
|
153
|
+
}
|
|
154
|
+
const currentClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(key => `solana:${SOLANA_CLUSTER_TO_CHAIN_ID_PART[key]}` === this.currentCaipNetworkId);
|
|
155
|
+
if (targetClusterName === currentClusterName && this.isConnected()) {
|
|
156
|
+
return Promise.resolve();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Phantom doesn't provide a way to switch network, so we need to disconnect and reconnect.
|
|
160
|
+
await this.disconnect(); // Clear current session
|
|
161
|
+
|
|
162
|
+
// Create a temporary options object to guide the new connection
|
|
163
|
+
const tempConnectOpts = {
|
|
164
|
+
defaultChain: `solana:${SOLANA_CLUSTER_TO_CHAIN_ID_PART[targetClusterName]}`
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// Attempt to connect to the new cluster
|
|
168
|
+
// The connect method will use the defaultChain from opts to determine the cluster.
|
|
169
|
+
await this.connect(tempConnectOpts);
|
|
170
|
+
this.getProvider().emit('chainChanged', network.id);
|
|
171
|
+
|
|
172
|
+
// Verify if the connection was successful and to the correct new network
|
|
173
|
+
if (!this.isConnected() || this.getChainId(PhantomConnector.SUPPORTED_NAMESPACE) !== tempConnectOpts.defaultChain) {
|
|
174
|
+
throw new Error(`Failed to switch network to ${targetClusterName}. Please try connecting manually.`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Orchestrates session restoration
|
|
179
|
+
async restoreSession() {
|
|
180
|
+
try {
|
|
181
|
+
const providerSession = await this.getProvider().restoreSession();
|
|
182
|
+
if (!providerSession) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// If provider session is restored, try to restore connector data
|
|
187
|
+
const connectorData = await this.getStorage().getItem(PHANTOM_CONNECTOR_STORAGE_KEY);
|
|
188
|
+
if (!connectorData) {
|
|
189
|
+
return false; // Provider session exists but connector data is missing
|
|
190
|
+
}
|
|
191
|
+
this.namespaces = connectorData.namespaces;
|
|
192
|
+
this.wallet = connectorData.wallet;
|
|
193
|
+
this.currentCaipNetworkId = connectorData.currentCaipNetworkId;
|
|
194
|
+
|
|
195
|
+
// await this.initializeKeyPair();
|
|
196
|
+
|
|
197
|
+
// Final validation
|
|
198
|
+
if (this.isConnected()) {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// If validation fails, something is out of sync. Clear everything.
|
|
203
|
+
await this.disconnect();
|
|
204
|
+
return false;
|
|
205
|
+
} catch (error) {
|
|
206
|
+
// On any error, disconnect to ensure a clean state
|
|
207
|
+
await this.disconnect();
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Saves only connector-specific data
|
|
213
|
+
async saveSession() {
|
|
214
|
+
if (!this.namespaces || !this.wallet || !this.currentCaipNetworkId) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const connectorData = {
|
|
218
|
+
namespaces: this.namespaces,
|
|
219
|
+
wallet: this.wallet,
|
|
220
|
+
currentCaipNetworkId: this.currentCaipNetworkId
|
|
221
|
+
};
|
|
222
|
+
try {
|
|
223
|
+
await this.getStorage().setItem(PHANTOM_CONNECTOR_STORAGE_KEY, connectorData);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
// console.error('PhantomConnector: Failed to save session.', error);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Clears only connector-specific data from storage
|
|
230
|
+
async clearSessionStorage() {
|
|
231
|
+
try {
|
|
232
|
+
await this.getStorage().removeItem(PHANTOM_CONNECTOR_STORAGE_KEY);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
// console.error('PhantomConnector: Failed to clear session from storage.', error);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=PhantomConnector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["WalletConnector","solana","solanaDevnet","solanaTestnet","ConstantsUtil","nacl","bs58","PhantomProvider","SOLANA_SIGNING_METHODS","SOLANA_CLUSTER_TO_CHAIN_ID_PART","id","PHANTOM_CONNECTOR_STORAGE_KEY","DAPP_KEYPAIR_STORAGE_KEY","PhantomConnector","currentCaipNetworkId","SUPPORTED_NAMESPACE","constructor","config","type","cluster","init","ops","storage","initializeKeyPair","appScheme","metadata","redirect","universal","native","Error","providerConfig","dappUrl","url","dappEncryptionKeyPair","provider","restoreSession","secretKeyB58","getStorage","getItem","secretKey","decode","box","keyPair","fromSecretKey","newKeyPair","setItem","encode","error","disconnect","connect","opts","isConnected","namespaces","defaultChain","split","chains","requestedCluster","Object","keys","find","key","connectResult","getProvider","solanaChainIdPart","wallet","PHANTOM_CUSTOM_WALLET","userPublicKey","getUserPublicKey","caipAddress","accounts","methods","values","events","saveSession","clearSession","undefined","clearSessionStorage","getNamespaces","getChainId","namespace","getProperties","properties","getWalletInfo","switchNetwork","network","targetClusterName","currentClusterName","Promise","resolve","tempConnectOpts","emit","providerSession","connectorData","removeItem"],"sourceRoot":"../../../src","sources":["connectors/PhantomConnector.ts"],"mappings":";;AAAA,SACEA,eAAe,EAUfC,MAA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C,IAAI,CAACY,MAAM,IAAI,CAAC,IAAI,CAAClD,oBAAoB,EAAE;MAClE;IACF;IAEA,MAAM8E,aAA0C,GAAG;MACjDxC,UAAU,EAAE,IAAI,CAACA,UAAU;MAC3BY,MAAM,EAAE,IAAI,CAACA,MAAM;MACnBlD,oBAAoB,EAAE,IAAI,CAACA;IAC7B,CAAC;IAED,IAAI;MACF,MAAM,IAAI,CAACuB,UAAU,CAAC,CAAC,CAACQ,OAAO,CAAClC,6BAA6B,EAAEiF,aAAa,CAAC;IAC/E,CAAC,CAAC,OAAO7C,KAAK,EAAE;MACd;IAAA;EAEJ;;EAEA;EACA,MAAc6B,mBAAmBA,CAAA,EAAkB;IACjD,IAAI;MACF,MAAM,IAAI,CAACvC,UAAU,CAAC,CAAC,CAACwD,UAAU,CAAClF,6BAA6B,CAAC;IACnE,CAAC,CAAC,OAAOoC,KAAK,EAAE;MACd;IAAA;EAEJ;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates if the given string is a Solana address.
|
|
5
|
+
* @param address The string to validate.
|
|
6
|
+
* @returns True if the address is valid, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export function isSolanaAddress(address) {
|
|
9
|
+
const solanaAddressRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
10
|
+
return solanaAddressRegex.test(address);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Helper to fetch SOL balance using JSON-RPC
|
|
15
|
+
* @param rpcUrl Solana RPC endpoint
|
|
16
|
+
* @param address Solana public address (base58)
|
|
17
|
+
*/
|
|
18
|
+
export async function getSolanaNativeBalance(rpcUrl, address) {
|
|
19
|
+
if (!isSolanaAddress(address)) {
|
|
20
|
+
throw new Error('Invalid Solana address format');
|
|
21
|
+
}
|
|
22
|
+
const response = await fetch(rpcUrl, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
'Content-Type': 'application/json'
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify({
|
|
28
|
+
jsonrpc: '2.0',
|
|
29
|
+
id: 1,
|
|
30
|
+
method: 'getBalance',
|
|
31
|
+
params: [address]
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
const json = await response.json();
|
|
35
|
+
if (json.error) throw new Error(json.error.message);
|
|
36
|
+
return json.result.value / 1000000000; // Convert lamports to SOL
|
|
37
|
+
}
|
|
38
|
+
let tokenCache = {};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Fetch metadata for a Solana SPL token using the Jupiter token list.
|
|
42
|
+
* @param mint - The token's mint address
|
|
43
|
+
* @returns TokenInfo if found, or undefined
|
|
44
|
+
*/
|
|
45
|
+
export async function getSolanaTokenMetadata(mint) {
|
|
46
|
+
// Return from cache if available
|
|
47
|
+
if (tokenCache[mint]) return tokenCache[mint];
|
|
48
|
+
try {
|
|
49
|
+
const res = await fetch('https://token.jup.ag/all');
|
|
50
|
+
const list = await res.json();
|
|
51
|
+
for (const token of list) {
|
|
52
|
+
tokenCache[token.address] = token;
|
|
53
|
+
}
|
|
54
|
+
return tokenCache[mint];
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get the balance of a token for a given address
|
|
62
|
+
* @param rpcUrl - The RPC URL to use
|
|
63
|
+
* @param address - The address to get the balance for
|
|
64
|
+
* @param tokenAddress - The address of the token to get the balance for
|
|
65
|
+
* @returns The balance of the token for the given address
|
|
66
|
+
*/
|
|
67
|
+
export async function getSolanaTokenBalance(rpcUrl, address, tokenAddress) {
|
|
68
|
+
if (!isSolanaAddress(address)) {
|
|
69
|
+
throw new Error('Invalid Solana address format');
|
|
70
|
+
}
|
|
71
|
+
const token = await getSolanaTokenMetadata(tokenAddress);
|
|
72
|
+
const response = await fetch(rpcUrl, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: {
|
|
75
|
+
'Content-Type': 'application/json'
|
|
76
|
+
},
|
|
77
|
+
body: JSON.stringify({
|
|
78
|
+
jsonrpc: '2.0',
|
|
79
|
+
id: 1,
|
|
80
|
+
method: 'getTokenAccountsByOwner',
|
|
81
|
+
params: [address, {
|
|
82
|
+
mint: tokenAddress
|
|
83
|
+
}, {
|
|
84
|
+
encoding: 'jsonParsed'
|
|
85
|
+
}]
|
|
86
|
+
})
|
|
87
|
+
});
|
|
88
|
+
const result = await response.json();
|
|
89
|
+
const balance = result.result.value[0]?.account?.data?.parsed?.info?.tokenAmount?.uiAmount;
|
|
90
|
+
return {
|
|
91
|
+
amount: balance?.toString() ?? '0',
|
|
92
|
+
symbol: token?.symbol ?? 'SOL'
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isSolanaAddress","address","solanaAddressRegex","test","getSolanaNativeBalance","rpcUrl","Error","response","fetch","method","headers","body","JSON","stringify","jsonrpc","id","params","json","error","message","result","value","tokenCache","getSolanaTokenMetadata","mint","res","list","token","undefined","getSolanaTokenBalance","tokenAddress","encoding","balance","account","data","parsed","info","tokenAmount","uiAmount","amount","toString","symbol"],"sourceRoot":"../../src","sources":["helpers.ts"],"mappings":";;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,eAAeA,CAACC,OAAe,EAAW;EACxD,MAAMC,kBAAkB,GAAG,+BAA+B;EAE1D,OAAOA,kBAAkB,CAACC,IAAI,CAACF,OAAO,CAAC;AACzC;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeG,sBAAsBA,CAACC,MAAc,EAAEJ,OAAe,EAAmB;EAC7F,IAAI,CAACD,eAAe,CAACC,OAAO,CAAC,EAAE;IAC7B,MAAM,IAAIK,KAAK,CAAC,+BAA+B,CAAC;EAClD;EAEA,MAA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