@dynamic-labs/solana 4.39.0 → 4.40.1

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 (44) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/package.cjs +1 -1
  3. package/package.js +1 -1
  4. package/package.json +17 -12
  5. package/src/CoinbaseSolana/CoinbaseSolana.cjs +4 -0
  6. package/src/CoinbaseSolana/CoinbaseSolana.js +4 -0
  7. package/src/Phantom/Phantom.cjs +4 -0
  8. package/src/Phantom/Phantom.js +4 -0
  9. package/src/SolanaWalletConnectors.cjs +17 -8
  10. package/src/SolanaWalletConnectors.js +17 -8
  11. package/src/Solflare/Solflare.cjs +4 -0
  12. package/src/Solflare/Solflare.js +4 -0
  13. package/src/index.cjs +3 -1
  14. package/src/index.js +3 -1
  15. package/src/injected/BackpackSol/BackpackSol.cjs +4 -0
  16. package/src/injected/BackpackSol/BackpackSol.js +4 -0
  17. package/src/injected/FallbackSolanaConnector/FallbackSolanaConnector.cjs +4 -0
  18. package/src/injected/FallbackSolanaConnector/FallbackSolanaConnector.js +4 -0
  19. package/src/injected/InjectedWalletBase/InjectedWalletBase.cjs +37 -14
  20. package/src/injected/InjectedWalletBase/InjectedWalletBase.d.ts +8 -0
  21. package/src/injected/InjectedWalletBase/InjectedWalletBase.js +37 -14
  22. package/src/injected/fetchInjectedWalletConnectors.cjs +2 -13
  23. package/src/injected/fetchInjectedWalletConnectors.js +2 -13
  24. package/src/utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.cjs +46 -0
  25. package/src/utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.d.ts +9 -0
  26. package/src/utils/getConnectorConstructorInjectedWallet/getConnectorConstructorInjectedWallet.js +42 -0
  27. package/src/utils/getConnectorConstructorInjectedWallet/index.d.ts +1 -0
  28. package/src/walletConnect/SolanaWalletConnectConnector/SolanaWalletConnectConnector.cjs +426 -0
  29. package/src/walletConnect/SolanaWalletConnectConnector/SolanaWalletConnectConnector.d.ts +64 -0
  30. package/src/walletConnect/SolanaWalletConnectConnector/SolanaWalletConnectConnector.js +417 -0
  31. package/src/walletConnect/SolanaWalletConnectConnector/createSolanaSignerForWalletConnect/createSolanaSignerForWalletConnect.cjs +126 -0
  32. package/src/walletConnect/SolanaWalletConnectConnector/createSolanaSignerForWalletConnect/createSolanaSignerForWalletConnect.d.ts +7 -0
  33. package/src/walletConnect/SolanaWalletConnectConnector/createSolanaSignerForWalletConnect/createSolanaSignerForWalletConnect.js +122 -0
  34. package/src/walletConnect/SolanaWalletConnectConnector/createSolanaSignerForWalletConnect/index.d.ts +1 -0
  35. package/src/walletConnect/SolanaWalletConnectConnector/index.d.ts +1 -0
  36. package/src/walletConnect/SolanaWalletConnectConnector/types.d.ts +40 -0
  37. package/src/walletConnect/utils/addSolanaWalletConnectConnectors/addSolanaWalletConnectConnectors.cjs +64 -0
  38. package/src/walletConnect/utils/addSolanaWalletConnectConnectors/addSolanaWalletConnectConnectors.d.ts +12 -0
  39. package/src/walletConnect/utils/addSolanaWalletConnectConnectors/addSolanaWalletConnectConnectors.js +60 -0
  40. package/src/walletConnect/utils/addSolanaWalletConnectConnectors/index.d.ts +1 -0
  41. package/src/walletConnect/utils/getSolanaWalletConnectConnector/getSolanaWalletConnectConnector.cjs +21 -0
  42. package/src/walletConnect/utils/getSolanaWalletConnectConnector/getSolanaWalletConnectConnector.d.ts +2 -0
  43. package/src/walletConnect/utils/getSolanaWalletConnectConnector/getSolanaWalletConnectConnector.js +17 -0
  44. package/src/walletConnect/utils/getSolanaWalletConnectConnector/index.d.ts +1 -0
