Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encode waku message payloads in base64 #448

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions waku/v2/rpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ func (f *FilterService) GetV1Messages(req *http.Request, args *ContentTopicArgs,
return fmt.Errorf("topic %s not subscribed", args.ContentTopic)
}

for i := range f.messages[args.ContentTopic] {
*reply = append(*reply, ProtoWakuMessageToRPCWakuMessage(f.messages[args.ContentTopic][i]))
}
*reply = f.messages[args.ContentTopic]

f.messages[args.ContentTopic] = make([]*pb.WakuMessage, 0)
return nil
Expand Down
24 changes: 10 additions & 14 deletions waku/v2/rpc/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type KeyPairReply struct {
}

type SymmetricMessageArgs struct {
Topic string `json:"topic"`
Message RPCWakuMessage `json:"message"`
SymKey string `json:"symkey"`
Topic string `json:"topic"`
Message *pb.WakuMessage `json:"message"`
SymKey string `json:"symkey"`
}

type AsymmetricMessageArgs struct {
Topic string `json:"topic"`
Message RPCWakuMessage `json:"message"`
PublicKey string `json:"publicKey"`
Topic string `json:"topic"`
Message *pb.WakuMessage `json:"message"`
PublicKey string `json:"publicKey"`
}

type SymmetricMessagesArgs struct {
Expand Down Expand Up @@ -125,7 +125,7 @@ func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *Symmetr
keyInfo.Kind = payload.Symmetric
keyInfo.SymKey = symKeyBytes

msg := args.Message.toProto()
msg := args.Message
msg.Version = 1

err = payload.EncodeWakuMessage(msg, keyInfo)
Expand Down Expand Up @@ -163,7 +163,7 @@ func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *Asymme

keyInfo.PubKey = *pubKey

msg := args.Message.toProto()
msg := args.Message
msg.Version = 1

err = payload.EncodeWakuMessage(msg, keyInfo)
Expand Down Expand Up @@ -214,9 +214,7 @@ func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *Symmetr
decodedMessages = append(decodedMessages, msg)
}

for i := range decodedMessages {
*reply = append(*reply, ProtoWakuMessageToRPCWakuMessage(decodedMessages[i]))
}
*reply = decodedMessages

return nil
}
Expand Down Expand Up @@ -250,9 +248,7 @@ func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *Asymme
decodedMessages = append(decodedMessages, msg)
}

for i := range decodedMessages {
*reply = append(*reply, ProtoWakuMessageToRPCWakuMessage(decodedMessages[i]))
}
*reply = decodedMessages

