@hiero-ledger/sdk 2.73.1 → 2.73.2

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.
@@ -179,17 +179,15 @@ export default class AccountId {
179
179
  if (this.evmAddress === null) {
180
180
  throw new Error("field `evmAddress` should not be null");
181
181
  }
182
- const mirrorUrl = client.mirrorNetwork[0].slice(
183
- 0,
184
- client.mirrorNetwork[0].indexOf(":"),
185
- );
182
+ const mirrorRestApiBaseUrl = client.mirrorRestApiBaseUrl;
183
+
184
+ const url = `${mirrorRestApiBaseUrl}/accounts/${this.evmAddress.toString()}`;
186
185
 
187
186
  await new Promise((resolve) => {
188
187
  setTimeout(resolve, 3000);
189
188
  });
190
189
 
191
190
  /* eslint-disable */
192
- const url = `https://${mirrorUrl}/api/v1/accounts/${this.evmAddress.toString()}`;
193
191
  const response = await fetch(url);
194
192
  const data = await response.json();
195
193
  const mirrorAccountId = data.account;
@@ -211,17 +209,15 @@ export default class AccountId {
211
209
  if (this.num === null) {
212
210
  throw new Error("field `num` should not be null");
213
211
  }
214
- const mirrorUrl = client.mirrorNetwork[0].slice(
215
- 0,
216
- client.mirrorNetwork[0].indexOf(":"),
217
- );
212
+ const mirrorRestApiBaseUrl = client.mirrorRestApiBaseUrl;
213
+
214
+ const url = `${mirrorRestApiBaseUrl}/accounts/${this.num.toString()}`;
218
215
 
219
216
  await new Promise((resolve) => {
220
217
  setTimeout(resolve, 3000);
221
218
  });
222
219
 
223
220
  /* eslint-disable */
224
- const url = `https://${mirrorUrl}/api/v1/accounts/${this.num.toString()}`;
225
221
  const response = await fetch(url);
226
222
  const data = await response.json();
227
223
  const mirrorAccountId = data.evm_address;
@@ -267,6 +267,33 @@ export default class Client {
267
267
  return this._mirrorNetwork.network;
268
268
  }
269
269
 
270
+ /**
271
+ * @returns {string}
272
+ * @throws {Error} When no mirror network is configured or available
273
+ */
274
+ get mirrorRestApiBaseUrl() {
275
+ try {
276
+ const mirrorNode = this._mirrorNetwork.getNextMirrorNode();
277
+ const host = mirrorNode.address.address;
278
+ const port = mirrorNode.address.port;
279
+
280
+ if (!host || !port) {
281
+ throw new Error(
282
+ "Mirror node has invalid address configuration",
283
+ );
284
+ }
285
+
286
+ const scheme = this._getSchemeFromHostAndPort(host, port);
287
+
288
+ return `${scheme}://${host}:${port}/api/v1`;
289
+ } catch (error) {
290
+ // Re-throw with a more descriptive error message
291
+ throw new Error(
292
+ "Client has no mirror network configured or no healthy mirror nodes are available",
293
+ );
294
+ }
295
+ }
296
+
270
297
  /**
271
298
  * @returns {boolean}
272
299
  */
@@ -805,6 +832,34 @@ export default class Client {
805
832
  }, this._networkUpdatePeriod);
806
833
  }
807
834
 
835
+ /**
836
+ * Determines the appropriate scheme (http/https) based on the host and port.
837
+ *
838
+ * @private
839
+ * @param {string} host - The host address
840
+ * @param {number} port - The port number
841
+ * @returns {string} - The scheme ('http' or 'https')
842
+ */
843
+ _getSchemeFromHostAndPort(host, port) {
844
+ // For localhost and 127.0.0.1, use HTTP scheme
845
+ if (host === "localhost" || host === "127.0.0.1") {
846
+ return "http";
847
+ }
848
+
849
+ // Standard HTTPS ports
850
+ if (port === 443) {
851
+ return "https";
852
+ }
853
+
854
+ // Standard HTTP ports
855
+ if (port === 80) {
856
+ return "http";
857
+ }
858
+
859
+ // For other ports, assume HTTPS for security
860
+ return "https";
861
+ }
862
+
808
863
  /**
809
864
  * @returns {boolean}
810
865
  */
@@ -217,29 +217,26 @@ export default class MirrorNodeContractQuery {
217
217
  throw new Error("Contract ID is not set");
218
218
  }
219
219
  this._fillEvmAddress();
220
- let mirrorNetworkAddress = client.mirrorNetwork[0];
221
- const contractCallEndpoint = "/api/v1/contracts/call";
220
+ let mirrorRestApiBaseUrl = client.mirrorRestApiBaseUrl;
221
+ const contractCallEndpointPath = "/contracts/call";
222
222
 
223
- if (!client.ledgerId || client.ledgerId?.isLocalNode()) {
224
- const currentMirrorNetworkPort =
225
- client.mirrorNetwork[0].split(":")[1];
226
- mirrorNetworkAddress = "http://"
227
- .concat(
228
- client.mirrorNetwork[0].replace(
229
- currentMirrorNetworkPort,
230
- "8545",
231
- ),
232
- )
233
- .concat(contractCallEndpoint);
234
- } else {
235
- let trimmed = client.mirrorNetwork[0].split(":");
236
- mirrorNetworkAddress = "https://"
237
- .concat(trimmed[0])
238
- .concat(contractCallEndpoint);
223
+ // Check if this is a local environment (localhost or 127.0.0.1)
224
+ const mirrorNode = client._mirrorNetwork.getNextMirrorNode();
225
+ const host = mirrorNode.address.address;
226
+ const isLocalEnvironment = host === "localhost" || host === "127.0.0.1";
227
+
228
+ if (isLocalEnvironment) {
229
+ // For local environments, use HTTP scheme and port 8545
230
+ const url = new URL(mirrorRestApiBaseUrl);
231
+ url.protocol = "http:";
232
+ url.port = "8545";
233
+ mirrorRestApiBaseUrl = url.toString();
239
234
  }
240
235
 
236
+ const contractCallEndpointUrl = `${mirrorRestApiBaseUrl}${contractCallEndpointPath}`;
237
+
241
238
  // eslint-disable-next-line n/no-unsupported-features/node-builtins
242
- const response = await fetch(mirrorNetworkAddress, {
239
+ const response = await fetch(contractCallEndpointUrl, {
243
240
  method: "POST",
244
241
  headers: {
245
242
  "Content-Type": "application/json",