@hiero-ledger/sdk 2.70.0 → 2.71.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.
Files changed (83) hide show
  1. package/README.md +4 -4
  2. package/dist/umd.js +158 -40
  3. package/dist/umd.min.js +2 -2
  4. package/lib/LedgerId.cjs +4 -4
  5. package/lib/LedgerId.js +1 -1
  6. package/lib/LedgerId.js.map +1 -1
  7. package/lib/channel/NativeChannel.cjs +3 -2
  8. package/lib/channel/NativeChannel.js +1 -1
  9. package/lib/channel/NativeChannel.js.map +1 -1
  10. package/lib/channel/NodeMirrorChannel.cjs +0 -2
  11. package/lib/channel/NodeMirrorChannel.js +1 -1
  12. package/lib/channel/NodeMirrorChannel.js.map +1 -1
  13. package/lib/client/Client.cjs +3 -2
  14. package/lib/client/Client.d.ts +2 -2
  15. package/lib/client/Client.js +1 -1
  16. package/lib/client/Client.js.map +1 -1
  17. package/lib/client/NativeClient.cjs +141 -44
  18. package/lib/client/NativeClient.d.ts +48 -16
  19. package/lib/client/NativeClient.js +1 -1
  20. package/lib/client/NativeClient.js.map +1 -1
  21. package/lib/client/NodeClient.cjs +63 -2
  22. package/lib/client/NodeClient.d.ts +42 -0
  23. package/lib/client/NodeClient.js +1 -1
  24. package/lib/client/NodeClient.js.map +1 -1
  25. package/lib/client/WebClient.cjs +64 -34
  26. package/lib/client/WebClient.d.ts +35 -15
  27. package/lib/client/WebClient.js +1 -1
  28. package/lib/client/WebClient.js.map +1 -1
  29. package/lib/client/addressbooks/mainnet.cjs +1 -1
  30. package/lib/client/addressbooks/mainnet.d.ts +1 -1
  31. package/lib/client/addressbooks/mainnet.js +1 -1
  32. package/lib/client/addressbooks/mainnet.js.map +1 -1
  33. package/lib/client/addressbooks/previewnet.cjs +1 -1
  34. package/lib/client/addressbooks/previewnet.d.ts +1 -1
  35. package/lib/client/addressbooks/previewnet.js +1 -1
  36. package/lib/client/addressbooks/previewnet.js.map +1 -1
  37. package/lib/client/addressbooks/testnet.cjs +1 -1
  38. package/lib/client/addressbooks/testnet.d.ts +1 -1
  39. package/lib/client/addressbooks/testnet.js +1 -1
  40. package/lib/client/addressbooks/testnet.js.map +1 -1
  41. package/lib/constants/ClientConstants.cjs +22 -7
  42. package/lib/constants/ClientConstants.d.ts +12 -6
  43. package/lib/constants/ClientConstants.js +1 -1
  44. package/lib/constants/ClientConstants.js.map +1 -1
  45. package/lib/contract/ContractCreateTransaction.cjs +5 -0
  46. package/lib/contract/ContractCreateTransaction.js +1 -1
  47. package/lib/contract/ContractCreateTransaction.js.map +1 -1
  48. package/lib/contract/ContractDeleteTransaction.cjs +35 -2
  49. package/lib/contract/ContractDeleteTransaction.d.ts +18 -0
  50. package/lib/contract/ContractDeleteTransaction.js +1 -1
  51. package/lib/contract/ContractDeleteTransaction.js.map +1 -1
  52. package/lib/contract/ContractInfo.cjs +11 -2
  53. package/lib/contract/ContractInfo.d.ts +6 -0
  54. package/lib/contract/ContractInfo.js +1 -1
  55. package/lib/contract/ContractInfo.js.map +1 -1
  56. package/lib/contract/ContractUpdateTransaction.cjs +2 -1
  57. package/lib/contract/ContractUpdateTransaction.js +1 -1
  58. package/lib/contract/ContractUpdateTransaction.js.map +1 -1
  59. package/lib/encoding/hex.cjs +11 -0
  60. package/lib/encoding/hex.js +1 -1
  61. package/lib/encoding/hex.js.map +1 -1
  62. package/lib/native.cjs +2 -2
  63. package/lib/native.d.ts +1 -1
  64. package/lib/native.js +1 -1
  65. package/lib/version.js +1 -1
  66. package/package.json +2 -2
  67. package/src/LedgerId.js +4 -4
  68. package/src/channel/NativeChannel.js +9 -1
  69. package/src/channel/NodeMirrorChannel.js +0 -2
  70. package/src/client/Client.js +4 -2
  71. package/src/client/NativeClient.js +151 -47
  72. package/src/client/NodeClient.js +56 -3
  73. package/src/client/WebClient.js +63 -44
  74. package/src/client/addressbooks/mainnet.js +1 -1
  75. package/src/client/addressbooks/previewnet.js +1 -1
  76. package/src/client/addressbooks/testnet.js +1 -1
  77. package/src/constants/ClientConstants.js +26 -8
  78. package/src/contract/ContractCreateTransaction.js +5 -3
  79. package/src/contract/ContractDeleteTransaction.js +34 -0
  80. package/src/contract/ContractInfo.js +16 -0
  81. package/src/contract/ContractUpdateTransaction.js +4 -1
  82. package/src/encoding/hex.js +20 -0
  83. package/src/native.js +1 -1
