mirror of https://github.com/status-im/go-waku.git
refactor: use an address instead of public key for the node setup
This commit is contained in:
parent
aed730c634
commit
38a9fc4b19
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{
|
||||
|
|
Loading…
Reference in New Issue