@@ -0,0 +1,417 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { PublicKey, VersionedTransaction, Transaction } from '@solana/web3.js';
4
+ import { SDK_ERRORS } from '@walletconnect/utils';
5
+ import bs58 from 'bs58';
6
+ import EventEmitter from 'eventemitter3';
7
+ import { SolanaWalletConnector, isVersionedTransaction } from '@dynamic-labs/solana-core';
8
+ import { StorageService, DynamicError, filterDuplicates, isMobile, PlatformService, bufferToBase64 } from '@dynamic-labs/utils';
9
+ import { getSignClientSingleton } from '@dynamic-labs/wallet-connect';
10
+ import { performPlatformSpecificConnectionMethod, getDeepLink, isSameAddress } from '@dynamic-labs/wallet-connector-core';
11
+ import { logger } from '../../utils/logger.js';
12
+ import { createSolanaSignerForWalletConnect } from './createSolanaSignerForWalletConnect/createSolanaSignerForWalletConnect.js';
13
+
14
+ const WC_SOLANA_CURRENT_CHAIN_KEY = 'dynamic-wc2-solana-current-chain';
15
+ class SolanaWalletConnectConnector extends SolanaWalletConnector {
16
+ constructor(opts) {
17
+ super(opts);
18
+ this.activeAccountEmitter = new EventEmitter();
19
+ this.isInitialized = false;
20
+ this.canConnectViaQrCode = true;
21
+ this.isWalletConnect = true;
22
+ this.canHandleMultipleConnections = false;
23
+ this.name = opts.walletName;
24
+ const storedChainId = StorageService.getItem(WC_SOLANA_CURRENT_CHAIN_KEY);
25
+ if (storedChainId) {
26
+ this.setNetworkId(storedChainId);
27
+ }
28
+ if (opts.overrideKey) {
29
+ this.overrideKey = opts.overrideKey;
30
+ }
31
+ if (!opts.projectId) {
32
+ throw new DynamicError('WalletConnect project ID is required');
33
+ }
34
+ }
35
+ get deepLinkPreference() {
36
+ var _a;
37
+ return (_a = this.constructorProps.deepLinkPreference) !== null && _a !== void 0 ? _a : 'native';
38
+ }
39
+ /**
40
+ * Accesses the sign client singleton instance, and throws if it is not
41
+ * immediately available.
42
+ */
43
+ get signClient() {
44
+ if (!SolanaWalletConnectConnector.signClientReference) {
45
+ throw new DynamicError('Failed to access sign client for Wallet Connect Solana: Sign client not initialized');
46
+ }
47
+ return SolanaWalletConnectConnector.signClientReference;
48
+ }
49
+ getSupportedNetworks() {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ var _a, _b;
52
+ if (!this.session) {
53
+ return [];
54
+ }
55
+ const sessionChains = (_b = (_a = this.session.namespaces.solana) === null || _a === void 0 ? void 0 : _a.chains) !== null && _b !== void 0 ? _b : [];
56
+ return this.solNetworks
57
+ .filter((network) => sessionChains.includes(`solana:${network.genesisHash}`))
58
+ .map((network) => network.chainId.toString());
59
+ });
60
+ }
61
+ getActiveAddress() {
62
+ var _a, _b;
63
+ if (!this.session) {
64
+ return undefined;
65
+ }
66
+ return (_b = (_a = this.session.namespaces.solana) === null || _a === void 0 ? void 0 : _a.accounts) === null || _b === void 0 ? void 0 : _b[0].split(':')[2];
67
+ }
68
+ listenToActiveAccountChange(listener) {
69
+ this.activeAccountEmitter.on('activeAccountDidChange', listener);
70
+ return () => {
71
+ this.activeAccountEmitter.off('activeAccountDidChange', listener);
72
+ };
73
+ }
74
+ getSigner() {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ return createSolanaSignerForWalletConnect({ walletConnector: this });
77
+ });
78
+ }
79
+ connect() {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ throw new Error('Connect method not implemented.');
82
+ });
83
+ }
84
+ init() {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ if (this.isInitialized) {
87
+ return;
88
+ }
89
+ logger.logVerboseTroubleshootingMessage('[SolanaWalletConnect] init called');
90
+ this.isInitialized = true;
91
+ if (SolanaWalletConnectConnector.signClientPromise) {
92
+ yield SolanaWalletConnectConnector.signClientPromise;
93
+ this.setupWCEventListeners();
94
+ return;
95
+ }
96
+ const { appLogoUrl, appName, projectId } = this.constructorProps;
97
+ SolanaWalletConnectConnector.signClientPromise = getSignClientSingleton({
98
+ appIcon: appLogoUrl !== null && appLogoUrl !== void 0 ? appLogoUrl : '',
99
+ appName: appName !== null && appName !== void 0 ? appName : '',
100
+ projectId,
101
+ });
102
+ SolanaWalletConnectConnector.signClientReference =
103
+ yield SolanaWalletConnectConnector.signClientPromise;
104
+ this.setupWCEventListeners();
105
+ });
106
+ }
107
+ setupWCEventListeners() {
108
+ logger.debug('[SolanaWalletConnect] setupWCEventListeners');
109
+ this.signClient.on('session_event', ({ params: { event }, topic }) => {
110
+ var _a, _b;
111
+ // Ignore events for wallets other than the one we are connected to
112
+ if (!this.session || topic !== this.session.topic) {
113
+ return;
114
+ }
115
+ if (event.name === 'chainChanged') {
116
+ const chainParam = event.data;
117
+ let chainId = chainParam;
118
+ // Handle potentially CAIP-2 format
119
+ if (chainParam.startsWith('solana:')) {
120
+ const chainHash = chainParam.split(':')[1];
121
+ chainId =
122
+ (_b = (_a = this.solNetworks
123
+ .find((network) => network.genesisHash === chainHash)) === null || _a === void 0 ? void 0 : _a.networkId.toString()) !== null && _b !== void 0 ? _b : chainParam;
124
+ }
125
+ logger.debug('[SolanaWalletConnect] onChainChange', { chainId });
126
+ if (chainId === this.getNetworkId()) {
127
+ logger.debug(`[SolanaWalletConnect] onChainChange - ignoring chainChanged event with same chain id as current chain id: ${chainId}`);
128
+ return;
129
+ }
130
+ // This will already emit an event so no need to call this.emit('chainChange', { chain: chainId });
131
+ this.switchNetwork({ networkChainId: parseInt(chainId) });
132
+ return;
133
+ }
134
+ if (event.name === 'accountsChanged') {
135
+ const accountsParam = event.data;
136
+ const accounts = filterDuplicates(accountsParam.map((account) => {
137
+ // Handle potentially CAIP-10 format
138
+ if (account.startsWith('solana:')) {
139
+ return account.split(':')[2];
140
+ }
141
+ return account;
142
+ }));
143
+ logger.debug('[SolanaWalletConnect] onAccountChanged', { accounts });
144
+ this.emit('accountChange', { accounts });
145
+ return;
146
+ }
147
+ if (event.name === 'disconnected') {
148
+ logger.debug('[SolanaWalletConnect] onDisconnect');
149
+ this.emit('disconnect');
150
+ this.endSession();
151
+ return;
152
+ }
153
+ });
154
+ }
155
+ endSession() {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ logger.debug('[SolanaWalletConnect] endSession');
158
+ this.connectionUri = undefined;
159
+ if (!this.session) {
160
+ return;
161
+ }
162
+ yield this.signClient.disconnect({
163
+ reason: SDK_ERRORS.USER_DISCONNECTED,
164
+ topic: this.session.topic,
165
+ });
166
+ this.session = undefined;
167
+ });
168
+ }
169
+ displayUri(connectionOpts) {
170
+ if (!this.connectionUri) {
171
+ return;
172
+ }
173
+ logger.debug('[SolanaWalletConnect] handleDisplayURI', this.connectionUri);
174
+ performPlatformSpecificConnectionMethod(this.connectionUri, this.metadata.deepLinks, {
175
+ onDesktopUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDesktopUri,
176
+ onDisplayUri: connectionOpts === null || connectionOpts === void 0 ? void 0 : connectionOpts.onDisplayUri,
177
+ }, this.deepLinkPreference);
178
+ }
179
+ getAddress(opts) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ var _a;
182
+ logger.debug('[SolanaWalletConnect] getAddress', opts);
183
+ /**
184
+ * In mobile/Safari, if the user just navigates back after the deeplink prompt the connection
185
+ * is not rejected, so the previous connection URI is still valid and we can use it to handle the connection
186
+ */
187
+ if (this.connectionUri) {
188
+ logger.debug('[SolanaWalletConnect] getAddress - connecting to WalletConnect with existing connection URI');
189
+ this.displayUri(opts);
190
+ return;
191
+ }
192
+ /**
193
+ * If we are already connected to a session, we need to end it first
194
+ */
195
+ yield this.endSession();
196
+ logger.logVerboseTroubleshootingMessage('[WalletConnectConnector] getAddress', {
197
+ inAppBrowserUrl: (_a = this.metadata) === null || _a === void 0 ? void 0 : _a.inAppBrowserUrl,
198
+ isMobile: isMobile(),
199
+ mobileExperience: this.mobileExperience,
200
+ });
201
+ const didOpenInAppBrowser = this.openInAppBrowserIfRequired();
202
+ if (didOpenInAppBrowser) {
203
+ return;
204
+ }
205
+ logger.debug('[SolanaWalletConnect] getAddress - connecting to WalletConnect');
206
+ const chains = this.solNetworks.map((network) => `solana:${network.genesisHash}`);
207
+ try {
208
+ const { approval, uri } = yield this.signClient.connect({
209
+ optionalNamespaces: {
210
+ solana: {
211
+ chains,
212
+ events: ['chainChanged', 'accountsChanged', 'disconnected'],
213
+ methods: [
214
+ 'solana_signMessage',
215
+ 'solana_signTransaction',
216
+ 'solana_requestAccounts',
217
+ 'solana_getAccounts',
218
+ 'solana_signAllTransactions',
219
+ 'solana_signAndSendTransaction',
220
+ ],
221
+ },
222
+ },
223
+ });
224
+ logger.debug('[SolanaWalletConnect] getAddress - connection URI', uri);
225
+ this.connectionUri = uri;
226
+ this.displayUri(opts);
227
+ this.session = yield approval();
228
+ const activeAddress = this.getActiveAddress();
229
+ this.activeAccountEmitter.emit('activeAccountDidChange', activeAddress);
230
+ return activeAddress;
231
+ }
232
+ finally {
233
+ this.connectionUri = undefined;
234
+ }
235
+ });
236
+ }
237
+ signClientRequest(_a) {
238
+ return __awaiter(this, arguments, void 0, function* ({ method, params, }) {
239
+ var _b, _c;
240
+ if (!this.session) {
241
+ throw new DynamicError('Session not initialized. Please connect to a wallet first.');
242
+ }
243
+ const chainHash = (_c = (_b = this.getSelectedNetwork()) === null || _b === void 0 ? void 0 : _b.genesisHash) !== null && _c !== void 0 ? _c : this.solNetworks[0].genesisHash;
244
+ const chainId = `solana:${chainHash}`;
245
+ try {
246
+ const requestPromise = this.signClient.request({
247
+ chainId,
248
+ request: {
249
+ method,
250
+ params,
251
+ },
252
+ topic: this.session.topic,
253
+ });
254
+ this.deepLinkIfApplicable({ method });
255
+ return yield requestPromise;
256
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
257
+ }
258
+ catch (error) {
259
+ // If they are throwing a generic error, lets end session to ensure the user will be prompted to reconnect
260
+ // on the next attempt. This should fix the issue.
261
+ // Yes their error message has a typo on "occured"
262
+ if (error.message === 'An error has occured. Please, try again.') {
263
+ yield this.endSession();
264
+ throw new DynamicError('Wallet connection lost. Please, try again.');
265
+ }
266
+ throw error;
267
+ }
268
+ });
269
+ }
270
+ deepLinkIfApplicable(params) {
271
+ const methodsThatRequireDeepLink = [
272
+ 'solana_signMessage',
273
+ 'solana_signTransaction',
274
+ 'solana_signAllTransactions',
275
+ 'solana_signAndSendTransaction',
276
+ ];
277
+ const deepLink = this.getDeepLink();
278
+ if (isMobile() &&
279
+ deepLink &&
280
+ methodsThatRequireDeepLink.includes(params.method)) {
281
+ PlatformService.openURL(deepLink);
282
+ }
283
+ }
284
+ signMessage(messageToSign, options) {
285
+ return __awaiter(this, void 0, void 0, function* () {
286
+ logger.logVerboseTroubleshootingMessage('[SolanaWalletConnect] signMessage', messageToSign, options);
287
+ const encodedMessage = new TextEncoder().encode(messageToSign);
288
+ const decodedSignature = yield this.signEncodedMessage(encodedMessage, options);
289
+ return bufferToBase64(decodedSignature);
290
+ });
291
+ }
292
+ signEncodedMessage(encodedMessage, options) {
293
+ return __awaiter(this, void 0, void 0, function* () {
294
+ logger.logVerboseTroubleshootingMessage('[SolanaWalletConnect] signEncodedMessage', encodedMessage, options);
295
+ if (options === null || options === void 0 ? void 0 : options.address) {
296
+ yield this.validateActiveWallet(options.address);
297
+ }
298
+ const activeAddress = this.getActiveAddress();
299
+ if (!activeAddress) {
300
+ throw new DynamicError('No active address found');
301
+ }
302
+ const { signature } = yield this.signClientRequest({
303
+ method: 'solana_signMessage',
304
+ params: {
305
+ message: bs58.encode(encodedMessage),
306
+ pubkey: new PublicKey(activeAddress).toBase58(),
307
+ },
308
+ });
309
+ return bs58.decode(signature);
310
+ });
311
+ }
312
+ serializeTransaction(transaction) {
313
+ const serialized = transaction.serialize({
314
+ requireAllSignatures: false,
315
+ verifySignatures: false,
316
+ });
317
+ return bs58.encode(new Uint8Array(serialized));
318
+ }
319
+ signTransaction(transaction) {
320
+ return __awaiter(this, void 0, void 0, function* () {
321
+ const activeAddress = this.getActiveAddress();
322
+ if (!activeAddress) {
323
+ throw new DynamicError('Active account address is required');
324
+ }
325
+ const serializedTransaction = this.serializeTransaction(transaction);
326
+ const { transaction: signedTransaction } = yield this.signClientRequest({
327
+ method: 'solana_signTransaction',
328
+ params: { transaction: serializedTransaction },
329
+ });
330
+ const decodedTransaction = bs58.decode(signedTransaction);
331
+ if (isVersionedTransaction(transaction)) {
332
+ return VersionedTransaction.deserialize(decodedTransaction);
333
+ }
334
+ return Transaction.from(decodedTransaction);
335
+ });
336
+ }
337
+ signAllTransactions(transactions) {
338
+ return __awaiter(this, void 0, void 0, function* () {
339
+ const serializedTransactions = transactions.map(this.serializeTransaction);
340
+ const { transactions: signedTransactions } = yield this.signClientRequest({
341
+ method: 'solana_signAllTransactions',
342
+ params: { transactions: serializedTransactions },
343
+ });
344
+ return signedTransactions.map((signedTransaction, index) => {
345
+ const originalTransaction = transactions[index];
346
+ const decodedTransaction = bs58.decode(signedTransaction);
347
+ if (isVersionedTransaction(originalTransaction)) {
348
+ return VersionedTransaction.deserialize(decodedTransaction);
349
+ }
350
+ return Transaction.from(decodedTransaction);
351
+ });
352
+ });
353
+ }
354
+ signAndSendTransaction(transaction, options) {
355
+ return __awaiter(this, void 0, void 0, function* () {
356
+ const serializedTransaction = this.serializeTransaction(transaction);
357
+ const { signature } = yield this.signClientRequest({
358
+ method: 'solana_signAndSendTransaction',
359
+ params: { options, transaction: serializedTransaction },
360
+ });
361
+ return signature;
362
+ });
363
+ }
364
+ getDeepLink() {
365
+ const deepLink = getDeepLink({
366
+ deepLinks: this.metadata.deepLinks,
367
+ mode: 'regular',
368
+ preference: this.deepLinkPreference,
369
+ // TODO: verify whether we need the uri
370
+ // uri: provider.signer.uri,
371
+ });
372
+ logger.logVerboseTroubleshootingMessage('[SolanaWalletConnect] getDeepLink - deepLink', deepLink);
373
+ if (!deepLink) {
374
+ return;
375
+ }
376
+ if (this.session) {
377
+ // we need to include the session topic here because it helps the wallet
378
+ // auto redirect back to the dapp after signing
379
+ return `${deepLink}?sessionTopic=${this.session.topic}`;
380
+ }
381
+ return deepLink;
382
+ }
383
+ getConnectedAccounts() {
384
+ return __awaiter(this, void 0, void 0, function* () {
385
+ /**
386
+ * Each session has a single account (it might be on multiple chains hence the array),
387
+ * but that account is stored as a CAIP-10 string, so we need to parse it to get the address
388
+ */
389
+ const account = this.getActiveAddress();
390
+ if (!account) {
391
+ return [];
392
+ }
393
+ logger.logVerboseTroubleshootingMessage('[SolanaWalletConnect] getConnectedAccounts - activeAccounts', [account]);
394
+ return [account];
395
+ });
396
+ }
397
+ getConnectionUri() {
398
+ return this.connectionUri;
399
+ }
400
+ validateActiveWallet(expectedAddress) {
401
+ return __awaiter(this, void 0, void 0, function* () {
402
+ logger.debug('[SolanaWalletConnect] validateActiveWallet - validating wallet', expectedAddress);
403
+ const [activeAddress] = yield this.getConnectedAccounts();
404
+ const isWalletActive = isSameAddress(activeAddress, expectedAddress, this.connectedChain);
405
+ if (isWalletActive) {
406
+ logger.debug('[SolanaWalletConnect] validateActiveWallet - wallet is active');
407
+ return;
408
+ }
409
+ return this.handleWalletNotActive({
410
+ activeAddress,
411
+ expectedAddress,
412
+ });
413
+ });
414
+ }
415
+ }
416
+
417
+ export { SolanaWalletConnectConnector };
@@ -0,0 +1,126 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../../_virtual/_tslib.cjs');
7
+ var web3_js = require('@solana/web3.js');
8
+ var utils = require('@dynamic-labs/utils');
9
+ var logger = require('../../../utils/logger.cjs');
10
+
11
+ const createSolanaSignerForWalletConnect = ({ walletConnector, }) => {
12
+ const connect = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
13
+ const address = walletConnector.getActiveAddress();
14
+ if (!address) {
15
+ return undefined;
16
+ }
17
+ return { address, publicKey: new web3_js.PublicKey(address) };
18
+ });
19
+ // eslint-disable-next-line arrow-body-style
20
+ const disconnect = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
21
+ yield walletConnector.endSession();
22
+ });
23
+ const on = (event, listener) => {
24
+ if (event === 'connect') {
25
+ logger.logger.debug('on - Not implemented for event: connect');
26
+ return signer;
27
+ }
28
+ if (event === 'activeWalletDidChange') {
29
+ walletConnector.listenToActiveAccountChange(listener);
30
+ return signer;
31
+ }
32
+ walletConnector.signClient.on('session_event', (sessionEvent) => {
33
+ // Skip events from other sessions
34
+ if (!walletConnector.session ||
35
+ sessionEvent.topic !== walletConnector.session.topic) {
36
+ return;
37
+ }
38
+ if (sessionEvent.params.event.name === 'accountsChanged' &&
39
+ event === 'accountChanged') {
40
+ const accountsParam = sessionEvent.params.event.data;
41
+ const accounts = utils.filterDuplicates(accountsParam.map((account) => {
42
+ // Handle potentially CAIP-10 format
43
+ if (account.startsWith('solana:')) {
44
+ return account.split(':')[2];
45
+ }
46
+ return account;
47
+ }));
48
+ listener(accounts[0]);
49
+ return;
50
+ }
51
+ if (sessionEvent.params.event.name === 'disconnected' &&
52
+ event === 'disconnect') {
53
+ listener('');
54
+ return;
55
+ }
56
+ });
57
+ return signer;
58
+ };
59
+ const signMessage = (messageArrayBuffer) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
60
+ const signature = yield walletConnector.signEncodedMessage(messageArrayBuffer);
61
+ return { signature };
62
+ });
63
+ const signAllTransactions = (transactions) => _tslib.__awaiter(void 0, void 0, void 0, function* () { return walletConnector.signAllTransactions(transactions); });
64
+ const signAndSendTransaction = (transaction, options) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
65
+ const signature = yield walletConnector.signAndSendTransaction(transaction, options);
66
+ return { signature };
67
+ });
68
+ const signTransaction = (transaction) => _tslib.__awaiter(void 0, void 0, void 0, function* () { return walletConnector.signTransaction(transaction); });
69
+ const signer = {
70
+ addListener: () => {
71
+ throw new Error('addListener - Not implemented');
72
+ },
73
+ connect,
74
+ disconnect,
75
+ emit: () => {
76
+ throw new Error('emit - Not implemented');
77
+ },
78
+ eventNames: () => {
79
+ logger.logger.error('eventNames - Not implemented');
80
+ return [];
81
+ },
82
+ isBackpack: false,
83
+ isBraveWallet: false,
84
+ get isConnected() {
85
+ return Boolean(walletConnector.getActiveAddress());
86
+ },
87
+ isExodus: false,
88
+ isGlow: false,
89
+ isMagicEden: false,
90
+ isPhantom: false,
91
+ isSolflare: false,
92
+ listenerCount: () => {
93
+ logger.logger.error('listenerCount - Not implemented');
94
+ return 0;
95
+ },
96
+ listeners: () => {
97
+ logger.logger.error('listeners - Not implemented');
98
+ return [];
99
+ },
100
+ off: () => {
101
+ throw new Error('off - Not implemented');
102
+ },
103
+ on,
104
+ once: () => {
105
+ throw new Error('once - Not implemented');
106
+ },
107
+ providers: [],
108
+ get publicKey() {
109
+ const address = walletConnector.getActiveAddress();
110
+ return address ? new web3_js.PublicKey(address) : undefined;
111
+ },
112
+ removeAllListeners: () => {
113
+ throw new Error('removeAllListeners - Not implemented');
114
+ },
115
+ removeListener: () => {
116
+ throw new Error('removeListener - Not implemented');
117
+ },
118
+ signAllTransactions,
119
+ signAndSendTransaction,
120
+ signMessage,
121
+ signTransaction,
122
+ };
123
+ return signer;
124
+ };
125
+
126
+ exports.createSolanaSignerForWalletConnect = createSolanaSignerForWalletConnect;
@@ -0,0 +1,7 @@
1
+ import { ISolanaSigner } from '@dynamic-labs/solana-core';
2
+ import { SolanaWalletConnectConnector } from '../SolanaWalletConnectConnector';
3
+ type CreateSolanaSignerForWalletConnectProps = {
4
+ walletConnector: SolanaWalletConnectConnector;
5
+ };
6
+ export declare const createSolanaSignerForWalletConnect: ({ walletConnector, }: CreateSolanaSignerForWalletConnectProps) => ISolanaSigner;
7
+ export {};
@@ -0,0 +1,122 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../../_virtual/_tslib.js';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ import { filterDuplicates } from '@dynamic-labs/utils';
5
+ import { logger } from '../../../utils/logger.js';
6
+
7
+ const createSolanaSignerForWalletConnect = ({ walletConnector, }) => {
8
+ const connect = () => __awaiter(void 0, void 0, void 0, function* () {
9
+ const address = walletConnector.getActiveAddress();
10
+ if (!address) {
11
+ return undefined;
12
+ }
13
+ return { address, publicKey: new PublicKey(address) };
14
+ });
15
+ // eslint-disable-next-line arrow-body-style
16
+ const disconnect = () => __awaiter(void 0, void 0, void 0, function* () {
17
+ yield walletConnector.endSession();
18
+ });
19
+ const on = (event, listener) => {
20
+ if (event === 'connect') {
21
+ logger.debug('on - Not implemented for event: connect');
22
+ return signer;
23
+ }
24
+ if (event === 'activeWalletDidChange') {
25
+ walletConnector.listenToActiveAccountChange(listener);
26
+ return signer;
27
+ }
28
+ walletConnector.signClient.on('session_event', (sessionEvent) => {
29
+ // Skip events from other sessions
30
+ if (!walletConnector.session ||
31
+ sessionEvent.topic !== walletConnector.session.topic) {
32
+ return;
33
+ }
34
+ if (sessionEvent.params.event.name === 'accountsChanged' &&
35
+ event === 'accountChanged') {
36
+ const accountsParam = sessionEvent.params.event.data;
37
+ const accounts = filterDuplicates(accountsParam.map((account) => {
38
+ // Handle potentially CAIP-10 format
39
+ if (account.startsWith('solana:')) {
40
+ return account.split(':')[2];
41
+ }
42
+ return account;
43
+ }));
44
+ listener(accounts[0]);
45
+ return;
46
+ }
47
+ if (sessionEvent.params.event.name === 'disconnected' &&
48
+ event === 'disconnect') {
49
+ listener('');
50
+ return;
51
+ }
52
+ });
53
+ return signer;
54
+ };
55
+ const signMessage = (messageArrayBuffer) => __awaiter(void 0, void 0, void 0, function* () {
56
+ const signature = yield walletConnector.signEncodedMessage(messageArrayBuffer);
57
+ return { signature };
58
+ });
59
+ const signAllTransactions = (transactions) => __awaiter(void 0, void 0, void 0, function* () { return walletConnector.signAllTransactions(transactions); });
60
+ const signAndSendTransaction = (transaction, options) => __awaiter(void 0, void 0, void 0, function* () {
61
+ const signature = yield walletConnector.signAndSendTransaction(transaction, options);
62
+ return { signature };
63
+ });
64
+ const signTransaction = (transaction) => __awaiter(void 0, void 0, void 0, function* () { return walletConnector.signTransaction(transaction); });
65
+ const signer = {
66
+ addListener: () => {
67
+ throw new Error('addListener - Not implemented');
68
+ },
69
+ connect,
70
+ disconnect,
71
+ emit: () => {
72
+ throw new Error('emit - Not implemented');
73
+ },
74
+ eventNames: () => {
75
+ logger.error('eventNames - Not implemented');
76
+ return [];
77
+ },
78
+ isBackpack: false,
79
+ isBraveWallet: false,
80
+ get isConnected() {
81
+ return Boolean(walletConnector.getActiveAddress());
82
+ },
83
+ isExodus: false,
84
+ isGlow: false,
85
+ isMagicEden: false,
86
+ isPhantom: false,
87
+ isSolflare: false,
88
+ listenerCount: () => {
89
+ logger.error('listenerCount - Not implemented');
90
+ return 0;
91
+ },
92
+ listeners: () => {
93
+ logger.error('listeners - Not implemented');
94
+ return [];
95
+ },
96
+ off: () => {
97
+ throw new Error('off - Not implemented');
98
+ },
99
+ on,
100
+ once: () => {
101
+ throw new Error('once - Not implemented');
102
+ },
103
+ providers: [],
104
+ get publicKey() {
105
+ const address = walletConnector.getActiveAddress();
106
+ return address ? new PublicKey(address) : undefined;
107
+ },
108
+ removeAllListeners: () => {
109
+ throw new Error('removeAllListeners - Not implemented');
110
+ },
111
+ removeListener: () => {
112
+ throw new Error('removeListener - Not implemented');
113
+ },
114
+ signAllTransactions,
115
+ signAndSendTransaction,
116
+ signMessage,
117
+ signTransaction,
118
+ };
119
+ return signer;
120
+ };
121
+
122
+ export { createSolanaSignerForWalletConnect };
@@ -0,0 +1 @@
1
+ export { createSolanaSignerForWalletConnect } from './createSolanaSignerForWalletConnect';
@@ -0,0 +1 @@
1
+ export { SolanaWalletConnectConnector } from './SolanaWalletConnectConnector';