@@ -5,40 +5,13 @@ import NativeChannel from "../channel/NativeChannel.js";
5
5
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
6
  import AccountId from "../account/AccountId.js";
7
7
  import LedgerId from "../LedgerId.js";
8
- import {
9
- MAINNET,
10
- NATIVE_TESTNET,
11
- NATIVE_PREVIEWNET,
12
- } from "../constants/ClientConstants.js";
8
+ import { MirrorNetwork, WebNetwork } from "../constants/ClientConstants.js";
9
+ import AddressBookQuery from "../network/AddressBookQueryWeb.js";
10
+ import FileId from "../file/FileId.js";
13
11
 
14
12
  /**
15
13
  * @typedef {import("./Client.js").ClientConfiguration} ClientConfiguration
16
14
  */
17
- export const Network = {
18
- /**
19
- * @param {string} name
20
- * @returns {{[key: string]: (string | AccountId)}}
21
- */
22
- fromName(name) {
23
- switch (name) {
24
- case "mainnet":
25
- return Network.MAINNET;
26
-
27
- case "testnet":
28
- return Network.TESTNET;
29
-
30
- case "previewnet":
31
- return Network.PREVIEWNET;
32
-
33
- default:
34
- throw new Error(`unknown network name: ${name}`);
35
- }
36
- },
37
-
38
- MAINNET: MAINNET,
39
- TESTNET: NATIVE_TESTNET,
40
- PREVIEWNET: NATIVE_PREVIEWNET,
41
- };
42
15
 
43
16
  /**
44
17
  * @augments {Client<NativeChannel, *>}
@@ -54,18 +27,21 @@ export default class NativeClient extends Client {
54
27
  if (typeof props.network === "string") {
55
28
  switch (props.network) {
56
29
  case "mainnet":
57
- this.setNetwork(Network.MAINNET);
30
+ this.setNetwork(WebNetwork.MAINNET);
58
31
  this.setLedgerId(LedgerId.MAINNET);
32
+ this.setMirrorNetwork(MirrorNetwork.MAINNET);
59
33
  break;
60
34
 
61
35
  case "testnet":
62
- this.setNetwork(Network.TESTNET);
36
+ this.setNetwork(WebNetwork.TESTNET);
63
37
  this.setLedgerId(LedgerId.TESTNET);
38
+ this.setMirrorNetwork(MirrorNetwork.TESTNET);
64
39
  break;
65
40
 
66
41
  case "previewnet":
67
- this.setNetwork(Network.PREVIEWNET);
42
+ this.setNetwork(WebNetwork.PREVIEWNET);
68
43
  this.setLedgerId(LedgerId.PREVIEWNET);
44
+ this.setMirrorNetwork(MirrorNetwork.PREVIEWNET);
69
45
  break;
70
46
 
71
47
  default:
@@ -121,7 +97,6 @@ export default class NativeClient extends Client {
121
97
  static forNetwork(network) {
122
98
  return new NativeClient({
123
99
  network,
124
- scheduleNetworkUpdate: false,
125
100
  });
126
101
  }
127
102
 
@@ -130,7 +105,7 @@ export default class NativeClient extends Client {
130
105
  * @returns {NativeClient}
131
106
  */