return nil
}
Expand Down
9 changes: 5 additions & 4 deletions waku/v2/rpc/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func TestPostV1SymmetricMessage(t *testing.T) {
makeRequest(t),
&SymmetricMessageArgs{
Topic: "test",
Message: RPCWakuMessage{Payload: []byte("test")},
Message: &pb.WakuMessage{Payload: []byte("test")},
SymKey: "0x1122334455667788991011223344556677889910112233445566778899101122",
},
&reply,
Expand All @@ -77,7 +78,7 @@ func TestPostV1AsymmetricMessage(t *testing.T) {
makeRequest(t),
&AsymmetricMessageArgs{
Topic: "test",
Message: RPCWakuMessage{Payload: []byte("test")},
Message: &pb.WakuMessage{Payload: []byte("test")},
PublicKey: "0x045ded6a56c88173e87a88c55b96956964b1bd3351b5fcb70950a4902fbc1bc0ceabb0ac846c3a4b8f2f6024c0e19f0a7f6a4865035187de5463f34012304fc7c5",
},
&reply,
Expand All @@ -100,7 +101,7 @@ func TestGetV1SymmetricMessages(t *testing.T) {
makeRequest(t),
&SymmetricMessageArgs{
Topic: "test",
Message: RPCWakuMessage{Payload: []byte("test")},
Message: &pb.WakuMessage{Payload: []byte("test")},
SymKey: "0x1122334455667788991011223344556677889910112233445566778899101122",
},
&reply,
Expand Down Expand Up @@ -140,7 +141,7 @@ func TestGetV1AsymmetricMessages(t *testing.T) {
makeRequest(t),
&AsymmetricMessageArgs{
Topic: "test",
Message: RPCWakuMessage{Payload: []byte("test")},
Message: &pb.WakuMessage{Payload: []byte("test")},
PublicKey: hexutil.Encode(crypto.FromECDSAPub(&prvKey.PublicKey)),
},
&reply,
Expand Down
14 changes: 5 additions & 9 deletions waku/v2/rpc/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type RelayService struct {
}

type RelayMessageArgs struct {
Topic string `json:"topic,omitempty"`
Message RPCWakuRelayMessage `json:"message,omitempty"`
Topic string `json:"topic,omitempty"`
Message *pb.WakuMessage `json:"message,omitempty"`
}

type TopicsArgs struct {
Expand Down Expand Up @@ -85,12 +85,10 @@ func (r *RelayService) Stop() {
func (r *RelayService) PostV1Message(req *http.Request, args *RelayMessageArgs, reply *SuccessReply) error {
var err error

msg := args.Message.toProto()

if args.Topic == "" {
_, err = r.node.Relay().Publish(req.Context(), msg)
_, err = r.node.Relay().Publish(req.Context(), args.Message)
} else {
_, err = r.node.Relay().PublishToTopic(req.Context(), msg, args.Topic)
_, err = r.node.Relay().PublishToTopic(req.Context(), args.Message, args.Topic)
}
if err != nil {
r.log.Error("publishing message", zap.Error(err))
Expand Down Expand Up @@ -151,9 +149,7 @@ func (r *RelayService) GetV1Messages(req *http.Request, args *TopicArgs, reply *
return fmt.Errorf("topic %s not subscribed", args.Topic)
}

for i := range r.messages[args.Topic] {
*reply = append(*reply, ProtoWakuMessageToRPCWakuRelayMessage(r.messages[args.Topic][i]))
}
*reply = r.messages[args.Topic]

r.messages[args.Topic] = make([]*pb.WakuMessage, 0)

Expand Down
14 changes: 12 additions & 2 deletions waku/v2/rpc/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
)

Expand All @@ -27,9 +28,18 @@ func TestPostV1Message(t *testing.T) {

d := makeRelayService(t)

msg := &pb.WakuMessage{
Payload: []byte{1, 2, 3},
ContentTopic: "abc",
Version: 0,
Timestamp: utils.GetUnixEpoch(),
}

err := d.PostV1Message(
makeRequest(t),
&RelayMessageArgs{},
&RelayMessageArgs{
Message: msg,
},
&reply,
)
require.NoError(t, err)
Expand Down Expand Up @@ -100,7 +110,7 @@ func TestRelayGetV1Messages(t *testing.T) {
makeRequest(t),
&RelayMessageArgs{
Topic: "test",
Message: RPCWakuRelayMessage{
Message: &pb.WakuMessage{
Payload: []byte("test"),
},
},
Expand Down
6 changes: 4 additions & 2 deletions waku/v2/rpc/rpc_type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package rpc

import "github.com/waku-org/go-waku/waku/v2/protocol/pb"

type SuccessReply = bool

type Empty struct {
}

type MessagesReply = []*RPCWakuMessage
type MessagesReply = []*pb.WakuMessage

type RelayMessagesReply = []*RPCWakuRelayMessage
type RelayMessagesReply = []*pb.WakuMessage
7 changes: 2 additions & 5 deletions waku/v2/rpc/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type StoreMessagesArgs struct {
}

type StoreMessagesReply struct {
Messages []RPCWakuMessage `json:"messages,omitempty"`
Messages []*pb.WakuMessage `json:"messages,omitempty"`
PagingInfo StorePagingOptions `json:"pagingInfo,omitempty"`
Error string `json:"error,omitempty"`
}
Expand Down Expand Up @@ -61,10 +61,7 @@ func (s *StoreService) GetV1Messages(req *http.Request, args *StoreMessagesArgs,
return nil
}

reply.Messages = make([]RPCWakuMessage, len(res.Messages))
for i := range res.Messages {
reply.Messages[i] = *ProtoWakuMessageToRPCWakuMessage(res.Messages[i])
}
reply.Messages = res.Messages

reply.PagingInfo = StorePagingOptions{
PageSize: args.PagingOptions.PageSize,
Expand Down
Loading