@hashgraphonline/standards-sdk 0.0.30 → 0.0.32

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.
@@ -1,6 +1,6 @@
1
+ import { AccountResponse, HederaMirrorNode, TopicInfo } from '../services/mirror-node';
1
2
  import { Logger, LogLevel } from '../utils/logger';
2
3
  import { Registration } from './registrations';
3
- import { HederaMirrorNode, TopicInfo, AccountResponse } from '../services/mirror-node';
4
4
  export interface HCS10Config {
5
5
  network: 'mainnet' | 'testnet';
6
6
  logLevel?: LogLevel;
@@ -23,6 +23,13 @@ export interface HCSMessage {
23
23
  sequence_number: number;
24
24
  operator_id?: string;
25
25
  }
26
+ export interface ProfileResponse {
27
+ profile: any;
28
+ topicInfo?: TopicInfo;
29
+ profileTopicId?: string;
30
+ success: boolean;
31
+ error?: string;
32
+ }
26
33
  export declare abstract class HCS10BaseClient extends Registration {
27
34
  protected network: string;
28
35
  protected logger: Logger;
@@ -42,10 +49,17 @@ export declare abstract class HCS10BaseClient extends Registration {
42
49
  getAccountMemo(accountId: string): Promise<string | null>;
43
50
  protected getTopicInfoFromMemo(memo: string): Promise<TopicInfo | null>;
44
51
  getTopicInfo(accountId: string): Promise<TopicInfo | null>;
52
+ retrieveProfile(accountId: string): Promise<ProfileResponse>;
45
53
  retrieveOutboundConnectTopic(accountId: string): Promise<TopicInfo>;
46
54
  retrieveOutboundMessages(agentAccountId: string): Promise<HCSMessage[]>;
47
55
  hasConnectionCreated(agentAccountId: string, connectionId: number): Promise<boolean>;
48
56
  clearCache(): void;
57
+ /**
58
+ * Gets message content, resolving any HRL references if needed
59
+ * @param data The message data which might be an HRL reference
60
+ * @returns The resolved content
61
+ */
62
+ getMessageContent(data: string): Promise<string>;
49
63
  }
50
64
  export declare class HCS10Cache {
51
65
  private static instance;
@@ -15311,176 +15311,6 @@ const {
15311
15311
  getAdapter,
15312
15312
  mergeConfig
15313
15313
  } = axios;
15314
- class Registration {
15315
- async checkRegistrationStatus(transactionId, network, baseUrl, logger) {
15316
- try {
15317
- const response = await fetch(`${baseUrl}/api/request-confirm`, {
15318
- method: "POST",
15319
- headers: {
15320
- "Content-Type": "application/json",
15321
- "X-Network": network
15322
- },
15323
- body: JSON.stringify({ transaction_id: transactionId })
15324
- });
15325
- if (!response.ok) {
15326
- const error = `Failed to confirm registration: ${response.statusText}`;
15327
- if (logger) {
15328
- logger.error(error);
15329
- }
15330
- throw new Error(error);
15331
- }
15332
- return await response.json();
15333
- } catch (error) {
15334
- if (logger) {
15335
- logger.error(`Error checking registration status: ${error.message}`);
15336
- }
15337
- throw error;
15338
- }
15339
- }
15340
- async waitForRegistrationConfirmation(transactionId, network, baseUrl, maxAttempts = 60, delayMs = 2e3, logger) {
15341
- let attempts = 0;
15342
- while (attempts < maxAttempts) {
15343
- if (logger) {
15344
- logger.info(
15345
- `Checking registration status. Attempt ${attempts + 1}/${maxAttempts}`
15346
- );
15347
- }
15348
- const status = await this.checkRegistrationStatus(
15349
- transactionId,
15350
- network,
15351
- baseUrl,
15352
- logger
15353
- );
15354
- if (status.status === "success") {
15355
- if (logger) {
15356
- logger.info("Registration confirmed successfully");
15357
- }
15358
- return true;
15359
- }
15360
- if (status.status === "failed") {
15361
- if (logger) {
15362
- logger.error("Registration confirmation failed");
15363
- }
15364
- throw new Error("Registration confirmation failed");
15365
- }
15366
- if (logger) {
15367
- logger.info(
15368
- `Registration still pending. Waiting ${delayMs}ms before next attempt`
15369
- );
15370
- }
15371
- await new Promise((resolve2) => setTimeout(resolve2, delayMs));
15372
- attempts++;
15373
- }
15374
- if (logger) {
15375
- logger.warn(`Registration not confirmed after ${maxAttempts} attempts`);
15376
- }
15377
- return false;
15378
- }
15379
- async executeRegistration(accountId, inboundTopicId, network, baseUrl, logger, metadata = { tags: [] }) {
15380
- var _a;
15381
- try {
15382
- if (logger) {
15383
- logger.info("Registering agent with guarded registry");
15384
- }
15385
- const response = await fetch(`${baseUrl}/api/request-register`, {
15386
- method: "POST",
15387
- headers: {
15388
- "Content-Type": "application/json",
15389
- Accept: "*/*",
15390
- "Accept-Language": "en;q=0.5",
15391
- Origin: baseUrl,
15392
- Referer: `${baseUrl}/`,
15393
- "X-Network": network
15394
- },
15395
- body: JSON.stringify({
15396
- accountId,
15397
- inboundTopicId,
15398
- metadata
15399
- })
15400
- });
15401
- const data = await response.json();
15402
- if (!response.ok) {
15403
- if (((_a = data.details) == null ? void 0 : _a.length) > 0) {
15404
- return {
15405
- validationErrors: data.details,
15406
- error: data.error || "Validation failed",
15407
- success: false
15408
- };
15409
- }
15410
- return {
15411
- error: data.error || "Failed to register agent",
15412
- success: false
15413
- };
15414
- }
15415
- if (logger) {
15416
- logger.info(
15417
- `Created new registration request. Transaction ID: ${data.transaction_id}`
15418
- );
15419
- }
15420
- return {
15421
- transactionId: data.transaction_id,
15422
- transaction: data.transaction,
15423
- success: true
15424
- };
15425
- } catch (error) {
15426
- return {
15427
- error: `Error during registration request: ${error.message}`,
15428
- success: false
15429
- };
15430
- }
15431
- }
15432
- async findRegistrations(options = {}, baseUrl = "https://hcs-10.hashgraphonline.com") {
15433
- var _a;
15434
- try {
15435
- const queryParams = new URLSearchParams();
15436
- (_a = options.tags) == null ? void 0 : _a.forEach((tag) => queryParams.append("tags", tag));
15437
- if (options.accountId) {
15438
- queryParams.append("accountId", options.accountId);
15439
- }
15440
- if (options.network) {
15441
- queryParams.append("network", options.network);
15442
- }
15443
- const response = await fetch(
15444
- `${baseUrl}/api/registrations?${queryParams}`,
15445
- {
15446
- headers: {
15447
- Accept: "*/*",
15448
- "Accept-Language": "en;q=0.5",
15449
- Origin: baseUrl,
15450
- Referer: `${baseUrl}/`
15451
- }
15452
- }
15453
- );
15454
- if (!response.ok) {
15455
- const error = await response.text();
15456
- return {
15457
- registrations: [],
15458
- error: error || "Failed to fetch registrations",
15459
- success: false
15460
- };
15461
- }
15462
- const data = await response.json();
15463
- if (data.error) {
15464
- return {
15465
- registrations: [],
15466
- error: data.error,
15467
- success: false
15468
- };
15469
- }
15470
- return {
15471
- registrations: data.registrations || [],
15472
- success: true
15473
- };
15474
- } catch (e) {
15475
- const error = e;
15476
- return {
15477
- registrations: [],
15478
- error: `Error fetching registrations: ${error.message}`,
15479
- success: false
15480
- };
15481
- }
15482
- }
15483
- }
15484
15314
  class HederaMirrorNode {
15485
15315
  constructor(network, logger) {
15486
15316
  this.network = network;
@@ -15670,6 +15500,176 @@ class HederaMirrorNode {
15670
15500
  }
15671
15501
  }
15672
15502
  }
15503
+ class Registration {
15504
+ async checkRegistrationStatus(transactionId, network, baseUrl, logger) {
15505
+ try {
15506
+ const response = await fetch(`${baseUrl}/api/request-confirm`, {
15507
+ method: "POST",
15508
+ headers: {
15509
+ "Content-Type": "application/json",
15510
+ "X-Network": network
15511
+ },
15512
+ body: JSON.stringify({ transaction_id: transactionId })
15513
+ });
15514
+ if (!response.ok) {
15515
+ const error = `Failed to confirm registration: ${response.statusText}`;
15516
+ if (logger) {
15517
+ logger.error(error);
15518
+ }
15519
+ throw new Error(error);
15520
+ }
15521
+ return await response.json();
15522
+ } catch (error) {
15523
+ if (logger) {
15524
+ logger.error(`Error checking registration status: ${error.message}`);
15525
+ }
15526
+ throw error;
15527
+ }
15528
+ }
15529
+ async waitForRegistrationConfirmation(transactionId, network, baseUrl, maxAttempts = 60, delayMs = 2e3, logger) {
15530
+ let attempts = 0;
15531
+ while (attempts < maxAttempts) {
15532
+ if (logger) {
15533
+ logger.info(
15534
+ `Checking registration status. Attempt ${attempts + 1}/${maxAttempts}`
15535
+ );
15536
+ }
15537
+ const status = await this.checkRegistrationStatus(
15538
+ transactionId,
15539
+ network,
15540
+ baseUrl,
15541
+ logger
15542
+ );
15543
+ if (status.status === "success") {
15544
+ if (logger) {
15545
+ logger.info("Registration confirmed successfully");
15546
+ }
15547
+ return true;
15548
+ }
15549
+ if (status.status === "failed") {
15550
+ if (logger) {
15551
+ logger.error("Registration confirmation failed");
15552
+ }
15553
+ throw new Error("Registration confirmation failed");
15554
+ }
15555
+ if (logger) {
15556
+ logger.info(
15557
+ `Registration still pending. Waiting ${delayMs}ms before next attempt`
15558
+ );
15559
+ }
15560
+ await new Promise((resolve2) => setTimeout(resolve2, delayMs));
15561
+ attempts++;
15562
+ }
15563
+ if (logger) {
15564
+ logger.warn(`Registration not confirmed after ${maxAttempts} attempts`);
15565
+ }
15566
+ return false;
15567
+ }
15568
+ async executeRegistration(accountId, inboundTopicId, network, baseUrl, logger, metadata = { tags: [] }) {
15569
+ var _a;
15570
+ try {
15571
+ if (logger) {
15572
+ logger.info("Registering agent with guarded registry");
15573
+ }
15574
+ const response = await fetch(`${baseUrl}/api/request-register`, {
15575
+ method: "POST",
15576
+ headers: {
15577
+ "Content-Type": "application/json",
15578
+ Accept: "*/*",
15579
+ "Accept-Language": "en;q=0.5",
15580
+ Origin: baseUrl,
15581
+ Referer: `${baseUrl}/`,
15582
+ "X-Network": network
15583
+ },
15584
+ body: JSON.stringify({
15585
+ accountId,
15586
+ inboundTopicId,
15587
+ metadata
15588
+ })
15589
+ });
15590
+ const data = await response.json();
15591
+ if (!response.ok) {
15592
+ if (((_a = data.details) == null ? void 0 : _a.length) > 0) {
15593
+ return {
15594
+ validationErrors: data.details,
15595
+ error: data.error || "Validation failed",
15596
+ success: false
15597
+ };
15598
+ }
15599
+ return {
15600
+ error: data.error || "Failed to register agent",
15601
+ success: false
15602
+ };
15603
+ }
15604
+ if (logger) {
15605
+ logger.info(
15606
+ `Created new registration request. Transaction ID: ${data.transaction_id}`
15607
+ );
15608
+ }
15609
+ return {
15610
+ transactionId: data.transaction_id,
15611
+ transaction: data.transaction,
15612
+ success: true
15613
+ };
15614
+ } catch (error) {
15615
+ return {
15616
+ error: `Error during registration request: ${error.message}`,
15617
+ success: false
15618
+ };
15619
+ }
15620
+ }
15621
+ async findRegistrations(options = {}, baseUrl = "https://hcs-10.hashgraphonline.com") {
15622
+ var _a;
15623
+ try {
15624
+ const queryParams = new URLSearchParams();
15625
+ (_a = options.tags) == null ? void 0 : _a.forEach((tag) => queryParams.append("tags", tag));
15626
+ if (options.accountId) {
15627
+ queryParams.append("accountId", options.accountId);
15628
+ }
15629
+ if (options.network) {
15630
+ queryParams.append("network", options.network);
15631
+ }
15632
+ const response = await fetch(
15633
+ `${baseUrl}/api/registrations?${queryParams}`,
15634
+ {
15635
+ headers: {
15636
+ Accept: "*/*",
15637
+ "Accept-Language": "en;q=0.5",
15638
+ Origin: baseUrl,
15639
+ Referer: `${baseUrl}/`
15640
+ }
15641
+ }
15642
+ );
15643
+ if (!response.ok) {
15644
+ const error = await response.text();
15645
+ return {
15646
+ registrations: [],
15647
+ error: error || "Failed to fetch registrations",
15648
+ success: false
15649
+ };
15650
+ }
15651
+ const data = await response.json();
15652
+ if (data.error) {
15653
+ return {
15654
+ registrations: [],
15655
+ error: data.error,
15656
+ success: false
15657
+ };
15658
+ }
15659
+ return {
15660
+ registrations: data.registrations || [],
15661
+ success: true
15662
+ };
15663
+ } catch (e) {
15664
+ const error = e;
15665
+ return {
15666
+ registrations: [],
15667
+ error: `Error fetching registrations: ${error.message}`,
15668
+ success: false
15669
+ };
15670
+ }
15671
+ }
15672
+ }
15673
15673
  class HCS10BaseClient extends Registration {
15674
15674
  constructor(config) {
15675
15675
  super();
@@ -15746,25 +15746,31 @@ class HCS10BaseClient extends Registration {
15746
15746
  }
15747
15747
  return topicInfo;
15748
15748
  }
15749
- async retrieveOutboundConnectTopic(accountId) {
15750
- this.logger.info(`Retrieving topics for account: ${accountId}`);
15751
- const cachedInfo = await this.getTopicInfo(accountId);
15752
- if (cachedInfo) return cachedInfo;
15749
+ async retrieveProfile(accountId) {
15750
+ this.logger.info(`Retrieving profile for account: ${accountId}`);
15753
15751
  try {
15754
15752
  const accountMemo = await this.getAccountMemo(accountId);
15755
15753
  if (!accountMemo) {
15756
- throw new Error(`Failed to retrieve memo for account ID: ${accountId}`);
15754
+ return {
15755
+ profile: null,
15756
+ success: false,
15757
+ error: `Failed to retrieve memo for account ID: ${accountId}`
15758
+ };
15757
15759
  }
15758
15760
  if (!accountMemo.startsWith("hcs-11:")) {
15759
- throw new Error(
15760
- `Invalid memo format: ${accountMemo}. Memo must start with 'hcs-11:'`
15761
- );
15761
+ return {
15762
+ profile: null,
15763
+ success: false,
15764
+ error: `Invalid memo format: ${accountMemo}. Memo must start with 'hcs-11:'`
15765
+ };
15762
15766
  }
15763
15767
  const hrlMatch = accountMemo.match(/^hcs-11:hcs:\/\/(\d+)\/(.+)$/);
15764
15768
  if (!hrlMatch) {
15765
- throw new Error(
15766
- `Invalid HCS-11 memo format: ${accountMemo}. Expected format: hcs-11:hcs://{standard}/{topicId}`
15767
- );
15769
+ return {
15770
+ profile: null,
15771
+ success: false,
15772
+ error: `Invalid HCS-11 memo format: ${accountMemo}. Expected format: hcs-11:hcs://{standard}/{topicId}`
15773
+ };
15768
15774
  }
15769
15775
  const [, standard, profileTopicId] = hrlMatch;
15770
15776
  this.logger.info(
@@ -15773,11 +15779,47 @@ class HCS10BaseClient extends Registration {
15773
15779
  const cdnUrl = `https://kiloscribe.com/api/inscription-cdn/${profileTopicId}?network=${this.network}`;
15774
15780
  const profileResponse = await axios.get(cdnUrl);
15775
15781
  if (!profileResponse.data) {
15776
- throw new Error(
15777
- `Failed to fetch profile from topic: ${profileTopicId}`
15778
- );
15782
+ return {
15783
+ profile: null,
15784
+ success: false,
15785
+ error: `Failed to fetch profile from topic: ${profileTopicId}`
15786
+ };
15779
15787
  }
15780
15788
  const profile = profileResponse.data;
15789
+ let topicInfo = null;
15790
+ if (profile.inboundTopicId && profile.outboundTopicId) {
15791
+ topicInfo = {
15792
+ inboundTopic: profile.inboundTopicId,
15793
+ outboundTopic: profile.outboundTopicId
15794
+ };
15795
+ const cacheKey = `${accountId}-${this.network}`;
15796
+ HCS10Cache.getInstance().set(cacheKey, topicInfo);
15797
+ }
15798
+ return {
15799
+ profile,
15800
+ topicInfo,
15801
+ profileTopicId,
15802
+ success: true
15803
+ };
15804
+ } catch (error) {
15805
+ this.logger.error("Failed to retrieve profile:", error);
15806
+ return {
15807
+ profile: null,
15808
+ success: false,
15809
+ error: error instanceof Error ? error.message : String(error)
15810
+ };
15811
+ }
15812
+ }
15813
+ async retrieveOutboundConnectTopic(accountId) {
15814
+ this.logger.info(`Retrieving topics for account: ${accountId}`);
15815
+ const cachedInfo = await this.getTopicInfo(accountId);
15816
+ if (cachedInfo) return cachedInfo;
15817
+ try {
15818
+ const profileResponse = await this.retrieveProfile(accountId);
15819
+ if (!profileResponse.success) {
15820
+ throw new Error(profileResponse.error);
15821
+ }
15822
+ const profile = profileResponse.profile;
15781
15823
  if (!profile.inboundTopicId || !profile.outboundTopicId) {
15782
15824
  throw new Error(
15783
15825
  `Invalid HCS-11 profile for HCS-10 agent: missing inboundTopicId or outboundTopicId`
@@ -15797,9 +15839,7 @@ class HCS10BaseClient extends Registration {
15797
15839
  }
15798
15840
  async retrieveOutboundMessages(agentAccountId) {
15799
15841
  try {
15800
- const topicInfo = await this.retrieveOutboundConnectTopic(
15801
- agentAccountId
15802
- );
15842
+ const topicInfo = await this.retrieveOutboundConnectTopic(agentAccountId);
15803
15843
  if (!topicInfo) {
15804
15844
  this.logger.warn(
15805
15845
  `No outbound connect topic found for agentAccountId: ${agentAccountId}`
@@ -15834,6 +15874,37 @@ class HCS10BaseClient extends Registration {
15834
15874
  clearCache() {
15835
15875
  HCS10Cache.getInstance().clear();
15836
15876
  }
15877
+ /**
15878
+ * Gets message content, resolving any HRL references if needed
15879
+ * @param data The message data which might be an HRL reference
15880
+ * @returns The resolved content
15881
+ */
15882
+ async getMessageContent(data) {
15883
+ const hrlPattern = /^hcs:\/\/(\d+)\/([0-9.]+)$/;
15884
+ const match = data.match(hrlPattern);
15885
+ if (!match) {
15886
+ return data;
15887
+ }
15888
+ const [_, standard, topicId] = match;
15889
+ this.logger.info(
15890
+ `Resolving HRL reference: standard=${standard}, topicId=${topicId}`
15891
+ );
15892
+ try {
15893
+ const cdnUrl = `https://kiloscribe.com/api/inscription-cdn/${topicId}?network=${this.network}`;
15894
+ const response = await axios.get(cdnUrl);
15895
+ if (!response.data) {
15896
+ throw new Error(`Failed to fetch content from topic: ${topicId}`);
15897
+ }
15898
+ return response.data.content || response.data.text || JSON.stringify(response.data);
15899
+ } catch (error) {
15900
+ this.logger.error(
15901
+ `Error resolving HRL reference: ${error instanceof Error ? error.message : "Unknown error"}`
15902
+ );
15903
+ throw new Error(
15904
+ `Failed to resolve HRL reference: ${error instanceof Error ? error.message : "Unknown error"}`
15905
+ );
15906
+ }
15907
+ }
15837
15908
  }
15838
15909
  class HCS10Cache {
15839
15910
  constructor() {
@@ -32410,6 +32481,35 @@ class HCS10Client extends HCS10BaseClient {
32410
32481
  data,
32411
32482
  m: memo
32412
32483
  };
32484
+ const payloadString = JSON.stringify(payload);
32485
+ const isLargePayload = Buffer2.from(payloadString).length > 1e3;
32486
+ if (isLargePayload) {
32487
+ this.logger.info(
32488
+ "Message payload exceeds 1000 bytes, storing via inscription"
32489
+ );
32490
+ try {
32491
+ const contentBuffer = Buffer2.from(data);
32492
+ const fileName = `message-${Date.now()}.json`;
32493
+ const inscriptionResult = await this.inscribeFile(
32494
+ contentBuffer,
32495
+ fileName
32496
+ );
32497
+ if (inscriptionResult == null ? void 0 : inscriptionResult.topic_id) {
32498
+ payload.data = `hcs://1/${inscriptionResult.topic_id}`;
32499
+ this.logger.info(
32500
+ `Large message inscribed with topic ID: ${inscriptionResult.topic_id}`
32501
+ );
32502
+ } else {
32503
+ throw new Error("Failed to inscribe large message content");
32504
+ }
32505
+ } catch (error) {
32506
+ this.logger.error("Error inscribing large message:", error);
32507
+ throw new Error(
32508
+ `Failed to handle large message: ${error instanceof Error ? error.message : "Unknown error"}`
32509
+ );
32510
+ }
32511
+ }
32512
+ this.logger.info("Submitting message to connection topic", payload);
32413
32513
  await this.submitPayload(
32414
32514
  connectionTopicId,
32415
32515
  payload,
@@ -32467,9 +32567,9 @@ class HCS10Client extends HCS10BaseClient {
32467
32567
  async submitPayload(topicId, payload, submitKey, requiresFee = false) {
32468
32568
  const message = typeof payload === "string" ? payload : JSON.stringify(payload);
32469
32569
  const payloadSizeInBytes = Buffer2.byteLength(message, "utf8");
32470
- if (payloadSizeInBytes > 1024) {
32570
+ if (payloadSizeInBytes > 1e3) {
32471
32571
  throw new PayloadSizeError(
32472
- "Payload size exceeds 1 KB limit",
32572
+ "Payload size exceeds 1000 bytes limit",
32473
32573
  payloadSizeInBytes
32474
32574
  );
32475
32575
  }
@@ -33056,6 +33156,34 @@ class BrowserHCSClient extends HCS10BaseClient {
33056
33156
  data,
33057
33157
  m: memo
33058
33158
  };
33159
+ const payloadString = JSON.stringify(payload);
33160
+ const isLargePayload = Buffer2.from(payloadString).length > 1e3;
33161
+ if (isLargePayload) {
33162
+ this.logger.info(
33163
+ "Message payload exceeds 1000 bytes, storing via inscription"
33164
+ );
33165
+ try {
33166
+ const contentBuffer = Buffer2.from(data);
33167
+ const fileName = `message-${Date.now()}.json`;
33168
+ const inscriptionResult = await this.inscribeFile(
33169
+ contentBuffer,
33170
+ fileName
33171
+ );
33172
+ if (inscriptionResult == null ? void 0 : inscriptionResult.topic_id) {
33173
+ payload.data = `hcs://1/${inscriptionResult.topic_id}`;
33174
+ this.logger.info(
33175
+ `Large message inscribed with topic ID: ${inscriptionResult.topic_id}`
33176
+ );
33177
+ } else {
33178
+ throw new Error("Failed to inscribe large message content");
33179
+ }
33180
+ } catch (error) {
33181
+ this.logger.error("Error inscribing large message:", error);
33182
+ throw new Error(
33183
+ `Failed to handle large message: ${error instanceof Error ? error.message : "Unknown error"}`
33184
+ );
33185
+ }
33186
+ }
33059
33187
  await this.submitPayload(connectionTopicId, payload);
33060
33188
  }
33061
33189
  async submitConnectionRequest(inboundTopicId, requestingAccountId, operatorId, memo) {