132
107
  static forName(network) {
133
- return new NativeClient({ network, scheduleNetworkUpdate: false });
108
+ return new NativeClient({ network });
134
109
  }
135
110
 
136
111
  /**
@@ -141,7 +116,6 @@ export default class NativeClient extends Client {
141
116
  static forMainnet() {
142
117
  return new NativeClient({
143
118
  network: "mainnet",
144
- scheduleNetworkUpdate: false,
145
119
  });
146
120
  }
147
121
 
@@ -153,7 +127,6 @@ export default class NativeClient extends Client {
153
127
  static forTestnet() {
154
128
  return new NativeClient({
155
129
  network: "testnet",
156
- scheduleNetworkUpdate: false,
157
130
  });
158
131
  }
159
132
 
@@ -165,10 +138,76 @@ export default class NativeClient extends Client {
165
138
  static forPreviewnet() {
166
139
  return new NativeClient({
167
140
  network: "previewnet",
168
- scheduleNetworkUpdate: false,
169
141
  });
170
142
  }
171
143
 
144
+ /**
145
+ * Construct a Hedera client pre-configured for Mainnet access with network update.
146
+ *
147
+ * @returns {Promise<NativeClient>}
148
+ */
149
+ static async forMainnetAsync() {
150
+ return new NativeClient({
151
+ network: "mainnet",
152
+ }).updateNetwork();
153
+ }
154
+
155
+ /**
156
+ * Construct a Hedera client pre-configured for Testnet access with network update.
157
+ *
158
+ * @returns {Promise<NativeClient>}
159
+ */
160
+ static async forTestnetAsync() {
161
+ return new NativeClient({
162
+ network: "testnet",
163
+ }).updateNetwork();
164
+ }
165
+
166
+ /**
167
+ * Construct a Hedera client pre-configured for Previewnet access with network update.
168
+ *
169
+ * @returns {Promise<NativeClient>}
170
+ */
171
+ static async forPreviewnetAsync() {
172
+ return new NativeClient({
173
+ network: "previewnet",
174
+ }).updateNetwork();
175
+ }
176
+
177
+ /**
178
+ * Construct a client for a specific network with optional network update.
179
+ * Updates network only if the network is not "local-node".
180
+ *
181
+ * @param {string} network
182
+ * @returns {Promise<NativeClient>}
183
+ */
184
+ static async forNameAsync(network) {
185
+ const client = new NativeClient({ network });
186
+
187
+ if (network !== "local-node") {
188
+ await client.updateNetwork();
189
+ }
190
+
191
+ return client;
192
+ }
193
+
194
+ /**
195
+ * Construct a client configured to use mirror nodes.
196
+ * This will query the address book to get the network nodes.
197
+ *
198
+ * @param {string[] | string} mirrorNetwork
199
+ * @returns {Promise<NativeClient>}
200
+ */
201
+ static async forMirrorNetwork(mirrorNetwork) {
202
+ const client = new NativeClient({
203
+ mirrorNetwork,
204
+ });
205
+
206
+ await client.updateNetwork();
207
+
208
+ return client;
209
+ }
210
+
172
211
  /**
173
212
  * @param {{[key: string]: (string | AccountId)} | string} network
174
213
  * @returns {void}
@@ -177,13 +216,13 @@ export default class NativeClient extends Client {
177
216
  if (typeof network === "string") {
178
217
  switch (network) {
179
218
  case "previewnet":
180
- this._network.setNetwork(Network.PREVIEWNET);
219
+ this._network.setNetwork(WebNetwork.PREVIEWNET);
181
220
  break;
182
221
  case "testnet":
183
- this._network.setNetwork(Network.TESTNET);
222
+ this._network.setNetwork(WebNetwork.TESTNET);
184
223
  break;
185
224
  case "mainnet":
186
- this._network.setNetwork(Network.MAINNET);
225
+ this._network.setNetwork(WebNetwork.MAINNET);
187
226
  }
188
227
  } else {
189
228
  this._network.setNetwork(network);
@@ -192,11 +231,75 @@ export default class NativeClient extends Client {
192
231
 
193
232
  /**
194
233
  * @param {string[] | string} mirrorNetwork
195
- * @returns {void}
234
+ * @returns {this}
196
235
  */
