2023-05-02 15:10:45 +00:00
|
|
|
package relay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-03 15:59:47 +00:00
|
|
|
"encoding/hex"
|
2023-05-02 15:10:45 +00:00
|
|
|
"testing"
|
2023-05-04 15:56:56 +00:00
|
|
|
"time"
|
2023-05-02 15:10:45 +00:00
|
|
|
|
|
|
|
"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"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2023-05-04 15:56:56 +00:00
|
|
|
type FakeTimesource struct {
|
|
|
|
now time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeTimesource(now time.Time) FakeTimesource {
|
|
|
|
return FakeTimesource{now}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FakeTimesource) Start(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FakeTimesource) Stop() {}
|
|
|
|
|
|
|
|
func (f FakeTimesource) Now() time.Time {
|
|
|
|
return f.now
|
|
|
|
}
|
|
|
|
|
|
|
|
type Timesource interface {
|
|
|
|
Now() time.Time
|
|
|
|
}
|
|
|
|
|
2023-05-02 15:10:45 +00:00
|
|
|
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"
|
2023-05-04 15:56:56 +00:00
|
|
|
ephemeral := true
|
|
|
|
timestamp := time.Unix(0, 1683208172339052800)
|
2023-05-02 15:10:45 +00:00
|
|
|
|
|
|
|
msg := &pb.WakuMessage{
|
|
|
|
Payload: payload,
|
|
|
|
ContentTopic: contentTopic,
|
2023-05-04 15:56:56 +00:00
|
|
|
Timestamp: timestamp.UnixNano(),
|
|
|
|
Ephemeral: ephemeral,
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 14:48:55 +00:00
|
|
|
err := SignMessage(prvKey, msg)
|
2023-05-02 15:10:45 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-09 14:48:55 +00:00
|
|
|
// expectedSignature, _ := hex.DecodeString("127FA211B2514F0E974A055392946DC1A14052182A6ABEFB8A6CD7C51DA1BF2E40595D28EF1A9488797C297EED3AAC45430005FB3A7F037BDD9FC4BD99F59E63")
|
|
|
|
// require.True(t, bytes.Equal(expectedSignature, msg.Meta))
|
2023-05-03 15:59:47 +00:00
|
|
|
|
2023-05-02 15:10:45 +00:00
|
|
|
msgData, _ := proto.Marshal(msg)
|
|
|
|
|
2023-05-09 14:48:55 +00:00
|
|
|
//expectedMessageHash, _ := hex.DecodeString("662F8C20A335F170BD60ABC1F02AD66F0C6A6EE285DA2A53C95259E7937C0AE9")
|
|
|
|
//messageHash := MsgHash(pubsubTopic, msg)
|
|
|
|
//require.True(t, bytes.Equal(expectedMessageHash, messageHash))
|
2023-05-02 15:10:45 +00:00
|
|
|
|
2023-05-22 21:03:40 +00:00
|
|
|
myValidator, err := validatorFnBuilder(NewFakeTimesource(timestamp), &prvKey.PublicKey)
|
2023-05-09 14:48:55 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-02 15:10:45 +00:00
|
|
|
result := myValidator(context.Background(), "", &pubsub.Message{
|
|
|
|
Message: &pubsub_pb.Message{
|
|
|
|
Data: msgData,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.True(t, result)
|
2023-05-04 15:56:56 +00:00
|
|
|
|
|
|
|
// Exceed 5m window in both directions
|
2023-05-09 14:48:55 +00:00
|
|
|
now5m1sInPast := timestamp.Add(-5 * time.Minute).Add(-1 * time.Second)
|
2023-05-22 21:03:40 +00:00
|
|
|
myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInPast), &prvKey.PublicKey)
|
2023-05-09 14:48:55 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-04 15:56:56 +00:00
|
|
|
result = myValidator(context.Background(), "", &pubsub.Message{
|
|
|
|
Message: &pubsub_pb.Message{
|
|
|
|
Data: msgData,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.False(t, result)
|
|
|
|
|
2023-05-09 14:48:55 +00:00
|
|
|
now5m1sInFuture := timestamp.Add(5 * time.Minute).Add(1 * time.Second)
|
2023-05-22 21:03:40 +00:00
|
|
|
myValidator, err = validatorFnBuilder(NewFakeTimesource(now5m1sInFuture), &prvKey.PublicKey)
|
2023-05-09 14:48:55 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-04 15:56:56 +00:00
|
|
|
result = myValidator(context.Background(), "", &pubsub.Message{
|
|
|
|
Message: &pubsub_pb.Message{
|
|
|
|
Data: msgData,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.False(t, result)
|
2023-05-02 15:10:45 +00:00
|
|
|
}
|