mirror of
https://github.com/logos-messaging/logos-messaging-go-bindings.git
synced 2026-01-06 16:03:07 +00:00
39 lines
779 B
Go
39 lines
779 B
Go
package common
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
// MessageHash represents an unique identifier for a message within a pubsub topic
|
|
type MessageHash string
|
|
|
|
func ToMessageHash(val string) (MessageHash, error) {
|
|
if len(val) == 0 {
|
|
return "", fmt.Errorf("empty string not allowed")
|
|
}
|
|
|
|
if len(val) < 2 || val[:2] != "0x" {
|
|
return "", fmt.Errorf("string must start with 0x")
|
|
}
|
|
|
|
// Remove "0x" prefix for hex decoding
|
|
hexStr := val[2:]
|
|
|
|
// Verify the remaining string is valid hex
|
|
_, err := hex.DecodeString(hexStr)
|
|
if err != nil {
|
|
return "", fmt.Errorf("invalid hex string: %v", err)
|
|
}
|
|
|
|
return MessageHash(val), nil
|
|
}
|
|
|
|
func (h MessageHash) String() string {
|
|
return string(h)
|
|
}
|
|
|
|
func (h MessageHash) Bytes() ([]byte, error) {
|
|
return hex.DecodeString(string(h))
|
|
}
|