197
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
198
236
  setMirrorNetwork(mirrorNetwork) {
199
- // Do nothing as this is not currently supported
237
+ if (typeof mirrorNetwork === "string") {
238
+ switch (mirrorNetwork) {
239
+ case "local-node":
240
+ this._mirrorNetwork.setNetwork(MirrorNetwork.LOCAL_NODE);
241
+ break;
242
+ case "previewnet":
243
+ this._mirrorNetwork.setNetwork(MirrorNetwork.PREVIEWNET);
244
+ break;
245
+ case "testnet":
246
+ this._mirrorNetwork.setNetwork(MirrorNetwork.TESTNET);
247
+ break;
248
+ case "mainnet":
249
+ this._mirrorNetwork.setNetwork(MirrorNetwork.MAINNET);
250
+ break;
251
+ default:
252
+ this._mirrorNetwork.setNetwork([mirrorNetwork]);
253
+ }
254
+ } else {
255
+ this._mirrorNetwork.setNetwork(mirrorNetwork);
256
+ }
257
+
258
+ return this;
259
+ }
260
+
261
+ /**
262
+ * @override
263
+ * @returns {Promise<this>}
264
+ */
265
+ async updateNetwork() {
266
+ if (this._isUpdatingNetwork) {
267
+ return this;
268
+ }
269
+
270
+ this._isUpdatingNetwork = true;
271
+
272
+ try {
273
+ const addressBook = await new AddressBookQuery()
274
+ .setFileId(
275
+ FileId.getAddressBookFileIdFor(this.shard, this.realm),
276
+ )
277
+ .execute(this);
278
+
279
+ /** @type {Record<string, AccountId>} */
280
+ const network = {};
281
+ for (const nodeAddress of addressBook.nodeAddresses) {
282
+ for (const endpoint of nodeAddress.addresses) {
283
+ if (nodeAddress.accountId != null) {
284
+ network[endpoint.toString()] = nodeAddress.accountId;
285
+ }
286
+ }
287
+ }
288
+
289
+ this.setNetwork(network);
290
+ } catch (/** @type {unknown} */ error) {
291
+ if (this._logger) {
292
+ const errorMessage =
293
+ error instanceof Error ? error.message : String(error);
294
+ this._logger.trace(
295
+ `failed to update client address book: ${errorMessage}`,
296
+ );
297
+ }
298
+ } finally {
299
+ this._isUpdatingNetwork = false;
300
+ }
301
+
302
+ return this;
200
303
  }
201
304
 
202
305
  /**
@@ -208,11 +311,12 @@ export default class NativeClient extends Client {
208
311
  }
209
312
 
210
313
  /**
211
- * @abstract
314
+ * @override
212
315
  * @returns {(address: string) => *}
213
316
  */
214
317
  _createMirrorNetworkChannel() {
215
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
216
- return (address) => null;
318
+ return () => {
319
+ throw new Error("mirror support is not supported in native");
320
+ };
217
321
  }
218
322
  }
@@ -165,13 +165,11 @@ export default class NodeClient extends Client {
165
165
  * @returns {Promise<NodeClient>}
166
166
  */
167
167
  static async forMirrorNetwork(mirrorNetwork, shard, realm) {
168
- const INITIAL_UPDATE_PERIOD = 10_000;
169
-
170
168
  const client = new NodeClient({
171
169
  mirrorNetwork,
172
170
  shard,
173
171
  realm,
174
- }).setNetworkUpdatePeriod(INITIAL_UPDATE_PERIOD);
172
+ });
175
173
 
176
174
  await client.updateNetwork();
177
175
 
@@ -203,6 +201,61 @@ export default class NodeClient extends Client {
203
201
  });
204
202
  }
205
203
 
