@hashgraph/hedera-wallet-connect 1.3.7-canary.d60d7af.0 → 1.3.8-canary.1b49a36.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. package/dist/index.d.ts +1 -3
  2. package/dist/index.js +1 -3
  3. package/dist/{dapp → lib/dapp}/DAppSigner.d.ts +18 -2
  4. package/dist/{dapp → lib/dapp}/DAppSigner.js +109 -30
  5. package/dist/{dapp → lib/dapp}/index.js +1 -1
  6. package/dist/lib/index.d.ts +3 -0
  7. package/dist/lib/index.js +22 -0
  8. package/dist/{shared → lib/shared}/logger.d.ts +1 -0
  9. package/dist/{shared → lib/shared}/logger.js +3 -0
  10. package/dist/{shared → lib/shared}/utils.d.ts +8 -2
  11. package/dist/{shared → lib/shared}/utils.js +10 -2
  12. package/package.json +4 -3
  13. /package/dist/{dapp → lib/dapp}/index.d.ts +0 -0
  14. /package/dist/{shared → lib/shared}/chainIds.d.ts +0 -0
  15. /package/dist/{shared → lib/shared}/chainIds.js +0 -0
  16. /package/dist/{shared → lib/shared}/errors.d.ts +0 -0
  17. /package/dist/{shared → lib/shared}/errors.js +0 -0
  18. /package/dist/{shared → lib/shared}/events.d.ts +0 -0
  19. /package/dist/{shared → lib/shared}/events.js +0 -0
  20. /package/dist/{shared → lib/shared}/extensionController.d.ts +0 -0
  21. /package/dist/{shared → lib/shared}/extensionController.js +0 -0
  22. /package/dist/{shared → lib/shared}/index.d.ts +0 -0
  23. /package/dist/{shared → lib/shared}/index.js +0 -0
  24. /package/dist/{shared → lib/shared}/methods.d.ts +0 -0
  25. /package/dist/{shared → lib/shared}/methods.js +0 -0
  26. /package/dist/{shared → lib/shared}/payloads.d.ts +0 -0
  27. /package/dist/{shared → lib/shared}/payloads.js +0 -0
  28. /package/dist/{wallet → lib/wallet}/index.d.ts +0 -0
  29. /package/dist/{wallet → lib/wallet}/index.js +0 -0
  30. /package/dist/{wallet → lib/wallet}/provider.d.ts +0 -0
  31. /package/dist/{wallet → lib/wallet}/provider.js +0 -0
  32. /package/dist/{wallet → lib/wallet}/types.d.ts +0 -0
  33. /package/dist/{wallet → lib/wallet}/types.js +0 -0
package/dist/index.d.ts CHANGED
@@ -1,3 +1 @@
1
- export * from './shared';
2
- export { default as Wallet } from './wallet';
3
- export * from './dapp';
1
+ export * from './lib';
package/dist/index.js CHANGED
@@ -17,6 +17,4 @@
17
17
  * limitations under the License.
18
18
  *
19
19
  */
