@hashgraph/hedera-wallet-connect 1.3.8-canary.9350581.0 → 1.3.8-canary.a98a883.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -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,10 @@ export class DAppSigner {
|
|
60
61
|
return allNodes.slice(0, numberOfNodes);
|
61
62
|
}
|
62
63
|
request(request) {
|
64
|
+
// Avoid a wallet call if the session is no longer valid
|
65
|
+
if (!this.signClient.session.get(this.topic)) {
|
66
|
+
throw new SessionNotFoundError('Session no longer exists. Please reconnect to the wallet.');
|
67
|
+
}
|
63
68
|
if (this.extensionId)
|
64
69
|
extensionOpen(this.extensionId);
|
65
70
|
return this.signClient.request({
|
@@ -164,6 +169,10 @@ export class DAppSigner {
|
|
164
169
|
}
|
165
170
|
async _tryExecuteTransactionRequest(request) {
|
166
171
|
try {
|
172
|
+
// Verify session is still valid before proceeding
|
173
|
+
if (!this.signClient.session.get(this.topic)) {
|
174
|
+
throw new Error('Session no longer exists. Please reconnect to the wallet.');
|
175
|
+
}
|
167
176
|
const requestToBytes = request.toBytes();
|
168
177
|
this.logger.debug('Creating transaction from bytes', requestToBytes, request);
|
169
178
|
const transaction = Transaction.fromBytes(requestToBytes);
|
@@ -179,7 +188,22 @@ export class DAppSigner {
|
|
179
188
|
return { result: TransactionResponse.fromJSON(result) };
|
180
189
|
}
|
181
190
|
catch (error) {
|
182
|
-
|
191
|
+
// Check if error is due to session being deleted
|
192
|
+
if (error instanceof SessionNotFoundError) {
|
193
|
+
this.logger.error('Session was deleted, removing signer');
|
194
|
+
// Notify DAppConnector to remove this signer
|
195
|
+
this.signClient.emit({
|
196
|
+
topic: this.topic,
|
197
|
+
event: {
|
198
|
+
name: 'session_delete',
|
199
|
+
data: { topic: this.topic },
|
200
|
+
},
|
201
|
+
chainId: ledgerIdToCAIPChainId(this.ledgerId),
|
202
|
+
});
|
203
|
+
}
|
204
|
+
else {
|
205
|
+
this.logger.error('Error executing transaction request:', error);
|
206
|
+
}
|
183
207
|
return { error };
|
184
208
|
}
|
185
209
|
}
|
@@ -255,6 +279,7 @@ export class DAppSigner {
|
|
255
279
|
return { result: this._parseQueryResponse(query, result.response) };
|
256
280
|
}
|
257
281
|
catch (error) {
|
282
|
+
this.logger.error('Error executing query request:', error);
|
258
283
|
return { error };
|
259
284
|
}
|
260
285
|
}
|
package/dist/lib/dapp/index.d.ts
CHANGED
@@ -79,9 +79,10 @@ export declare class DAppConnector {
|
|
79
79
|
*/
|
80
80
|
connectExtension(extensionId: string, pairingTopic?: string): Promise<SessionTypes.Struct>;
|
81
81
|
/**
|
82
|
-
* Validates the session by checking if the session exists.
|
82
|
+
* Validates the session by checking if the session exists and is valid.
|
83
|
+
* Also ensures the signer exists for the session.
|
83
84
|
* @param topic - The topic of the session to validate.
|
84
|
-
* @returns {boolean} - True if the session exists, false otherwise.
|
85
|
+
* @returns {boolean} - True if the session exists and has a valid signer, false otherwise.
|
85
86
|
*/
|
86
87
|
private validateSession;
|
87
88
|
/**
|
package/dist/lib/dapp/index.js
CHANGED
@@ -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 {
|
@@ -228,11 +231,23 @@ export class DAppConnector {
|
|
228
231
|
}
|
229
232
|
const session = this.walletConnectClient.session.get(topic);
|
230
233
|
if (!session) {
|
234
|
+
// If session doesn't exist but we have a signer for it, clean up
|
235
|
+
const hasSigner = this.signers.some((signer) => signer.topic === topic);
|
236
|
+
if (hasSigner) {
|
237
|
+
this.handleSessionDelete({ topic });
|
238
|
+
}
|
239
|
+
return false;
|
240
|
+
}
|
241
|
+
// Verify we have a signer for this session
|
242
|
+
const hasSigner = this.signers.some((signer) => signer.topic === topic);
|
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
|
}
|
@@ -521,14 +536,24 @@ export class DAppConnector {
|
|
521
536
|
}
|
522
537
|
handleSessionDelete(event) {
|
523
538
|
this.logger.info('Session deleted:', event);
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
539
|
+
let deletedSigner = false;
|
540
|
+
this.signers = this.signers.filter((signer) => {
|
541
|
+
if (signer.topic !== event.topic) {
|
542
|
+
return true;
|
543
|
+
}
|
544
|
+
deletedSigner = true;
|
545
|
+
return false;
|
546
|
+
});
|
547
|
+
// prevent emitting disconnected event if signers is untouched.
|
548
|
+
if (deletedSigner) {
|
549
|
+
try {
|
550
|
+
this.disconnect(event.topic);
|
551
|
+
}
|
552
|
+
catch (e) {
|
553
|
+
this.logger.error('Error disconnecting session:', e);
|
554
|
+
}
|
555
|
+
this.logger.info('Session deleted and signer removed');
|
530
556
|
}
|
531
|
-
this.logger.info('Session deleted by wallet');
|
532
557
|
}
|
533
558
|
handlePairingDelete(event) {
|
534
559
|
this.logger.info('Pairing deleted:', event);
|
package/package.json
CHANGED