2023-05-02 15:10:45 +00:00
|
|
|
package cliutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProtectedTopic struct {
|
2023-05-10 12:17:23 +00:00
|
|
|
Topic string
|
|
|
|
Address common.Address
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p ProtectedTopic) String() string {
|
2023-05-10 12:17:23 +00:00
|
|
|
return fmt.Sprintf("%s:%s", p.Topic, p.Address.String())
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProtectedTopicSlice struct {
|
|
|
|
Values *[]ProtectedTopic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *ProtectedTopicSlice) Set(value string) error {
|
|
|
|
protectedTopicParts := strings.Split(value, ":")
|
|
|
|
if len(protectedTopicParts) != 2 {
|
|
|
|
return errors.New("expected topic_name:hex_encoded_public_key")
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:17:23 +00:00
|
|
|
if !common.IsHexAddress(protectedTopicParts[1]) {
|
|
|
|
return errors.New("invalid address format")
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
2023-05-10 12:17:23 +00:00
|
|
|
|
2023-05-02 15:10:45 +00:00
|
|
|
*k.Values = append(*k.Values, ProtectedTopic{
|
2023-05-10 12:17:23 +00:00
|
|
|
Topic: protectedTopicParts[0],
|
|
|
|
Address: common.HexToAddress(protectedTopicParts[1]),
|
2023-05-02 15:10:45 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ProtectedTopicSlice) String() string {
|
|
|
|
if v.Values == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var output []string
|
|
|
|
for _, v := range *v.Values {
|
|
|
|
output = append(output, v.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(output, ", ")
|
|
|
|
}
|