20
- export * from './shared';
21
- export { default as Wallet } from './wallet';
22
- export * from './dapp';
20
+ export * from './lib';
@@ -6,7 +6,13 @@ export declare class DAppSigner implements Signer {
6
6
  readonly topic: string;
7
7
  private readonly ledgerId;
8
8
  readonly extensionId?: string | undefined;
9
- constructor(accountId: AccountId, signClient: ISignClient, topic: string, ledgerId?: LedgerId, extensionId?: string | undefined);
9
+ private logger;
10
+ constructor(accountId: AccountId, signClient: ISignClient, topic: string, ledgerId?: LedgerId, extensionId?: string | undefined, logLevel?: 'error' | 'warn' | 'info' | 'debug');
11
+ /**
12
+ * Sets the logging level for the DAppSigner
13
+ * @param level - The logging level to set
14
+ */
15
+ setLogLevel(level: 'error' | 'warn' | 'info' | 'debug'): void;
10
16
  private _getHederaClient;
11
17
  private get _signerAccountId();
12
18
  private _getRandomNodes;
@@ -25,7 +31,9 @@ export declare class DAppSigner implements Signer {
25
31
  getAccountInfo(): Promise<AccountInfo>;
26
32
  getAccountRecords(): Promise<TransactionRecord[]>;
27
33
  getMetadata(): CoreTypes.Metadata;
28
- sign(data: Uint8Array[], signOptions?: Record<string, any>): Promise<SignerSignature[]>;
34
+ sign(data: Uint8Array[], signOptions?: {
35
+ encoding?: 'utf-8' | 'base64';
36
+ }): Promise<SignerSignature[]>;
29
37
  checkTransaction<T extends Transaction>(transaction: T): Promise<T>;
30
38
  populateTransaction<T extends Transaction>(transaction: T): Promise<T>;
31
39
  /**
@@ -39,6 +47,14 @@ export declare class DAppSigner implements Signer {
39
47
  signTransaction<T extends Transaction>(transaction: T): Promise<T>;
40
48
  private _tryExecuteTransactionRequest;
41
49
  private _parseQueryResponse;
50
+ /**
51
+ * Executes a free receipt query without signing a transaction.
52
+ * Enables the DApp to fetch the receipt of a transaction without making a new request
53
+ * to the wallet.
54
+ * @param request - The query to execute
55
+ * @returns The result of the query
56
+ */
57
+ private executeReceiptQueryFromRequest;
42
58
  private _tryExecuteQueryRequest;
43
59
  call<RequestT, ResponseT, OutputT>(request: Executable<RequestT, ResponseT, OutputT>): Promise<OutputT>;
44
60
  }
@@ -19,15 +19,26 @@
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, Uint8ArrayToBase64String, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, } from '../shared';
22
+ import { HederaJsonRpcMethod, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, Uint8ArrayToBase64String, Uint8ArrayToString, } from '../shared';
23
+ import { DefaultLogger } from '../shared/logger';
23
24
  const clients = {};
