From 3c470af5d00d0c36b3bdb55db1fe1383bd3f3d58 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 13:01:29 -0300 Subject: [PATCH 1/6] feat: create getMessageByMessageID and GetMessageFormattedByMessageID --- messages/getMessageByMessageId.go | 101 ++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 messages/getMessageByMessageId.go diff --git a/messages/getMessageByMessageId.go b/messages/getMessageByMessageId.go new file mode 100644 index 0000000..59bde40 --- /dev/null +++ b/messages/getMessageByMessageId.go @@ -0,0 +1,101 @@ +package messages + +import ( + "context" + "errors" + "log" + "strings" + + "github.com/allancapistrano/tangle-client-go/utils" + iotago "github.com/iotaledger/iota.go/v2" +) + +// Get a message on the node by a given message ID. +func getMessageByMessageID(nodeUrl string, messageIdHex string) (*iotago.Message, error) { + node := iotago.NewNodeHTTPAPIClient(nodeUrl) + + messageId, err := iotago.MessageIDFromHexString(messageIdHex) + if err != nil { + return &iotago.Message{}, errors.New("unable to convert message ID from hex to message ID representation") + } + + messageReturned, err := node.MessageByMessageID(context.Background(), messageId) + if err != nil { + return &iotago.Message{}, errors.New("unable to get message by given message ID") + } + + return messageReturned, nil +} + +// Get a message on the node by a given message ID, into a custom message type. +func GetMessageFormattedByMessageID(nodeUrl string, messageIdHex string) (Message, error) { + var message Message + + messageReturned, err := getMessageByMessageID(nodeUrl, messageIdHex) + if err != nil { + log.Println(err) + + message = Message{ + Index: "Error", + Content: err.Error(), + } + } else { + message, err = formatMessagePayloadWithoutIndex(messageReturned) + if err != nil { + log.Println(err) + + message = Message{ + Index: "Error", + Content: err.Error(), + } + } + + SanitizeMessage(&message) + } + + return message, nil +} + +// Formats the message payload into a custom message type. +func formatMessagePayloadWithoutIndex(message *iotago.Message) (Message, error) { + payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) + if err != nil { + return Message{}, err + } + + index := "" + content := "" + + if strings.Contains(payloadInString, "/") { + payloadTemp := strings.Split(payloadInString, "/") + + index = payloadTemp[0] + content = payloadTemp[1] + } else if strings.Contains(payloadInString, "\v") { + payloadTemp := strings.Split(payloadInString, "\v") + + if len(payloadTemp) == 2 { + index = payloadTemp[0] + content = payloadTemp[1] + } else if len(payloadTemp) == 3 { + index = payloadTemp[1] + content = payloadTemp[2] + } else { + return Message{}, errors.New("unexpected array length") + } + } else if strings.Contains(payloadInString, "\t") { + payloadTemp := strings.Split(payloadInString, "\t") + + index = payloadTemp[0] + content = payloadTemp[1] + } else { + return Message{}, errors.New("malformed payload") + } + + formattedMessage := Message{ + Index: strings.Trim(index, "\f"), + Content: strings.Trim(content, "\f"), + } + + return formattedMessage, nil +} From 1bc644e1eac845c7dd5202802798f5d92e162b34 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 13:04:48 -0300 Subject: [PATCH 2/6] feat: using GetMessageFormattedByMessageID --- main.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index bc03429..0252c0e 100644 --- a/main.go +++ b/main.go @@ -24,13 +24,25 @@ func main() { // messages.SubmitMessage(nodeURL, "LB_REPLY", "{asdfghjkl}", 15) // Reading some messages by an index. - messages, err := messages.GetAllMessagesByIndex(nodeURL, "LB_ENTRY_REPLY") + messagesByIndex, err := messages.GetAllMessagesByIndex(nodeURL, "LB_ENTRY_REPLY") if err != nil { log.Fatal(err) } - for _, v := range messages { + for _, v := range messagesByIndex { fmt.Println([]byte(strings.Trim(v.Content, "\t"))) fmt.Printf("Index: %s | Content: %s\n", v.Index, v.Content) } + + messageID := "9597556533c5e91112c0b02244799a4d308ca007486e8e844d5d78b0f298b667" + + message, err := messages.GetMessageFormattedByMessageID( + nodeURL, + messageID, + ) + if err != nil { + log.Fatal(err) + } + fmt.Println(message.Index) + fmt.Println(message.Content) } From 0f25d1b0a4e83b0191a7b563bf10d7166e2378b1 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:07:31 -0300 Subject: [PATCH 3/6] chore: using getMessageByMessageID --- messages/getAllMessagesByIndex.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/messages/getAllMessagesByIndex.go b/messages/getAllMessagesByIndex.go index f2d00ec..2932616 100644 --- a/messages/getAllMessagesByIndex.go +++ b/messages/getAllMessagesByIndex.go @@ -28,17 +28,10 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) { if msgIdsResponse.Count > 0 { for i = 0; i < msgIdsResponse.Count; i++ { - messageId, err := iotago.MessageIDFromHexString(msgIdsResponse.MessageIDs[i]) - if err != nil { - return nil, errors.New("unable to convert message ID from hex to message ID representation") - } + var message Message - messageReturned, err := node.MessageByMessageID(context.Background(), messageId) - if err != nil { - return nil, errors.New("unable to get message by given message ID") - } + messageReturned, err := getMessageByMessageID(nodeUrl, msgIdsResponse.MessageIDs[i]) - message, err := formatMessagePayload(*messageReturned, index) if err != nil { log.Println(err) @@ -46,11 +39,22 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) { Index: "Error", Content: err.Error(), } - } + } else { + message, err = formatMessagePayload(*messageReturned, index) + + if err != nil { + log.Println(err) - SanitizeMessage(&message) + message = Message{ + Index: "Error", + Content: err.Error(), + } + } else { + SanitizeMessage(&message) - messages = append(messages, message) + messages = append(messages, message) + } + } } } else { log.Println("No messages with this index were found.") From ca2b0a383bb3359ba5256964c386251060a62b53 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:09:39 -0300 Subject: [PATCH 4/6] chore: change function visibility --- messages/getAllMessagesByIndex.go | 2 +- messages/getMessageByMessageId.go | 2 +- messages/message.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/messages/getAllMessagesByIndex.go b/messages/getAllMessagesByIndex.go index 2932616..07b8d4b 100644 --- a/messages/getAllMessagesByIndex.go +++ b/messages/getAllMessagesByIndex.go @@ -50,7 +50,7 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) { Content: err.Error(), } } else { - SanitizeMessage(&message) + sanitizeMessage(&message) messages = append(messages, message) } diff --git a/messages/getMessageByMessageId.go b/messages/getMessageByMessageId.go index 59bde40..e8f4284 100644 --- a/messages/getMessageByMessageId.go +++ b/messages/getMessageByMessageId.go @@ -50,7 +50,7 @@ func GetMessageFormattedByMessageID(nodeUrl string, messageIdHex string) (Messag } } - SanitizeMessage(&message) + sanitizeMessage(&message) } return message, nil diff --git a/messages/message.go b/messages/message.go index d28d08b..d03fa35 100644 --- a/messages/message.go +++ b/messages/message.go @@ -8,7 +8,7 @@ type Message struct { } // Sanitizes a message. -func SanitizeMessage(message *Message) { +func sanitizeMessage(message *Message) { message.Content = utils.SanitizeString(message.Content) message.Index = utils.SanitizeString(message.Index) } From a644e98ce62f7125a20ac2eb46b590b9fff48883 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:11:41 -0300 Subject: [PATCH 5/6] chore: moving format message functions --- messages/getAllMessagesByIndex.go | 51 --------------- messages/getMessageByMessageId.go | 46 -------------- messages/message.go | 102 +++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 98 deletions(-) diff --git a/messages/getAllMessagesByIndex.go b/messages/getAllMessagesByIndex.go index 07b8d4b..1e55e4b 100644 --- a/messages/getAllMessagesByIndex.go +++ b/messages/getAllMessagesByIndex.go @@ -4,9 +4,7 @@ import ( "context" "errors" "log" - "strings" - "github.com/allancapistrano/tangle-client-go/utils" iotago "github.com/iotaledger/iota.go/v2" ) @@ -62,52 +60,3 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) { return messages, nil } - -// Formats the message payload into a custom message type. -func formatMessagePayload(message iotago.Message, messageIndex string) (Message, error) { - payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) - if err != nil { - return Message{}, err - } - - index := "" - content := "" - - if strings.Contains(payloadInString, "/") { - payloadTemp := strings.Split(payloadInString, "/") - - index = payloadTemp[0] - content = payloadTemp[1] - } else if strings.Contains(payloadInString, "\v") { - payloadTemp := strings.Split(payloadInString, "\v") - - if len(payloadTemp) == 2 { - index = payloadTemp[0] - content = payloadTemp[1] - } else if len(payloadTemp) == 3 { - index = payloadTemp[1] - content = payloadTemp[2] - } else { - return Message{}, errors.New("unexpected array length") - } - } else if strings.Contains(payloadInString, "\t") { - payloadTemp := strings.Split(payloadInString, "\t") - - index = payloadTemp[0] - content = payloadTemp[1] - } else if strings.Contains(payloadInString, messageIndex) { - payloadTemp := strings.Split(payloadInString, messageIndex) - - index = messageIndex - content = payloadTemp[1] - } else { - return Message{}, errors.New("malformed payload") - } - - formattedMessage := Message{ - Index: strings.Trim(index, "\f"), - Content: strings.Trim(content, "\f"), - } - - return formattedMessage, nil -} diff --git a/messages/getMessageByMessageId.go b/messages/getMessageByMessageId.go index e8f4284..0e107d7 100644 --- a/messages/getMessageByMessageId.go +++ b/messages/getMessageByMessageId.go @@ -4,9 +4,7 @@ import ( "context" "errors" "log" - "strings" - "github.com/allancapistrano/tangle-client-go/utils" iotago "github.com/iotaledger/iota.go/v2" ) @@ -55,47 +53,3 @@ func GetMessageFormattedByMessageID(nodeUrl string, messageIdHex string) (Messag return message, nil } - -// Formats the message payload into a custom message type. -func formatMessagePayloadWithoutIndex(message *iotago.Message) (Message, error) { - payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) - if err != nil { - return Message{}, err - } - - index := "" - content := "" - - if strings.Contains(payloadInString, "/") { - payloadTemp := strings.Split(payloadInString, "/") - - index = payloadTemp[0] - content = payloadTemp[1] - } else if strings.Contains(payloadInString, "\v") { - payloadTemp := strings.Split(payloadInString, "\v") - - if len(payloadTemp) == 2 { - index = payloadTemp[0] - content = payloadTemp[1] - } else if len(payloadTemp) == 3 { - index = payloadTemp[1] - content = payloadTemp[2] - } else { - return Message{}, errors.New("unexpected array length") - } - } else if strings.Contains(payloadInString, "\t") { - payloadTemp := strings.Split(payloadInString, "\t") - - index = payloadTemp[0] - content = payloadTemp[1] - } else { - return Message{}, errors.New("malformed payload") - } - - formattedMessage := Message{ - Index: strings.Trim(index, "\f"), - Content: strings.Trim(content, "\f"), - } - - return formattedMessage, nil -} diff --git a/messages/message.go b/messages/message.go index d03fa35..78c20b3 100644 --- a/messages/message.go +++ b/messages/message.go @@ -1,6 +1,13 @@ package messages -import "github.com/allancapistrano/tangle-client-go/utils" +import ( + "errors" + "strings" + + "github.com/allancapistrano/tangle-client-go/utils" + + iotago "github.com/iotaledger/iota.go/v2" +) type Message struct { Index string `json:"index"` @@ -12,3 +19,96 @@ func sanitizeMessage(message *Message) { message.Content = utils.SanitizeString(message.Content) message.Index = utils.SanitizeString(message.Index) } + +// Formats the message payload into a custom message type. +func formatMessagePayload(message iotago.Message, messageIndex string) (Message, error) { + payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) + if err != nil { + return Message{}, err + } + + index := "" + content := "" + + if strings.Contains(payloadInString, "/") { + payloadTemp := strings.Split(payloadInString, "/") + + index = payloadTemp[0] + content = payloadTemp[1] + } else if strings.Contains(payloadInString, "\v") { + payloadTemp := strings.Split(payloadInString, "\v") + + if len(payloadTemp) == 2 { + index = payloadTemp[0] + content = payloadTemp[1] + } else if len(payloadTemp) == 3 { + index = payloadTemp[1] + content = payloadTemp[2] + } else { + return Message{}, errors.New("unexpected array length") + } + } else if strings.Contains(payloadInString, "\t") { + payloadTemp := strings.Split(payloadInString, "\t") + + index = payloadTemp[0] + content = payloadTemp[1] + } else if strings.Contains(payloadInString, messageIndex) { + payloadTemp := strings.Split(payloadInString, messageIndex) + + index = messageIndex + content = payloadTemp[1] + } else { + return Message{}, errors.New("malformed payload") + } + + formattedMessage := Message{ + Index: strings.Trim(index, "\f"), + Content: strings.Trim(content, "\f"), + } + + return formattedMessage, nil +} + +// Formats the message payload into a custom message type. +func formatMessagePayloadWithoutIndex(message *iotago.Message) (Message, error) { + payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) + if err != nil { + return Message{}, err + } + + index := "" + content := "" + + if strings.Contains(payloadInString, "/") { + payloadTemp := strings.Split(payloadInString, "/") + + index = payloadTemp[0] + content = payloadTemp[1] + } else if strings.Contains(payloadInString, "\v") { + payloadTemp := strings.Split(payloadInString, "\v") + + if len(payloadTemp) == 2 { + index = payloadTemp[0] + content = payloadTemp[1] + } else if len(payloadTemp) == 3 { + index = payloadTemp[1] + content = payloadTemp[2] + } else { + return Message{}, errors.New("unexpected array length") + } + } else if strings.Contains(payloadInString, "\t") { + payloadTemp := strings.Split(payloadInString, "\t") + + index = payloadTemp[0] + content = payloadTemp[1] + } else { + return Message{}, errors.New("malformed payload") + } + + formattedMessage := Message{ + Index: strings.Trim(index, "\f"), + Content: strings.Trim(content, "\f"), + } + + return formattedMessage, nil +} From 9c84645a8ba7b69c84467e89b8fe6722f151b60c Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:12:17 -0300 Subject: [PATCH 6/6] style: formatting code --- messages/message.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/messages/message.go b/messages/message.go index 78c20b3..087f06c 100644 --- a/messages/message.go +++ b/messages/message.go @@ -4,9 +4,9 @@ import ( "errors" "strings" - "github.com/allancapistrano/tangle-client-go/utils" - iotago "github.com/iotaledger/iota.go/v2" + + "github.com/allancapistrano/tangle-client-go/utils" ) type Message struct {