@hashgraph/hedera-wallet-connect 1.5.2-canary.8dde86c.0 → 1.5.2-canary.98cc802.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.
@@ -16,6 +16,7 @@ export declare class DAppSigner implements Signer {
16
16
  setLogLevel(level: LogLevel): void;
17
17
  private _getHederaClient;
18
18
  private get _signerAccountId();
19
+ private _getRandomNodes;
19
20
  request<T>(request: {
20
21
  method: string;
21
22
  params: any;
@@ -17,7 +17,7 @@
17
17
  * limitations under the License.
18
18
  *
19
19
  */
20
- import { AccountBalance, AccountInfo, LedgerId, SignerSignature, Transaction, TransactionRecord, Client, PublicKey, TransactionId, TransactionResponse, Query, AccountRecordsQuery, AccountInfoQuery, AccountBalanceQuery, TransactionReceiptQuery, TransactionReceipt, TransactionRecordQuery, } from '@hashgraph/sdk';
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
22
  import { HederaJsonRpcMethod, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, Uint8ArrayToBase64String, Uint8ArrayToString, } from '../shared';
23
23
  import { DefaultLogger } from '../shared/logger';
@@ -51,6 +51,15 @@ export class DAppSigner {
51
51
  get _signerAccountId() {
52
52
  return `${ledgerIdToCAIPChainId(this.ledgerId)}:${this.accountId.toString()}`;
53
53
  }
54
+ _getRandomNodes(numberOfNodes) {
55
+ const allNodes = Object.values(this._getHederaClient().network).map((o) => typeof o === 'string' ? AccountId.fromString(o) : o);
56
+ // shuffle nodes
57
+ for (let i = allNodes.length - 1; i > 0; i--) {
58
+ const j = Math.floor(Math.random() * (i + 1));
59
+ [allNodes[i], allNodes[j]] = [allNodes[j], allNodes[i]];
60
+ }
61
+ return allNodes.slice(0, numberOfNodes);
62
+ }
54
63
  request(request) {
55
64
  var _a, _b;
56
65
  // Avoid a wallet call if the session is no longer valid
@@ -135,7 +144,9 @@ export class DAppSigner {
135
144
  throw new Error('Method not implemented.');
136
145
  }
137
146
  async populateTransaction(transaction) {
138
- return transaction.setTransactionId(TransactionId.generate(this.getAccountId()));
147
+ return transaction
148
+ .setNodeAccountIds(this._getRandomNodes(10)) // allow retrying on up to 10 nodes
149
+ .setTransactionId(TransactionId.generate(this.getAccountId()));
139
150
  }
140
151
  /**
141
152
  * Prepares a transaction object for signing using a single node account id.
@@ -146,7 +157,12 @@ export class DAppSigner {
146
157
  * @returns transaction - `Transaction` object with signature
147
158
  */
148
159
  async signTransaction(transaction) {
149
- const transactionBody = transactionToTransactionBody(transaction);
160
+ let nodeAccountId;
161
+ if (!transaction.nodeAccountIds || transaction.nodeAccountIds.length === 0)
162
+ nodeAccountId = this._getRandomNodes(1)[0];
163
+ else
164
+ nodeAccountId = transaction.nodeAccountIds[0];
165
+ const transactionBody = transactionToTransactionBody(transaction, nodeAccountId);
150
166
  if (!transactionBody)
151
167
  throw new Error('Failed to serialize transaction body');
152
168
  const transactionBodyBase64 = transactionBodyToBase64String(transactionBody);
@@ -1,6 +1,23 @@
1
1
  import { AccountId, PublicKey, Transaction, LedgerId, Query, SignerSignature } from '@hashgraph/sdk';
2
2
  import { ProposalTypes, SessionTypes } from '@walletconnect/types';
3
3
  import { proto } from '@hashgraph/proto';
4
+ /**
5
+ * Freezes a transaction if it is not already frozen. Transactions must
6
+ * be frozen before they can be converted to bytes.
7
+ *
8
+ * @param transaction - Any instance of a class that extends `Transaction`
9
+ */
10
+ export declare function freezeTransaction<T extends Transaction>(transaction: T): void;
11
+ /**
12
+ * Sets default consensus nodes that a transaction will be submitted to. Node Account ID(s)
13
+ * must be set before a transaction can be frozen. If they have already been set, this
14
+ * function will not modify the transaction.
15
+ * @param transaction - any instance of a class that extends `Transaction`
16
+ *
17
+ * @see {@link https://docs.hedera.com/hedera/networks/testnet/testnet-nodes | Full list of Testnet-nodes}
18
+ * @see {@link https://docs.hedera.com/hedera/networks/mainnet/mainnet-nodes | Full list of Mainnet-nodes}
19
+ */
20
+ export declare function setDefaultNodeAccountIds<T extends Transaction>(transaction: T): void;
4
21
  /**
5
22
  * Converts `Transaction` to a Base64-string.
6
23
  *
@@ -32,7 +49,7 @@ export declare function base64StringToTransaction<T extends Transaction>(transac
32
49
  * @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
33
50
  * @returns `string`
34
51
  * */
35
- export declare function transactionToTransactionBody<T extends Transaction>(transaction: T): any;
52
+ export declare function transactionToTransactionBody<T extends Transaction>(transaction: T, nodeAccountId: AccountId): any;
36
53
  export declare function transactionBodyToBase64String(transactionBody: proto.ITransactionBody): string;
37
54
  /**
38
55
  * @param transactionList - a proto.TransactionList object
@@ -20,6 +20,30 @@
20
20
  import { Buffer } from 'buffer';
21
21
  import { AccountId, Transaction, LedgerId, Query, } from '@hashgraph/sdk';
22
22
  import { proto } from '@hashgraph/proto';
23
+ /**
24
+ * Freezes a transaction if it is not already frozen. Transactions must
25
+ * be frozen before they can be converted to bytes.
26
+ *
27
+ * @param transaction - Any instance of a class that extends `Transaction`
28
+ */
29
+ export function freezeTransaction(transaction) {
30
+ if (!transaction.isFrozen())
31
+ transaction.freeze();
32
+ }
33
+ /**
34
+ * Sets default consensus nodes that a transaction will be submitted to. Node Account ID(s)
35
+ * must be set before a transaction can be frozen. If they have already been set, this
36
+ * function will not modify the transaction.
37
+ * @param transaction - any instance of a class that extends `Transaction`
38
+ *
39
+ * @see {@link https://docs.hedera.com/hedera/networks/testnet/testnet-nodes | Full list of Testnet-nodes}
40
+ * @see {@link https://docs.hedera.com/hedera/networks/mainnet/mainnet-nodes | Full list of Mainnet-nodes}
41
+ */
42
+ export function setDefaultNodeAccountIds(transaction) {
43
+ const isNodeAccountIdNotSet = !transaction.nodeAccountIds || transaction.nodeAccountIds.length === 0;
44
+ if (!transaction.isFrozen() && isNodeAccountIdNotSet)
45
+ transaction.setNodeAccountIds([new AccountId(3), new AccountId(4), new AccountId(5)]);
46
+ }
23
47
  /**
24
48
  * Converts `Transaction` to a Base64-string.
25
49
  *
@@ -29,6 +53,8 @@ import { proto } from '@hashgraph/proto';
29
53
  * @returns Base64 encoded representation of the input `Transaction` object
30
54
  */
31
55
  export function transactionToBase64String(transaction) {
56
+ setDefaultNodeAccountIds(transaction);
57
+ freezeTransaction(transaction);
32
58
  const transactionBytes = transaction.toBytes();
33
59
  return Buffer.from(transactionBytes).toString('base64');
34
60
  }
@@ -57,10 +83,10 @@ export function base64StringToTransaction(transactionBytes) {
57
83
  * @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
58
84
  * @returns `string`
59
85
  * */
60
- export function transactionToTransactionBody(transaction) {
86
+ export function transactionToTransactionBody(transaction, nodeAccountId) {
61
87
  // This is a private function, though provides the capabilities to construct a proto.TransactionBody
62
88
  //@ts-ignore
63
- return transaction._makeTransactionBody(null);
89
+ return transaction._makeTransactionBody(nodeAccountId);
64
90
  }
65
91
  export function transactionBodyToBase64String(transactionBody) {
66
92
  return Uint8ArrayToBase64String(proto.TransactionBody.encode(transactionBody).finish());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hashgraph/hedera-wallet-connect",
3
- "version": "1.5.2-canary.8dde86c.0",
3
+ "version": "1.5.2-canary.98cc802.0",
4
4
  "description": "A library to facilitate integrating Hedera with WalletConnect",
5
5
  "repository": {
6
6
  "type": "git",