@hashgraph/hedera-wallet-connect 1.3.8-canary.9350581.0 → 1.3.8-canary.a321e2e.0

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.
@@ -1,5 +1,6 @@
1
1
  import { Signer, AccountBalance, AccountId, AccountInfo, Executable, Key, LedgerId, SignerSignature, Transaction, TransactionRecord } from '@hashgraph/sdk';
2
2
  import type { CoreTypes, ISignClient } from '@walletconnect/types';
3
+ import { LogLevel } from '../shared/logger';
3
4
  export declare class DAppSigner implements Signer {
4
5
  private readonly accountId;
5
6
  private readonly signClient;
@@ -7,12 +8,12 @@ export declare class DAppSigner implements Signer {
7
8
  private readonly ledgerId;
8
9
  readonly extensionId?: string | undefined;
9
10
  private logger;
10
- constructor(accountId: AccountId, signClient: ISignClient, topic: string, ledgerId?: LedgerId, extensionId?: string | undefined, logLevel?: 'error' | 'warn' | 'info' | 'debug');
11
+ constructor(accountId: AccountId, signClient: ISignClient, topic: string, ledgerId?: LedgerId, extensionId?: string | undefined, logLevel?: LogLevel);
11
12
  /**
12
13
  * Sets the logging level for the DAppSigner
13
14
  * @param level - The logging level to set
14
15
  */
15
- setLogLevel(level: 'error' | 'warn' | 'info' | 'debug'): void;
16
+ setLogLevel(level: LogLevel): void;
16
17
  private _getHederaClient;
17
18
  private get _signerAccountId();
18
19
  private _getRandomNodes;
@@ -21,6 +21,7 @@ import { AccountBalance, AccountId, AccountInfo, LedgerId, SignerSignature, Tran
21
21
  import { proto } from '@hashgraph/proto';
22
22
  import { HederaJsonRpcMethod, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, Uint8ArrayToBase64String, Uint8ArrayToString, } from '../shared';
23
23
  import { DefaultLogger } from '../shared/logger';
24
+ import { SessionNotFoundError } from './SessionNotFoundError';
24
25
  const clients = {};
25
26
  export class DAppSigner {
26
27
  constructor(accountId, signClient, topic, ledgerId = LedgerId.MAINNET, extensionId, logLevel = 'debug') {
@@ -60,6 +61,12 @@ export class DAppSigner {
60
61
  return allNodes.slice(0, numberOfNodes);
61
62
  }
62
63
  request(request) {
64
+ var _a, _b;
65
+ // Avoid a wallet call if the session is no longer valid
66
+ if (!((_b = (_a = this === null || this === void 0 ? void 0 : this.signClient) === null || _a === void 0 ? void 0 : _a.session) === null || _b === void 0 ? void 0 : _b.get(this.topic))) {
67
+ this.logger.error('Session no longer exists. Please reconnect to the wallet.');
68
+ throw new SessionNotFoundError('Session no longer exists. Please reconnect to the wallet.');
69
+ }
63
70
  if (this.extensionId)
64
71
  extensionOpen(this.extensionId);
65
72
  return this.signClient.request({
@@ -164,6 +171,10 @@ export class DAppSigner {
164
171
  }
165
172
  async _tryExecuteTransactionRequest(request) {
166
173
  try {
174
+ // Verify session is still valid before proceeding
175
+ if (!this.signClient.session.get(this.topic)) {
176
+ throw new Error('Session no longer exists. Please reconnect to the wallet.');
177
+ }
167
178
  const requestToBytes = request.toBytes();
168
179
  this.logger.debug('Creating transaction from bytes', requestToBytes, request);
169
180
  const transaction = Transaction.fromBytes(requestToBytes);
@@ -180,6 +191,18 @@ export class DAppSigner {
180
191
  }
181
192
  catch (error) {
182
193
  this.logger.error('Error executing transaction request:', error);
194
+ if (error instanceof SessionNotFoundError) {
195
+ this.logger.error('Session was deleted, removing signer');
196
+ // Notify DAppConnector to remove this signer
197
+ this.signClient.emit({
198
+ topic: this.topic,
199
+ event: {
200
+ name: 'session_delete',
201
+ data: { topic: this.topic },
202
+ },
203
+ chainId: ledgerIdToCAIPChainId(this.ledgerId),
204
+ });
205
+ }
183
206
  return { error };
184
207
  }
185
208
  }
@@ -255,6 +278,7 @@ export class DAppSigner {
255
278
  return { result: this._parseQueryResponse(query, result.response) };
256
279
  }
257
280
  catch (error) {
281
+ this.logger.error('Error executing query request:', error);
258
282
  return { error };
259
283
  }
260
284
  }
@@ -0,0 +1,3 @@
1
+ export declare class SessionNotFoundError extends Error {
2
+ constructor(message: string);
3
+ }
@@ -0,0 +1,6 @@
1
+ export class SessionNotFoundError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = 'SessionNotFoundError';
5
+ }
6
+ }
@@ -2,6 +2,7 @@ import { AccountId, LedgerId } from '@hashgraph/sdk';
2
2
  import { SessionTypes, SignClientTypes } from '@walletconnect/types';
3
3
  import { WalletConnectModal } from '@walletconnect/modal';
4
4
  import SignClient from '@walletconnect/sign-client';
5
+ import { LogLevel } from '../shared/logger';
5
6
  import { GetNodeAddressesResult, ExecuteTransactionParams, ExecuteTransactionResult, SignMessageParams, SignMessageResult, SignAndExecuteQueryResult, SignAndExecuteQueryParams, SignAndExecuteTransactionParams, SignAndExecuteTransactionResult, SignTransactionParams, SignTransactionResult, ExtensionData } from '../shared';
6
7
  import { DAppSigner } from './DAppSigner';
7
8
  export * from './DAppSigner';
@@ -30,12 +31,12 @@ export declare class DAppConnector {
30
31
  * @param chains - Array of supported chains for the DApp (optional).
31
32
  * @param logLevel - Logging level for the DAppConnector (optional).
32
33
  */
33
- constructor(metadata: SignClientTypes.Metadata, network: LedgerId, projectId: string, methods?: string[], events?: string[], chains?: string[], logLevel?: 'error' | 'warn' | 'info' | 'debug');
34
+ constructor(metadata: SignClientTypes.Metadata, network: LedgerId, projectId: string, methods?: string[], events?: string[], chains?: string[], logLevel?: LogLevel);
34
35
  /**
35
36
  * Sets the logging level for the DAppConnector
36
37
  * @param level - The logging level to set
37
38
  */
38
- setLogLevel(level: 'error' | 'warn' | 'info' | 'debug'): void;
39
+ setLogLevel(level: LogLevel): void;
39
40
  /**
40
41
  * Initializes the DAppConnector instance.
41
42
  * @param logger - `BaseLogger` for logging purposes (optional).
@@ -79,9 +80,10 @@ export declare class DAppConnector {
79
80
  */
80
81
  connectExtension(extensionId: string, pairingTopic?: string): Promise<SessionTypes.Struct>;
81
82
  /**
82
- * Validates the session by checking if the session exists.
83
+ * Validates the session by checking if the session exists and is valid.
84
+ * Also ensures the signer exists for the session.
83
85
  * @param topic - The topic of the session to validate.
84
- * @returns {boolean} - True if the session exists, false otherwise.
86
+ * @returns {boolean} - True if the session exists and has a valid signer, false otherwise.
85
87
  */
86
88
  private validateSession;
87
89
  /**
@@ -113,6 +113,8 @@ export class DAppConnector {
113
113
  this.walletConnectClient.on('session_event', this.handleSessionEvent.bind(this));
114
114
  this.walletConnectClient.on('session_update', this.handleSessionUpdate.bind(this));
115
115
  this.walletConnectClient.on('session_delete', this.handleSessionDelete.bind(this));
116
+ // Listen for custom session_delete events from DAppSigner
117
+ this.walletConnectClient.core.events.on('session_delete', this.handleSessionDelete.bind(this));
116
118
  this.walletConnectClient.core.pairing.events.on('pairing_delete', this.handlePairingDelete.bind(this));
117
119
  }
118
120
  catch (e) {
@@ -217,9 +219,10 @@ export class DAppConnector {
217
219
  }, pairingTopic, extension.availableInIframe ? undefined : extensionId);
218
220
  }
219
221
  /**
220
- * Validates the session by checking if the session exists.
222
+ * Validates the session by checking if the session exists and is valid.
223
+ * Also ensures the signer exists for the session.
221
224
  * @param topic - The topic of the session to validate.
222
- * @returns {boolean} - True if the session exists, false otherwise.
225
+ * @returns {boolean} - True if the session exists and has a valid signer, false otherwise.
223
226
  */
224
227
  validateSession(topic) {
225
228
  try {
@@ -227,12 +230,23 @@ export class DAppConnector {
227
230
  return false;
228
231
  }
229
232
  const session = this.walletConnectClient.session.get(topic);
233
+ const hasSigner = this.signers.some((signer) => signer.topic === topic);
230
234
  if (!session) {
235
+ // If session doesn't exist but we have a signer for it, clean up
236
+ if (hasSigner) {
237
+ this.logger.warn(`Signer exists but no session found for topic: ${topic}`);
238
+ this.handleSessionDelete({ topic });
239
+ }
240
+ return false;
241
+ }
242
+ if (!hasSigner) {
243
+ this.logger.warn(`Session exists but no signer found for topic: ${topic}`);
231
244
  return false;
232
245
  }
233
246
  return true;
234
247
  }
235
- catch (_a) {
248
+ catch (e) {
249
+ this.logger.error('Error validating session:', e);
236
250
  return false;
237
251
  }
238
252
  }
@@ -521,14 +535,24 @@ export class DAppConnector {
521
535
  }
522
536
  handleSessionDelete(event) {
523
537
  this.logger.info('Session deleted:', event);
524
- this.signers = this.signers.filter((signer) => signer.topic !== event.topic);
525
- try {
526
- this.disconnect(event.topic);
527
- }
528
- catch (e) {
529
- this.logger.error('Error disconnecting session:', e);
538
+ let deletedSigner = false;
539
+ this.signers = this.signers.filter((signer) => {
540
+ if (signer.topic !== event.topic) {
541
+ return true;
542
+ }
543
+ deletedSigner = true;
544
+ return false;
545
+ });
546
+ // prevent emitting disconnected event if signers is untouched.
547
+ if (deletedSigner) {
548
+ try {
549
+ this.disconnect(event.topic);
550
+ }
551
+ catch (e) {
552
+ this.logger.error('Error disconnecting session:', e);
553
+ }
554
+ this.logger.info('Session deleted and signer removed');
530
555
  }
531
- this.logger.info('Session deleted by wallet');
532
556
  }
533
557
  handlePairingDelete(event) {
534
558
  this.logger.info('Pairing deleted:', event);
@@ -4,11 +4,12 @@ export interface ILogger {
4
4
  info(message: string, ...args: any[]): void;
5
5
  debug(message: string, ...args: any[]): void;
6
6
  }
7
+ export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'off';
7
8
  export declare class DefaultLogger implements ILogger {
8
9
  private logLevel;
9
- constructor(logLevel?: 'error' | 'warn' | 'info' | 'debug');
10
- setLogLevel(level: 'error' | 'warn' | 'info' | 'debug'): void;
11
- getLogLevel(): 'error' | 'warn' | 'info' | 'debug';
10
+ constructor(logLevel?: LogLevel);
11
+ setLogLevel(level: LogLevel): void;
12
+ getLogLevel(): LogLevel;
12
13
  error(message: string, ...args: any[]): void;
13
14
  warn(message: string, ...args: any[]): void;
14
15
  info(message: string, ...args: any[]): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hashgraph/hedera-wallet-connect",
3
- "version": "1.3.8-canary.9350581.0",
3
+ "version": "1.3.8-canary.a321e2e.0",
4
4
  "description": "A library to facilitate integrating Hedera with WalletConnect",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  "concurrently": "^9.0.1",
29
29
  "esbuild": "^0.24.0",
30
30
  "esbuild-plugin-copy": "^2.1.1",
31
- "eslint-plugin-tsdoc": "^0.3.0",
31
+ "eslint-plugin-tsdoc": "^0.4.0",
32
32
  "husky": "^9.0.6",
33
33
  "jest": "^29.7.0",
34
34
  "lint-staged": "^15.1.0",