@hashgraphonline/standards-sdk 0.0.44 → 0.0.45

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.
@@ -9,7 +9,7 @@ export interface HCS10Config {
9
9
  }
10
10
  export interface HCSMessage {
11
11
  p: 'hcs-10';
12
- op: 'connection_request' | 'connection_created' | 'message';
12
+ op: 'connection_request' | 'connection_created' | 'message' | 'close_connection';
13
13
  data: string;
14
14
  created?: Date;
15
15
  consensus_timestamp?: string;
@@ -24,6 +24,7 @@ export interface HCSMessage {
24
24
  connection_id?: number;
25
25
  sequence_number: number;
26
26
  operator_id?: string;
27
+ reason?: string;
27
28
  }
28
29
  export interface ProfileResponse {
29
30
  profile: any;
@@ -40,6 +41,25 @@ export declare abstract class HCS10BaseClient extends Registration {
40
41
  accountId: string;
41
42
  signer: any;
42
43
  };
44
+ /**
45
+ * Get a stream of messages from a connection topic
46
+ * @param topicId The connection topic ID to get messages from
47
+ * @returns A stream of filtered messages valid for connection topics
48
+ */
49
+ getMessageStream(topicId: string): Promise<{
50
+ messages: HCSMessage[];
51
+ }>;
52
+ /**
53
+ * Validates if an operator_id follows the correct format (agentTopicId@accountId)
54
+ * @param operatorId The operator ID to validate
55
+ * @returns True if the format is valid, false otherwise
56
+ */
57
+ protected isValidOperatorId(operatorId: string): boolean;
58
+ /**
59
+ * Get all messages from a topic
60
+ * @param topicId The topic ID to get messages from
61
+ * @returns All messages from the topic
62
+ */
43
63
  getMessages(topicId: string): Promise<{
44
64
  messages: HCSMessage[];
45
65
  }>;
@@ -23824,11 +23824,106 @@ class HCS10BaseClient extends Registration {
23824
23824
  this.logger
23825
23825
  );
23826
23826
  }
23827
+ /**
23828
+ * Get a stream of messages from a connection topic
23829
+ * @param topicId The connection topic ID to get messages from
23830
+ * @returns A stream of filtered messages valid for connection topics
23831
+ */
23832
+ async getMessageStream(topicId) {
23833
+ try {
23834
+ const messages = await this.mirrorNode.getTopicMessages(topicId);
23835
+ const validOps = ["message", "close_connection"];
23836
+ const filteredMessages = messages.filter((msg) => {
23837
+ if (msg.p !== "hcs-10" || !validOps.includes(msg.op)) {
23838
+ return false;
23839
+ }
23840
+ if (msg.op === "message") {
23841
+ if (!msg.data) {
23842
+ return false;
23843
+ }
23844
+ if (!msg.operator_id) {
23845
+ return false;
23846
+ }
23847
+ if (!this.isValidOperatorId(msg.operator_id)) {
23848
+ return false;
23849
+ }
23850
+ }
23851
+ if (msg.op === "close_connection") {
23852
+ if (!msg.operator_id) {
23853
+ return false;
23854
+ }
23855
+ if (!this.isValidOperatorId(msg.operator_id)) {
23856
+ return false;
23857
+ }
23858
+ }
23859
+ return true;
23860
+ });
23861
+ return {
23862
+ messages: filteredMessages
23863
+ };
23864
+ } catch (error) {
23865
+ if (this.logger) {
23866
+ this.logger.error(`Error fetching messages: ${error.message}`);
23867
+ }
23868
+ return { messages: [] };
23869
+ }
23870
+ }
23871
+ /**
23872
+ * Validates if an operator_id follows the correct format (agentTopicId@accountId)
23873
+ * @param operatorId The operator ID to validate
23874
+ * @returns True if the format is valid, false otherwise
23875
+ */
23876
+ isValidOperatorId(operatorId) {
23877
+ if (!operatorId) {
23878
+ return false;
23879
+ }
23880
+ const parts = operatorId.split("@");
23881
+ if (parts.length !== 2) {
23882
+ return false;
23883
+ }
23884
+ const agentTopicId = parts[0];
23885
+ const accountId = parts[1];
23886
+ if (!agentTopicId) {
23887
+ return false;
23888
+ }
23889
+ if (!accountId) {
23890
+ return false;
23891
+ }
23892
+ const hederaIdPattern = /^[0-9]+\.[0-9]+\.[0-9]+$/;
23893
+ if (!hederaIdPattern.test(accountId)) {
23894
+ return false;
23895
+ }
23896
+ if (!hederaIdPattern.test(agentTopicId)) {
23897
+ return false;
23898
+ }
23899
+ return true;
23900
+ }
23901
+ /**
23902
+ * Get all messages from a topic
23903
+ * @param topicId The topic ID to get messages from
23904
+ * @returns All messages from the topic
23905
+ */
23827
23906
  async getMessages(topicId) {
23828
23907
  try {
23829
23908
  const messages = await this.mirrorNode.getTopicMessages(topicId);
23909
+ const validatedMessages = messages.filter((msg) => {
23910
+ if (msg.p !== "hcs-10") {
23911
+ return false;
23912
+ }
23913
+ if (msg.op === "message") {
23914
+ if (!msg.data) {
23915
+ return false;
23916
+ }
23917
+ if (msg.operator_id) {
23918
+ if (!this.isValidOperatorId(msg.operator_id)) {
23919
+ return false;
23920
+ }
23921
+ }
23922
+ }
23923
+ return true;
23924
+ });
23830
23925
  return {
23831
- messages
23926
+ messages: validatedMessages
23832
23927
  };
23833
23928
  } catch (error) {
23834
23929
  if (this.logger) {