2023-05-02 15:10:45 +00:00
|
|
|
package relay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-05-03 15:59:47 +00:00
|
|
|
"encoding/hex"
|
2023-05-02 15:10:45 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2023-05-04 14:04:54 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/timesource"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2023-05-02 15:10:45 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMsgHash(t *testing.T) {
|
2023-05-03 15:59:47 +00:00
|
|
|
privKeyB, _ := hex.DecodeString("5526a8990317c9b7b58d07843d270f9cd1d9aaee129294c1c478abf7261dd9e6")
|
2023-05-02 15:10:45 +00:00
|
|
|
prvKey, _ := crypto.ToECDSA(privKeyB)
|
|
|
|
|
2023-05-03 15:59:47 +00:00
|
|
|
payload, _ := hex.DecodeString("1A12E077D0E89F9CAC11FBBB6A676C86120B5AD3E248B1F180E98F15EE43D2DFCF62F00C92737B2FF6F59B3ABA02773314B991C41DC19ADB0AD8C17C8E26757B")
|
|
|
|
contentTopic := "content-topic"
|
|
|
|
pubsubTopic := "pubsub-topic"
|
2023-05-02 15:10:45 +00:00
|
|
|
|
|
|
|
msg := &pb.WakuMessage{
|
|
|
|
Payload: payload,
|
|
|
|
ContentTopic: contentTopic,
|
2023-05-04 14:04:54 +00:00
|
|
|
Timestamp: utils.GetUnixEpoch(timesource.NewDefaultClock()),
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SignMessage(prvKey, pubsubTopic, msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-03 15:59:47 +00:00
|
|
|
expectedSignature, _ := hex.DecodeString("B139487797A242291E0DD3F689777E559FB749D565D55FF202C18E24F21312A555043437B4F808BB0D21D542D703873DC712D76A3BAF1C5C8FF754210D894AD4")
|
|
|
|
require.True(t, bytes.Equal(expectedSignature, msg.Meta))
|
|
|
|
|
2023-05-02 15:10:45 +00:00
|
|
|
msgData, _ := proto.Marshal(msg)
|
|
|
|
|
2023-05-03 15:59:47 +00:00
|
|
|
expectedMessageHash, _ := hex.DecodeString("0914369D6D0C13783A8E86409FE42C68D8E8296456B9A9468C845006BCE5B9B2")
|
2023-05-02 15:10:45 +00:00
|
|
|
messageHash := MsgHash(pubsubTopic, msg)
|
|
|
|
require.True(t, bytes.Equal(expectedMessageHash, messageHash))
|
|
|
|
|
|
|
|
myValidator := validatorFnBuilder(pubsubTopic, &prvKey.PublicKey)
|
|
|
|
|
|
|
|
result := myValidator(context.Background(), "", &pubsub.Message{
|
|
|
|
Message: &pubsub_pb.Message{
|
|
|
|
Data: msgData,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.True(t, result)
|
|
|
|
}
|