204
+ /**
205
+ * Construct a Hedera client pre-configured for Mainnet access with network update.
206
+ *
207
+ * @param {object} [props]
208
+ * @param {boolean} [props.scheduleNetworkUpdate]
209
+ * @returns {Promise<NodeClient>}
210
+ */
211
+ static async forMainnetAsync(props = {}) {
212
+ return new NodeClient({ network: "mainnet", ...props }).updateNetwork();
213
+ }
214
+
215
+ /**
216
+ * Construct a Hedera client pre-configured for Testnet access with network update.
217
+ *
218
+ * @param {object} [props]
219
+ * @param {boolean} [props.scheduleNetworkUpdate]
220
+ * @returns {Promise<NodeClient>}
221
+ */
222
+ static async forTestnetAsync(props = {}) {
223
+ return new NodeClient({ network: "testnet", ...props }).updateNetwork();
224
+ }
225
+
226
+ /**
227
+ * Construct a Hedera client pre-configured for Previewnet access with network update.
228
+ *
229
+ * @param {object} [props]
230
+ * @param {boolean} [props.scheduleNetworkUpdate]
231
+ * @returns {Promise<NodeClient>}
232
+ */
233
+ static async forPreviewnetAsync(props = {}) {
234
+ return new NodeClient({
235
+ network: "previewnet",
236
+ ...props,
237
+ }).updateNetwork();
238
+ }
239
+
240
+ /**
241
+ * Construct a client for a specific network with optional network update.
242
+ * Updates network only if the network is not "local-node".
243
+ *
244
+ * @param {string} network
245
+ * @param {object} [props]
246
+ * @param {boolean} [props.scheduleNetworkUpdate]
247
+ * @returns {Promise<NodeClient>}
248
+ */
249
+ static async forNameAsync(network, props = {}) {
250
+ const client = new NodeClient({ network, ...props });
251
+
252
+ if (network !== "local-node") {
253
+ await client.updateNetwork();
254
+ }
255
+
256
+ return client;
257
+ }
258
+
206
259
  /**
207
260
  * @param {{[key: string]: (string | AccountId)} | string} network
208
261
  * @returns {void}
@@ -3,12 +3,7 @@
3
3
  import Client from "./Client.js";
4
4
  import WebChannel from "../channel/WebChannel.js";
5
5
  import LedgerId from "../LedgerId.js";
6
- import {
7
- MAINNET,
8
- WEB_TESTNET,
9
- WEB_PREVIEWNET,
10
- MirrorNetwork,
11
- } from "../constants/ClientConstants.js";
6
+ import { WebNetwork, MirrorNetwork } from "../constants/ClientConstants.js";
12
7
  import AddressBookQuery from "../network/AddressBookQueryWeb.js";
13
8
  import FileId from "../file/FileId.js";
14
9
 
@@ -17,32 +12,6 @@ import FileId from "../file/FileId.js";
17
12
  * @typedef {import("../account/AccountId.js").default} AccountId
18
13
  */
19
14
 
