From 38a9fc4b19ba9eae64f8f8da0388e2a9b06cf72f Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 10 May 2023 08:17:23 -0400 Subject: [PATCH] refactor: use an address instead of public key for the node setup --- waku/cliutils/protected_topic.go | 20 ++++++++------------ waku/node.go | 2 +- waku/v2/protocol/relay/validators.go | 12 +++++------- waku/v2/protocol/relay/validators_test.go | 8 +++++--- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/waku/cliutils/protected_topic.go b/waku/cliutils/protected_topic.go index e3302690..2537f45d 100644 --- a/waku/cliutils/protected_topic.go +++ b/waku/cliutils/protected_topic.go @@ -1,24 +1,20 @@ package cliutils import ( - "crypto/ecdsa" - "encoding/hex" "errors" "fmt" "strings" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" ) type ProtectedTopic struct { - Topic string - PublicKey *ecdsa.PublicKey + Topic string + Address common.Address } func (p ProtectedTopic) String() string { - pubKBytes := crypto.FromECDSAPub(p.PublicKey) - return fmt.Sprintf("%s:%s", p.Topic, hex.EncodeToString(pubKBytes)) + return fmt.Sprintf("%s:%s", p.Topic, p.Address.String()) } type ProtectedTopicSlice struct { @@ -31,13 +27,13 @@ func (k *ProtectedTopicSlice) Set(value string) error { return errors.New("expected topic_name:hex_encoded_public_key") } - pubk, err := crypto.UnmarshalPubkey(common.FromHex(protectedTopicParts[1])) - if err != nil { - return err + if !common.IsHexAddress(protectedTopicParts[1]) { + return errors.New("invalid address format") } + *k.Values = append(*k.Values, ProtectedTopic{ - Topic: protectedTopicParts[0], - PublicKey: pubk, + Topic: protectedTopicParts[0], + Address: common.HexToAddress(protectedTopicParts[1]), }) return nil } diff --git a/waku/node.go b/waku/node.go index a6c76cbe..7375deca 100644 --- a/waku/node.go +++ b/waku/node.go @@ -311,7 +311,7 @@ func Execute(options Options) { } for _, protectedTopic := range options.Relay.ProtectedTopics { - err := wakuNode.Relay().AddSignedTopicValidator(protectedTopic.Topic, protectedTopic.PublicKey) + err := wakuNode.Relay().AddSignedTopicValidator(protectedTopic.Topic, protectedTopic.Address) failOnErr(err, "Error adding signed topic validator") } } diff --git a/waku/v2/protocol/relay/validators.go b/waku/v2/protocol/relay/validators.go index dac50714..418b6f4d 100644 --- a/waku/v2/protocol/relay/validators.go +++ b/waku/v2/protocol/relay/validators.go @@ -4,11 +4,10 @@ import ( "bytes" "context" "crypto/ecdsa" - "crypto/elliptic" "encoding/binary" - "encoding/hex" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/secp256k1" @@ -56,8 +55,7 @@ func withinTimeWindow(t timesource.Timesource, msg *pb.WakuMessage) bool { type validatorFn = func(ctx context.Context, peerID peer.ID, message *pubsub.Message) bool -func validatorFnBuilder(t timesource.Timesource, publicKey *ecdsa.PublicKey) (validatorFn, error) { - address := crypto.PubkeyToAddress(*publicKey) +func validatorFnBuilder(t timesource.Timesource, address common.Address) (validatorFn, error) { topic := protocol.NewNamedShardingPubsubTopic(address.String() + "/proto").String() return func(ctx context.Context, peerID peer.ID, message *pubsub.Message) bool { msg := new(pb.WakuMessage) @@ -84,10 +82,10 @@ func validatorFnBuilder(t timesource.Timesource, publicKey *ecdsa.PublicKey) (va }, nil } -func (w *WakuRelay) AddSignedTopicValidator(topic string, publicKey *ecdsa.PublicKey) error { - w.log.Info("adding validator to signed topic", zap.String("topic", topic), zap.String("publicKey", hex.EncodeToString(elliptic.Marshal(publicKey.Curve, publicKey.X, publicKey.Y)))) +func (w *WakuRelay) AddSignedTopicValidator(topic string, address common.Address) error { + w.log.Info("adding validator to signed topic", zap.String("topic", topic), zap.String("address", address.String())) - fn, err := validatorFnBuilder(w.timesource, publicKey) + fn, err := validatorFnBuilder(w.timesource, address) if err != nil { return err } diff --git a/waku/v2/protocol/relay/validators_test.go b/waku/v2/protocol/relay/validators_test.go index 352b1b1f..8b0820eb 100644 --- a/waku/v2/protocol/relay/validators_test.go +++ b/waku/v2/protocol/relay/validators_test.go @@ -60,11 +60,13 @@ func TestMsgHash(t *testing.T) { msgData, _ := proto.Marshal(msg) + address := crypto.PubkeyToAddress(prvKey.PublicKey) + //expectedMessageHash, _ := hex.DecodeString("662F8C20A335F170BD60ABC1F02AD66F0C6A6EE285DA2A53C95259E7937C0AE9") //messageHash := MsgHash(pubsubTopic, msg) //require.True(t, bytes.Equal(expectedMessageHash, messageHash)) - myValidator, err := validatorFnBuilder(NewFakeTimesource(timestamp), &prvKey.PublicKey) + myValidator, err := validatorFnBuilder(NewFakeTimesource(timestamp), address) require.NoError(t, err) result := myValidator(context.Background(), "", &pubsub.Message{ Message: &pubsub_pb.Message{ @@ -75,7 +77,7 @@ func TestMsgHash(t *testing.T) { // Exceed 5m window in both directions now5m1sInPast := timestamp.Add(-5 * time.Minute).Add(-1 * time.Second) - myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInPast), &prvKey.PublicKey) + myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInPast), address) require.NoError(t, err) result = myValidator(context.Background(), "", &pubsub.Message{ Message: &pubsub_pb.Message{ @@ -85,7 +87,7 @@ func TestMsgHash(t *testing.T) { require.False(t, result) now5m1sInFuture := timestamp.Add(5 * time.Minute).Add(1 * time.Second) - myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInFuture), &prvKey.PublicKey) + myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInFuture), address) require.NoError(t, err) result = myValidator(context.Background(), "", &pubsub.Message{ Message: &pubsub_pb.Message{