@hashgraph/hedera-wallet-connect 1.4.2-canary.dd8e029.0 → 1.4.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/dist/lib/dapp/DAppSigner.d.ts +3 -2
- package/dist/lib/dapp/DAppSigner.js +17 -0
- package/dist/lib/dapp/SessionNotFoundError.d.ts +3 -0
- package/dist/lib/dapp/SessionNotFoundError.js +6 -0
- package/dist/lib/dapp/index.d.ts +7 -4
- package/dist/lib/dapp/index.js +35 -10
- package/dist/lib/shared/logger.d.ts +4 -3
- package/package.json +1 -1
@@ -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?:
|
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:
|
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,21 @@ 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, signer will be removed. Please reconnect to the wallet.');
|
68
|
+
// Notify DAppConnector to remove this signer
|
69
|
+
this.signClient.emit({
|
70
|
+
topic: this.topic,
|
71
|
+
event: {
|
72
|
+
name: 'session_delete',
|
73
|
+
data: { topic: this.topic },
|
74
|
+
},
|
75
|
+
chainId: ledgerIdToCAIPChainId(this.ledgerId),
|
76
|
+
});
|
77
|
+
throw new SessionNotFoundError('Session no longer exists. Please reconnect to the wallet.');
|
78
|
+
}
|
63
79
|
if (this.extensionId)
|
64
80
|
extensionOpen(this.extensionId);
|
65
81
|
return this.signClient.request({
|
@@ -255,6 +271,7 @@ export class DAppSigner {
|
|
255
271
|
return { result: this._parseQueryResponse(query, result.response) };
|
256
272
|
}
|
257
273
|
catch (error) {
|
274
|
+
this.logger.error('Error executing query request:', error);
|
258
275
|
return { error };
|
259
276
|
}
|
260
277
|
}
|
package/dist/lib/dapp/index.d.ts
CHANGED
@@ -2,9 +2,11 @@ import { AccountId, LedgerId, Transaction } 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';
|
9
|
+
export { SessionNotFoundError } from './SessionNotFoundError';
|
8
10
|
type BaseLogger = 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'fatal';
|
9
11
|
export declare class DAppConnector {
|
10
12
|
private logger;
|
@@ -30,12 +32,12 @@ export declare class DAppConnector {
|
|
30
32
|
* @param chains - Array of supported chains for the DApp (optional).
|
31
33
|
* @param logLevel - Logging level for the DAppConnector (optional).
|
32
34
|
*/
|
33
|
-
constructor(metadata: SignClientTypes.Metadata, network: LedgerId, projectId: string, methods?: string[], events?: string[], chains?: string[], logLevel?:
|
35
|
+
constructor(metadata: SignClientTypes.Metadata, network: LedgerId, projectId: string, methods?: string[], events?: string[], chains?: string[], logLevel?: LogLevel);
|
34
36
|
/**
|
35
37
|
* Sets the logging level for the DAppConnector
|
36
38
|
* @param level - The logging level to set
|
37
39
|
*/
|
38
|
-
setLogLevel(level:
|
40
|
+
setLogLevel(level: LogLevel): void;
|
39
41
|
/**
|
40
42
|
* Initializes the DAppConnector instance.
|
41
43
|
* @param logger - `BaseLogger` for logging purposes (optional).
|
@@ -79,9 +81,10 @@ export declare class DAppConnector {
|
|
79
81
|
*/
|
80
82
|
connectExtension(extensionId: string, pairingTopic?: string): Promise<SessionTypes.Struct>;
|
81
83
|
/**
|
82
|
-
* Validates the session by checking if the session exists.
|
84
|
+
* Validates the session by checking if the session exists and is valid.
|
85
|
+
* Also ensures the signer exists for the session.
|
83
86
|
* @param topic - The topic of the session to validate.
|
84
|
-
* @returns {boolean} - True if the session exists, false otherwise.
|
87
|
+
* @returns {boolean} - True if the session exists and has a valid signer, false otherwise.
|
85
88
|
*/
|
86
89
|
private validateSession;
|
87
90
|
/**
|
package/dist/lib/dapp/index.js
CHANGED
@@ -26,6 +26,7 @@ import { DefaultLogger } from '../shared/logger';
|
|
26
26
|
import { HederaJsonRpcMethod, accountAndLedgerFromSession, networkNamespaces, extensionConnect, findExtensions, } from '../shared';
|
27
27
|
import { DAppSigner } from './DAppSigner';
|
28
28
|
export * from './DAppSigner';
|
29
|
+
export { SessionNotFoundError } from './SessionNotFoundError';
|
29
30
|
export class DAppConnector {
|
30
31
|
/**
|
31
32
|
* Initializes the DAppConnector instance.
|
@@ -113,6 +114,8 @@ export class DAppConnector {
|
|
113
114
|
this.walletConnectClient.on('session_event', this.handleSessionEvent.bind(this));
|
114
115
|
this.walletConnectClient.on('session_update', this.handleSessionUpdate.bind(this));
|
115
116
|
this.walletConnectClient.on('session_delete', this.handleSessionDelete.bind(this));
|
117
|
+
// Listen for custom session_delete events from DAppSigner
|
118
|
+
this.walletConnectClient.core.events.on('session_delete', this.handleSessionDelete.bind(this));
|
116
119
|
this.walletConnectClient.core.pairing.events.on('pairing_delete', this.handlePairingDelete.bind(this));
|
117
120
|
}
|
118
121
|
catch (e) {
|
@@ -217,9 +220,10 @@ export class DAppConnector {
|
|
217
220
|
}, pairingTopic, extension.availableInIframe ? undefined : extensionId);
|
218
221
|
}
|
219
222
|
/**
|
220
|
-
* Validates the session by checking if the session exists.
|
223
|
+
* Validates the session by checking if the session exists and is valid.
|
224
|
+
* Also ensures the signer exists for the session.
|
221
225
|
* @param topic - The topic of the session to validate.
|
222
|
-
* @returns {boolean} - True if the session exists, false otherwise.
|
226
|
+
* @returns {boolean} - True if the session exists and has a valid signer, false otherwise.
|
223
227
|
*/
|
224
228
|
validateSession(topic) {
|
225
229
|
try {
|
@@ -227,12 +231,23 @@ export class DAppConnector {
|
|
227
231
|
return false;
|
228
232
|
}
|
229
233
|
const session = this.walletConnectClient.session.get(topic);
|
234
|
+
const hasSigner = this.signers.some((signer) => signer.topic === topic);
|
230
235
|
if (!session) {
|
236
|
+
// If session doesn't exist but we have a signer for it, clean up
|
237
|
+
if (hasSigner) {
|
238
|
+
this.logger.warn(`Signer exists but no session found for topic: ${topic}`);
|
239
|
+
this.handleSessionDelete({ topic });
|
240
|
+
}
|
241
|
+
return false;
|
242
|
+
}
|
243
|
+
if (!hasSigner) {
|
244
|
+
this.logger.warn(`Session exists but no signer found for topic: ${topic}`);
|
231
245
|
return false;
|
232
246
|
}
|
233
247
|
return true;
|
234
248
|
}
|
235
|
-
catch (
|
249
|
+
catch (e) {
|
250
|
+
this.logger.error('Error validating session:', e);
|
236
251
|
return false;
|
237
252
|
}
|
238
253
|
}
|
@@ -536,14 +551,24 @@ export class DAppConnector {
|
|
536
551
|
}
|
537
552
|
handleSessionDelete(event) {
|
538
553
|
this.logger.info('Session deleted:', event);
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
554
|
+
let deletedSigner = false;
|
555
|
+
this.signers = this.signers.filter((signer) => {
|
556
|
+
if (signer.topic !== event.topic) {
|
557
|
+
return true;
|
558
|
+
}
|
559
|
+
deletedSigner = true;
|
560
|
+
return false;
|
561
|
+
});
|
562
|
+
// prevent emitting disconnected event if signers is untouched.
|
563
|
+
if (deletedSigner) {
|
564
|
+
try {
|
565
|
+
this.disconnect(event.topic);
|
566
|
+
}
|
567
|
+
catch (e) {
|
568
|
+
this.logger.error('Error disconnecting session:', e);
|
569
|
+
}
|
570
|
+
this.logger.info('Session deleted and signer removed');
|
545
571
|
}
|
546
|
-
this.logger.info('Session deleted by wallet');
|
547
572
|
}
|
548
573
|
handlePairingDelete(event) {
|
549
574
|
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?:
|
10
|
-
setLogLevel(level:
|
11
|
-
getLogLevel():
|
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;
|