20
- export const Network = {
21
- /**
22
- * @param {string} name
23
- * @returns {{[key: string]: (string | AccountId)}}
24
- */
25
- fromName(name) {
26
- switch (name) {
27
- case "mainnet":
28
- return Network.MAINNET;
29
-
30
- case "testnet":
31
- return Network.TESTNET;
32
-
33
- case "previewnet":
34
- return Network.PREVIEWNET;
35
-
36
- default:
37
- throw new Error(`unknown network name: ${name}`);
38
- }
39
- },
40
-
41
- MAINNET: MAINNET,
42
- TESTNET: WEB_TESTNET,
43
- PREVIEWNET: WEB_PREVIEWNET,
44
- };
45
-
46
15
  /**
47
16
  * Represents a client for interacting with the Hedera network over the web.
48
17
  * The `WebClient` class extends the base `Client` class and provides methods
@@ -61,19 +30,19 @@ export default class WebClient extends Client {
61
30
  if (typeof props.network === "string") {
62
31
  switch (props.network) {
63
32
  case "mainnet":
64
- this.setNetwork(Network.MAINNET);
65
- this.setMirrorNetwork(MirrorNetwork.MAINNET);
33
+ this.setNetwork(WebNetwork.MAINNET);
66
34
  this.setLedgerId(LedgerId.MAINNET);
35
+ this.setMirrorNetwork(MirrorNetwork.MAINNET);
67
36
  break;
68
37
 
69
38
  case "testnet":
70
- this.setNetwork(Network.TESTNET);
39
+ this.setNetwork(WebNetwork.TESTNET);
71
40
  this.setLedgerId(LedgerId.TESTNET);
72
41
  this.setMirrorNetwork(MirrorNetwork.TESTNET);
73
42
  break;
74
43
 
75
44
  case "previewnet":
76
- this.setNetwork(Network.PREVIEWNET);
45
+ this.setNetwork(WebNetwork.PREVIEWNET);
77
46
  this.setLedgerId(LedgerId.PREVIEWNET);
78
47
  this.setMirrorNetwork(MirrorNetwork.PREVIEWNET);
79
48
  break;
@@ -173,6 +142,56 @@ export default class WebClient extends Client {
173
142
  });
174
143
  }
175
144
 
145
+ /**
146
+ * Construct a Hedera client pre-configured for Mainnet access with network update.
147
+ *
148
+ * @returns {Promise<WebClient>}
149
+ */
150
+ static async forMainnetAsync() {
151
+ return new WebClient({
152
+ network: "mainnet",
153
+ }).updateNetwork();
154
+ }
155
+
156
+ /**
157
+ * Construct a Hedera client pre-configured for Testnet access with network update.
158
+ *
159
+ * @returns {Promise<WebClient>}
160
+ */
161
+ static async forTestnetAsync() {
162
+ return new WebClient({
163
+ network: "testnet",
164
+ }).updateNetwork();
165
+ }
166
+
167
+ /**
168
+ * Construct a Hedera client pre-configured for Previewnet access with network update.
169
+ *
170
+ * @returns {Promise<WebClient>}
171
+ */
172
+ static async forPreviewnetAsync() {
173
+ return new WebClient({
174
+ network: "previewnet",
175
+ }).updateNetwork();
176
+ }
177
+
178
+ /**
179
+ * Construct a client for a specific network with optional network update.
180
+ * Updates network only if the network is not "local-node".
181
+ *
182
+ * @param {string} network
183
+ * @returns {Promise<WebClient>}
184
+ */
185
+ static async forNameAsync(network) {
186
+ const client = new WebClient({ network });
187
+
188
+ if (network !== "local-node") {
189
+ await client.updateNetwork();
190
+ }
191
+
192
+ return client;
193
+ }
194
+
176
195
  /**
177
196
  * Construct a client configured to use mirror nodes.
178
197
  * This will query the address book to get the network nodes.
@@ -181,9 +200,7 @@ export default class WebClient extends Client {
181
200
  * @returns {Promise<WebClient>}
182
201
  */
183
202
  static async forMirrorNetwork(mirrorNetwork) {
184
- const client = new WebClient();
185
-
186
- client.setMirrorNetwork(mirrorNetwork);
203
+ const client = new WebClient({ mirrorNetwork: mirrorNetwork });
187
204
 
188
205
  await client.updateNetwork();
189
206
 
@@ -198,13 +215,13 @@ export default class WebClient extends Client {
198
215
  if (typeof network === "string") {
199
216
  switch (network) {
200
217
  case "previewnet":
201
- this._network.setNetwork(Network.PREVIEWNET);
218
+ this._network.setNetwork(WebNetwork.PREVIEWNET);
202
219
  break;
203
220
  case "testnet":
204
- this._network.setNetwork(Network.TESTNET);
221
+ this._network.setNetwork(WebNetwork.TESTNET);
205
222
  break;
206
223
  case "mainnet":
207
- this._network.setNetwork(Network.MAINNET);
224
+ this._network.setNetwork(WebNetwork.MAINNET);
208
225
  }
209
226
  } else {
210
227
  this._network.setNetwork(network);
@@ -242,11 +259,11 @@ export default class WebClient extends Client {
242
259
 
243
260
  /**
244
261
  * @override
245
- * @returns {Promise<void>}
262
+ * @returns {Promise<this>}
246
263
  */
247
264
  async updateNetwork() {
248
265
  if (this._isUpdatingNetwork) {
249
- return;
266
+ return this;
250
267
  }
251
268
 
252
269
  this._isUpdatingNetwork = true;
@@ -280,6 +297,8 @@ export default class WebClient extends Client {
280
297
  } finally {
281
298
  this._isUpdatingNetwork = false;
282
299
  }
300
+
301
+ return this;
283
302
  }
284
303
 
285
304
  /**