44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package vo
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// TransactionHash is a value object representing an on-chain transaction hash.
|
|
type TransactionHash struct {
|
|
value string
|
|
}
|
|
|
|
// txHashPattern matches a hex-encoded transaction hash (0x-prefixed, 64 hex chars for 32 bytes).
|
|
var txHashPattern = regexp.MustCompile(`^0x[0-9a-fA-F]{64}$`)
|
|
|
|
// NewTransactionHash creates a validated TransactionHash value object.
|
|
func NewTransactionHash(raw string) (TransactionHash, error) {
|
|
trimmed := strings.TrimSpace(raw)
|
|
if trimmed == "" {
|
|
return TransactionHash{}, fmt.Errorf("transaction hash must not be empty")
|
|
}
|
|
if !txHashPattern.MatchString(trimmed) {
|
|
return TransactionHash{}, fmt.Errorf("invalid transaction hash format: %s", trimmed)
|
|
}
|
|
// Normalise to lowercase
|
|
return TransactionHash{value: strings.ToLower(trimmed)}, nil
|
|
}
|
|
|
|
// Value returns the string value of the transaction hash.
|
|
func (h TransactionHash) Value() string {
|
|
return h.value
|
|
}
|
|
|
|
// String returns the string representation.
|
|
func (h TransactionHash) String() string {
|
|
return h.value
|
|
}
|
|
|
|
// IsValid reports whether the hash passes format validation.
|
|
func (h TransactionHash) IsValid() bool {
|
|
return txHashPattern.MatchString(h.value)
|
|
}
|