2023-05-02 15:10:45 +00:00
|
|
|
package cliutils
|
|
|
|
|
|
|
|
import (
|
2023-05-22 21:03:40 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/hex"
|
2023-05-02 15:10:45 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-05-22 21:03:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-05-02 15:10:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProtectedTopic struct {
|
2023-05-22 21:03:40 +00:00
|
|
|
Topic string
|
|
|
|
PublicKey *ecdsa.PublicKey
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p ProtectedTopic) String() string {
|
2023-05-22 21:03:40 +00:00
|
|
|
pubKBytes := crypto.FromECDSAPub(p.PublicKey)
|
|
|
|
return fmt.Sprintf("%s:%s", p.Topic, hex.EncodeToString(pubKBytes))
|
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-22 21:03:40 +00:00
|
|
|
pubk, err := crypto.UnmarshalPubkey(common.FromHex(protectedTopicParts[1]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
|
|
|
*k.Values = append(*k.Values, ProtectedTopic{
|
2023-05-22 21:03:40 +00:00
|
|
|
Topic: protectedTopicParts[0],
|
|
|
|
PublicKey: pubk,
|
2023-05-02 15:10:45 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
func (k *ProtectedTopicSlice) String() string {
|
|
|
|
if k.Values == nil {
|
2023-05-02 15:10:45 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var output []string
|
2023-09-11 14:24:05 +00:00
|
|
|
for _, v := range *k.Values {
|
2023-05-02 15:10:45 +00:00
|
|
|
output = append(output, v.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(output, ", ")
|
|
|
|
}
|