24
25
  export class DAppSigner {
25
- constructor(accountId, signClient, topic, ledgerId = LedgerId.MAINNET, extensionId) {
26
+ constructor(accountId, signClient, topic, ledgerId = LedgerId.MAINNET, extensionId, logLevel = 'debug') {
26
27
  this.accountId = accountId;
27
28
  this.signClient = signClient;
28
29
  this.topic = topic;
29
30
  this.ledgerId = ledgerId;
30
31
  this.extensionId = extensionId;
32
+ this.logger = new DefaultLogger(logLevel);
33
+ }
34
+ /**
35
+ * Sets the logging level for the DAppSigner
36
+ * @param level - The logging level to set
37
+ */
38
+ setLogLevel(level) {
39
+ if (this.logger instanceof DefaultLogger) {
40
+ this.logger.setLogLevel(level);
41
+ }
31
42
  }
32
43
  _getHederaClient() {
33
44
  const ledgerIdString = this.ledgerId.toString();
@@ -84,22 +95,34 @@ export class DAppSigner {
84
95
  getMetadata() {
85
96
  return this.signClient.metadata;
86
97
  }
87
- async sign(data, signOptions) {
88
- const { signatureMap } = await this.request({
89
- method: HederaJsonRpcMethod.SignMessage,
90
- params: {
91
- signerAccountId: this._signerAccountId,
92
- message: Uint8ArrayToBase64String(data[0]),
93
- },
94
- });
95
- const sigmap = base64StringToSignatureMap(signatureMap);
96
- const signerSignature = new SignerSignature({
97
- accountId: this.getAccountId(),
98
- publicKey: PublicKey.fromBytes(sigmap.sigPair[0].pubKeyPrefix),
99
- signature: sigmap.sigPair[0].ed25519 ||
100
- sigmap.sigPair[0].ECDSASecp256k1,
101
- });
102
- return [signerSignature];
98
+ async sign(data, signOptions = {
99
+ encoding: 'utf-8',
100
+ }) {
101
+ try {
102
+ const messageToSign = signOptions.encoding === 'base64'
103
+ ? Uint8ArrayToBase64String(data[0])
104
+ : Uint8ArrayToString(data[0]);
105
+ const { signatureMap } = await this.request({
106
+ method: HederaJsonRpcMethod.SignMessage,
107
+ params: {
108
+ signerAccountId: this._signerAccountId,
109
+ message: messageToSign,
110
+ },
111
+ });
112
+ const sigmap = base64StringToSignatureMap(signatureMap);
113
+ const signerSignature = new SignerSignature({
114
+ accountId: this.getAccountId(),
115
+ publicKey: PublicKey.fromBytes(sigmap.sigPair[0].pubKeyPrefix),
116
+ signature: sigmap.sigPair[0].ed25519 ||
117
+ sigmap.sigPair[0].ECDSASecp256k1,
118
+ });
119
+ this.logger.debug('Data signed successfully');
120
+ return [signerSignature];
121
+ }
122
+ catch (error) {
123
+ this.logger.error('Error signing data:', error);
124
+ throw error;
125
+ }
103
126
  }
104
127
  async checkTransaction(transaction) {
105
128
  throw new Error('Method not implemented.');
@@ -141,7 +164,10 @@ export class DAppSigner {
141
164
  }
142
165
  async _tryExecuteTransactionRequest(request) {
143
166
  try {
144
- const transaction = Transaction.fromBytes(request.toBytes());
167
+ const requestToBytes = request.toBytes();
168
+ this.logger.debug('Creating transaction from bytes', requestToBytes, request);
169
+ const transaction = Transaction.fromBytes(requestToBytes);
170
+ this.logger.debug('Executing transaction request', transaction);
145
171
  const result = await this.request({
146
172
  method: HederaJsonRpcMethod.SignAndExecuteTransaction,
147
173
  params: {
@@ -149,9 +175,11 @@ export class DAppSigner {
149
175
  transactionList: transactionToBase64String(transaction),
150
176
  },
151
177
  });
178
+ this.logger.debug('Transaction request completed successfully');
152
179
  return { result: TransactionResponse.fromJSON(result) };
153
180
  }
154
181
  catch (error) {
182
+ this.logger.error('Error executing transaction request:', error);
155
183
  return { error };
156
184
  }
157
185
  }
@@ -178,9 +206,44 @@ export class DAppSigner {
178
206
  throw new Error('Unsupported query type');
179
207
  }
180
208
  }
209
+ /**
210
+ * Executes a free receipt query without signing a transaction.
211
+ * Enables the DApp to fetch the receipt of a transaction without making a new request
212
+ * to the wallet.
213
+ * @param request - The query to execute
214
+ * @returns The result of the query
215
+ */
216
+ async executeReceiptQueryFromRequest(request) {
217
+ try {
218
+ const isMainnet = this.ledgerId === LedgerId.MAINNET;
219
+ const client = isMainnet ? Client.forMainnet() : Client.forTestnet();
220
+ const receipt = TransactionReceiptQuery.fromBytes(request.toBytes());
221
+ const result = await receipt.execute(client);
222
+ return { result };
223
+ }
224
+ catch (error) {
225
+ return { error };
226
+ }
227
+ }
181
228
  async _tryExecuteQueryRequest(request) {
182
229
  try {
183
- const query = Query.fromBytes(request.toBytes());
230
+ const isReceiptQuery = request instanceof TransactionReceiptQuery;
231
+ if (isReceiptQuery) {
232
+ this.logger.debug('Attempting to execute free receipt query', request);
233
+ const result = await this.executeReceiptQueryFromRequest(request);
234
+ if (!(result === null || result === void 0 ? void 0 : result.error)) {
235
+ return { result: result.result };
236
+ }
237
+ this.logger.error('Error executing free receipt query. Sending to wallet.', result.error);
238
+ }
239
+ /**
240
+ * Note, should we be converting these to specific query types?
241
+ * Left alone to avoid changing the API for other requests.
242
+ */
243
+ const query = isReceiptQuery
244
+ ? TransactionReceiptQuery.fromBytes(request.toBytes())
245
+ : Query.fromBytes(request.toBytes());
246
+ this.logger.debug('Executing query request', query, queryToBase64String(query), isReceiptQuery);
184
247
  const result = await this.request({
185
248
  method: HederaJsonRpcMethod.SignAndExecuteQuery,
186
249
  params: {
@@ -188,6 +251,7 @@ export class DAppSigner {
188
251
  query: queryToBase64String(query),
189
252
  },
190
253
  });
254
+ this.logger.debug('Query request completed successfully', result);
191
255
  return { result: this._parseQueryResponse(query, result.response) };
192
256
  }
193
257
  catch (error) {
@@ -195,27 +259,42 @@ export class DAppSigner {
195
259
  }
196
260
  }
197
261
  async call(request) {
198
- var _a, _b, _c, _d, _e, _f;
199
- const txResult = await this._tryExecuteTransactionRequest(request);
200
- if (txResult.result) {
201
- return txResult.result;
262
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
263
+ const isReceiptQuery = request instanceof TransactionReceiptQuery;
264
+ let txResult = undefined;
265
+ // a receipt query is a free query and we should not execute a transaction.
266
+ if (!isReceiptQuery) {
267
+ txResult = await this._tryExecuteTransactionRequest(request);
268
+ if (txResult.result) {
269
+ return txResult.result;
270
+ }
202
271
  }
203
272
  const queryResult = await this._tryExecuteQueryRequest(request);
204
273
  if (queryResult.result) {
205
274
  return queryResult.result;
206
275
  }
207
276
  // TODO: make this error more usable
277
+ if (isReceiptQuery) {
278
+ throw new Error('Error executing receipt query: \n' +
279
+ JSON.stringify({
280
+ queryError: {
281
+ name: (_a = queryResult.error) === null || _a === void 0 ? void 0 : _a.name,
282
+ message: (_b = queryResult.error) === null || _b === void 0 ? void 0 : _b.message,
283
+ stack: (_c = queryResult.error) === null || _c === void 0 ? void 0 : _c.stack,
284
+ },
285
+ }));
286
+ }
208
287
  throw new Error('Error executing transaction or query: \n' +
209
288
  JSON.stringify({
210
289
  txError: {
211
- name: (_a = txResult.error) === null || _a === void 0 ? void 0 : _a.name,
212
- message: (_b = txResult.error) === null || _b === void 0 ? void 0 : _b.message,
213
- stack: (_c = txResult.error) === null || _c === void 0 ? void 0 : _c.stack,
290
+ name: (_d = txResult === null || txResult === void 0 ? void 0 : txResult.error) === null || _d === void 0 ? void 0 : _d.name,
291
+ message: (_e = txResult === null || txResult === void 0 ? void 0 : txResult.error) === null || _e === void 0 ? void 0 : _e.message,
292
+ stack: (_f = txResult === null || txResult === void 0 ? void 0 : txResult.error) === null || _f === void 0 ? void 0 : _f.stack,
214
293
  },
215
294
  queryError: {
216
- name: (_d = queryResult.error) === null || _d === void 0 ? void 0 : _d.name,
217
- message: (_e = queryResult.error) === null || _e === void 0 ? void 0 : _e.message,
218
- stack: (_f = queryResult.error) === null || _f === void 0 ? void 0 : _f.stack,
295
+ name: (_g = queryResult.error) === null || _g === void 0 ? void 0 : _g.name,
296
+ message: (_h = queryResult.error) === null || _h === void 0 ? void 0 : _h.message,
297
+ stack: (_j = queryResult.error) === null || _j === void 0 ? void 0 : _j.stack,
219
298
  },
220
299
  }, null, 2));
221
300
  }
@@ -309,7 +309,7 @@ export class DAppConnector {
309
309
  const allNamespaceAccounts = accountAndLedgerFromSession(session);
310
310
  return allNamespaceAccounts.map(({ account, network }) => {
311
311
  var _a;
312
- return new DAppSigner(account, this.walletConnectClient, session.topic, network, (_a = session.sessionProperties) === null || _a === void 0 ? void 0 : _a.extensionId);
312
+ return new DAppSigner(account, this.walletConnectClient, session.topic, network, (_a = session.sessionProperties) === null || _a === void 0 ? void 0 : _a.extensionId, this.logger instanceof DefaultLogger ? this.logger.getLogLevel() : 'debug');
313
313
  });
314
314
  }
315
315
  async onSessionConnected(session) {
@@ -0,0 +1,3 @@
1
+ export * from './shared';
2
+ export { default as Wallet } from './wallet';
3
+ export * from './dapp';
@@ -0,0 +1,22 @@
1
+ /*
2
+ *
3
+ * Hedera Wallet Connect
4
+ *
5
+ * Copyright (C) 2023 Hedera Hashgraph, LLC
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ *
19
+ */
20
+ export * from './shared';
21
+ export { default as Wallet } from './wallet';
22
+ export * from './dapp';
@@ -8,6 +8,7 @@ export declare class DefaultLogger implements ILogger {
8
8
  private logLevel;
9
9
  constructor(logLevel?: 'error' | 'warn' | 'info' | 'debug');
10
10
  setLogLevel(level: 'error' | 'warn' | 'info' | 'debug'): void;
11
+ getLogLevel(): 'error' | 'warn' | 'info' | 'debug';
11
12
  error(message: string, ...args: any[]): void;
12
13
  warn(message: string, ...args: any[]): void;
13
14
  info(message: string, ...args: any[]): void;
@@ -6,6 +6,9 @@ export class DefaultLogger {
6
6
  setLogLevel(level) {
7
7
  this.logLevel = level;
8
8
  }
9
+ getLogLevel() {
10
+ return this.logLevel;
11
+ }
9
12
  error(message, ...args) {
10
13
  if (['error', 'warn', 'info', 'debug'].includes(this.logLevel)) {
11
14
  console.error(`[ERROR] ${message}`, ...args);
@@ -49,8 +49,8 @@ export declare function base64StringToTransaction<T extends Transaction>(transac
49
49
  * @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
50
50
  * @returns `string`
51
51
  * */
52
- export declare function transactionToTransactionBody<T extends Transaction>(transaction: T, nodeAccountId: AccountId): Uint8Array | null | undefined;
53
- export declare function transactionBodyToBase64String(transactionBody: Uint8Array): string;
52
+ export declare function transactionToTransactionBody<T extends Transaction>(transaction: T, nodeAccountId: AccountId): any;
53
+ export declare function transactionBodyToBase64String(transactionBody: proto.ITransactionBody): string;
54
54
  /**
55
55
  * @param transactionList - a proto.TransactionList object
56
56
  * @returns `string`
@@ -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
@@ -86,10 +86,10 @@ export function base64StringToTransaction(transactionBytes) {
86
86
  export function transactionToTransactionBody(transaction, nodeAccountId) {
87
87
  // This is a private function, though provides the capabilities to construct a proto.TransactionBody
88
88
  //@ts-ignore
89
- return transaction._signedTransactions.current.bodyBytes;
89
+ return transaction._makeTransactionBody(nodeAccountId);
90
90
  }
91
91
  export function transactionBodyToBase64String(transactionBody) {
92
- return Uint8ArrayToBase64String(transactionBody);
92
+ return Uint8ArrayToBase64String(proto.TransactionBody.encode(transactionBody).finish());
93
93
  }
94
94
  /**
95
95
  * @param transactionList - a proto.TransactionList object
@@ -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,13 +1,13 @@
1
1
  {
2
2
  "name": "@hashgraph/hedera-wallet-connect",
3
- "version": "1.3.7-canary.d60d7af.0",
3
+ "version": "1.3.8-canary.1b49a36.0",
4
4
  "description": "A library to facilitate integrating Hedera with WalletConnect",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/hashgraph/hedera-wallet-connect.git"
8
8
  },
9
- "main": "./dist/src/index.js",
10
- "types": "./dist/src/index.d.ts",
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
11
  "author": "Hgraph <support@hgraph.io>",
12
12
  "keywords": [
13
13
  "hedera",
@@ -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",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes