@hashgraphonline/standards-sdk 0.1.141 → 0.1.143

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 (273) hide show
  1. package/README.md +24 -1
  2. package/dist/cjs/hcs-11/client.d.ts.map +1 -1
  3. package/dist/cjs/hcs-11/types.d.ts +1 -0
  4. package/dist/cjs/hcs-11/types.d.ts.map +1 -1
  5. package/dist/cjs/hcs-16/base-client.d.ts.map +1 -1
  6. package/dist/cjs/hcs-17/types.d.ts.map +1 -1
  7. package/dist/cjs/hcs-20/sdk.d.ts.map +1 -1
  8. package/dist/cjs/hcs-20/types.d.ts +9 -8
  9. package/dist/cjs/hcs-20/types.d.ts.map +1 -1
  10. package/dist/cjs/hcs-21/base-client.d.ts +14 -12
  11. package/dist/cjs/hcs-21/base-client.d.ts.map +1 -1
  12. package/dist/cjs/hcs-21/browser.d.ts +17 -7
  13. package/dist/cjs/hcs-21/browser.d.ts.map +1 -1
  14. package/dist/cjs/hcs-21/index.d.ts +1 -0
  15. package/dist/cjs/hcs-21/index.d.ts.map +1 -1
  16. package/dist/cjs/hcs-21/sdk.d.ts +70 -6
  17. package/dist/cjs/hcs-21/sdk.d.ts.map +1 -1
  18. package/dist/cjs/hcs-21/tx.d.ts +6 -2
  19. package/dist/cjs/hcs-21/tx.d.ts.map +1 -1
  20. package/dist/cjs/hcs-21/types.d.ts +751 -66
  21. package/dist/cjs/hcs-21/types.d.ts.map +1 -1
  22. package/dist/cjs/hcs-21/verify.d.ts +6 -0
  23. package/dist/cjs/hcs-21/verify.d.ts.map +1 -0
  24. package/dist/cjs/inscribe/inscriber.d.ts +3 -1
  25. package/dist/cjs/inscribe/inscriber.d.ts.map +1 -1
  26. package/dist/cjs/inscribe/quote-cache.d.ts.map +1 -1
  27. package/dist/cjs/inscribe/types.d.ts +7 -0
  28. package/dist/cjs/inscribe/types.d.ts.map +1 -1
  29. package/dist/cjs/services/registry-broker/client.d.ts +4 -3
  30. package/dist/cjs/services/registry-broker/client.d.ts.map +1 -1
  31. package/dist/cjs/services/registry-broker/private-key-signer.d.ts.map +1 -1
  32. package/dist/cjs/services/registry-broker/schemas.d.ts +49 -25
  33. package/dist/cjs/services/registry-broker/schemas.d.ts.map +1 -1
  34. package/dist/cjs/services/registry-broker/types.d.ts +1 -2
  35. package/dist/cjs/services/registry-broker/types.d.ts.map +1 -1
  36. package/dist/cjs/services/types.d.ts +1 -0
  37. package/dist/cjs/services/types.d.ts.map +1 -1
  38. package/dist/cjs/standards-sdk.cjs +2 -2
  39. package/dist/cjs/standards-sdk.cjs.map +1 -1
  40. package/dist/cjs/utils/dynamic-import.d.ts +1 -0
  41. package/dist/cjs/utils/dynamic-import.d.ts.map +1 -1
  42. package/dist/es/hcs-11/client.d.ts.map +1 -1
  43. package/dist/es/hcs-11/types.d.ts +1 -0
  44. package/dist/es/hcs-11/types.d.ts.map +1 -1
  45. package/dist/es/hcs-16/base-client.d.ts.map +1 -1
  46. package/dist/es/hcs-17/types.d.ts.map +1 -1
  47. package/dist/es/hcs-20/sdk.d.ts.map +1 -1
  48. package/dist/es/hcs-20/types.d.ts +9 -8
  49. package/dist/es/hcs-20/types.d.ts.map +1 -1
  50. package/dist/es/hcs-21/base-client.d.ts +14 -12
  51. package/dist/es/hcs-21/base-client.d.ts.map +1 -1
  52. package/dist/es/hcs-21/browser.d.ts +17 -7
  53. package/dist/es/hcs-21/browser.d.ts.map +1 -1
  54. package/dist/es/hcs-21/index.d.ts +1 -0
  55. package/dist/es/hcs-21/index.d.ts.map +1 -1
  56. package/dist/es/hcs-21/sdk.d.ts +70 -6
  57. package/dist/es/hcs-21/sdk.d.ts.map +1 -1
  58. package/dist/es/hcs-21/tx.d.ts +6 -2
  59. package/dist/es/hcs-21/tx.d.ts.map +1 -1
  60. package/dist/es/hcs-21/types.d.ts +751 -66
  61. package/dist/es/hcs-21/types.d.ts.map +1 -1
  62. package/dist/es/hcs-21/verify.d.ts +6 -0
  63. package/dist/es/hcs-21/verify.d.ts.map +1 -0
  64. package/dist/es/inscribe/inscriber.d.ts +3 -1
  65. package/dist/es/inscribe/inscriber.d.ts.map +1 -1
  66. package/dist/es/inscribe/quote-cache.d.ts.map +1 -1
  67. package/dist/es/inscribe/types.d.ts +7 -0
  68. package/dist/es/inscribe/types.d.ts.map +1 -1
  69. package/dist/es/services/registry-broker/client.d.ts +4 -3
  70. package/dist/es/services/registry-broker/client.d.ts.map +1 -1
  71. package/dist/es/services/registry-broker/private-key-signer.d.ts.map +1 -1
  72. package/dist/es/services/registry-broker/schemas.d.ts +49 -25
  73. package/dist/es/services/registry-broker/schemas.d.ts.map +1 -1
  74. package/dist/es/services/registry-broker/types.d.ts +1 -2
  75. package/dist/es/services/registry-broker/types.d.ts.map +1 -1
  76. package/dist/es/services/types.d.ts +1 -0
  77. package/dist/es/services/types.d.ts.map +1 -1
  78. package/dist/es/standards-sdk.es.js +64 -51
  79. package/dist/es/standards-sdk.es.js.map +1 -1
  80. package/dist/es/standards-sdk.es10.js +2 -2
  81. package/dist/es/standards-sdk.es100.js +26 -229
  82. package/dist/es/standards-sdk.es100.js.map +1 -1
  83. package/dist/es/standards-sdk.es101.js +228 -109
  84. package/dist/es/standards-sdk.es101.js.map +1 -1
  85. package/dist/es/standards-sdk.es102.js +94 -15
  86. package/dist/es/standards-sdk.es102.js.map +1 -1
  87. package/dist/es/standards-sdk.es103.js +30 -80
  88. package/dist/es/standards-sdk.es103.js.map +1 -1
  89. package/dist/es/standards-sdk.es104.js +80 -27
  90. package/dist/es/standards-sdk.es104.js.map +1 -1
  91. package/dist/es/standards-sdk.es105.js +25 -136
  92. package/dist/es/standards-sdk.es105.js.map +1 -1
  93. package/dist/es/standards-sdk.es106.js +140 -27
  94. package/dist/es/standards-sdk.es106.js.map +1 -1
  95. package/dist/es/standards-sdk.es107.js +27 -20
  96. package/dist/es/standards-sdk.es107.js.map +1 -1
  97. package/dist/es/standards-sdk.es108.js +18 -156
  98. package/dist/es/standards-sdk.es108.js.map +1 -1
  99. package/dist/es/standards-sdk.es109.js +148 -196
  100. package/dist/es/standards-sdk.es109.js.map +1 -1
  101. package/dist/es/standards-sdk.es110.js +160 -747
  102. package/dist/es/standards-sdk.es110.js.map +1 -1
  103. package/dist/es/standards-sdk.es111.js +786 -9
  104. package/dist/es/standards-sdk.es111.js.map +1 -1
  105. package/dist/es/standards-sdk.es112.js +13 -567
  106. package/dist/es/standards-sdk.es112.js.map +1 -1
  107. package/dist/es/standards-sdk.es113.js +541 -576
  108. package/dist/es/standards-sdk.es113.js.map +1 -1
  109. package/dist/es/standards-sdk.es114.js +601 -12
  110. package/dist/es/standards-sdk.es114.js.map +1 -1
  111. package/dist/es/standards-sdk.es115.js +13 -2
  112. package/dist/es/standards-sdk.es115.js.map +1 -1
  113. package/dist/es/standards-sdk.es116.js +2 -83
  114. package/dist/es/standards-sdk.es116.js.map +1 -1
  115. package/dist/es/standards-sdk.es117.js +80 -36
  116. package/dist/es/standards-sdk.es117.js.map +1 -1
  117. package/dist/es/standards-sdk.es118.js +39 -2
  118. package/dist/es/standards-sdk.es118.js.map +1 -1
  119. package/dist/es/standards-sdk.es119.js +2 -223
  120. package/dist/es/standards-sdk.es119.js.map +1 -1
  121. package/dist/es/standards-sdk.es12.js +1 -1
  122. package/dist/es/standards-sdk.es120.js +193 -1110
  123. package/dist/es/standards-sdk.es120.js.map +1 -1
  124. package/dist/es/standards-sdk.es121.js +1059 -225
  125. package/dist/es/standards-sdk.es121.js.map +1 -1
  126. package/dist/es/standards-sdk.es122.js +303 -419
  127. package/dist/es/standards-sdk.es122.js.map +1 -1
  128. package/dist/es/standards-sdk.es123.js +418 -351
  129. package/dist/es/standards-sdk.es123.js.map +1 -1
  130. package/dist/es/standards-sdk.es124.js +348 -754
  131. package/dist/es/standards-sdk.es124.js.map +1 -1
  132. package/dist/es/standards-sdk.es125.js +854 -178
  133. package/dist/es/standards-sdk.es125.js.map +1 -1
  134. package/dist/es/standards-sdk.es126.js +153 -1512
  135. package/dist/es/standards-sdk.es126.js.map +1 -1
  136. package/dist/es/standards-sdk.es127.js +1373 -1977
  137. package/dist/es/standards-sdk.es127.js.map +1 -1
  138. package/dist/es/standards-sdk.es128.js +2211 -50
  139. package/dist/es/standards-sdk.es128.js.map +1 -1
  140. package/dist/es/standards-sdk.es129.js +59 -79
  141. package/dist/es/standards-sdk.es129.js.map +1 -1
  142. package/dist/es/standards-sdk.es13.js +1 -1
  143. package/dist/es/standards-sdk.es130.js +80 -152
  144. package/dist/es/standards-sdk.es130.js.map +1 -1
  145. package/dist/es/standards-sdk.es131.js +159 -7
  146. package/dist/es/standards-sdk.es131.js.map +1 -1
  147. package/dist/es/standards-sdk.es132.js +7 -86
  148. package/dist/es/standards-sdk.es132.js.map +1 -1
  149. package/dist/es/standards-sdk.es133.js +65 -44
  150. package/dist/es/standards-sdk.es133.js.map +1 -1
  151. package/dist/es/standards-sdk.es134.js +65 -28
  152. package/dist/es/standards-sdk.es134.js.map +1 -1
  153. package/dist/es/standards-sdk.es135.js +28 -138
  154. package/dist/es/standards-sdk.es135.js.map +1 -1
  155. package/dist/es/standards-sdk.es136.js +133 -37
  156. package/dist/es/standards-sdk.es136.js.map +1 -1
  157. package/dist/es/standards-sdk.es137.js +42 -732
  158. package/dist/es/standards-sdk.es137.js.map +1 -1
  159. package/dist/es/standards-sdk.es138.js +746 -12254
  160. package/dist/es/standards-sdk.es138.js.map +1 -1
  161. package/dist/es/standards-sdk.es139.js +12269 -12
  162. package/dist/es/standards-sdk.es139.js.map +1 -1
  163. package/dist/es/standards-sdk.es14.js +1 -1
  164. package/dist/es/standards-sdk.es141.js +20 -2
  165. package/dist/es/standards-sdk.es141.js.map +1 -1
  166. package/dist/es/standards-sdk.es142.js +13 -168
  167. package/dist/es/standards-sdk.es142.js.map +1 -1
  168. package/dist/es/standards-sdk.es143.js +139 -289
  169. package/dist/es/standards-sdk.es143.js.map +1 -1
  170. package/dist/es/standards-sdk.es144.js +274 -298
  171. package/dist/es/standards-sdk.es144.js.map +1 -1
  172. package/dist/es/standards-sdk.es145.js +262 -369
  173. package/dist/es/standards-sdk.es145.js.map +1 -1
  174. package/dist/es/standards-sdk.es146.js +316 -194
  175. package/dist/es/standards-sdk.es146.js.map +1 -1
  176. package/dist/es/standards-sdk.es147.js +319 -64
  177. package/dist/es/standards-sdk.es147.js.map +1 -1
  178. package/dist/es/standards-sdk.es148.js +79 -0
  179. package/dist/es/standards-sdk.es148.js.map +1 -0
  180. package/dist/es/standards-sdk.es15.js +1 -1
  181. package/dist/es/standards-sdk.es16.js +1 -1
  182. package/dist/es/standards-sdk.es17.js +5 -10
  183. package/dist/es/standards-sdk.es17.js.map +1 -1
  184. package/dist/es/standards-sdk.es19.js +12 -16
  185. package/dist/es/standards-sdk.es19.js.map +1 -1
  186. package/dist/es/standards-sdk.es20.js +9 -13
  187. package/dist/es/standards-sdk.es20.js.map +1 -1
  188. package/dist/es/standards-sdk.es21.js +1 -1
  189. package/dist/es/standards-sdk.es22.js +1 -1
  190. package/dist/es/standards-sdk.es23.js +1 -1
  191. package/dist/es/standards-sdk.es24.js +1 -1
  192. package/dist/es/standards-sdk.es25.js +1 -1
  193. package/dist/es/standards-sdk.es26.js +1 -1
  194. package/dist/es/standards-sdk.es27.js +1 -1
  195. package/dist/es/standards-sdk.es28.js +16 -18
  196. package/dist/es/standards-sdk.es28.js.map +1 -1
  197. package/dist/es/standards-sdk.es29.js.map +1 -1
  198. package/dist/es/standards-sdk.es3.js +2 -2
  199. package/dist/es/standards-sdk.es31.js +2 -2
  200. package/dist/es/standards-sdk.es32.js +4 -4
  201. package/dist/es/standards-sdk.es33.js +1 -1
  202. package/dist/es/standards-sdk.es36.js +8 -12
  203. package/dist/es/standards-sdk.es36.js.map +1 -1
  204. package/dist/es/standards-sdk.es37.js +4 -4
  205. package/dist/es/standards-sdk.es38.js +2 -2
  206. package/dist/es/standards-sdk.es39.js +2 -2
  207. package/dist/es/standards-sdk.es40.js +1 -1
  208. package/dist/es/standards-sdk.es41.js +1 -1
  209. package/dist/es/standards-sdk.es42.js +2 -2
  210. package/dist/es/standards-sdk.es47.js +1 -1
  211. package/dist/es/standards-sdk.es5.js +2 -2
  212. package/dist/es/standards-sdk.es52.js +1 -1
  213. package/dist/es/standards-sdk.es54.js +1 -1
  214. package/dist/es/standards-sdk.es57.js +1 -1
  215. package/dist/es/standards-sdk.es6.js +2 -2
  216. package/dist/es/standards-sdk.es61.js +7 -11
  217. package/dist/es/standards-sdk.es61.js.map +1 -1
  218. package/dist/es/standards-sdk.es65.js +2 -2
  219. package/dist/es/standards-sdk.es66.js +3 -3
  220. package/dist/es/standards-sdk.es69.js +2 -2
  221. package/dist/es/standards-sdk.es7.js +2 -2
  222. package/dist/es/standards-sdk.es70.js +3 -3
  223. package/dist/es/standards-sdk.es71.js +2 -2
  224. package/dist/es/standards-sdk.es72.js +1 -1
  225. package/dist/es/standards-sdk.es73.js.map +1 -1
  226. package/dist/es/standards-sdk.es75.js +2 -2
  227. package/dist/es/standards-sdk.es77.js +5 -3
  228. package/dist/es/standards-sdk.es77.js.map +1 -1
  229. package/dist/es/standards-sdk.es78.js +4 -10
  230. package/dist/es/standards-sdk.es78.js.map +1 -1
  231. package/dist/es/standards-sdk.es79.js +1 -1
  232. package/dist/es/standards-sdk.es8.js +1 -1
  233. package/dist/es/standards-sdk.es80.js +113 -29
  234. package/dist/es/standards-sdk.es80.js.map +1 -1
  235. package/dist/es/standards-sdk.es82.js +23 -4
  236. package/dist/es/standards-sdk.es82.js.map +1 -1
  237. package/dist/es/standards-sdk.es83.js +39 -14
  238. package/dist/es/standards-sdk.es83.js.map +1 -1
  239. package/dist/es/standards-sdk.es84.js +198 -17
  240. package/dist/es/standards-sdk.es84.js.map +1 -1
  241. package/dist/es/standards-sdk.es85.js +256 -9
  242. package/dist/es/standards-sdk.es85.js.map +1 -1
  243. package/dist/es/standards-sdk.es86.js +55 -21
  244. package/dist/es/standards-sdk.es86.js.map +1 -1
  245. package/dist/es/standards-sdk.es87.js +22 -75
  246. package/dist/es/standards-sdk.es87.js.map +1 -1
  247. package/dist/es/standards-sdk.es88.js +45 -30
  248. package/dist/es/standards-sdk.es88.js.map +1 -1
  249. package/dist/es/standards-sdk.es89.js +57 -22
  250. package/dist/es/standards-sdk.es89.js.map +1 -1
  251. package/dist/es/standards-sdk.es90.js +28 -23
  252. package/dist/es/standards-sdk.es90.js.map +1 -1
  253. package/dist/es/standards-sdk.es91.js +23 -167
  254. package/dist/es/standards-sdk.es91.js.map +1 -1
  255. package/dist/es/standards-sdk.es92.js +158 -119
  256. package/dist/es/standards-sdk.es92.js.map +1 -1
  257. package/dist/es/standards-sdk.es93.js +68 -95
  258. package/dist/es/standards-sdk.es93.js.map +1 -1
  259. package/dist/es/standards-sdk.es94.js +136 -119
  260. package/dist/es/standards-sdk.es94.js.map +1 -1
  261. package/dist/es/standards-sdk.es95.js +139 -39
  262. package/dist/es/standards-sdk.es95.js.map +1 -1
  263. package/dist/es/standards-sdk.es96.js +42 -257
  264. package/dist/es/standards-sdk.es96.js.map +1 -1
  265. package/dist/es/standards-sdk.es97.js +243 -80
  266. package/dist/es/standards-sdk.es97.js.map +1 -1
  267. package/dist/es/standards-sdk.es98.js +47 -48
  268. package/dist/es/standards-sdk.es98.js.map +1 -1
  269. package/dist/es/standards-sdk.es99.js +100 -28
  270. package/dist/es/standards-sdk.es99.js.map +1 -1
  271. package/dist/es/utils/dynamic-import.d.ts +1 -0
  272. package/dist/es/utils/dynamic-import.d.ts.map +1 -1
  273. package/package.json +3 -3
@@ -1,2174 +1,1570 @@
1
- import * as path from "node:path";
2
- import { Buffer } from "node:buffer";
3
- import { randomBytes, createHash, createCipheriv, createDecipheriv } from "node:crypto";
4
- import { secp256k1 } from "@noble/curves/secp256k1.js";
5
- import { canonicalizeLedgerNetwork } from "./standards-sdk.es129.js";
1
+ import { PublicKey, Timestamp, AccountId } from "@hashgraph/sdk";
6
2
  import axios from "axios";
7
- import { privateKeyToAccount } from "viem/accounts";
8
- import "viem/chains";
9
- import { withPaymentInterceptor, decodeXPaymentResponse } from "x402-axios";
10
- import { createSigner } from "x402/types";
11
- import { searchResponseSchema, statsResponseSchema, registriesResponseSchema, additionalRegistryCatalogResponseSchema, popularResponseSchema, resolveResponseSchema, registerAgentResponseSchema, registrationQuoteResponseSchema, registrationProgressResponseSchema, creditPurchaseResponseSchema, x402MinimumsResponseSchema, x402CreditPurchaseResponseSchema, ledgerChallengeResponseSchema, ledgerVerifyResponseSchema, protocolsResponseSchema, detectProtocolResponseSchema, registrySearchByNamespaceSchema, vectorSearchResponseSchema, searchStatusResponseSchema, adapterDetailsResponseSchema, websocketStatsResponseSchema, metricsSummaryResponseSchema, uaidValidationResponseSchema, uaidConnectionStatusSchema, dashboardStatsResponseSchema, adaptersResponseSchema, searchFacetsResponseSchema, createSessionResponseSchema, chatHistorySnapshotResponseSchema, chatHistoryCompactionResponseSchema, sessionEncryptionStatusResponseSchema, encryptionHandshakeResponseSchema, registerEncryptionKeyResponseSchema, sendMessageResponseSchema } from "./standards-sdk.es137.js";
12
- import { ZodError } from "zod";
13
- import { createPrivateKeySigner } from "./standards-sdk.es128.js";
14
- const getFs = () => {
15
- if (typeof require !== "function") {
16
- return null;
17
- }
18
- try {
19
- const fsModule = require("node:fs");
20
- if (fsModule && typeof fsModule.existsSync === "function" && typeof fsModule.readFileSync === "function" && typeof fsModule.writeFileSync === "function" && typeof fsModule.appendFileSync === "function") {
21
- return fsModule;
22
- }
23
- } catch {
24
- }
25
- return null;
26
- };
27
- const DEFAULT_USER_AGENT = "@hashgraphonline/standards-sdk/registry-broker-client";
28
- const DEFAULT_PROGRESS_INTERVAL_MS = 1500;
29
- const DEFAULT_PROGRESS_TIMEOUT_MS = 5 * 60 * 1e3;
30
- const createAbortError = () => typeof DOMException === "function" ? new DOMException("Aborted", "AbortError") : new Error("The operation was aborted");
31
- const normaliseHeaderName = (name) => name.trim().toLowerCase();
32
- const isBrowserRuntime = () => typeof window !== "undefined" && typeof window.fetch === "function";
33
- const DEFAULT_BASE_URL = "https://hol.org/registry/api/v1";
34
- const JSON_CONTENT_TYPE = /application\/json/i;
35
- const DEFAULT_HISTORY_TOP_UP_HBAR = 0.25;
36
- const MINIMUM_REGISTRATION_AUTO_TOP_UP_CREDITS = 1;
37
- const toJsonValue = (value) => {
38
- if (value === null) {
39
- return null;
40
- }
41
- if (value instanceof Date) {
42
- return value.toISOString();
43
- }
44
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
45
- return value;
46
- }
47
- if (Array.isArray(value)) {
48
- return value.map((item) => item === void 0 ? null : toJsonValue(item));
49
- }
50
- if (typeof value === "object") {
51
- const result = {};
52
- Object.entries(value).forEach(
53
- ([key, entryValue]) => {
54
- if (entryValue !== void 0) {
55
- result[key] = toJsonValue(entryValue);
56
- }
57
- }
3
+ import { Logger } from "./standards-sdk.es106.js";
4
+ import { proto } from "@hashgraph/proto";
5
+ class HederaMirrorNode {
6
+ constructor(network, logger, config) {
7
+ this.maxRetries = 5;
8
+ this.initialDelayMs = 2e3;
9
+ this.maxDelayMs = 3e4;
10
+ this.backoffFactor = 2;
11
+ this.network = network;
12
+ this.apiKey = config?.apiKey;
13
+ this.customHeaders = config?.headers || {};
14
+ this.baseUrl = config?.customUrl || this.getMirrorNodeUrl();
15
+ this.logger = logger || new Logger({
16
+ level: "debug",
17
+ module: "MirrorNode"
18
+ });
19
+ this.isServerEnvironment = typeof window === "undefined";
20
+ if (config?.customUrl) {
21
+ this.logger.info(`Using custom mirror node URL: ${config.customUrl}`);
22
+ }
23
+ if (config?.apiKey) {
24
+ this.logger.info("Using API key for mirror node requests");
25
+ }
26
+ }
27
+ /**
28
+ * Configures the retry mechanism for API requests.
29
+ * @param config The retry configuration.
30
+ */
31
+ configureRetry(config) {
32
+ this.maxRetries = config.maxRetries ?? this.maxRetries;
33
+ this.initialDelayMs = config.initialDelayMs ?? this.initialDelayMs;
34
+ this.maxDelayMs = config.maxDelayMs ?? this.maxDelayMs;
35
+ this.backoffFactor = config.backoffFactor ?? this.backoffFactor;
36
+ this.logger.info(
37
+ `Retry configuration updated: maxRetries=${this.maxRetries}, initialDelayMs=${this.initialDelayMs}, maxDelayMs=${this.maxDelayMs}, backoffFactor=${this.backoffFactor}`
58
38
  );
59
- return result;
60
- }
61
- throw new TypeError("Only JSON-compatible values are supported");
62
- };
63
- const isJsonObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
64
- const toJsonObject = (value) => {
65
- const normalised = toJsonValue(value);
66
- if (isJsonObject(normalised)) {
67
- return normalised;
68
- }
69
- throw new TypeError("Expected JSON object value");
70
- };
71
- const serialiseAuthConfig = (auth) => {
72
- const authPayload = {};
73
- if (auth.type) {
74
- authPayload.type = auth.type;
75
- }
76
- if (auth.token) {
77
- authPayload.token = auth.token;
78
- }
79
- if (auth.username) {
80
- authPayload.username = auth.username;
81
- }
82
- if (auth.password) {
83
- authPayload.password = auth.password;
84
- }
85
- if (auth.headerName) {
86
- authPayload.headerName = auth.headerName;
87
- }
88
- if (auth.headerValue) {
89
- authPayload.headerValue = auth.headerValue;
90
- }
91
- if (auth.headers) {
92
- authPayload.headers = { ...auth.headers };
93
- }
94
- return authPayload;
95
- };
96
- const serialiseAgentRegistrationRequest = (payload) => {
97
- const body = {
98
- profile: toJsonObject(payload.profile)
99
- };
100
- if (payload.endpoint !== void 0) {
101
- body.endpoint = payload.endpoint;
102
- }
103
- if (payload.protocol !== void 0) {
104
- body.protocol = payload.protocol;
105
- }
106
- if (payload.communicationProtocol !== void 0) {
107
- body.communicationProtocol = payload.communicationProtocol;
108
- }
109
- if (payload.registry !== void 0) {
110
- body.registry = payload.registry;
111
- }
112
- if (payload.additionalRegistries !== void 0) {
113
- body.additionalRegistries = payload.additionalRegistries;
114
- }
115
- if (payload.metadata !== void 0) {
116
- body.metadata = toJsonObject(payload.metadata);
117
- }
118
- return body;
119
- };
120
- const normalizeHexPrivateKey = (value) => {
121
- const trimmed = value.trim();
122
- if (!trimmed) {
123
- throw new Error("evmPrivateKey is required");
124
- }
125
- return trimmed.startsWith("0x") ? trimmed : `0x${trimmed}`;
126
- };
127
- class RegistryBrokerError extends Error {
128
- constructor(message, details) {
129
- super(message);
130
- this.status = details.status;
131
- this.statusText = details.statusText;
132
- this.body = details.body;
133
- }
134
- }
135
- class RegistryBrokerParseError extends Error {
136
- constructor(message, cause, rawValue) {
137
- super(message);
138
- this.cause = cause;
139
- this.rawValue = rawValue;
140
39
  }
141
- }
142
- function normaliseBaseUrl(input) {
143
- const trimmed = input?.trim();
144
- let baseCandidate = trimmed && trimmed.length > 0 ? trimmed : DEFAULT_BASE_URL;
145
- try {
146
- const url = new URL(baseCandidate.replace(/\/+$/, ""));
147
- const hostname = url.hostname.toLowerCase();
148
- const ensureRegistryPrefix = () => {
149
- if (!url.pathname.startsWith("/registry")) {
150
- url.pathname = url.pathname === "/" ? "/registry" : `/registry${url.pathname}`;
40
+ /**
41
+ * Updates the mirror node configuration.
42
+ * @param config The new mirror node configuration.
43
+ */
44
+ configureMirrorNode(config) {
45
+ if (config.customUrl) {
46
+ this.baseUrl = config.customUrl;
47
+ this.logger.info(`Updated mirror node URL: ${config.customUrl}`);
48
+ }
49
+ if (config.apiKey) {
50
+ this.apiKey = config.apiKey;
51
+ this.logger.info("Updated API key for mirror node requests");
52
+ }
53
+ if (config.headers) {
54
+ this.customHeaders = { ...this.customHeaders, ...config.headers };
55
+ this.logger.info("Updated custom headers for mirror node requests");
56
+ }
57
+ }
58
+ /**
59
+ * Constructs a full URL for API requests, handling custom providers with API keys in the path.
60
+ * @param endpoint The API endpoint (e.g., '/api/v1/accounts/0.0.123')
61
+ * @returns The full URL for the request
62
+ */
63
+ constructUrl(endpoint) {
64
+ if (this.baseUrl.includes("<API-KEY>") && this.apiKey) {
65
+ const baseUrlWithKey = this.baseUrl.replace("<API-KEY>", this.apiKey);
66
+ return endpoint.startsWith("/") ? `${baseUrlWithKey}${endpoint}` : `${baseUrlWithKey}/${endpoint}`;
67
+ }
68
+ return endpoint.startsWith("/") ? `${this.baseUrl}${endpoint}` : `${this.baseUrl}/${endpoint}`;
69
+ }
70
+ /**
71
+ * Returns the base URL for the Hedera mirror node based on the network type
72
+ * @returns The mirror node base URL
73
+ * @private
74
+ */
75
+ getMirrorNodeUrl() {
76
+ return this.network === "mainnet" ? "https://mainnet-public.mirrornode.hedera.com" : "https://testnet.mirrornode.hedera.com";
77
+ }
78
+ getBaseUrl() {
79
+ return this.baseUrl;
80
+ }
81
+ /**
82
+ * Retrieves the public key for a given account ID from the mirror node.
83
+ * @param accountId The ID of the account to retrieve the public key for.
84
+ * @returns A promise that resolves to the public key for the given account.
85
+ * @throws An error if the account ID is invalid or the public key cannot be retrieved.
86
+ */
87
+ async getPublicKey(accountId) {
88
+ this.logger.debug(`Getting public key for account ${accountId}`);
89
+ const accountInfo = await this.requestAccount(accountId);
90
+ try {
91
+ if (!accountInfo || !accountInfo.key) {
92
+ throw new Error(
93
+ `Failed to retrieve public key for account ID: ${accountId}`
94
+ );
151
95
  }
152
- };
153
- if (hostname === "hol.org") {
154
- ensureRegistryPrefix();
155
- baseCandidate = url.toString();
156
- } else if (hostname === "registry.hashgraphonline.com" || hostname === "hashgraphonline.com") {
157
- ensureRegistryPrefix();
158
- url.hostname = "hol.org";
159
- baseCandidate = url.toString();
160
- }
161
- } catch {
162
- }
163
- const withoutTrailing = baseCandidate.replace(/\/+$/, "");
164
- if (/\/api\/v\d+$/i.test(withoutTrailing)) {
165
- return withoutTrailing;
166
- }
167
- if (/\/api$/i.test(withoutTrailing)) {
168
- return `${withoutTrailing}/v1`;
169
- }
170
- return `${withoutTrailing}/api/v1`;
171
- }
172
- function buildSearchQuery(params) {
173
- const query = new URLSearchParams();
174
- const appendList = (key, values) => {
175
- if (!values) {
176
- return;
177
- }
178
- values.forEach((value) => {
179
- if (typeof value === "string") {
180
- const trimmed = value.trim();
181
- if (trimmed.length > 0) {
182
- query.append(key, trimmed);
183
- }
96
+ return PublicKey.fromString(accountInfo.key.key);
97
+ } catch (e) {
98
+ const error = e;
99
+ const logMessage = `Error fetching public key from Mirror Node: ${error.message}`;
100
+ this.logger.error(logMessage);
101
+ throw new Error(logMessage);
102
+ }
103
+ }
104
+ /**
105
+ * Retrieves the memo for a given account ID from the mirror node.
106
+ * @param accountId The ID of the account to retrieve the memo for.
107
+ * @returns A promise that resolves to the memo for the given account.
108
+ * @throws An error if the account ID is invalid or the memo cannot be retrieved.
109
+ */
110
+ async getAccountMemo(accountId) {
111
+ this.logger.debug(`Getting account memo for account ID: ${accountId}`);
112
+ try {
113
+ const accountInfo = await this._requestWithRetry(
114
+ `/api/v1/accounts/${accountId}`
115
+ );
116
+ if (accountInfo?.memo) {
117
+ return accountInfo.memo;
184
118
  }
185
- });
186
- };
187
- if (params.q) {
188
- const trimmed = params.q.trim();
189
- if (trimmed.length > 0) {
190
- query.set("q", trimmed);
191
- }
192
- }
193
- if (typeof params.page === "number") {
194
- query.set("page", params.page.toString());
195
- }
196
- if (typeof params.limit === "number") {
197
- query.set("limit", params.limit.toString());
198
- }
199
- if (params.registry) {
200
- const trimmed = params.registry.trim();
201
- if (trimmed.length > 0) {
202
- query.set("registry", trimmed);
119
+ this.logger.warn(`No memo found for account ${accountId}`);
120
+ return null;
121
+ } catch (e) {
122
+ const error = e;
123
+ this.logger.error(
124
+ `Failed to get account memo for ${accountId} after retries: ${error.message}`
125
+ );
126
+ return null;
203
127
  }
204
128
  }
205
- appendList("registries", params.registries);
206
- if (typeof params.minTrust === "number") {
207
- query.set("minTrust", params.minTrust.toString());
208
- }
209
- appendList("capabilities", params.capabilities);
210
- appendList("protocols", params.protocols);
211
- appendList("adapters", params.adapters);
212
- if (params.metadata) {
213
- Object.entries(params.metadata).forEach(([key, values]) => {
214
- if (!key || !Array.isArray(values) || values.length === 0) {
215
- return;
216
- }
217
- const trimmedKey = key.trim();
218
- if (trimmedKey.length === 0) {
219
- return;
220
- }
221
- values.forEach((value) => {
222
- if (value === void 0 || value === null) {
223
- return;
224
- }
225
- query.append(`metadata.${trimmedKey}`, String(value));
226
- });
227
- });
228
- }
229
- if (params.type) {
230
- const trimmedType = params.type.trim();
231
- if (trimmedType.length > 0 && trimmedType.toLowerCase() !== "all") {
232
- query.set("type", trimmedType);
129
+ /**
130
+ * Retrieves topic information for a given topic ID from the mirror node.
131
+ * @param topicId The ID of the topic to retrieve information for.
132
+ * @returns A promise that resolves to the topic information.
133
+ * @throws An error if the topic ID is invalid or the information cannot be retrieved.
134
+ */
135
+ async getTopicInfo(topicId) {
136
+ try {
137
+ this.logger.debug(`Fetching topic info for ${topicId}`);
138
+ const data = await this._requestWithRetry(
139
+ `/api/v1/topics/${topicId}`
140
+ );
141
+ return data;
142
+ } catch (e) {
143
+ const error = e;
144
+ const logMessage = `Error retrieving topic information for ${topicId} after retries: ${error.message}`;
145
+ this.logger.error(logMessage);
146
+ throw new Error(logMessage);
147
+ }
148
+ }
149
+ /**
150
+ * Retrieves custom fees for a given topic ID from the mirror node.
151
+ * @param topicId The ID of the topic to retrieve custom fees for.
152
+ * @returns A promise that resolves to the custom fees for the given topic.
153
+ * @throws An error if the topic ID is invalid or the custom fees cannot be retrieved.
154
+ */
155
+ async getTopicFees(topicId) {
156
+ try {
157
+ const topicInfo = await this.getTopicInfo(topicId);
158
+ return topicInfo.custom_fees;
159
+ } catch (e) {
160
+ const error = e;
161
+ const logMessage = `Error retrieving topic fees: ${error.message}`;
162
+ this.logger.error(logMessage);
163
+ return null;
233
164
  }
234
165
  }
235
- if (params.verified === true) {
236
- query.set("verified", "true");
237
- }
238
- if (params.online === true) {
239
- query.set("online", "true");
240
- }
241
- if (params.sortBy) {
242
- const trimmedSort = params.sortBy.trim();
243
- if (trimmedSort.length > 0) {
244
- query.set("sortBy", trimmedSort);
166
+ /**
167
+ * Retrieves the current HBAR price from the mirror node.
168
+ * @param date The date to retrieve the HBAR price for.
169
+ * @returns A promise that resolves to the HBAR price for the given date.
170
+ * @throws An error if the date is invalid or the price cannot be retrieved.
171
+ */
172
+ async getHBARPrice(date) {
173
+ try {
174
+ const timestamp = Timestamp.fromDate(date).toString();
175
+ this.logger.debug(`Fetching HBAR price for timestamp ${timestamp}`);
176
+ const response = await this._requestWithRetry(
177
+ `/api/v1/network/exchangerate?timestamp=${timestamp}`
178
+ );
179
+ const usdPrice = Number(response?.current_rate?.cent_equivalent) / Number(response?.current_rate?.hbar_equivalent) / 100;
180
+ return usdPrice;
181
+ } catch (e) {
182
+ const error = e;
183
+ const logMessage = `Error retrieving HBAR price: ${error.message}`;
184
+ this.logger.error(logMessage);
185
+ return null;
245
186
  }
246
187
  }
247
- if (params.sortOrder) {
248
- const lowered = params.sortOrder.toLowerCase();
249
- if (lowered === "asc" || lowered === "desc") {
250
- query.set("sortOrder", lowered);
188
+ /**
189
+ * Retrieves token information for a given token ID from the mirror node.
190
+ * @param tokenId The ID of the token to retrieve information for.
191
+ * @returns A promise that resolves to the token information.
192
+ * @throws An error if the token ID is invalid or the information cannot be retrieved.
193
+ */
194
+ async getTokenInfo(tokenId) {
195
+ this.logger.debug(`Fetching token info for ${tokenId}`);
196
+ try {
197
+ const data = await this._requestWithRetry(
198
+ `/api/v1/tokens/${tokenId}`
199
+ );
200
+ if (data) {
201
+ this.logger.trace(`Token info found for ${tokenId}:`, data);
202
+ return data;
203
+ }
204
+ this.logger.warn(`No token info found for ${tokenId}`);
205
+ return null;
206
+ } catch (e) {
207
+ const error = e;
208
+ const logMessage = `Error fetching token info for ${tokenId}: ${error.message}`;
209
+ this.logger.error(logMessage);
210
+ return null;
251
211
  }
252
212
  }
253
- const queryString = query.toString();
254
- return queryString.length > 0 ? `?${queryString}` : "";
255
- }
256
- class RegistryBrokerClient {
257
- constructor(options = {}) {
258
- this.conversationContexts = /* @__PURE__ */ new Map();
259
- this.request = async (path2, config) => {
260
- const headers = new Headers();
261
- Object.entries(this.defaultHeaders).forEach(([key, value]) => {
262
- headers.set(key, value);
263
- });
264
- if (config.headers) {
265
- Object.entries(config.headers).forEach(([key, value]) => {
266
- headers.set(key, value);
267
- });
268
- }
269
- if (!headers.has("accept")) {
270
- headers.set("accept", "application/json");
271
- }
272
- if (!headers.has("user-agent") && !isBrowserRuntime()) {
273
- headers.set("user-agent", DEFAULT_USER_AGENT);
274
- }
275
- const init = {
276
- method: config.method ?? "GET",
277
- headers
278
- };
279
- if (config.body !== void 0) {
280
- init.body = JSON.stringify(config.body);
281
- if (!headers.has("content-type")) {
282
- headers.set("content-type", "application/json");
213
+ /**
214
+ * Retrieves messages for a given topic ID from the mirror node. Supports filtering by sequence number
215
+ * based on the OpenAPI specification.
216
+ * @param topicId The ID of the topic to retrieve messages for.
217
+ * @param options Optional filtering parameters.
218
+ * @returns A promise that resolves to the messages for the given topic.
219
+ */
220
+ async getTopicMessages(topicId, options) {
221
+ this.logger.trace(
222
+ `Querying messages for topic ${topicId}${options ? " with filters" : ""}`
223
+ );
224
+ let endpoint = `/api/v1/topics/${topicId}/messages`;
225
+ const params = new URLSearchParams();
226
+ if (options) {
227
+ if (options.sequenceNumber !== void 0) {
228
+ const seqNum = typeof options.sequenceNumber === "number" ? options.sequenceNumber.toString() : options.sequenceNumber;
229
+ if (!seqNum.match(/^(gt|gte|lt|lte|eq|ne):/)) {
230
+ params.append("sequencenumber", `gt:${seqNum}`);
231
+ } else {
232
+ params.append("sequencenumber", seqNum);
283
233
  }
284
234
  }
285
- const response = await this.fetchImpl(this.buildUrl(path2), init);
286
- if (response.ok) {
287
- return response;
235
+ if (options.limit) {
236
+ params.append("limit", options.limit.toString());
237
+ }
238
+ if (options.order) {
239
+ params.append("order", options.order);
288
240
  }
289
- const errorBody = await this.extractErrorBody(response);
290
- throw new RegistryBrokerError("Registry broker request failed", {
291
- status: response.status,
292
- statusText: response.statusText,
293
- body: errorBody
294
- });
295
- };
296
- this.baseUrl = normaliseBaseUrl(options.baseUrl);
297
- const fetchCandidate = options.fetchImplementation ?? globalThis.fetch;
298
- if (!fetchCandidate) {
299
- throw new Error(
300
- "A fetch implementation is required for RegistryBrokerClient"
301
- );
302
- }
303
- this.fetchImpl = fetchCandidate;
304
- this.defaultHeaders = {};
305
- if (options.defaultHeaders) {
306
- Object.entries(options.defaultHeaders).forEach(([name, value]) => {
307
- if (typeof value === "string") {
308
- this.setDefaultHeader(name, value);
309
- }
310
- });
311
241
  }
312
- if (options.apiKey && options.apiKey.trim().length > 0) {
313
- this.setApiKey(options.apiKey);
242
+ const queryString = params.toString();
243
+ if (queryString) {
244
+ endpoint += `?${queryString}`;
314
245
  }
315
- if (options.ledgerApiKey && options.ledgerApiKey.trim().length > 0) {
316
- this.setLedgerApiKey(options.ledgerApiKey);
246
+ const messages = [];
247
+ let nextEndpoint = endpoint;
248
+ while (nextEndpoint) {
249
+ try {
250
+ const data = await this._requestWithRetry(nextEndpoint);
251
+ if (data.messages && data.messages.length > 0) {
252
+ for (const message of data.messages) {
253
+ try {
254
+ if (!message.message) {
255
+ continue;
256
+ }
257
+ let messageContent;
258
+ try {
259
+ if (this.isServerEnvironment) {
260
+ messageContent = Buffer.from(
261
+ message.message,
262
+ "base64"
263
+ ).toString("utf-8");
264
+ } else {
265
+ messageContent = new TextDecoder().decode(
266
+ Uint8Array.from(
267
+ atob(message.message),
268
+ (c) => c.charCodeAt(0)
269
+ )
270
+ );
271
+ }
272
+ } catch (error) {
273
+ const logMessage = `Error decoding message: ${error}`;
274
+ this.logger.error(logMessage);
275
+ continue;
276
+ }
277
+ let messageJson;
278
+ try {
279
+ messageJson = JSON.parse(messageContent);
280
+ } catch (error) {
281
+ const logMessage = `Invalid JSON message content: ${messageContent}`;
282
+ this.logger.error(logMessage);
283
+ continue;
284
+ }
285
+ messageJson.sequence_number = message.sequence_number;
286
+ messages.push({
287
+ ...messageJson,
288
+ consensus_timestamp: message.consensus_timestamp,
289
+ sequence_number: message.sequence_number,
290
+ running_hash: message.running_hash,
291
+ running_hash_version: message.running_hash_version,
292
+ topic_id: message.topic_id,
293
+ payer: message.payer_account_id,
294
+ created: new Date(Number(message.consensus_timestamp) * 1e3)
295
+ });
296
+ } catch (error) {
297
+ const logMessage = `Error processing message: ${error.message}`;
298
+ this.logger.error(logMessage);
299
+ }
300
+ }
301
+ }
302
+ nextEndpoint = data.links?.next || "";
303
+ } catch (e) {
304
+ const error = e;
305
+ const logMessage = `Error querying topic messages for topic ${topicId} (endpoint: ${nextEndpoint}) after retries: ${error.message}`;
306
+ this.logger.error(logMessage);
307
+ throw new Error(logMessage);
308
+ }
317
309
  }
318
- this.registrationAutoTopUp = options.registrationAutoTopUp;
319
- this.historyAutoTopUp = options.historyAutoTopUp;
320
- this.encryptedChatManager = new EncryptedChatManager(this);
321
- this.encryptionOptions = options.encryption;
322
- this.encryptionBootstrapPromise = this.bootstrapEncryptionOptions(
323
- options.encryption
324
- );
325
- this.chat = {
326
- start: (options2) => this.startChat(options2),
327
- createSession: (payload) => this.createSession(payload),
328
- sendMessage: (payload) => this.sendMessage(payload),
329
- endSession: (sessionId) => this.endSession(sessionId),
330
- getHistory: (sessionId, options2) => this.fetchHistorySnapshot(sessionId, options2),
331
- compactHistory: (payload) => this.compactHistory(payload),
332
- getEncryptionStatus: (sessionId) => this.fetchEncryptionStatus(sessionId),
333
- submitEncryptionHandshake: (sessionId, payload) => this.postEncryptionHandshake(sessionId, payload),
334
- startConversation: (opts) => this.startConversation(opts),
335
- acceptConversation: (opts) => this.acceptConversation(opts)
336
- };
337
- this.encryption = {
338
- registerKey: (payload) => this.registerEncryptionKey(payload),
339
- generateEphemeralKeyPair: () => this.createEphemeralKeyPair(),
340
- deriveSharedSecret: (options2) => this.deriveSharedSecret(options2),
341
- encryptCipherEnvelope: (options2) => this.buildCipherEnvelope(options2),
342
- decryptCipherEnvelope: (options2) => this.openCipherEnvelope(options2),
343
- ensureAgentKey: (options2) => this.ensureAgentEncryptionKey(options2)
344
- };
345
- this.chat.createEncryptedSession = (options2) => this.encryptedChatManager.startSession(options2);
346
- this.chat.acceptEncryptedSession = (options2) => this.encryptedChatManager.acceptSession(options2);
310
+ return messages;
347
311
  }
348
- static async initializeAgent(options) {
349
- const { uaid, ensureEncryptionKey = true, ...clientOptions } = options;
350
- const client = new RegistryBrokerClient(clientOptions);
351
- let encryption = null;
352
- if (ensureEncryptionKey) {
353
- const ensureOptions = typeof ensureEncryptionKey === "object" ? ensureEncryptionKey : { generateIfMissing: true };
354
- encryption = await client.encryption.ensureAgentKey({
355
- uaid,
356
- ...ensureOptions
357
- });
358
- }
359
- return { client, encryption };
360
- }
361
- setApiKey(apiKey) {
362
- this.setDefaultHeader("x-api-key", apiKey);
363
- }
364
- setLedgerApiKey(apiKey) {
365
- this.setDefaultHeader("x-ledger-api-key", apiKey);
366
- }
367
- setDefaultHeader(name, value) {
368
- if (!name || name.trim().length === 0) {
369
- return;
370
- }
371
- const headerName = normaliseHeaderName(name);
372
- if (!value || value.trim().length === 0) {
373
- delete this.defaultHeaders[headerName];
374
- return;
312
+ /**
313
+ * Requests account information for a given account ID from the mirror node.
314
+ * @param accountId The ID of the account to retrieve information for.
315
+ * @returns A promise that resolves to the account information.
316
+ * @throws An error if the account ID is invalid or the information cannot be retrieved.
317
+ */
318
+ async requestAccount(accountId) {
319
+ try {
320
+ this.logger.debug(`Requesting account info for ${accountId}`);
321
+ const data = await this._requestWithRetry(
322
+ `/api/v1/accounts/${accountId}`
323
+ );
324
+ if (!data) {
325
+ throw new Error(
326
+ `No data received from mirror node for account: ${accountId}`
327
+ );
328
+ }
329
+ return data;
330
+ } catch (e) {
331
+ const error = e;
332
+ const logMessage = `Failed to fetch account ${accountId} after retries: ${error.message}`;
333
+ this.logger.error(logMessage);
334
+ throw new Error(logMessage);
335
+ }
336
+ }
337
+ /**
338
+ * Checks if a user has access to a given key list.
339
+ * @param keyBytes The key list to check access for.
340
+ * @param userPublicKey The public key of the user to check access for.
341
+ * @returns A promise that resolves to true if the user has access, false otherwise.
342
+ */
343
+ async checkKeyListAccess(keyBytes, userPublicKey) {
344
+ try {
345
+ const key = proto.Key.decode(keyBytes);
346
+ return this.evaluateKeyAccess(key, userPublicKey);
347
+ } catch (e) {
348
+ const error = e;
349
+ const logMessage = `Error decoding protobuf key: ${error.message}`;
350
+ this.logger.error(logMessage);
351
+ throw new Error(logMessage);
352
+ }
353
+ }
354
+ /**
355
+ * Evaluates the access of a given key to a user's public key.
356
+ * @param key The key to evaluate access for.
357
+ * @param userPublicKey The public key of the user to evaluate access for.
358
+ * @returns A promise that resolves to true if the key has access, false otherwise.
359
+ */
360
+ async evaluateKeyAccess(key, userPublicKey) {
361
+ if (key.ed25519) {
362
+ return this.compareEd25519Key(key.ed25519, userPublicKey);
363
+ }
364
+ if (key.keyList) {
365
+ return this.evaluateKeyList(key.keyList, userPublicKey);
366
+ }
367
+ if (key.thresholdKey && key.thresholdKey.keys) {
368
+ return this.evaluateKeyList(key.thresholdKey.keys, userPublicKey);
375
369
  }
376
- this.defaultHeaders[headerName] = value.trim();
377
- }
378
- getDefaultHeaders() {
379
- return { ...this.defaultHeaders };
370
+ return false;
380
371
  }
381
- async encryptionReady() {
382
- if (!this.encryptionBootstrapPromise) {
383
- return;
372
+ /**
373
+ * Evaluates the access of a given key list to a user's public key.
374
+ * @param keyList The key list to evaluate access for.
375
+ * @param userPublicKey The public key of the user to evaluate access for.
376
+ * @returns A promise that resolves to true if the key list has access, false otherwise.
377
+ */
378
+ async evaluateKeyList(keyList, userPublicKey) {
379
+ const keys = keyList.keys || [];
380
+ for (const listKey of keys) {
381
+ if (!listKey) continue;
382
+ if (listKey.ed25519) {
383
+ if (this.compareEd25519Key(listKey.ed25519, userPublicKey)) {
384
+ return true;
385
+ }
386
+ } else if (listKey.keyList || listKey.thresholdKey) {
387
+ try {
388
+ const nestedKeyBytes = proto.Key.encode({
389
+ ...listKey.keyList ? { keyList: listKey.keyList } : {},
390
+ ...listKey.thresholdKey ? { thresholdKey: listKey.thresholdKey } : {}
391
+ }).finish();
392
+ const hasNestedAccess = await this.checkKeyListAccess(
393
+ Buffer.from(nestedKeyBytes),
394
+ userPublicKey
395
+ );
396
+ if (hasNestedAccess) {
397
+ return true;
398
+ }
399
+ } catch (e) {
400
+ const error = e;
401
+ const logMessage = `Error in nested key: ${error.message}`;
402
+ this.logger.debug(logMessage);
403
+ }
404
+ }
384
405
  }
385
- await this.encryptionBootstrapPromise;
386
- }
387
- async search(params = {}) {
388
- const query = buildSearchQuery(params);
389
- const raw = await this.requestJson(`/search${query}`, {
390
- method: "GET"
391
- });
392
- return this.parseWithSchema(raw, searchResponseSchema, "search response");
393
- }
394
- async stats() {
395
- const raw = await this.requestJson("/stats", { method: "GET" });
396
- return this.parseWithSchema(raw, statsResponseSchema, "stats response");
406
+ return false;
397
407
  }
398
- async registries() {
399
- const raw = await this.requestJson("/registries", {
400
- method: "GET"
401
- });
402
- return this.parseWithSchema(
403
- raw,
404
- registriesResponseSchema,
405
- "registries response"
406
- );
408
+ /**
409
+ * Compares an Ed25519 key with a user's public key.
410
+ * @param keyData The Ed25519 key data to compare.
411
+ * @param userPublicKey The public key of the user to compare with.
412
+ * @returns A boolean indicating whether the key matches the user's public key.
413
+ */
414
+ compareEd25519Key(keyData, userPublicKey) {
415
+ try {
416
+ const decodedKey = PublicKey.fromBytes(Buffer.from(keyData));
417
+ return decodedKey.toString() === userPublicKey.toString();
418
+ } catch (e) {
419
+ const error = e;
420
+ const logMessage = `Error comparing Ed25519 key: ${error.message}`;
421
+ this.logger.debug(logMessage);
422
+ return false;
423
+ }
407
424
  }
408
- async getAdditionalRegistries() {
409
- const raw = await this.requestJson(
410
- "/register/additional-registries",
411
- {
412
- method: "GET"
425
+ /**
426
+ * Retrieves information about a scheduled transaction
427
+ * @param scheduleId The ID of the scheduled transaction
428
+ * @returns A promise that resolves to the scheduled transaction information
429
+ */
430
+ async getScheduleInfo(scheduleId) {
431
+ try {
432
+ this.logger.info(
433
+ `Getting information for scheduled transaction ${scheduleId}`
434
+ );
435
+ const data = await this._requestWithRetry(
436
+ `/api/v1/schedules/${scheduleId}`
437
+ );
438
+ if (data) {
439
+ return data;
413
440
  }
414
- );
415
- return this.parseWithSchema(
416
- raw,
417
- additionalRegistryCatalogResponseSchema,
418
- "additional registry catalog response"
419
- );
420
- }
421
- bootstrapEncryptionOptions(options) {
422
- if (!options?.autoRegister || options.autoRegister.enabled === false) {
441
+ this.logger.warn(
442
+ `No schedule info found for ${scheduleId} after retries.`
443
+ );
444
+ return null;
445
+ } catch (error) {
446
+ this.logger.error(
447
+ `Error fetching schedule info for ${scheduleId} after retries: ${error.message}`
448
+ );
423
449
  return null;
424
450
  }
425
- return this.autoRegisterEncryptionKey(options.autoRegister).then(
426
- () => void 0
427
- );
428
451
  }
429
- async autoRegisterEncryptionKey(config) {
430
- const identity = this.normalizeAutoRegisterIdentity(config);
431
- if (!identity) {
432
- throw new Error(
433
- "Auto-registration requires uaid, ledgerAccountId, or email"
452
+ /**
453
+ * Checks the status of a scheduled transaction
454
+ * @param scheduleId The schedule ID to check
455
+ * @returns Status of the scheduled transaction
456
+ */
457
+ async getScheduledTransactionStatus(scheduleId) {
458
+ try {
459
+ this.logger.info(
460
+ `Checking status of scheduled transaction ${scheduleId}`
434
461
  );
435
- }
436
- const material = await this.resolveAutoRegisterKeyMaterial(config);
437
- if (!material) {
438
- throw new Error(
439
- "Unable to resolve encryption public key for auto-registration"
462
+ const scheduleInfo = await this.getScheduleInfo(scheduleId);
463
+ if (!scheduleInfo) {
464
+ throw new Error(`Schedule ${scheduleId} not found`);
465
+ }
466
+ return {
467
+ executed: Boolean(scheduleInfo.executed_timestamp),
468
+ executedDate: scheduleInfo.executed_timestamp ? new Date(Number(scheduleInfo.executed_timestamp) * 1e3) : void 0,
469
+ deleted: scheduleInfo.deleted || false
470
+ };
471
+ } catch (error) {
472
+ this.logger.error(
473
+ `Error checking scheduled transaction status: ${error}`
440
474
  );
475
+ throw error;
441
476
  }
442
- await this.registerEncryptionKey({
443
- keyType: config.keyType ?? "secp256k1",
444
- publicKey: material.publicKey,
445
- ...identity
446
- });
447
- return material;
448
477
  }
449
- normalizeAutoRegisterIdentity(config) {
450
- const identity = {};
451
- if (config.uaid) {
452
- identity.uaid = config.uaid;
453
- }
454
- if (config.ledgerAccountId) {
455
- identity.ledgerAccountId = config.ledgerAccountId;
456
- if (config.ledgerNetwork) {
457
- identity.ledgerNetwork = config.ledgerNetwork;
478
+ /**
479
+ * Retrieves details for a given transaction ID or hash from the mirror node.
480
+ * @param transactionIdOrHash The ID or hash of the transaction.
481
+ * @returns A promise that resolves to the transaction details.
482
+ * @throws An error if the transaction ID/hash is invalid or details cannot be retrieved.
483
+ */
484
+ async getTransaction(transactionIdOrHash) {
485
+ this.logger.info(
486
+ `Getting transaction details for ID/hash: ${transactionIdOrHash}`
487
+ );
488
+ try {
489
+ const response = await this._requestWithRetry(`/api/v1/transactions/${transactionIdOrHash}`);
490
+ if (response?.transactions?.length > 0) {
491
+ this.logger.trace(
492
+ `Transaction details found for ${transactionIdOrHash}:`,
493
+ response.transactions[0]
494
+ );
495
+ return response.transactions[0];
458
496
  }
497
+ this.logger.warn(
498
+ `No transaction details found for ${transactionIdOrHash} or unexpected response structure.`
499
+ );
500
+ return null;
501
+ } catch (e) {
502
+ const error = e;
503
+ this.logger.error(
504
+ `Failed to get transaction details for ${transactionIdOrHash} after retries: ${error.message}`
505
+ );
506
+ return null;
459
507
  }
460
- if (config.email) {
461
- identity.email = config.email;
462
- }
463
- if (identity.uaid || identity.ledgerAccountId || identity.email) {
464
- return identity;
465
- }
466
- return null;
467
508
  }
468
- async resolveAutoRegisterKeyMaterial(config) {
469
- if (config.publicKey?.trim()) {
470
- return { publicKey: config.publicKey.trim() };
471
- }
472
- let privateKey = config.privateKey?.trim();
473
- const envVar = config.envVar ?? "RB_ENCRYPTION_PRIVATE_KEY";
474
- if (!privateKey && envVar && process?.env?.[envVar]?.trim()) {
475
- privateKey = process.env[envVar]?.trim();
476
- }
477
- if (!privateKey && config.generateIfMissing) {
478
- const pair = await this.generateEncryptionKeyPair({
479
- keyType: config.keyType ?? "secp256k1",
480
- envVar,
481
- envPath: config.envPath,
482
- overwrite: config.overwriteEnv
483
- });
484
- if (envVar) {
485
- process.env[envVar] = pair.privateKey;
509
+ /**
510
+ * Private helper to make GET requests with retry logic using Axios.
511
+ */
512
+ async _requestWithRetry(endpoint, axiosConfig) {
513
+ let attempt = 0;
514
+ let delay = this.initialDelayMs;
515
+ const url = this.constructUrl(endpoint);
516
+ const config = {
517
+ ...axiosConfig,
518
+ headers: {
519
+ ...this.customHeaders,
520
+ ...axiosConfig?.headers
486
521
  }
487
- return { publicKey: pair.publicKey, privateKey: pair.privateKey };
488
- }
489
- if (privateKey) {
490
- const publicKey = this.derivePublicKeyFromPrivateKey(privateKey);
491
- return { publicKey, privateKey };
522
+ };
523
+ if (this.apiKey) {
524
+ config.headers = {
525
+ ...config.headers,
526
+ Authorization: `Bearer ${this.apiKey}`,
527
+ "X-API-Key": this.apiKey
528
+ };
492
529
  }
493
- return null;
494
- }
495
- derivePublicKeyFromPrivateKey(privateKey) {
496
- const normalized = this.hexToBuffer(privateKey);
497
- const publicKey = secp256k1.getPublicKey(normalized, true);
498
- return Buffer.from(publicKey).toString("hex");
499
- }
500
- ensureAgentEncryptionKey(options) {
501
- return this.autoRegisterEncryptionKey({
502
- ...options,
503
- uaid: options.uaid,
504
- enabled: true
505
- });
506
- }
507
- async popularSearches() {
508
- const raw = await this.requestJson("/popular", {
509
- method: "GET"
510
- });
511
- return this.parseWithSchema(
512
- raw,
513
- popularResponseSchema,
514
- "popular searches response"
515
- );
516
- }
517
- async resolveUaid(uaid) {
518
- const raw = await this.requestJson(
519
- `/resolve/${encodeURIComponent(uaid)}`,
520
- {
521
- method: "GET"
522
- }
523
- );
524
- return this.parseWithSchema(
525
- raw,
526
- resolveResponseSchema,
527
- "resolve UAID response"
528
- );
529
- }
530
- async performRegisterAgent(payload) {
531
- const raw = await this.requestJson("/register", {
532
- method: "POST",
533
- body: serialiseAgentRegistrationRequest(payload),
534
- headers: { "content-type": "application/json" }
535
- });
536
- return this.parseWithSchema(
537
- raw,
538
- registerAgentResponseSchema,
539
- "register agent response"
540
- );
541
- }
542
- async registerAgent(payload, options) {
543
- const autoTopUp = options?.autoTopUp ?? this.registrationAutoTopUp;
544
- if (!autoTopUp) {
545
- return this.performRegisterAgent(payload);
546
- }
547
- await this.ensureCreditsForRegistration(payload, autoTopUp);
548
- let retried = false;
549
- while (true) {
530
+ while (attempt < this.maxRetries) {
550
531
  try {
551
- return await this.performRegisterAgent(payload);
532
+ const response = await axios.get(url, config);
533
+ return response.data;
552
534
  } catch (error) {
553
- const shortfall = this.extractInsufficientCreditsDetails(error);
554
- if (shortfall && !retried) {
555
- await this.ensureCreditsForRegistration(payload, autoTopUp);
556
- retried = true;
557
- continue;
535
+ attempt++;
536
+ const isLastAttempt = attempt >= this.maxRetries;
537
+ const statusCode = error.response?.status;
538
+ if (statusCode && statusCode > 404 && statusCode < 500 && statusCode !== 429) {
539
+ this.logger.error(
540
+ `Client error for ${url} (status ${statusCode}): ${error.message}. Not retrying.`
541
+ );
542
+ throw error;
543
+ }
544
+ if (isLastAttempt) {
545
+ this.logger.error(
546
+ `Max retries (${this.maxRetries}) reached for ${url}. Last error: ${error.message}`
547
+ );
548
+ throw error;
558
549
  }
559
- throw error;
550
+ this.logger.warn(
551
+ `Attempt ${attempt}/${this.maxRetries} failed for ${url}: ${error.message}. Retrying in ${delay}ms...`
552
+ );
553
+ await new Promise((resolve) => setTimeout(resolve, delay));
554
+ delay = Math.min(delay * this.backoffFactor, this.maxDelayMs);
560
555
  }
561
556
  }
562
- }
563
- async getRegistrationQuote(payload) {
564
- const raw = await this.requestJson("/register/quote", {
565
- method: "POST",
566
- body: serialiseAgentRegistrationRequest(payload),
567
- headers: { "content-type": "application/json" }
568
- });
569
- return this.parseWithSchema(
570
- raw,
571
- registrationQuoteResponseSchema,
572
- "registration quote response"
557
+ throw new Error(
558
+ `Failed to fetch data from ${url} after ${this.maxRetries} attempts.`
573
559
  );
574
560
  }
575
- async updateAgent(uaid, payload) {
576
- const raw = await this.requestJson(
577
- `/register/${encodeURIComponent(uaid)}`,
578
- {
579
- method: "PUT",
580
- body: serialiseAgentRegistrationRequest(payload),
581
- headers: { "content-type": "application/json" }
561
+ /**
562
+ * Private helper to make fetch requests with retry logic.
563
+ */
564
+ async _fetchWithRetry(url, fetchOptions) {
565
+ let attempt = 0;
566
+ let delay = this.initialDelayMs;
567
+ const headers = {
568
+ ...this.customHeaders
569
+ };
570
+ if (fetchOptions?.headers) {
571
+ if (fetchOptions.headers instanceof Headers) {
572
+ fetchOptions.headers.forEach((value, key) => {
573
+ headers[key] = value;
574
+ });
575
+ } else if (Array.isArray(fetchOptions.headers)) {
576
+ fetchOptions.headers.forEach(([key, value]) => {
577
+ headers[key] = value;
578
+ });
579
+ } else {
580
+ Object.assign(headers, fetchOptions.headers);
582
581
  }
583
- );
584
- return this.parseWithSchema(
585
- raw,
586
- registerAgentResponseSchema,
587
- "update agent response"
588
- );
589
- }
590
- async getRegistrationProgress(attemptId) {
591
- const normalisedAttemptId = attemptId.trim();
592
- if (!normalisedAttemptId) {
593
- throw new Error("attemptId is required");
594
582
  }
595
- try {
596
- const raw = await this.requestJson(
597
- `/register/progress/${encodeURIComponent(normalisedAttemptId)}`,
598
- { method: "GET" }
599
- );
600
- const parsed = this.parseWithSchema(
601
- raw,
602
- registrationProgressResponseSchema,
603
- "registration progress response"
604
- );
605
- return parsed.progress;
606
- } catch (error) {
607
- if (error instanceof RegistryBrokerError && error.status === 404) {
608
- return null;
609
- }
610
- throw error;
583
+ if (this.apiKey) {
584
+ headers["Authorization"] = `Bearer ${this.apiKey}`;
585
+ headers["X-API-Key"] = this.apiKey;
611
586
  }
612
- }
613
- async waitForRegistrationCompletion(attemptId, options = {}) {
614
- const normalisedAttemptId = attemptId.trim();
615
- if (!normalisedAttemptId) {
616
- throw new Error("attemptId is required");
617
- }
618
- const interval = Math.max(
619
- 250,
620
- options.intervalMs ?? DEFAULT_PROGRESS_INTERVAL_MS
621
- );
622
- const timeoutMs = options.timeoutMs ?? DEFAULT_PROGRESS_TIMEOUT_MS;
623
- const throwOnFailure = options.throwOnFailure ?? true;
624
- const signal = options.signal;
625
- const startedAt = Date.now();
626
- while (true) {
627
- if (signal?.aborted) {
628
- throw createAbortError();
629
- }
630
- const progress = await this.getRegistrationProgress(normalisedAttemptId);
631
- if (progress) {
632
- options.onProgress?.(progress);
633
- if (progress.status === "completed") {
634
- return progress;
635
- }
636
- if (progress.status === "partial" || progress.status === "failed") {
637
- if (throwOnFailure) {
638
- throw new RegistryBrokerError(
639
- "Registration did not complete successfully",
640
- {
641
- status: 409,
642
- statusText: progress.status,
643
- body: progress
644
- }
587
+ const options = {
588
+ ...fetchOptions,
589
+ headers
590
+ };
591
+ while (attempt < this.maxRetries) {
592
+ try {
593
+ const request = await fetch(url, options);
594
+ if (!request.ok) {
595
+ if (request.status >= 400 && request.status < 500 && request.status !== 429) {
596
+ this.logger.error(
597
+ `Client error for ${url} (status ${request.status}): ${request.statusText}. Not retrying.`
598
+ );
599
+ throw new Error(
600
+ `Fetch failed with status ${request.status}: ${request.statusText} for URL: ${url}`
645
601
  );
646
602
  }
647
- return progress;
603
+ throw new Error(
604
+ `Fetch failed with status ${request.status}: ${request.statusText} for URL: ${url}`
605
+ );
648
606
  }
649
- }
650
- if (Date.now() - startedAt >= timeoutMs) {
651
- throw new Error(
652
- `Registration progress polling timed out after ${timeoutMs}ms`
607
+ const response = await request.json();
608
+ return response;
609
+ } catch (error) {
610
+ attempt++;
611
+ if (attempt >= this.maxRetries) {
612
+ this.logger.error(
613
+ `Max retries (${this.maxRetries}) reached for ${url}. Last error: ${error.message}`
614
+ );
615
+ throw error;
616
+ }
617
+ this.logger.warn(
618
+ `Attempt ${attempt}/${this.maxRetries} failed for ${url}: ${error.message}. Retrying in ${delay}ms...`
653
619
  );
620
+ await new Promise((resolve) => setTimeout(resolve, delay));
621
+ delay = Math.min(delay * this.backoffFactor, this.maxDelayMs);
654
622
  }
655
- await this.delay(interval, signal);
656
623
  }
657
- }
658
- async purchaseCreditsWithHbar(params) {
659
- const body = {
660
- accountId: params.accountId,
661
- payerKey: params.privateKey,
662
- hbarAmount: this.calculateHbarAmountParam(params.hbarAmount)
663
- };
664
- if (params.memo) {
665
- body.memo = params.memo;
666
- }
667
- if (params.metadata) {
668
- body.metadata = params.metadata;
669
- }
670
- const raw = await this.requestJson("/credits/purchase", {
671
- method: "POST",
672
- headers: { "content-type": "application/json" },
673
- body
674
- });
675
- return this.parseWithSchema(
676
- raw,
677
- creditPurchaseResponseSchema,
678
- "credit purchase response"
624
+ throw new Error(
625
+ `Failed to fetch data from ${url} after ${this.maxRetries} attempts.`
679
626
  );
680
627
  }
681
- async getX402Minimums() {
682
- const raw = await this.requestJson(
683
- "/credits/purchase/x402/minimums",
684
- { method: "GET" }
685
- );
686
- return this.parseWithSchema(
687
- raw,
688
- x402MinimumsResponseSchema,
689
- "x402 minimums response"
690
- );
691
- }
692
- async purchaseCreditsWithX402(params) {
693
- if (!Number.isFinite(params.credits) || params.credits <= 0) {
694
- throw new Error("credits must be a positive number");
695
- }
696
- if (params.usdAmount !== void 0 && (!Number.isFinite(params.usdAmount) || params.usdAmount <= 0)) {
697
- throw new Error("usdAmount must be a positive number when provided");
698
- }
699
- const body = {
700
- accountId: params.accountId,
701
- credits: params.credits
702
- };
703
- if (params.usdAmount !== void 0) {
704
- body.usdAmount = params.usdAmount;
705
- }
706
- if (params.description) {
707
- body.description = params.description;
708
- }
709
- if (params.metadata) {
710
- body.metadata = params.metadata;
711
- }
712
- const axiosClient = axios.create({
713
- baseURL: this.baseUrl,
714
- headers: {
715
- ...this.getDefaultHeaders(),
716
- "content-type": "application/json"
628
+ /**
629
+ * Retrieves the numerical balance (in HBAR) for a given account ID.
630
+ * @param accountId The ID of the account.
631
+ * @returns A promise that resolves to the HBAR balance or null if an error occurs.
632
+ */
633
+ async getAccountBalance(accountId) {
634
+ this.logger.info(`Getting balance for account ${accountId}`);
635
+ try {
636
+ const accountInfo = await this.requestAccount(accountId);
637
+ if (accountInfo && accountInfo.balance) {
638
+ const hbarBalance = accountInfo.balance.balance / 1e8;
639
+ return hbarBalance;
717
640
  }
718
- });
719
- const paymentClient = withPaymentInterceptor(
720
- axiosClient,
721
- params.walletClient
722
- );
723
- const response = await paymentClient.post("/credits/purchase/x402", body);
724
- const parsed = this.parseWithSchema(
725
- response.data,
726
- x402CreditPurchaseResponseSchema,
727
- "x402 credit purchase response"
728
- );
729
- const paymentHeader = typeof response.headers["x-payment-response"] === "string" ? response.headers["x-payment-response"] : void 0;
730
- const decodedPayment = paymentHeader !== void 0 ? decodeXPaymentResponse(paymentHeader) : void 0;
731
- return {
732
- ...parsed,
733
- paymentResponseHeader: paymentHeader,
734
- paymentResponse: decodedPayment
735
- };
736
- }
737
- async buyCreditsWithX402(params) {
738
- const network = params.network ?? "base";
739
- const normalizedKey = normalizeHexPrivateKey(params.evmPrivateKey);
740
- const walletClient = await createSigner(network, normalizedKey);
741
- return this.purchaseCreditsWithX402({
742
- accountId: params.accountId,
743
- credits: params.credits,
744
- usdAmount: params.usdAmount,
745
- description: params.description,
746
- metadata: params.metadata,
747
- walletClient
748
- });
749
- }
750
- calculateHbarAmount(creditsToPurchase, creditsPerHbar) {
751
- if (creditsPerHbar <= 0) {
752
- throw new Error("creditsPerHbar must be positive");
753
- }
754
- if (creditsToPurchase <= 0) {
755
- throw new Error("creditsToPurchase must be positive");
641
+ this.logger.warn(
642
+ `Could not retrieve balance for account ${accountId} from account info.`
643
+ );
644
+ return null;
645
+ } catch (error) {
646
+ this.logger.error(
647
+ `Error fetching numerical balance for account ${accountId}: ${error.message}`
648
+ );
649
+ return null;
756
650
  }
757
- const rawHbar = creditsToPurchase / creditsPerHbar;
758
- const tinybars = Math.ceil(rawHbar * 1e8);
759
- return tinybars / 1e8;
760
651
  }
761
- resolveCreditsToPurchase(shortfallCredits) {
762
- if (!Number.isFinite(shortfallCredits) || shortfallCredits <= 0) {
763
- return 0;
764
- }
765
- return Math.max(
766
- Math.ceil(shortfallCredits),
767
- MINIMUM_REGISTRATION_AUTO_TOP_UP_CREDITS
652
+ /**
653
+ * Retrieves messages for a given topic ID with optional filters.
654
+ * @param topicId The ID of the topic.
655
+ * @param sequenceNumber Filter by sequence number (e.g., "gt:10", "lte:20").
656
+ * @param startTime Filter by consensus timestamp (e.g., "gt:1629400000.000000000").
657
+ * @param endTime Filter by consensus timestamp (e.g., "lt:1629500000.000000000").
658
+ * @param limit The maximum number of messages to return.
659
+ * @returns A promise that resolves to an array of HCSMessages or null.
660
+ */
661
+ async getTopicMessagesByFilter(topicId, options) {
662
+ this.logger.trace(
663
+ `Querying messages for topic ${topicId} with filters: ${JSON.stringify(
664
+ options
665
+ )}`
768
666
  );
769
- }
770
- calculateHbarAmountParam(hbarAmount) {
771
- const tinybars = Math.ceil(hbarAmount * 1e8);
772
- if (tinybars <= 0) {
773
- throw new Error("Calculated purchase amount must be positive");
667
+ let nextUrl = `/api/v1/topics/${topicId}/messages`;
668
+ const params = new URLSearchParams();
669
+ if (options?.limit) {
670
+ params.append("limit", options.limit.toString());
774
671
  }
775
- return tinybars / 1e8;
776
- }
777
- shouldAutoTopUpHistory(payload, error) {
778
- if (!this.historyAutoTopUp || payload.historyTtlSeconds === void 0) {
779
- return false;
672
+ if (options?.sequenceNumber) {
673
+ params.append("sequencenumber", options.sequenceNumber);
780
674
  }
781
- if (!(error instanceof RegistryBrokerError)) {
782
- return false;
675
+ if (options?.startTime) {
676
+ params.append("timestamp", `gte:${options.startTime}`);
783
677
  }
784
- if (error.status !== 402) {
785
- return false;
678
+ if (options?.endTime) {
679
+ params.append("timestamp", `lt:${options.endTime}`);
786
680
  }
787
- const message = this.extractErrorMessage(error.body);
788
- if (!message) {
789
- return true;
681
+ if (options?.order) {
682
+ params.append("order", options.order);
790
683
  }
791
- const normalised = message.toLowerCase();
792
- return normalised.includes("history") || normalised.includes("chat history");
793
- }
794
- extractErrorMessage(body) {
795
- if (typeof body === "string") {
796
- return body;
684
+ const queryString = params.toString();
685
+ if (queryString) {
686
+ nextUrl += `?${queryString}`;
797
687
  }
798
- if (isJsonObject(body) && typeof body.error === "string") {
799
- return body.error;
800
- }
801
- if (isJsonObject(body) && typeof body.message === "string") {
802
- return body.message;
803
- }
804
- return void 0;
805
- }
806
- async executeHistoryAutoTopUp(reason) {
807
- if (!this.historyAutoTopUp) {
808
- return;
809
- }
810
- const hbarAmount = this.historyAutoTopUp.hbarAmount && this.historyAutoTopUp.hbarAmount > 0 ? this.historyAutoTopUp.hbarAmount : DEFAULT_HISTORY_TOP_UP_HBAR;
811
- await this.purchaseCreditsWithHbar({
812
- accountId: this.historyAutoTopUp.accountId,
813
- privateKey: this.historyAutoTopUp.privateKey,
814
- hbarAmount,
815
- memo: this.historyAutoTopUp.memo ?? "registry-broker-client:chat-history-topup",
816
- metadata: {
817
- purpose: "chat-history",
818
- reason
819
- }
820
- });
821
- }
822
- async ensureCreditsForRegistration(payload, autoTopUp) {
823
- const details = autoTopUp ?? null;
824
- if (!details) {
825
- return;
826
- }
827
- if (!details.accountId || !details.accountId.trim()) {
828
- throw new Error("autoTopUp.accountId is required");
829
- }
830
- if (!details.privateKey || !details.privateKey.trim()) {
831
- throw new Error("autoTopUp.privateKey is required");
832
- }
833
- for (let attempt = 0; attempt < 3; attempt += 1) {
834
- const quote = await this.getRegistrationQuote(payload);
835
- const shortfall = quote.shortfallCredits ?? 0;
836
- if (shortfall <= 0) {
837
- return;
838
- }
839
- const creditsToPurchase = this.resolveCreditsToPurchase(shortfall);
840
- if (creditsToPurchase <= 0) {
841
- return;
842
- }
843
- const creditsPerHbar = quote.creditsPerHbar ?? null;
844
- if (!creditsPerHbar || creditsPerHbar <= 0) {
845
- throw new Error("Unable to determine credits per HBAR for auto top-up");
688
+ const messages = [];
689
+ let pagesFetched = 0;
690
+ const maxPages = 10;
691
+ try {
692
+ while (nextUrl && pagesFetched < maxPages) {
693
+ pagesFetched++;
694
+ const data = await this._requestWithRetry(nextUrl);
695
+ if (data.messages && data.messages.length > 0) {
696
+ for (const message of data.messages) {
697
+ try {
698
+ if (!message.message) {
699
+ continue;
700
+ }
701
+ let messageContent;
702
+ if (this.isServerEnvironment) {
703
+ messageContent = Buffer.from(
704
+ message.message,
705
+ "base64"
706
+ ).toString("utf-8");
707
+ } else {
708
+ messageContent = new TextDecoder().decode(
709
+ Uint8Array.from(atob(message.message), (c) => c.charCodeAt(0))
710
+ );
711
+ }
712
+ let messageJson = {};
713
+ try {
714
+ messageJson = JSON.parse(messageContent);
715
+ } catch (parseError) {
716
+ this.logger.debug(
717
+ `Message content is not valid JSON, using raw: ${messageContent}`
718
+ );
719
+ messageJson = { raw_content: messageContent };
720
+ }
721
+ const parsedContent = messageJson;
722
+ const hcsMsg = {
723
+ ...parsedContent,
724
+ consensus_timestamp: message.consensus_timestamp,
725
+ sequence_number: message.sequence_number,
726
+ payer_account_id: message.payer_account_id,
727
+ topic_id: message.topic_id,
728
+ running_hash: message.running_hash,
729
+ running_hash_version: message.running_hash_version,
730
+ chunk_info: message.chunk_info,
731
+ created: new Date(
732
+ Number(message.consensus_timestamp.split(".")[0]) * 1e3 + Number(message.consensus_timestamp.split(".")[1] || 0) / 1e6
733
+ ),
734
+ payer: message.payer_account_id
735
+ };
736
+ messages.push(hcsMsg);
737
+ } catch (error) {
738
+ this.logger.error(
739
+ `Error processing individual message: ${error.message}`
740
+ );
741
+ }
742
+ }
743
+ }
744
+ if (options?.limit && messages.length >= options.limit) break;
745
+ nextUrl = data.links?.next ? `${data.links.next}` : "";
846
746
  }
847
- const hbarAmount = this.calculateHbarAmount(
848
- creditsToPurchase,
849
- creditsPerHbar
747
+ return messages;
748
+ } catch (e) {
749
+ const error = e;
750
+ this.logger.error(
751
+ `Error querying filtered topic messages for ${topicId}: ${error.message}`
850
752
  );
851
- await this.purchaseCreditsWithHbar({
852
- accountId: details.accountId.trim(),
853
- privateKey: details.privateKey.trim(),
854
- hbarAmount,
855
- memo: details.memo ?? "Registry Broker auto top-up",
856
- metadata: {
857
- shortfallCredits: shortfall,
858
- requiredCredits: quote.requiredCredits,
859
- purchasedCredits: creditsToPurchase
860
- }
861
- });
862
- }
863
- const finalQuote = await this.getRegistrationQuote(payload);
864
- if ((finalQuote.shortfallCredits ?? 0) > 0) {
865
- throw new Error("Unable to purchase sufficient credits for registration");
866
- }
867
- }
868
- extractInsufficientCreditsDetails(error) {
869
- if (!(error instanceof RegistryBrokerError) || error.status !== 402) {
870
- return null;
871
- }
872
- const body = error.body;
873
- if (!body || typeof body !== "object" || Array.isArray(body)) {
874
- return null;
875
- }
876
- const maybeShortfall = body["shortfallCredits"];
877
- if (typeof maybeShortfall !== "number" || maybeShortfall <= 0) {
878
753
  return null;
879
754
  }
880
- return { shortfallCredits: maybeShortfall };
881
- }
882
- async createLedgerChallenge(payload) {
883
- const resolvedNetwork = canonicalizeLedgerNetwork(payload.network);
884
- const network = resolvedNetwork.kind === "hedera" ? resolvedNetwork.hederaNetwork ?? resolvedNetwork.canonical : resolvedNetwork.canonical;
885
- const raw = await this.requestJson("/auth/ledger/challenge", {
886
- method: "POST",
887
- headers: { "content-type": "application/json" },
888
- body: {
889
- accountId: payload.accountId,
890
- network
891
- }
892
- });
893
- return this.parseWithSchema(
894
- raw,
895
- ledgerChallengeResponseSchema,
896
- "ledger challenge response"
897
- );
898
- }
899
- async verifyLedgerChallenge(payload) {
900
- const resolvedNetwork = canonicalizeLedgerNetwork(payload.network);
901
- const network = resolvedNetwork.kind === "hedera" ? resolvedNetwork.hederaNetwork ?? resolvedNetwork.canonical : resolvedNetwork.canonical;
902
- const body = {
903
- challengeId: payload.challengeId,
904
- accountId: payload.accountId,
905
- network,
906
- signature: payload.signature
907
- };
908
- if (payload.signatureKind) {
909
- body.signatureKind = payload.signatureKind;
910
- }
911
- if (payload.publicKey) {
912
- body.publicKey = payload.publicKey;
913
- }
914
- if (typeof payload.expiresInMinutes === "number") {
915
- body.expiresInMinutes = payload.expiresInMinutes;
916
- }
917
- const raw = await this.requestJson("/auth/ledger/verify", {
918
- method: "POST",
919
- headers: { "content-type": "application/json" },
920
- body
921
- });
922
- const result = this.parseWithSchema(
923
- raw,
924
- ledgerVerifyResponseSchema,
925
- "ledger verification response"
926
- );
927
- this.setLedgerApiKey(result.key);
928
- return result;
929
- }
930
- async authenticateWithLedger(options) {
931
- const challenge = await this.createLedgerChallenge({
932
- accountId: options.accountId,
933
- network: options.network
934
- });
935
- const signed = await this.resolveLedgerAuthSignature(
936
- challenge.message,
937
- options
938
- );
939
- const verification = await this.verifyLedgerChallenge({
940
- challengeId: challenge.challengeId,
941
- accountId: options.accountId,
942
- network: options.network,
943
- signature: signed.signature,
944
- signatureKind: signed.signatureKind,
945
- publicKey: signed.publicKey,
946
- expiresInMinutes: options.expiresInMinutes
947
- });
948
- return verification;
949
755
  }
950
- async resolveLedgerAuthSignature(message, options) {
951
- if (typeof options.sign === "function") {
952
- const result = await options.sign(message);
953
- if (!result || typeof result.signature !== "string" || result.signature.length === 0) {
954
- throw new Error("Custom ledger signer failed to produce a signature.");
756
+ /**
757
+ * Retrieves token balances for a given account ID.
758
+ * @param accountId The ID of the account.
759
+ * @param limit The maximum number of tokens to return.
760
+ * @returns A promise that resolves to an array of AccountTokenBalance or null.
761
+ */
762
+ async getAccountTokens(accountId, limit = 100) {
763
+ this.logger.info(`Getting tokens for account ${accountId}`);
764
+ let allTokens = [];
765
+ let endpoint = `/api/v1/accounts/${accountId}/tokens?limit=${limit}`;
766
+ try {
767
+ for (let i = 0; i < 10 && endpoint; i++) {
768
+ const response = await this._requestWithRetry(endpoint);
769
+ if (response && response.tokens) {
770
+ allTokens = allTokens.concat(response.tokens);
771
+ }
772
+ endpoint = response.links?.next || "";
773
+ if (!endpoint || limit && allTokens.length >= limit) {
774
+ if (limit && allTokens.length > limit) {
775
+ allTokens = allTokens.slice(0, limit);
776
+ }
777
+ break;
778
+ }
955
779
  }
956
- return result;
957
- }
958
- if (!options.signer || typeof options.signer.sign !== "function") {
959
- throw new Error(
960
- "Ledger authentication requires a Hedera Signer or custom sign function."
780
+ return allTokens;
781
+ } catch (error) {
782
+ this.logger.error(
783
+ `Error fetching tokens for account ${accountId}: ${error.message}`
961
784
  );
785
+ return null;
962
786
  }
963
- const payload = Buffer.from(message, "utf8");
964
- const signatures = await options.signer.sign([payload]);
965
- const signatureEntry = signatures?.[0];
966
- if (!signatureEntry) {
967
- throw new Error("Signer did not return any signatures.");
968
- }
969
- let derivedPublicKey;
970
- if (signatureEntry.publicKey) {
971
- derivedPublicKey = signatureEntry.publicKey.toString();
972
- } else if (typeof options.signer.getAccountKey === "function") {
973
- const accountKey = await options.signer.getAccountKey();
974
- if (accountKey && typeof accountKey.toString === "function") {
975
- derivedPublicKey = accountKey.toString();
976
- }
977
- }
978
- return {
979
- signature: Buffer.from(signatureEntry.signature).toString("base64"),
980
- signatureKind: "raw",
981
- publicKey: derivedPublicKey
982
- };
983
787
  }
984
- async authenticateWithLedgerCredentials(options) {
985
- const {
986
- accountId,
987
- network,
988
- signer,
989
- sign,
990
- hederaPrivateKey,
991
- evmPrivateKey,
992
- expiresInMinutes,
993
- setAccountHeader = true,
994
- label,
995
- logger
996
- } = options;
997
- const resolvedNetwork = canonicalizeLedgerNetwork(network);
998
- const labelSuffix = label ? ` for ${label}` : "";
999
- const networkPayload = resolvedNetwork.canonical;
1000
- const authOptions = {
1001
- accountId,
1002
- network: networkPayload,
1003
- expiresInMinutes
1004
- };
1005
- if (sign) {
1006
- authOptions.sign = sign;
1007
- } else if (signer) {
1008
- authOptions.signer = signer;
1009
- } else if (hederaPrivateKey) {
1010
- if (resolvedNetwork.kind !== "hedera" || !resolvedNetwork.hederaNetwork) {
1011
- throw new Error(
1012
- "hederaPrivateKey can only be used with hedera:mainnet or hedera:testnet networks."
1013
- );
1014
- }
1015
- authOptions.signer = createPrivateKeySigner({
1016
- accountId,
1017
- privateKey: hederaPrivateKey,
1018
- network: resolvedNetwork.hederaNetwork
1019
- });
1020
- } else if (evmPrivateKey) {
1021
- if (resolvedNetwork.kind !== "evm") {
1022
- throw new Error(
1023
- "evmPrivateKey can only be used with CAIP-2 EVM networks (eip155:<chainId>)."
1024
- );
1025
- }
1026
- const formattedKey = evmPrivateKey.startsWith("0x") ? evmPrivateKey : `0x${evmPrivateKey}`;
1027
- const account = privateKeyToAccount(formattedKey);
1028
- authOptions.sign = async (message) => ({
1029
- signature: await account.signMessage({ message }),
1030
- signatureKind: "evm",
1031
- publicKey: account.publicKey
1032
- });
1033
- } else {
1034
- throw new Error(
1035
- "Provide a signer, sign function, hederaPrivateKey, or evmPrivateKey to authenticate with the ledger."
788
+ /**
789
+ * Retrieves transaction details by consensus timestamp.
790
+ * @param timestamp The consensus timestamp of the transaction (e.g., "1629400000.000000000").
791
+ * @returns A promise that resolves to the transaction details or null.
792
+ */
793
+ async getTransactionByTimestamp(timestamp) {
794
+ this.logger.info(`Getting transaction by timestamp: ${timestamp}`);
795
+ try {
796
+ const response = await this._requestWithRetry(`/api/v1/transactions?timestamp=${timestamp}&limit=1`);
797
+ return response.transactions;
798
+ } catch (error) {
799
+ this.logger.error(
800
+ `Error fetching transaction by timestamp ${timestamp}: ${error}`
1036
801
  );
1037
- }
1038
- logger?.info?.(
1039
- `Authenticating ledger account ${accountId} (${resolvedNetwork.canonical})${labelSuffix}...`
802
+ return [];
803
+ }
804
+ }
805
+ /**
806
+ * Retrieves NFTs for a given account ID, optionally filtered by token ID.
807
+ * @param accountId The ID of the account.
808
+ * @param tokenId Optional ID of the token to filter NFTs by.
809
+ * @param limit The maximum number of NFTs to return per page (API has its own max).
810
+ * @returns A promise that resolves to an array of NftDetail or null.
811
+ */
812
+ async getAccountNfts(accountId, tokenId, limit = 100) {
813
+ this.logger.info(
814
+ `Getting NFTs for account ${accountId}${tokenId ? ` for token ${tokenId}` : ""}`
1040
815
  );
1041
- const verification = await this.authenticateWithLedger(authOptions);
1042
- if (setAccountHeader) {
1043
- this.setDefaultHeader("x-account-id", verification.accountId);
816
+ let allNfts = [];
817
+ let endpoint = `/api/v1/accounts/${accountId}/nfts?limit=${limit}`;
818
+ if (tokenId) {
819
+ endpoint += `&token.id=${tokenId}`;
1044
820
  }
1045
- logger?.info?.(
1046
- `Ledger authentication complete${labelSuffix}. Issued key prefix: ${verification.apiKey.prefix}…${verification.apiKey.lastFour}`
1047
- );
1048
- return verification;
1049
- }
1050
- async listProtocols() {
1051
- const raw = await this.requestJson("/protocols", {
1052
- method: "GET"
1053
- });
1054
- return this.parseWithSchema(
1055
- raw,
1056
- protocolsResponseSchema,
1057
- "protocols response"
1058
- );
1059
- }
1060
- async detectProtocol(message) {
1061
- const raw = await this.requestJson("/detect-protocol", {
1062
- method: "POST",
1063
- body: { message },
1064
- headers: { "content-type": "application/json" }
1065
- });
1066
- return this.parseWithSchema(
1067
- raw,
1068
- detectProtocolResponseSchema,
1069
- "detect protocol response"
1070
- );
1071
- }
1072
- async registrySearchByNamespace(registry, query) {
1073
- const params = new URLSearchParams();
1074
- if (query) {
1075
- params.set("q", query);
1076
- }
1077
- const suffix = params.size > 0 ? `?${params.toString()}` : "";
1078
- const raw = await this.requestJson(
1079
- `/registries/${encodeURIComponent(registry)}/search${suffix}`,
1080
- {
1081
- method: "GET"
1082
- }
1083
- );
1084
- return this.parseWithSchema(
1085
- raw,
1086
- registrySearchByNamespaceSchema,
1087
- "registry search response"
1088
- );
1089
- }
1090
- async vectorSearch(request) {
1091
821
  try {
1092
- const raw = await this.requestJson("/search", {
1093
- method: "POST",
1094
- body: request,
1095
- headers: { "content-type": "application/json" }
1096
- });
1097
- return this.parseWithSchema(
1098
- raw,
1099
- vectorSearchResponseSchema,
1100
- "vector search response"
1101
- );
1102
- } catch (error) {
1103
- if (error instanceof RegistryBrokerError && error.status === 501) {
1104
- const fallback = await this.search(
1105
- this.buildVectorFallbackSearchParams(request)
1106
- );
1107
- return this.convertSearchResultToVectorResponse(fallback);
822
+ for (let i = 0; i < 10 && endpoint; i++) {
823
+ const response = await this._requestWithRetry(endpoint);
824
+ if (response && response.nfts) {
825
+ const nftsWithUri = response.nfts.map((nft) => {
826
+ let tokenUri = void 0;
827
+ if (nft.metadata) {
828
+ try {
829
+ if (this.isServerEnvironment) {
830
+ tokenUri = Buffer.from(nft.metadata, "base64").toString(
831
+ "utf-8"
832
+ );
833
+ } else {
834
+ tokenUri = new TextDecoder().decode(
835
+ Uint8Array.from(atob(nft.metadata), (c) => c.charCodeAt(0))
836
+ );
837
+ }
838
+ } catch (e) {
839
+ this.logger.warn(
840
+ `Failed to decode metadata for NFT ${nft.token_id} SN ${nft.serial_number}: ${e.message}`
841
+ );
842
+ }
843
+ }
844
+ return { ...nft, token_uri: tokenUri };
845
+ });
846
+ allNfts = allNfts.concat(nftsWithUri);
847
+ }
848
+ endpoint = response.links?.next || "";
849
+ if (!endpoint) break;
1108
850
  }
1109
- throw error;
851
+ return allNfts;
852
+ } catch (error) {
853
+ this.logger.error(
854
+ `Error fetching NFTs for account ${accountId}: ${error.message}`
855
+ );
856
+ return null;
1110
857
  }
1111
858
  }
1112
- async searchStatus() {
1113
- const raw = await this.requestJson("/search/status", {
1114
- method: "GET"
1115
- });
1116
- return this.parseWithSchema(
1117
- raw,
1118
- searchStatusResponseSchema,
1119
- "search status response"
1120
- );
1121
- }
1122
- async adaptersDetailed() {
1123
- const raw = await this.requestJson("/adapters/details", {
1124
- method: "GET"
1125
- });
1126
- return this.parseWithSchema(
1127
- raw,
1128
- adapterDetailsResponseSchema,
1129
- "adapter details response"
1130
- );
1131
- }
1132
- async websocketStats() {
1133
- const raw = await this.requestJson("/websocket/stats", {
1134
- method: "GET"
1135
- });
1136
- return this.parseWithSchema(
1137
- raw,
1138
- websocketStatsResponseSchema,
1139
- "websocket stats response"
1140
- );
1141
- }
1142
- async metricsSummary() {
1143
- const raw = await this.requestJson("/metrics", {
1144
- method: "GET"
1145
- });
1146
- return this.parseWithSchema(
1147
- raw,
1148
- metricsSummaryResponseSchema,
1149
- "metrics summary response"
1150
- );
1151
- }
1152
- async validateUaid(uaid) {
1153
- const raw = await this.requestJson(
1154
- `/uaids/validate/${encodeURIComponent(uaid)}`,
1155
- {
1156
- method: "GET"
1157
- }
859
+ /**
860
+ * Validates NFT ownership by checking if a specific serial number of a token ID exists for an account.
861
+ * @param accountId The ID of the account.
862
+ * @param tokenId The ID of the NFT's token.
863
+ * @param serialNumber The serial number of the NFT.
864
+ * @returns A promise that resolves to the NftDetail if owned, or null otherwise.
865
+ */
866
+ async validateNFTOwnership(accountId, tokenId, serialNumber) {
867
+ this.logger.info(
868
+ `Validating ownership of NFT ${tokenId} SN ${serialNumber} for account ${accountId}`
1158
869
  );
1159
- return this.parseWithSchema(
1160
- raw,
1161
- uaidValidationResponseSchema,
1162
- "UAID validation response"
1163
- );
1164
- }
1165
- async getUaidConnectionStatus(uaid) {
1166
- const raw = await this.requestJson(
1167
- `/uaids/connections/${encodeURIComponent(uaid)}/status`,
1168
- {
1169
- method: "GET"
870
+ try {
871
+ const nfts = await this.getAccountNfts(accountId, tokenId);
872
+ if (nfts) {
873
+ const foundNft = nfts.find(
874
+ (nft) => nft.token_id === tokenId && nft.serial_number === serialNumber
875
+ );
876
+ return foundNft || null;
1170
877
  }
1171
- );
1172
- return this.parseWithSchema(
1173
- raw,
1174
- uaidConnectionStatusSchema,
1175
- "UAID connection status"
1176
- );
1177
- }
1178
- async closeUaidConnection(uaid) {
1179
- await this.request(`/uaids/connections/${encodeURIComponent(uaid)}`, {
1180
- method: "DELETE"
1181
- });
878
+ return null;
879
+ } catch (error) {
880
+ this.logger.error(`Error validating NFT ownership: ${error.message}`);
881
+ return null;
882
+ }
1182
883
  }
1183
- async dashboardStats() {
1184
- const raw = await this.requestJson("/dashboard/stats", {
1185
- method: "GET"
1186
- });
1187
- return this.parseWithSchema(
1188
- raw,
1189
- dashboardStatsResponseSchema,
1190
- "dashboard stats response"
884
+ /**
885
+ * Performs a read-only query against a smart contract (eth_call like).
886
+ * @param contractIdOrAddress The contract ID (e.g., "0.0.123") or EVM address (e.g., "0x...").
887
+ * @param functionSelector The function selector and encoded parameters (e.g., "0xabcdef12...").
888
+ * @param payerAccountId The account ID of the payer (not strictly payer for read-only, but often required as 'from').
889
+ * @param estimate Whether this is an estimate call. Mirror node might not support this directly in /contracts/call for true estimation.
890
+ * @param block Block parameter, e.g., "latest", "pending", or block number.
891
+ * @param value The value in tinybars to send with the call (for payable view/pure functions, usually 0).
892
+ * @returns A promise that resolves to the contract call query response or null.
893
+ */
894
+ async readSmartContractQuery(contractIdOrAddress, functionSelector, payerAccountId, options) {
895
+ this.logger.info(
896
+ `Reading smart contract ${contractIdOrAddress} with selector ${functionSelector}`
1191
897
  );
1192
- }
1193
- async adapters() {
1194
- const raw = await this.requestJson("/adapters", {
1195
- method: "GET"
898
+ const toAddress = contractIdOrAddress.startsWith("0x") ? contractIdOrAddress : `0x${AccountId.fromString(contractIdOrAddress).toSolidityAddress()}`;
899
+ const fromAddress = payerAccountId.startsWith("0x") ? payerAccountId : `0x${AccountId.fromString(payerAccountId).toSolidityAddress()}`;
900
+ const body = {
901
+ block: options?.block || "latest",
902
+ data: functionSelector,
903
+ estimate: options?.estimate || false,
904
+ from: fromAddress,
905
+ to: toAddress,
906
+ gas: options?.gas,
907
+ gasPrice: options?.gasPrice,
908
+ value: options?.value || 0
909
+ };
910
+ Object.keys(body).forEach((key) => {
911
+ const K = key;
912
+ if (body[K] === void 0) {
913
+ delete body[K];
914
+ }
1196
915
  });
1197
- return this.parseWithSchema(
1198
- raw,
1199
- adaptersResponseSchema,
1200
- "adapters response"
1201
- );
1202
- }
1203
- async facets(adapter) {
1204
- const params = new URLSearchParams();
1205
- if (adapter) {
1206
- params.set("adapter", adapter);
916
+ try {
917
+ const url = this.constructUrl("/api/v1/contracts/call");
918
+ const response = await this._fetchWithRetry(
919
+ url,
920
+ {
921
+ method: "POST",
922
+ body: JSON.stringify(body),
923
+ headers: {
924
+ "Content-Type": "application/json"
925
+ }
926
+ }
927
+ );
928
+ return response;
929
+ } catch (error) {
930
+ this.logger.error(
931
+ `Error reading smart contract ${contractIdOrAddress}: ${error.message}`
932
+ );
933
+ return null;
1207
934
  }
1208
- const suffix = params.size > 0 ? `?${params.toString()}` : "";
1209
- const raw = await this.requestJson(`/search/facets${suffix}`, {
1210
- method: "GET"
1211
- });
1212
- return this.parseWithSchema(
1213
- raw,
1214
- searchFacetsResponseSchema,
1215
- "search facets response"
1216
- );
1217
935
  }
1218
- async createSession(payload, allowHistoryAutoTopUp = true) {
1219
- const body = {};
1220
- if ("uaid" in payload && payload.uaid) {
1221
- body.uaid = payload.uaid;
936
+ /**
937
+ * Retrieves outstanding token airdrops sent by an account.
938
+ * @param accountId The ID of the account that sent the airdrops.
939
+ * @param options Optional parameters for filtering airdrops.
940
+ * @returns A promise that resolves to an array of TokenAirdrop or null.
941
+ */
942
+ async getOutstandingTokenAirdrops(accountId, options) {
943
+ this.logger.info(
944
+ `Getting outstanding token airdrops sent by account ${accountId}`
945
+ );
946
+ let endpoint = `/api/v1/accounts/${accountId}/airdrops/outstanding`;
947
+ const params = new URLSearchParams();
948
+ if (options?.limit) {
949
+ params.append("limit", options.limit.toString());
1222
950
  }
1223
- if ("agentUrl" in payload && payload.agentUrl) {
1224
- body.agentUrl = payload.agentUrl;
951
+ if (options?.order) {
952
+ params.append("order", options.order);
1225
953
  }
1226
- if (payload.auth) {
1227
- body.auth = serialiseAuthConfig(payload.auth);
954
+ if (options?.receiverId) {
955
+ params.append("receiver.id", options.receiverId);
1228
956
  }
1229
- if (payload.historyTtlSeconds !== void 0) {
1230
- body.historyTtlSeconds = payload.historyTtlSeconds;
957
+ if (options?.serialNumber) {
958
+ params.append("serialnumber", options.serialNumber);
1231
959
  }
1232
- if (payload.encryptionRequested !== void 0) {
1233
- body.encryptionRequested = payload.encryptionRequested;
960
+ if (options?.tokenId) {
961
+ params.append("token.id", options.tokenId);
1234
962
  }
1235
- if (payload.senderUaid) {
1236
- body.senderUaid = payload.senderUaid;
963
+ const queryString = params.toString();
964
+ if (queryString) {
965
+ endpoint += `?${queryString}`;
1237
966
  }
1238
967
  try {
1239
- const raw = await this.requestJson("/chat/session", {
1240
- method: "POST",
1241
- body,
1242
- headers: { "content-type": "application/json" }
1243
- });
1244
- return this.parseWithSchema(
1245
- raw,
1246
- createSessionResponseSchema,
1247
- "chat session response"
1248
- );
968
+ const response = await this._requestWithRetry(endpoint);
969
+ return response.airdrops || [];
1249
970
  } catch (error) {
1250
- if (allowHistoryAutoTopUp && this.shouldAutoTopUpHistory(payload, error)) {
1251
- await this.executeHistoryAutoTopUp("chat.session");
1252
- return this.createSession(payload, false);
1253
- }
1254
- throw error;
1255
- }
1256
- }
1257
- async startChat(options) {
1258
- if ("uaid" in options && options.uaid) {
1259
- return this.startConversation({
1260
- uaid: options.uaid,
1261
- senderUaid: options.senderUaid,
1262
- historyTtlSeconds: options.historyTtlSeconds,
1263
- auth: options.auth,
1264
- encryption: options.encryption,
1265
- onSessionCreated: options.onSessionCreated
1266
- });
1267
- }
1268
- if ("agentUrl" in options && options.agentUrl) {
1269
- const session = await this.createSession({
1270
- agentUrl: options.agentUrl,
1271
- auth: options.auth,
1272
- historyTtlSeconds: options.historyTtlSeconds,
1273
- senderUaid: options.senderUaid
1274
- });
1275
- options.onSessionCreated?.(session.sessionId);
1276
- return this.createPlaintextConversationHandle(
1277
- session.sessionId,
1278
- session.encryption ?? null,
1279
- options.auth,
1280
- { agentUrl: options.agentUrl, uaid: options.uaid }
971
+ this.logger.error(
972
+ `Error fetching outstanding token airdrops for account ${accountId}: ${error.message}`
1281
973
  );
974
+ return null;
1282
975
  }
1283
- throw new Error("startChat requires either uaid or agentUrl");
1284
976
  }
1285
- async startConversation(options) {
1286
- const preference = options.encryption?.preference ?? "preferred";
1287
- const requestEncryption = preference !== "disabled";
1288
- if (!requestEncryption) {
1289
- const session = await this.createSession({
1290
- uaid: options.uaid,
1291
- auth: options.auth,
1292
- historyTtlSeconds: options.historyTtlSeconds,
1293
- senderUaid: options.senderUaid,
1294
- encryptionRequested: false
1295
- });
1296
- options.onSessionCreated?.(session.sessionId);
1297
- return this.createPlaintextConversationHandle(
1298
- session.sessionId,
1299
- session.encryption ?? null,
1300
- options.auth,
1301
- { uaid: options.uaid }
1302
- );
977
+ /**
978
+ * Retrieves pending token airdrops received by an account.
979
+ * @param accountId The ID of the account that received the airdrops.
980
+ * @param options Optional parameters for filtering airdrops.
981
+ * @returns A promise that resolves to an array of TokenAirdrop or null.
982
+ */
983
+ async getPendingTokenAirdrops(accountId, options) {
984
+ this.logger.info(
985
+ `Getting pending token airdrops received by account ${accountId}`
986
+ );
987
+ let endpoint = `/api/v1/accounts/${accountId}/airdrops/pending`;
988
+ const params = new URLSearchParams();
989
+ if (options?.limit) {
990
+ params.append("limit", options.limit.toString());
991
+ }
992
+ if (options?.order) {
993
+ params.append("order", options.order);
994
+ }
995
+ if (options?.senderId) {
996
+ params.append("sender.id", options.senderId);
997
+ }
998
+ if (options?.serialNumber) {
999
+ params.append("serialnumber", options.serialNumber);
1000
+ }
1001
+ if (options?.tokenId) {
1002
+ params.append("token.id", options.tokenId);
1003
+ }
1004
+ const queryString = params.toString();
1005
+ if (queryString) {
1006
+ endpoint += `?${queryString}`;
1303
1007
  }
1304
1008
  try {
1305
- const handle = await this.encryptedChatManager.startSession({
1306
- uaid: options.uaid,
1307
- senderUaid: options.senderUaid,
1308
- historyTtlSeconds: options.historyTtlSeconds,
1309
- handshakeTimeoutMs: options.encryption?.handshakeTimeoutMs,
1310
- pollIntervalMs: options.encryption?.pollIntervalMs,
1311
- onSessionCreated: (sessionId) => {
1312
- options.onSessionCreated?.(sessionId);
1313
- },
1314
- auth: options.auth
1315
- });
1316
- return handle;
1009
+ const response = await this._requestWithRetry(endpoint);
1010
+ return response.airdrops || [];
1317
1011
  } catch (error) {
1318
- if (error instanceof EncryptionUnavailableError) {
1319
- if (preference === "required") {
1320
- throw error;
1321
- }
1322
- return this.createPlaintextConversationHandle(
1323
- error.sessionId,
1324
- error.summary ?? null,
1325
- options.auth,
1326
- { uaid: options.uaid }
1327
- );
1328
- }
1329
- throw error;
1012
+ this.logger.error(
1013
+ `Error fetching pending token airdrops for account ${accountId}: ${error.message}`
1014
+ );
1015
+ return null;
1330
1016
  }
1331
1017
  }
1332
- async acceptConversation(options) {
1333
- const preference = options.encryption?.preference ?? "preferred";
1334
- if (preference === "disabled") {
1335
- return this.createPlaintextConversationHandle(options.sessionId, null);
1018
+ /**
1019
+ * Retrieves blocks from the network.
1020
+ * @param options Optional parameters for filtering blocks.
1021
+ * @returns A promise that resolves to an array of Block or null.
1022
+ */
1023
+ async getBlocks(options) {
1024
+ this.logger.info("Getting blocks from the network");
1025
+ let endpoint = `/api/v1/blocks`;
1026
+ const params = new URLSearchParams();
1027
+ if (options?.limit) {
1028
+ params.append("limit", options.limit.toString());
1029
+ }
1030
+ if (options?.order) {
1031
+ params.append("order", options.order);
1032
+ }
1033
+ if (options?.timestamp) {
1034
+ params.append("timestamp", options.timestamp);
1035
+ }
1036
+ if (options?.blockNumber) {
1037
+ params.append("block.number", options.blockNumber);
1038
+ }
1039
+ const queryString = params.toString();
1040
+ if (queryString) {
1041
+ endpoint += `?${queryString}`;
1336
1042
  }
1337
1043
  try {
1338
- const handle = await this.encryptedChatManager.acceptSession({
1339
- sessionId: options.sessionId,
1340
- responderUaid: options.responderUaid,
1341
- handshakeTimeoutMs: options.encryption?.handshakeTimeoutMs,
1342
- pollIntervalMs: options.encryption?.pollIntervalMs
1343
- });
1344
- return handle;
1044
+ const response = await this._requestWithRetry(endpoint);
1045
+ return response.blocks || [];
1345
1046
  } catch (error) {
1346
- if (error instanceof EncryptionUnavailableError && preference !== "required") {
1347
- return this.createPlaintextConversationHandle(
1348
- options.sessionId,
1349
- null,
1350
- void 0,
1351
- { uaid: options.responderUaid }
1352
- );
1353
- }
1354
- throw error;
1047
+ this.logger.error(`Error fetching blocks: ${error.message}`);
1048
+ return null;
1355
1049
  }
1356
1050
  }
1357
- async fetchHistorySnapshot(sessionId, options) {
1358
- if (!sessionId || sessionId.trim().length === 0) {
1359
- throw new Error("sessionId is required to fetch chat history");
1360
- }
1361
- const raw = await this.requestJson(
1362
- `/chat/session/${encodeURIComponent(sessionId)}/history`,
1363
- {
1364
- method: "GET"
1365
- }
1366
- );
1367
- const snapshot = this.parseWithSchema(
1368
- raw,
1369
- chatHistorySnapshotResponseSchema,
1370
- "chat history snapshot response"
1371
- );
1372
- return this.attachDecryptedHistory(sessionId, snapshot, options);
1373
- }
1374
- attachDecryptedHistory(sessionId, snapshot, options) {
1375
- const shouldDecrypt = options?.decrypt !== void 0 ? options.decrypt : this.encryptionOptions?.autoDecryptHistory === true;
1376
- if (!shouldDecrypt) {
1377
- return snapshot;
1378
- }
1379
- const context = this.resolveDecryptionContext(sessionId, options);
1380
- if (!context) {
1381
- throw new Error(
1382
- "Unable to decrypt chat history: encryption context unavailable"
1051
+ /**
1052
+ * Retrieves a specific block by number or hash.
1053
+ * @param blockNumberOrHash The block number or hash.
1054
+ * @returns A promise that resolves to a Block or null.
1055
+ */
1056
+ async getBlock(blockNumberOrHash) {
1057
+ this.logger.info(`Getting block ${blockNumberOrHash}`);
1058
+ try {
1059
+ const response = await this._requestWithRetry(
1060
+ `/api/v1/blocks/${blockNumberOrHash}`
1061
+ );
1062
+ return response;
1063
+ } catch (error) {
1064
+ this.logger.error(
1065
+ `Error fetching block ${blockNumberOrHash}: ${error.message}`
1383
1066
  );
1067
+ return null;
1384
1068
  }
1385
- const decryptedHistory = snapshot.history.map((entry) => ({
1386
- entry,
1387
- plaintext: this.decryptHistoryEntryFromContext(sessionId, entry, context)
1388
- }));
1389
- return { ...snapshot, decryptedHistory };
1390
1069
  }
1391
- registerConversationContext(context) {
1392
- const normalized = {
1393
- sessionId: context.sessionId,
1394
- sharedSecret: Buffer.from(context.sharedSecret),
1395
- identity: context.identity ? { ...context.identity } : void 0
1396
- };
1397
- const entries = this.conversationContexts.get(context.sessionId) ?? [];
1398
- const existingIndex = entries.findIndex(
1399
- (existing) => this.identitiesMatch(existing.identity, normalized.identity)
1400
- );
1401
- if (existingIndex >= 0) {
1402
- entries[existingIndex] = normalized;
1403
- } else {
1404
- entries.push(normalized);
1070
+ /**
1071
+ * Retrieves contract entities from the network.
1072
+ * @param options Optional parameters for filtering contracts.
1073
+ * @returns A promise that resolves to an array of ContractEntity or null.
1074
+ */
1075
+ async getContracts(options) {
1076
+ this.logger.info("Getting contracts from the network");
1077
+ let url = `/api/v1/contracts`;
1078
+ const params = new URLSearchParams();
1079
+ if (options?.contractId) {
1080
+ params.append("contract.id", options.contractId);
1405
1081
  }
1406
- this.conversationContexts.set(context.sessionId, entries);
1407
- }
1408
- // Exposed for EncryptedChatManager to persist decryption context
1409
- registerConversationContextForEncryption(context) {
1410
- this.registerConversationContext(context);
1411
- }
1412
- resolveDecryptionContext(sessionId, options) {
1413
- if (options?.sharedSecret) {
1414
- return {
1415
- sessionId,
1416
- sharedSecret: this.normalizeSharedSecret(options.sharedSecret),
1417
- identity: options.identity
1418
- };
1082
+ if (options?.limit) {
1083
+ params.append("limit", options.limit.toString());
1419
1084
  }
1420
- const contexts = this.conversationContexts.get(sessionId);
1421
- if (!contexts || contexts.length === 0) {
1422
- return null;
1085
+ if (options?.order) {
1086
+ params.append("order", options.order);
1423
1087
  }
1424
- if (options?.identity) {
1425
- const match = contexts.find(
1426
- (context) => this.identitiesMatch(context.identity, options.identity)
1427
- );
1428
- if (match) {
1429
- return match;
1430
- }
1088
+ const queryString = params.toString();
1089
+ if (queryString) {
1090
+ url += `?${queryString}`;
1091
+ }
1092
+ try {
1093
+ const response = await this._requestWithRetry(url);
1094
+ return response.contracts || [];
1095
+ } catch (error) {
1096
+ this.logger.error(`Error fetching contracts: ${error.message}`);
1097
+ return null;
1431
1098
  }
1432
- return contexts[0];
1433
1099
  }
1434
- decryptHistoryEntryFromContext(sessionId, entry, context) {
1435
- const envelope = entry.cipherEnvelope;
1436
- if (!envelope) {
1437
- return entry.content;
1100
+ /**
1101
+ * Retrieves a specific contract by ID or address.
1102
+ * @param contractIdOrAddress The contract ID or EVM address.
1103
+ * @param timestamp Optional timestamp for historical data.
1104
+ * @returns A promise that resolves to a ContractEntity or null.
1105
+ */
1106
+ async getContract(contractIdOrAddress, timestamp) {
1107
+ this.logger.info(`Getting contract ${contractIdOrAddress}`);
1108
+ let url = `/api/v1/contracts/${contractIdOrAddress}`;
1109
+ if (timestamp) {
1110
+ url += `?timestamp=${timestamp}`;
1438
1111
  }
1439
- const secret = Buffer.from(context.sharedSecret);
1440
1112
  try {
1441
- return this.encryption.decryptCipherEnvelope({
1442
- envelope,
1443
- sharedSecret: secret
1444
- });
1445
- } catch (_error) {
1113
+ const response = await this._requestWithRetry(url);
1114
+ return response;
1115
+ } catch (error) {
1116
+ this.logger.error(
1117
+ `Error fetching contract ${contractIdOrAddress}: ${error.message}`
1118
+ );
1446
1119
  return null;
1447
1120
  }
1448
1121
  }
1449
- identitiesMatch(a, b) {
1450
- if (!a && !b) {
1451
- return true;
1122
+ /**
1123
+ * Retrieves contract results from the network.
1124
+ * @param options Optional parameters for filtering contract results.
1125
+ * @returns A promise that resolves to an array of ContractResult or null.
1126
+ */
1127
+ async getContractResults(options) {
1128
+ this.logger.info("Getting contract results from the network");
1129
+ let url = `/api/v1/contracts/results`;
1130
+ const params = new URLSearchParams();
1131
+ if (options?.from) {
1132
+ params.append("from", options.from);
1452
1133
  }
1453
- if (!a || !b) {
1454
- return false;
1134
+ if (options?.blockHash) {
1135
+ params.append("block.hash", options.blockHash);
1455
1136
  }
1456
- if (a.uaid && b.uaid && a.uaid.toLowerCase() === b.uaid.toLowerCase()) {
1457
- return true;
1137
+ if (options?.blockNumber) {
1138
+ params.append("block.number", options.blockNumber);
1458
1139
  }
1459
- if (a.ledgerAccountId && b.ledgerAccountId && a.ledgerAccountId.toLowerCase() === b.ledgerAccountId.toLowerCase()) {
1460
- return true;
1140
+ if (options?.internal !== void 0) {
1141
+ params.append("internal", options.internal.toString());
1461
1142
  }
1462
- if (a.userId && b.userId && a.userId === b.userId) {
1463
- return true;
1143
+ if (options?.limit) {
1144
+ params.append("limit", options.limit.toString());
1464
1145
  }
1465
- if (a.email && b.email && a.email.toLowerCase() === b.email.toLowerCase()) {
1466
- return true;
1146
+ if (options?.order) {
1147
+ params.append("order", options.order);
1467
1148
  }
1468
- return false;
1469
- }
1470
- identityMatchesRecipient(recipient, identity) {
1471
- if (identity.uaid && recipient.uaid?.toLowerCase() === identity.uaid.toLowerCase()) {
1472
- return true;
1149
+ if (options?.timestamp) {
1150
+ params.append("timestamp", options.timestamp);
1473
1151
  }
1474
- if (identity.ledgerAccountId && recipient.ledgerAccountId?.toLowerCase() === identity.ledgerAccountId.toLowerCase()) {
1475
- return true;
1152
+ if (options?.transactionIndex) {
1153
+ params.append("transaction.index", options.transactionIndex.toString());
1476
1154
  }
1477
- if (identity.userId && recipient.userId === identity.userId) {
1478
- return true;
1155
+ const queryString = params.toString();
1156
+ if (queryString) {
1157
+ url += `?${queryString}`;
1479
1158
  }
1480
- if (identity.email && recipient.email?.toLowerCase() === identity.email.toLowerCase()) {
1481
- return true;
1159
+ try {
1160
+ const response = await this._requestWithRetry(url);
1161
+ return response.results || [];
1162
+ } catch (error) {
1163
+ this.logger.error(`Error fetching contract results: ${error.message}`);
1164
+ return null;
1482
1165
  }
1483
- return false;
1484
- }
1485
- createPlaintextConversationHandle(sessionId, summary, defaultAuth, context) {
1486
- const uaid = context?.uaid?.trim();
1487
- const agentUrl = context?.agentUrl?.trim();
1488
- return {
1489
- sessionId,
1490
- mode: "plaintext",
1491
- summary: summary ?? null,
1492
- send: async (options) => {
1493
- const plaintext = options.plaintext;
1494
- if (!plaintext || plaintext.trim().length === 0) {
1495
- throw new Error("plaintext is required for chat messages");
1496
- }
1497
- const message = options.message ?? plaintext;
1498
- return this.sendMessage({
1499
- sessionId,
1500
- message,
1501
- streaming: options.streaming,
1502
- auth: options.auth ?? defaultAuth,
1503
- uaid,
1504
- agentUrl
1505
- });
1506
- },
1507
- decryptHistoryEntry: (entry) => entry.content
1508
- };
1509
- }
1510
- async compactHistory(payload) {
1511
- if (!payload.sessionId || payload.sessionId.trim().length === 0) {
1512
- throw new Error("sessionId is required to compact chat history");
1513
- }
1514
- const body = {};
1515
- if (typeof payload.preserveEntries === "number" && Number.isFinite(payload.preserveEntries) && payload.preserveEntries >= 0) {
1516
- body.preserveEntries = Math.floor(payload.preserveEntries);
1517
- }
1518
- const raw = await this.requestJson(
1519
- `/chat/session/${encodeURIComponent(payload.sessionId)}/compact`,
1520
- {
1521
- method: "POST",
1522
- headers: { "content-type": "application/json" },
1523
- body
1524
- }
1525
- );
1526
- return this.parseWithSchema(
1527
- raw,
1528
- chatHistoryCompactionResponseSchema,
1529
- "chat history compaction response"
1530
- );
1531
- }
1532
- async fetchEncryptionStatus(sessionId) {
1533
- if (!sessionId || sessionId.trim().length === 0) {
1534
- throw new Error("sessionId is required for encryption status");
1535
- }
1536
- const raw = await this.requestJson(
1537
- `/chat/session/${encodeURIComponent(sessionId)}/encryption`,
1538
- {
1539
- method: "GET"
1540
- }
1541
- );
1542
- return this.parseWithSchema(
1543
- raw,
1544
- sessionEncryptionStatusResponseSchema,
1545
- "session encryption status response"
1546
- );
1547
1166
  }
1548
- async postEncryptionHandshake(sessionId, payload) {
1549
- if (!sessionId || sessionId.trim().length === 0) {
1550
- throw new Error("sessionId is required for encryption handshake");
1551
- }
1552
- const raw = await this.requestJson(
1553
- `/chat/session/${encodeURIComponent(sessionId)}/encryption-handshake`,
1554
- {
1555
- method: "POST",
1556
- headers: { "content-type": "application/json" },
1557
- body: {
1558
- role: payload.role,
1559
- keyType: payload.keyType,
1560
- ephemeralPublicKey: payload.ephemeralPublicKey,
1561
- longTermPublicKey: payload.longTermPublicKey,
1562
- signature: payload.signature,
1563
- uaid: payload.uaid,
1564
- userId: payload.userId,
1565
- ledgerAccountId: payload.ledgerAccountId,
1566
- metadata: payload.metadata
1567
- }
1568
- }
1569
- );
1570
- const response = this.parseWithSchema(
1571
- raw,
1572
- encryptionHandshakeResponseSchema,
1573
- "encryption handshake response"
1574
- );
1575
- return response.handshake;
1167
+ /**
1168
+ * Retrieves a specific contract result by transaction ID or hash.
1169
+ * @param transactionIdOrHash The transaction ID or hash.
1170
+ * @param nonce Optional nonce filter.
1171
+ * @returns A promise that resolves to a ContractResult or null.
1172
+ */
1173
+ async getContractResult(transactionIdOrHash, nonce) {
1174
+ this.logger.info(`Getting contract result for ${transactionIdOrHash}`);
1175
+ let url = `/api/v1/contracts/results/${transactionIdOrHash}`;
1176
+ if (nonce !== void 0) {
1177
+ url += `?nonce=${nonce}`;
1178
+ }
1179
+ try {
1180
+ const response = await this._requestWithRetry(url);
1181
+ return response;
1182
+ } catch (error) {
1183
+ this.logger.error(
1184
+ `Error fetching contract result for ${transactionIdOrHash}: ${error.message}`
1185
+ );
1186
+ return null;
1187
+ }
1576
1188
  }
1577
- async registerEncryptionKey(payload) {
1578
- const raw = await this.requestJson("/encryption/keys", {
1579
- method: "POST",
1580
- headers: { "content-type": "application/json" },
1581
- body: payload
1582
- });
1583
- return this.parseWithSchema(
1584
- raw,
1585
- registerEncryptionKeyResponseSchema,
1586
- "register encryption key response"
1189
+ /**
1190
+ * Retrieves contract results for a specific contract.
1191
+ * @param contractIdOrAddress The contract ID or EVM address.
1192
+ * @param options Optional parameters for filtering.
1193
+ * @returns A promise that resolves to an array of ContractResult or null.
1194
+ */
1195
+ async getContractResultsByContract(contractIdOrAddress, options) {
1196
+ this.logger.info(
1197
+ `Getting contract results for contract ${contractIdOrAddress}`
1587
1198
  );
1588
- }
1589
- async sendMessage(payload) {
1590
- const body = {
1591
- message: payload.message
1592
- };
1593
- if (payload.streaming !== void 0) {
1594
- body.streaming = payload.streaming;
1199
+ let url = `/api/v1/contracts/${contractIdOrAddress}/results`;
1200
+ const params = new URLSearchParams();
1201
+ if (options?.blockHash) {
1202
+ params.append("block.hash", options.blockHash);
1595
1203
  }
1596
- if (payload.auth) {
1597
- body.auth = serialiseAuthConfig(payload.auth);
1204
+ if (options?.blockNumber) {
1205
+ params.append("block.number", options.blockNumber);
1598
1206
  }
1599
- if ("uaid" in payload) {
1600
- body.uaid = payload.uaid;
1207
+ if (options?.from) {
1208
+ params.append("from", options.from);
1601
1209
  }
1602
- if ("sessionId" in payload && payload.sessionId) {
1603
- body.sessionId = payload.sessionId;
1210
+ if (options?.internal !== void 0) {
1211
+ params.append("internal", options.internal.toString());
1604
1212
  }
1605
- if ("agentUrl" in payload && payload.agentUrl) {
1606
- body.agentUrl = payload.agentUrl;
1213
+ if (options?.limit) {
1214
+ params.append("limit", options.limit.toString());
1607
1215
  }
1608
- let cipherEnvelope = payload.cipherEnvelope ?? null;
1609
- if (payload.encryption) {
1610
- const sessionIdForEncryption = payload.encryption.sessionId ?? (typeof body.sessionId === "string" ? body.sessionId : void 0);
1611
- if (!sessionIdForEncryption) {
1612
- throw new Error(
1613
- "sessionId is required when using encrypted chat payloads"
1614
- );
1615
- }
1616
- if (!payload.encryption.recipients?.length) {
1617
- throw new Error("recipients are required for encrypted chat payloads");
1618
- }
1619
- cipherEnvelope = this.encryption.encryptCipherEnvelope({
1620
- ...payload.encryption,
1621
- sessionId: sessionIdForEncryption
1622
- });
1623
- }
1624
- if (cipherEnvelope) {
1625
- body.cipherEnvelope = cipherEnvelope;
1626
- }
1627
- delete body.encryption;
1628
- const raw = await this.requestJson("/chat/message", {
1629
- method: "POST",
1630
- body,
1631
- headers: { "content-type": "application/json" }
1632
- });
1633
- return this.parseWithSchema(
1634
- raw,
1635
- sendMessageResponseSchema,
1636
- "chat message response"
1637
- );
1638
- }
1639
- async endSession(sessionId) {
1640
- await this.request(`/chat/session/${encodeURIComponent(sessionId)}`, {
1641
- method: "DELETE"
1642
- });
1643
- }
1644
- async generateEncryptionKeyPair(options = {}) {
1645
- this.assertNodeRuntime("generateEncryptionKeyPair");
1646
- const keyType = options.keyType ?? "secp256k1";
1647
- if (keyType !== "secp256k1") {
1648
- throw new Error("Only secp256k1 key generation is supported currently");
1649
- }
1650
- const privateKeyBytes = randomBytes(32);
1651
- const privateKey = Buffer.from(privateKeyBytes).toString("hex");
1652
- const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);
1653
- const publicKey = Buffer.from(publicKeyBytes).toString("hex");
1654
- const envVar = options.envVar ?? "RB_ENCRYPTION_PRIVATE_KEY";
1655
- const resolvedPath = options.envPath ? path.resolve(options.envPath) : void 0;
1656
- if (resolvedPath) {
1657
- const fsModule = getFs();
1658
- if (!fsModule) {
1659
- throw new Error(
1660
- "File system module is not available; cannot write encryption key env file"
1661
- );
1662
- }
1663
- const envLine = `${envVar}=${privateKey}`;
1664
- if (fsModule.existsSync(resolvedPath)) {
1665
- const content = fsModule.readFileSync(resolvedPath, "utf-8");
1666
- const lineRegex = new RegExp(`^${envVar}=.*$`, "m");
1667
- if (lineRegex.test(content)) {
1668
- if (!options.overwrite) {
1669
- throw new Error(
1670
- `${envVar} already exists in ${resolvedPath}; set overwrite=true to replace it`
1671
- );
1672
- }
1673
- const updated = content.replace(lineRegex, envLine);
1674
- fsModule.writeFileSync(resolvedPath, updated);
1675
- } else {
1676
- const needsNewline = !content.endsWith("\n");
1677
- fsModule.appendFileSync(
1678
- resolvedPath,
1679
- `${needsNewline ? "\n" : ""}${envLine}
1680
- `
1681
- );
1682
- }
1683
- } else {
1684
- fsModule.writeFileSync(resolvedPath, `${envLine}
1685
- `);
1686
- }
1216
+ if (options?.order) {
1217
+ params.append("order", options.order);
1218
+ }
1219
+ if (options?.timestamp) {
1220
+ params.append("timestamp", options.timestamp);
1221
+ }
1222
+ if (options?.transactionIndex) {
1223
+ params.append("transaction.index", options.transactionIndex.toString());
1224
+ }
1225
+ const queryString = params.toString();
1226
+ if (queryString) {
1227
+ url += `?${queryString}`;
1228
+ }
1229
+ try {
1230
+ const response = await this._requestWithRetry(url);
1231
+ return response.results || [];
1232
+ } catch (error) {
1233
+ this.logger.error(
1234
+ `Error fetching contract results for ${contractIdOrAddress}: ${error.message}`
1235
+ );
1236
+ return null;
1687
1237
  }
1688
- return {
1689
- privateKey,
1690
- publicKey,
1691
- envPath: resolvedPath,
1692
- envVar
1693
- };
1694
- }
1695
- buildUrl(path2) {
1696
- const normalisedPath = path2.startsWith("/") ? path2 : `/${path2}`;
1697
- return `${this.baseUrl}${normalisedPath}`;
1698
1238
  }
1699
- buildVectorFallbackSearchParams(request) {
1700
- const params = {
1701
- q: request.query
1702
- };
1703
- let effectiveLimit;
1704
- if (typeof request.limit === "number" && Number.isFinite(request.limit)) {
1705
- effectiveLimit = request.limit;
1706
- params.limit = request.limit;
1239
+ /**
1240
+ * Retrieves contract state for a specific contract.
1241
+ * @param contractIdOrAddress The contract ID or EVM address.
1242
+ * @param options Optional parameters for filtering.
1243
+ * @returns A promise that resolves to an array of ContractState or null.
1244
+ */
1245
+ async getContractState(contractIdOrAddress, options) {
1246
+ this.logger.info(`Getting contract state for ${contractIdOrAddress}`);
1247
+ let url = `/api/v1/contracts/${contractIdOrAddress}/state`;
1248
+ const params = new URLSearchParams();
1249
+ if (options?.limit) {
1250
+ params.append("limit", options.limit.toString());
1707
1251
  }
1708
- if (typeof request.offset === "number" && Number.isFinite(request.offset) && request.offset > 0) {
1709
- const limit = effectiveLimit && effectiveLimit > 0 ? effectiveLimit : 20;
1710
- params.limit = limit;
1711
- params.page = Math.floor(request.offset / limit) + 1;
1252
+ if (options?.order) {
1253
+ params.append("order", options.order);
1712
1254
  }
1713
- if (request.filter?.registry) {
1714
- params.registry = request.filter.registry;
1255
+ if (options?.slot) {
1256
+ params.append("slot", options.slot);
1715
1257
  }
1716
- if (request.filter?.protocols?.length) {
1717
- params.protocols = [...request.filter.protocols];
1258
+ if (options?.timestamp) {
1259
+ params.append("timestamp", options.timestamp);
1718
1260
  }
1719
- if (request.filter?.adapter?.length) {
1720
- params.adapters = [...request.filter.adapter];
1261
+ const queryString = params.toString();
1262
+ if (queryString) {
1263
+ url += `?${queryString}`;
1721
1264
  }
1722
- if (request.filter?.capabilities?.length) {
1723
- params.capabilities = request.filter.capabilities.map(
1724
- (value) => typeof value === "number" ? value.toString(10) : value
1265
+ try {
1266
+ const response = await this._requestWithRetry(url);
1267
+ return response.state || [];
1268
+ } catch (error) {
1269
+ this.logger.error(
1270
+ `Error fetching contract state for ${contractIdOrAddress}: ${error.message}`
1725
1271
  );
1272
+ return null;
1726
1273
  }
1727
- if (request.filter?.type) {
1728
- params.type = request.filter.type;
1729
- }
1730
- return params;
1731
- }
1732
- convertSearchResultToVectorResponse(result) {
1733
- const hits = result.hits.map((agent) => ({
1734
- agent,
1735
- score: 0,
1736
- highlights: {}
1737
- }));
1738
- const total = result.total;
1739
- const limit = result.limit;
1740
- const page = result.page;
1741
- const totalVisible = page * limit;
1742
- const limited = total > totalVisible || page > 1;
1743
- return {
1744
- hits,
1745
- total,
1746
- took: 0,
1747
- totalAvailable: total,
1748
- visible: hits.length,
1749
- limited,
1750
- credits_used: 0
1751
- };
1752
1274
  }
1753
- async delay(ms, signal) {
1754
- if (ms <= 0) {
1755
- if (signal?.aborted) {
1756
- throw createAbortError();
1757
- }
1758
- return;
1275
+ /**
1276
+ * Retrieves contract actions for a specific transaction.
1277
+ * @param transactionIdOrHash The transaction ID or hash.
1278
+ * @param options Optional parameters for filtering.
1279
+ * @returns A promise that resolves to an array of ContractAction or null.
1280
+ */
1281
+ async getContractActions(transactionIdOrHash, options) {
1282
+ this.logger.info(`Getting contract actions for ${transactionIdOrHash}`);
1283
+ let url = `/api/v1/contracts/results/${transactionIdOrHash}/actions`;
1284
+ const params = new URLSearchParams();
1285
+ if (options?.index) {
1286
+ params.append("index", options.index);
1759
1287
  }
1760
- await new Promise((resolve, reject) => {
1761
- const timer = setTimeout(() => {
1762
- if (signal) {
1763
- signal.removeEventListener("abort", onAbort);
1764
- }
1765
- resolve();
1766
- }, ms);
1767
- const onAbort = () => {
1768
- clearTimeout(timer);
1769
- signal?.removeEventListener("abort", onAbort);
1770
- reject(createAbortError());
1771
- };
1772
- if (signal) {
1773
- if (signal.aborted) {
1774
- clearTimeout(timer);
1775
- reject(createAbortError());
1776
- return;
1777
- }
1778
- signal.addEventListener("abort", onAbort, { once: true });
1779
- }
1780
- });
1781
- }
1782
- async requestJson(path2, config) {
1783
- const response = await this.request(path2, config);
1784
- const contentType = response.headers?.get("content-type") ?? "";
1785
- if (!JSON_CONTENT_TYPE.test(contentType)) {
1786
- const body = await response.text();
1787
- throw new RegistryBrokerParseError(
1788
- "Expected JSON response from registry broker",
1789
- body
1288
+ if (options?.limit) {
1289
+ params.append("limit", options.limit.toString());
1290
+ }
1291
+ if (options?.order) {
1292
+ params.append("order", options.order);
1293
+ }
1294
+ const queryString = params.toString();
1295
+ if (queryString) {
1296
+ url += `?${queryString}`;
1297
+ }
1298
+ try {
1299
+ const response = await this._requestWithRetry(url);
1300
+ return response.actions || [];
1301
+ } catch (error) {
1302
+ this.logger.error(
1303
+ `Error fetching contract actions for ${transactionIdOrHash}: ${error.message}`
1790
1304
  );
1305
+ return null;
1791
1306
  }
1792
- return await response.json();
1793
1307
  }
1794
- async extractErrorBody(response) {
1795
- const contentType = response.headers?.get("content-type") ?? "";
1796
- if (JSON_CONTENT_TYPE.test(contentType)) {
1797
- try {
1798
- return await response.json();
1799
- } catch (error) {
1800
- return { parseError: String(error) };
1801
- }
1308
+ /**
1309
+ * Retrieves contract logs from the network.
1310
+ * @param options Optional parameters for filtering logs.
1311
+ * @returns A promise that resolves to an array of ContractLog or null.
1312
+ */
1313
+ async getContractLogs(options) {
1314
+ this.logger.info("Getting contract logs from the network");
1315
+ let url = `/api/v1/contracts/results/logs`;
1316
+ const params = new URLSearchParams();
1317
+ if (options?.index) {
1318
+ params.append("index", options.index);
1319
+ }
1320
+ if (options?.limit) {
1321
+ params.append("limit", options.limit.toString());
1322
+ }
1323
+ if (options?.order) {
1324
+ params.append("order", options.order);
1325
+ }
1326
+ if (options?.timestamp) {
1327
+ params.append("timestamp", options.timestamp);
1328
+ }
1329
+ if (options?.topic0) {
1330
+ params.append("topic0", options.topic0);
1331
+ }
1332
+ if (options?.topic1) {
1333
+ params.append("topic1", options.topic1);
1334
+ }
1335
+ if (options?.topic2) {
1336
+ params.append("topic2", options.topic2);
1337
+ }
1338
+ if (options?.topic3) {
1339
+ params.append("topic3", options.topic3);
1340
+ }
1341
+ if (options?.transactionHash) {
1342
+ params.append("transaction.hash", options.transactionHash);
1343
+ }
1344
+ const queryString = params.toString();
1345
+ if (queryString) {
1346
+ url += `?${queryString}`;
1802
1347
  }
1803
1348
  try {
1804
- return await response.text();
1349
+ const response = await this._requestWithRetry(url);
1350
+ return response.logs || [];
1805
1351
  } catch (error) {
1806
- return { parseError: String(error) };
1352
+ this.logger.error(`Error fetching contract logs: ${error.message}`);
1353
+ return null;
1807
1354
  }
1808
1355
  }
1809
- parseWithSchema(value, schema, context) {
1356
+ /**
1357
+ * Retrieves contract logs for a specific contract.
1358
+ * @param contractIdOrAddress The contract ID or EVM address.
1359
+ * @param options Optional parameters for filtering logs.
1360
+ * @returns A promise that resolves to an array of ContractLog or null.
1361
+ */
1362
+ async getContractLogsByContract(contractIdOrAddress, options) {
1363
+ this.logger.info(
1364
+ `Getting contract logs for contract ${contractIdOrAddress}`
1365
+ );
1366
+ let url = `/api/v1/contracts/${contractIdOrAddress}/results/logs`;
1367
+ const params = new URLSearchParams();
1368
+ if (options?.index) {
1369
+ params.append("index", options.index);
1370
+ }
1371
+ if (options?.limit) {
1372
+ params.append("limit", options.limit.toString());
1373
+ }
1374
+ if (options?.order) {
1375
+ params.append("order", options.order);
1376
+ }
1377
+ if (options?.timestamp) {
1378
+ params.append("timestamp", options.timestamp);
1379
+ }
1380
+ if (options?.topic0) {
1381
+ params.append("topic0", options.topic0);
1382
+ }
1383
+ if (options?.topic1) {
1384
+ params.append("topic1", options.topic1);
1385
+ }
1386
+ if (options?.topic2) {
1387
+ params.append("topic2", options.topic2);
1388
+ }
1389
+ if (options?.topic3) {
1390
+ params.append("topic3", options.topic3);
1391
+ }
1392
+ const queryString = params.toString();
1393
+ if (queryString) {
1394
+ url += `?${queryString}`;
1395
+ }
1810
1396
  try {
1811
- return schema.parse(value);
1397
+ const response = await this._requestWithRetry(url);
1398
+ return response.logs || [];
1812
1399
  } catch (error) {
1813
- throw new RegistryBrokerParseError(
1814
- `Failed to parse ${context}`,
1815
- error instanceof ZodError || error instanceof Error ? error : String(error),
1816
- value
1400
+ this.logger.error(
1401
+ `Error fetching contract logs for ${contractIdOrAddress}: ${error.message}`
1817
1402
  );
1403
+ return null;
1818
1404
  }
1819
1405
  }
1820
- assertNodeRuntime(feature) {
1821
- if (typeof process === "undefined" || !process.versions?.node) {
1822
- throw new Error(`${feature} is only available in Node.js environments`);
1406
+ /**
1407
+ * Retrieves NFT information by token ID and serial number.
1408
+ * @param tokenId The token ID.
1409
+ * @param serialNumber The serial number of the NFT.
1410
+ * @returns A promise that resolves to an NftInfo or null.
1411
+ */
1412
+ async getNftInfo(tokenId, serialNumber) {
1413
+ this.logger.info(`Getting NFT info for ${tokenId}/${serialNumber}`);
1414
+ const url = `/api/v1/tokens/${tokenId}/nfts/${serialNumber}`;
1415
+ try {
1416
+ const response = await this._requestWithRetry(url);
1417
+ return response;
1418
+ } catch (error) {
1419
+ this.logger.error(
1420
+ `Error fetching NFT info for ${tokenId}/${serialNumber}: ${error.message}`
1421
+ );
1422
+ return null;
1823
1423
  }
1824
1424
  }
1825
- createEphemeralKeyPair() {
1826
- this.assertNodeRuntime("generateEphemeralKeyPair");
1827
- const privateKeyBytes = randomBytes(32);
1828
- const publicKey = secp256k1.getPublicKey(privateKeyBytes, true);
1829
- return {
1830
- privateKey: Buffer.from(privateKeyBytes).toString("hex"),
1831
- publicKey: Buffer.from(publicKey).toString("hex")
1832
- };
1833
- }
1834
- deriveSharedSecret(options) {
1835
- this.assertNodeRuntime("deriveSharedSecret");
1836
- const privateKey = this.hexToBuffer(options.privateKey);
1837
- const peerPublicKey = this.hexToBuffer(options.peerPublicKey);
1838
- const shared = secp256k1.getSharedSecret(privateKey, peerPublicKey, true);
1839
- return createHash("sha256").update(Buffer.from(shared)).digest();
1840
- }
1841
- buildCipherEnvelope(options) {
1842
- this.assertNodeRuntime("encryptCipherEnvelope");
1843
- const sharedSecret = this.normalizeSharedSecret(options.sharedSecret);
1844
- const iv = randomBytes(12);
1845
- const cipher = createCipheriv("aes-256-gcm", sharedSecret, iv);
1846
- const aadSource = options.associatedData ?? options.sessionId;
1847
- const associatedDataEncoded = aadSource ? Buffer.from(aadSource, "utf8").toString("base64") : void 0;
1848
- if (aadSource) {
1849
- cipher.setAAD(Buffer.from(aadSource, "utf8"));
1850
- }
1851
- const ciphertext = Buffer.concat([
1852
- cipher.update(Buffer.from(options.plaintext, "utf8")),
1853
- cipher.final()
1854
- ]);
1855
- const tag = cipher.getAuthTag();
1856
- const payload = Buffer.concat([ciphertext, tag]);
1857
- return {
1858
- algorithm: "aes-256-gcm",
1859
- ciphertext: payload.toString("base64"),
1860
- nonce: iv.toString("base64"),
1861
- associatedData: associatedDataEncoded,
1862
- keyLocator: {
1863
- sessionId: options.sessionId,
1864
- revision: options.revision ?? 1
1865
- },
1866
- recipients: options.recipients.map((recipient) => ({
1867
- ...recipient,
1868
- encryptedShare: ""
1869
- }))
1870
- };
1871
- }
1872
- openCipherEnvelope(options) {
1873
- this.assertNodeRuntime("decryptCipherEnvelope");
1874
- const sharedSecret = this.normalizeSharedSecret(options.sharedSecret);
1875
- const payload = Buffer.from(options.envelope.ciphertext, "base64");
1876
- const nonce = Buffer.from(options.envelope.nonce, "base64");
1877
- const ciphertext = payload.slice(0, payload.length - 16);
1878
- const tag = payload.slice(payload.length - 16);
1879
- const decipher = createDecipheriv("aes-256-gcm", sharedSecret, nonce);
1880
- if (options.envelope.associatedData) {
1881
- decipher.setAAD(Buffer.from(options.envelope.associatedData, "base64"));
1882
- }
1883
- decipher.setAuthTag(tag);
1884
- const plaintext = Buffer.concat([
1885
- decipher.update(ciphertext),
1886
- decipher.final()
1887
- ]);
1888
- return plaintext.toString(options.encoding ?? "utf8");
1889
- }
1890
- normalizeSharedSecret(input) {
1891
- if (Buffer.isBuffer(input)) {
1892
- return Buffer.from(input);
1893
- }
1894
- if (input instanceof Uint8Array) {
1895
- return Buffer.from(input);
1425
+ /**
1426
+ * Retrieves NFTs for a specific token.
1427
+ * @param tokenId The token ID.
1428
+ * @param options Optional parameters for filtering NFTs.
1429
+ * @returns A promise that resolves to an array of NftInfo or null.
1430
+ */
1431
+ async getNftsByToken(tokenId, options) {
1432
+ this.logger.info(`Getting NFTs for token ${tokenId}`);
1433
+ let url = `/api/v1/tokens/${tokenId}/nfts`;
1434
+ const params = new URLSearchParams();
1435
+ if (options?.accountId) {
1436
+ params.append("account.id", options.accountId);
1896
1437
  }
1897
- if (typeof input === "string") {
1898
- return this.bufferFromString(input);
1438
+ if (options?.limit) {
1439
+ params.append("limit", options.limit.toString());
1899
1440
  }
1900
- throw new Error("Unsupported shared secret input");
1901
- }
1902
- bufferFromString(value) {
1903
- const trimmed = value.trim();
1904
- if (!trimmed) {
1905
- throw new Error("sharedSecret string cannot be empty");
1441
+ if (options?.order) {
1442
+ params.append("order", options.order);
1906
1443
  }
1907
- const normalized = trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
1908
- if (/^[0-9a-fA-F]+$/.test(normalized) && normalized.length % 2 === 0) {
1909
- return Buffer.from(normalized, "hex");
1444
+ if (options?.serialNumber) {
1445
+ params.append("serialnumber", options.serialNumber);
1910
1446
  }
1911
- return Buffer.from(trimmed, "base64");
1912
- }
1913
- hexToBuffer(value) {
1914
- const normalized = value.startsWith("0x") ? value.slice(2) : value;
1915
- if (!/^[0-9a-fA-F]+$/.test(normalized) || normalized.length % 2 !== 0) {
1916
- throw new Error("Expected hex-encoded value");
1447
+ const queryString = params.toString();
1448
+ if (queryString) {
1449
+ url += `?${queryString}`;
1917
1450
  }
1918
- return Buffer.from(normalized, "hex");
1919
- }
1920
- }
1921
- const isPendingRegisterAgentResponse = (response) => response.status === "pending";
1922
- const isPartialRegisterAgentResponse = (response) => response.status === "partial" && response.success === false;
1923
- const isSuccessRegisterAgentResponse = (response) => response.success === true && response.status !== "pending";
1924
- class EncryptedChatManager {
1925
- constructor(client) {
1926
- this.client = client;
1927
- }
1928
- registerConversationContext(context) {
1929
- this.client.registerConversationContextForEncryption(context);
1930
- }
1931
- async startSession(options) {
1932
- await this.client.encryptionReady();
1933
- const session = await this.client.chat.createSession({
1934
- uaid: options.uaid,
1935
- senderUaid: options.senderUaid,
1936
- encryptionRequested: true,
1937
- historyTtlSeconds: options.historyTtlSeconds,
1938
- auth: options.auth
1939
- });
1940
- options.onSessionCreated?.(session.sessionId);
1941
- const summary = session.encryption;
1942
- if (!summary?.enabled) {
1943
- throw new EncryptionUnavailableError(
1944
- session.sessionId,
1945
- session.encryption ?? null
1451
+ try {
1452
+ const response = await this._requestWithRetry(url);
1453
+ return response.nfts || [];
1454
+ } catch (error) {
1455
+ this.logger.error(
1456
+ `Error fetching NFTs for token ${tokenId}: ${error.message}`
1946
1457
  );
1458
+ return null;
1947
1459
  }
1948
- const handle = await this.establishRequesterContext({
1949
- sessionId: session.sessionId,
1950
- summary,
1951
- senderUaid: options.senderUaid,
1952
- handshakeTimeoutMs: options.handshakeTimeoutMs,
1953
- pollIntervalMs: options.pollIntervalMs
1954
- });
1955
- return handle;
1956
- }
1957
- async acceptSession(options) {
1958
- await this.client.encryptionReady();
1959
- const summary = await this.waitForEncryptionSummary(
1960
- options.sessionId,
1961
- options.handshakeTimeoutMs,
1962
- options.pollIntervalMs
1963
- );
1964
- const handle = await this.establishResponderContext({
1965
- sessionId: options.sessionId,
1966
- summary,
1967
- responderUaid: options.responderUaid,
1968
- handshakeTimeoutMs: options.handshakeTimeoutMs,
1969
- pollIntervalMs: options.pollIntervalMs
1970
- });
1971
- return handle;
1972
- }
1973
- async establishRequesterContext(params) {
1974
- const keyPair = this.client.encryption.generateEphemeralKeyPair();
1975
- await this.client.chat.submitEncryptionHandshake(params.sessionId, {
1976
- role: "requester",
1977
- keyType: "secp256k1",
1978
- ephemeralPublicKey: keyPair.publicKey,
1979
- uaid: params.senderUaid ?? params.summary.requester?.uaid ?? void 0
1980
- });
1981
- const { summary, record } = await this.waitForHandshakeCompletion(
1982
- params.sessionId,
1983
- params.handshakeTimeoutMs,
1984
- params.pollIntervalMs
1985
- );
1986
- const responderKey = record.responder?.ephemeralPublicKey;
1987
- if (!responderKey) {
1988
- throw new Error("Responder handshake was not completed in time");
1989
- }
1990
- const sharedSecret = this.client.encryption.deriveSharedSecret({
1991
- privateKey: keyPair.privateKey,
1992
- peerPublicKey: responderKey
1993
- }).subarray();
1994
- const recipients = this.buildRecipients(summary);
1995
- return this.createHandle({
1996
- sessionId: params.sessionId,
1997
- sharedSecret,
1998
- summary,
1999
- recipients,
2000
- identity: summary.requester ?? void 0
2001
- });
2002
- }
2003
- async establishResponderContext(params) {
2004
- const keyPair = this.client.encryption.generateEphemeralKeyPair();
2005
- await this.client.chat.submitEncryptionHandshake(params.sessionId, {
2006
- role: "responder",
2007
- keyType: "secp256k1",
2008
- ephemeralPublicKey: keyPair.publicKey,
2009
- uaid: params.responderUaid ?? params.summary.responder?.uaid ?? void 0
2010
- });
2011
- const { summary, record } = await this.waitForHandshakeCompletion(
2012
- params.sessionId,
2013
- params.handshakeTimeoutMs,
2014
- params.pollIntervalMs
2015
- );
2016
- const requesterKey = record.requester?.ephemeralPublicKey;
2017
- if (!requesterKey) {
2018
- throw new Error("Requester handshake was not detected in time");
2019
- }
2020
- const sharedSecret = this.client.encryption.deriveSharedSecret({
2021
- privateKey: keyPair.privateKey,
2022
- peerPublicKey: requesterKey
2023
- }).subarray();
2024
- const recipients = this.buildRecipients(summary);
2025
- return this.createHandle({
2026
- sessionId: params.sessionId,
2027
- sharedSecret,
2028
- summary,
2029
- recipients,
2030
- identity: summary.responder ?? void 0
2031
- });
2032
1460
  }
2033
- async waitForHandshakeCompletion(sessionId, timeoutMs = 3e4, pollIntervalMs = 1e3) {
2034
- const deadline = Date.now() + timeoutMs;
2035
- while (true) {
2036
- const status = await this.client.chat.getEncryptionStatus(sessionId);
2037
- const summary = status.encryption;
2038
- const record = summary?.handshake;
2039
- if (summary && record && record.status === "complete") {
2040
- return { summary, record };
2041
- }
2042
- if (Date.now() >= deadline) {
2043
- throw new Error("Timed out waiting for encrypted handshake completion");
2044
- }
2045
- await this.delay(pollIntervalMs);
1461
+ /**
1462
+ * Retrieves network information.
1463
+ * @returns A promise that resolves to NetworkInfo or null.
1464
+ */
1465
+ async getNetworkInfo() {
1466
+ this.logger.info("Getting network information");
1467
+ const url = `/api/v1/network/nodes`;
1468
+ try {
1469
+ const response = await this._requestWithRetry(url);
1470
+ return response;
1471
+ } catch (error) {
1472
+ this.logger.error(`Error fetching network info: ${error.message}`);
1473
+ return null;
2046
1474
  }
2047
1475
  }
2048
- async waitForEncryptionSummary(sessionId, _timeoutMs = 3e4, _pollIntervalMs = 1e3) {
2049
- const status = await this.client.chat.getEncryptionStatus(sessionId);
2050
- if (!status.encryption?.enabled) {
2051
- throw new EncryptionUnavailableError(
2052
- sessionId,
2053
- status.encryption ?? null
2054
- );
1476
+ /**
1477
+ * Retrieves network fees.
1478
+ * @param timestamp Optional timestamp for historical fees.
1479
+ * @returns A promise that resolves to NetworkFees or null.
1480
+ */
1481
+ async getNetworkFees(timestamp) {
1482
+ this.logger.info("Getting network fees");
1483
+ let url = `/api/v1/network/fees`;
1484
+ if (timestamp) {
1485
+ url += `?timestamp=${timestamp}`;
1486
+ }
1487
+ try {
1488
+ const response = await this._requestWithRetry(url);
1489
+ return response;
1490
+ } catch (error) {
1491
+ this.logger.error(`Error fetching network fees: ${error.message}`);
1492
+ return null;
2055
1493
  }
2056
- return status.encryption;
2057
1494
  }
2058
- buildRecipients(summary) {
2059
- const candidates = [summary.requester, summary.responder].filter(Boolean);
2060
- const normalized = candidates.map((candidate) => {
2061
- if (!candidate) {
2062
- return null;
2063
- }
2064
- const recipient = {};
2065
- if (candidate.uaid) {
2066
- recipient.uaid = candidate.uaid;
2067
- }
2068
- if (candidate.ledgerAccountId) {
2069
- recipient.ledgerAccountId = candidate.ledgerAccountId;
2070
- }
2071
- if (candidate.userId) {
2072
- recipient.userId = candidate.userId;
2073
- }
2074
- if (candidate.email) {
2075
- recipient.email = candidate.email;
2076
- }
2077
- return recipient;
2078
- }).filter(
2079
- (entry) => Boolean(
2080
- entry?.uaid || entry?.ledgerAccountId || entry?.userId || entry?.email
2081
- )
2082
- );
2083
- if (normalized.length > 0) {
2084
- return normalized;
1495
+ /**
1496
+ * Retrieves network supply information.
1497
+ * @param timestamp Optional timestamp for historical supply data.
1498
+ * @returns A promise that resolves to NetworkSupply or null.
1499
+ */
1500
+ async getNetworkSupply(timestamp) {
1501
+ this.logger.info("Getting network supply");
1502
+ let url = `/api/v1/network/supply`;
1503
+ if (timestamp) {
1504
+ url += `?timestamp=${timestamp}`;
2085
1505
  }
2086
- if (summary.responder?.uaid) {
2087
- return [{ uaid: summary.responder.uaid }];
1506
+ try {
1507
+ const response = await this._requestWithRetry(url);
1508
+ return response;
1509
+ } catch (error) {
1510
+ this.logger.error(`Error fetching network supply: ${error.message}`);
1511
+ return null;
2088
1512
  }
2089
- return [];
2090
1513
  }
2091
- createHandle(context) {
2092
- const sharedSecret = context.sharedSecret;
2093
- const uaid = context.summary.requester?.uaid ?? context.summary.responder?.uaid ?? context.identity?.uaid;
2094
- const handle = {
2095
- sessionId: context.sessionId,
2096
- mode: "encrypted",
2097
- summary: context.summary,
2098
- send: async (options) => {
2099
- const recipients = options.recipients ?? context.recipients;
2100
- return this.client.chat.sendMessage({
2101
- sessionId: context.sessionId,
2102
- message: options.message ?? "[ciphertext omitted]",
2103
- streaming: options.streaming,
2104
- auth: options.auth,
2105
- uaid,
2106
- encryption: {
2107
- plaintext: options.plaintext,
2108
- sharedSecret: Buffer.from(sharedSecret),
2109
- recipients
2110
- }
2111
- });
2112
- },
2113
- decryptHistoryEntry: (entry) => this.decryptEntry(entry, context.identity, sharedSecret)
2114
- };
2115
- this.registerConversationContext({
2116
- sessionId: context.sessionId,
2117
- sharedSecret,
2118
- identity: context.identity
2119
- });
2120
- return handle;
2121
- }
2122
- decryptEntry(entry, identity, fallbackSecret) {
2123
- const envelope = entry.cipherEnvelope;
2124
- if (!envelope) {
2125
- return null;
1514
+ /**
1515
+ * Retrieves network stake information.
1516
+ * @param timestamp Optional timestamp for historical stake data.
1517
+ * @returns A promise that resolves to NetworkStake or null.
1518
+ */
1519
+ async getNetworkStake(timestamp) {
1520
+ this.logger.info("Getting network stake");
1521
+ let url = `/api/v1/network/stake`;
1522
+ if (timestamp) {
1523
+ url += `?timestamp=${timestamp}`;
2126
1524
  }
2127
- const secret = Buffer.from(fallbackSecret);
2128
1525
  try {
2129
- return this.client.encryption.decryptCipherEnvelope({
2130
- envelope,
2131
- sharedSecret: secret
2132
- });
2133
- } catch (_error) {
1526
+ const response = await this._requestWithRetry(url);
1527
+ return response;
1528
+ } catch (error) {
1529
+ this.logger.error(`Error fetching network stake: ${error.message}`);
2134
1530
  return null;
2135
1531
  }
2136
1532
  }
2137
- recipientMatches(candidate, target) {
2138
- if (target.uaid && candidate.uaid?.toLowerCase() === target.uaid.toLowerCase()) {
2139
- return true;
1533
+ /**
1534
+ * Retrieves opcode traces for a specific transaction.
1535
+ * @param transactionIdOrHash The transaction ID or hash.
1536
+ * @param options Optional parameters for trace details.
1537
+ * @returns A promise that resolves to an OpcodesResponse or null.
1538
+ */
1539
+ async getOpcodeTraces(transactionIdOrHash, options) {
1540
+ this.logger.info(`Getting opcode traces for ${transactionIdOrHash}`);
1541
+ let url = `/api/v1/contracts/results/${transactionIdOrHash}/opcodes`;
1542
+ const params = new URLSearchParams();
1543
+ if (options?.stack !== void 0) {
1544
+ params.append("stack", options.stack.toString());
2140
1545
  }
2141
- if (target.ledgerAccountId && candidate.ledgerAccountId?.toLowerCase() === target.ledgerAccountId.toLowerCase()) {
2142
- return true;
1546
+ if (options?.memory !== void 0) {
1547
+ params.append("memory", options.memory.toString());
2143
1548
  }
2144
- if (target.userId && candidate.userId === target.userId) {
2145
- return true;
1549
+ if (options?.storage !== void 0) {
1550
+ params.append("storage", options.storage.toString());
2146
1551
  }
2147
- if (target.email && candidate.email?.toLowerCase() === target.email.toLowerCase()) {
2148
- return true;
1552
+ const queryString = params.toString();
1553
+ if (queryString) {
1554
+ url += `?${queryString}`;
2149
1555
  }
2150
- return false;
2151
- }
2152
- async delay(ms) {
2153
- if (ms <= 0) {
2154
- return;
1556
+ try {
1557
+ const response = await this._requestWithRetry(url);
1558
+ return response;
1559
+ } catch (error) {
1560
+ this.logger.error(
1561
+ `Error fetching opcode traces for ${transactionIdOrHash}: ${error.message}`
1562
+ );
1563
+ return null;
2155
1564
  }
2156
- await new Promise((resolve) => setTimeout(resolve, ms));
2157
- }
2158
- }
2159
- class EncryptionUnavailableError extends Error {
2160
- constructor(sessionId, summary) {
2161
- super("Encryption is not enabled for this session");
2162
- this.sessionId = sessionId;
2163
- this.summary = summary;
2164
1565
  }
2165
1566
  }
2166
1567
  export {
2167
- RegistryBrokerClient,
2168
- RegistryBrokerError,
2169
- RegistryBrokerParseError,
2170
- isPartialRegisterAgentResponse,
2171
- isPendingRegisterAgentResponse,
2172
- isSuccessRegisterAgentResponse
1568
+ HederaMirrorNode
2173
1569
  };
2174
1570
  //# sourceMappingURL=standards-sdk.es127.js.map