@hashgraph/hedera-wallet-connect 1.3.8-canary.7e674ba.0 → 1.3.8-canary.a98a883.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/lib/dapp/DAppSigner.d.ts +3 -1
- package/dist/lib/dapp/DAppSigner.js +35 -8
- package/dist/lib/dapp/SessionNotFoundError.d.ts +3 -0
- package/dist/lib/dapp/SessionNotFoundError.js +6 -0
- package/dist/lib/dapp/index.d.ts +3 -2
- package/dist/lib/dapp/index.js +35 -10
- package/dist/lib/shared/utils.d.ts +6 -0
- package/dist/lib/shared/utils.js +8 -0
- package/package.json +2 -1
@@ -31,7 +31,9 @@ export declare class DAppSigner implements Signer {
|
|
31
31
|
getAccountInfo(): Promise<AccountInfo>;
|
32
32
|
getAccountRecords(): Promise<TransactionRecord[]>;
|
33
33
|
getMetadata(): CoreTypes.Metadata;
|
34
|
-
sign(data: Uint8Array[], signOptions?:
|
34
|
+
sign(data: Uint8Array[], signOptions?: {
|
35
|
+
encoding?: 'utf-8' | 'base64';
|
36
|
+
}): Promise<SignerSignature[]>;
|
35
37
|
checkTransaction<T extends Transaction>(transaction: T): Promise<T>;
|
36
38
|
populateTransaction<T extends Transaction>(transaction: T): Promise<T>;
|
37
39
|
/**
|
@@ -19,8 +19,9 @@
|
|
19
19
|
*/
|
20
20
|
import { AccountBalance, AccountId, AccountInfo, LedgerId, SignerSignature, Transaction, TransactionRecord, Client, PublicKey, TransactionId, TransactionResponse, Query, AccountRecordsQuery, AccountInfoQuery, AccountBalanceQuery, TransactionReceiptQuery, TransactionReceipt, TransactionRecordQuery, } from '@hashgraph/sdk';
|
21
21
|
import { proto } from '@hashgraph/proto';
|
22
|
-
import { HederaJsonRpcMethod,
|
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({
|
@@ -95,14 +100,18 @@ export class DAppSigner {
|
|
95
100
|
getMetadata() {
|
96
101
|
return this.signClient.metadata;
|
97
102
|
}
|
98
|
-
async sign(data, signOptions
|
99
|
-
|
103
|
+
async sign(data, signOptions = {
|
104
|
+
encoding: 'utf-8',
|
105
|
+
}) {
|
100
106
|
try {
|
107
|
+
const messageToSign = signOptions.encoding === 'base64'
|
108
|
+
? Uint8ArrayToBase64String(data[0])
|
109
|
+
: Uint8ArrayToString(data[0]);
|
101
110
|
const { signatureMap } = await this.request({
|
102
111
|
method: HederaJsonRpcMethod.SignMessage,
|
103
112
|
params: {
|
104
113
|
signerAccountId: this._signerAccountId,
|
105
|
-
message:
|
114
|
+
message: messageToSign,
|
106
115
|
},
|
107
116
|
});
|
108
117
|
const sigmap = base64StringToSignatureMap(signatureMap);
|
@@ -160,6 +169,10 @@ export class DAppSigner {
|
|
160
169
|
}
|
161
170
|
async _tryExecuteTransactionRequest(request) {
|
162
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
|
+
}
|
163
176
|
const requestToBytes = request.toBytes();
|
164
177
|
this.logger.debug('Creating transaction from bytes', requestToBytes, request);
|
165
178
|
const transaction = Transaction.fromBytes(requestToBytes);
|
@@ -175,7 +188,22 @@ export class DAppSigner {
|
|
175
188
|
return { result: TransactionResponse.fromJSON(result) };
|
176
189
|
}
|
177
190
|
catch (error) {
|
178
|
-
|
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
|
+
}
|
179
207
|
return { error };
|
180
208
|
}
|
181
209
|
}
|
@@ -230,9 +258,7 @@ export class DAppSigner {
|
|
230
258
|
if (!(result === null || result === void 0 ? void 0 : result.error)) {
|
231
259
|
return { result: result.result };
|
232
260
|
}
|
233
|
-
|
234
|
-
this.logger.error('Error executing free receipt query. Sending to wallet.', result.error);
|
235
|
-
}
|
261
|
+
this.logger.error('Error executing free receipt query. Sending to wallet.', result.error);
|
236
262
|
}
|
237
263
|
/**
|
238
264
|
* Note, should we be converting these to specific query types?
|
@@ -253,6 +279,7 @@ export class DAppSigner {
|
|
253
279
|
return { result: this._parseQueryResponse(query, result.response) };
|
254
280
|
}
|
255
281
|
catch (error) {
|
282
|
+
this.logger.error('Error executing query request:', error);
|
256
283
|
return { error };
|
257
284
|
}
|
258
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);
|
@@ -85,6 +85,12 @@ export declare function base64StringToSignatureMap(base64string: string): proto.
|
|
85
85
|
* @returns Base64-encoded string representation of the input `Uint8Array`
|
86
86
|
*/
|
87
87
|
export declare function Uint8ArrayToBase64String(binary: Uint8Array): string;
|
88
|
+
/**
|
89
|
+
* Encodes the binary data represented by the `Uint8Array` to a UTF-8 string.
|
90
|
+
* @param binary - The `Uint8Array` containing binary data to be converted
|
91
|
+
* @returns UTF-8 string representation of the input `Uint8Array`
|
92
|
+
*/
|
93
|
+
export declare function Uint8ArrayToString(binary: Uint8Array): string;
|
88
94
|
/**
|
89
95
|
* Converts a Base64-encoded string to a `Uint8Array`.
|
90
96
|
* @param base64string - Base64-encoded string to be converted
|
package/dist/lib/shared/utils.js
CHANGED
@@ -139,6 +139,14 @@ export function base64StringToSignatureMap(base64string) {
|
|
139
139
|
export function Uint8ArrayToBase64String(binary) {
|
140
140
|
return Buffer.from(binary).toString('base64');
|
141
141
|
}
|
142
|
+
/**
|
143
|
+
* Encodes the binary data represented by the `Uint8Array` to a UTF-8 string.
|
144
|
+
* @param binary - The `Uint8Array` containing binary data to be converted
|
145
|
+
* @returns UTF-8 string representation of the input `Uint8Array`
|
146
|
+
*/
|
147
|
+
export function Uint8ArrayToString(binary) {
|
148
|
+
return Buffer.from(binary).toString('utf-8');
|
149
|
+
}
|
142
150
|
/**
|
143
151
|
* Converts a Base64-encoded string to a `Uint8Array`.
|
144
152
|
* @param base64string - Base64-encoded string to be converted
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hashgraph/hedera-wallet-connect",
|
3
|
-
"version": "1.3.8-canary.
|
3
|
+
"version": "1.3.8-canary.a98a883.0",
|
4
4
|
"description": "A library to facilitate integrating Hedera with WalletConnect",
|
5
5
|
"repository": {
|
6
6
|
"type": "git",
|
@@ -55,6 +55,7 @@
|
|
55
55
|
"dev:ts-demo": "rimraf dist && npm run build && concurrently --raw \"npm run watch\" \"node scripts/demos/typescript/dev.mjs\"",
|
56
56
|
"dev:react-demo": "rimraf dist && npm run build && concurrently --raw \"npm run watch\" \"node scripts/demos/react/dev.mjs\"",
|
57
57
|
"test": "jest",
|
58
|
+
"test:watch": "jest --watch",
|
58
59
|
"test:connect": "jest --testMatch '**/DAppConnector.test.ts' --verbose",
|
59
60
|
"test:signer": "jest --testMatch '**/DAppSigner.test.ts' --verbose",
|
60
61
|
"prepublishOnly": "rm -Rf dist && npm run build",
|