refactor: move payload handling to its own package

This commit is contained in:
Richard Ramos 2022-12-15 20:47:14 -04:00 committed by RichΛrd
parent 17eb12e62c
commit 5740a6d571
8 changed files with 42 additions and 54 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol" "github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-waku/waku/v2/utils"
@ -350,7 +351,7 @@ func unmarshalPubkey(pub []byte) (ecdsa.PublicKey, error) {
return ecdsa.PublicKey{Curve: secp256k1.S256(), X: x, Y: y}, nil return ecdsa.PublicKey{Curve: secp256k1.S256(), X: x, Y: y}, nil
} }
func extractPubKeyAndSignature(payload *node.DecodedPayload) (pubkey string, signature string) { func extractPubKeyAndSignature(payload *payload.DecodedPayload) (pubkey string, signature string) {
pkBytes := crypto.FromECDSAPub(payload.PubKey) pkBytes := crypto.FromECDSAPub(payload.PubKey)
if len(pkBytes) != 0 { if len(pkBytes) != 0 {
pubkey = hexutil.Encode(pkBytes) pubkey = hexutil.Encode(pkBytes)
@ -376,8 +377,8 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string {
return MakeJSONResponse(errors.New("unsupported wakumessage version")) return MakeJSONResponse(errors.New("unsupported wakumessage version"))
} }
keyInfo := &node.KeyInfo{ keyInfo := &payload.KeyInfo{
Kind: node.Symmetric, Kind: payload.Symmetric,
} }
keyInfo.SymKey, err = utils.DecodeHexString(symmetricKey) keyInfo.SymKey, err = utils.DecodeHexString(symmetricKey)
@ -385,7 +386,7 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string {
return MakeJSONResponse(err) return MakeJSONResponse(err)
} }
payload, err := node.DecodePayload(&msg, keyInfo) payload, err := payload.DecodePayload(&msg, keyInfo)
if err != nil { if err != nil {
return MakeJSONResponse(err) return MakeJSONResponse(err)
} }
@ -420,8 +421,8 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string {
return MakeJSONResponse(errors.New("unsupported wakumessage version")) return MakeJSONResponse(errors.New("unsupported wakumessage version"))
} }
keyInfo := &node.KeyInfo{ keyInfo := &payload.KeyInfo{
Kind: node.Asymmetric, Kind: payload.Asymmetric,
} }
keyBytes, err := utils.DecodeHexString(privateKey) keyBytes, err := utils.DecodeHexString(privateKey)
@ -434,7 +435,7 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string {
return MakeJSONResponse(err) return MakeJSONResponse(err)
} }
payload, err := node.DecodePayload(&msg, keyInfo) payload, err := payload.DecodePayload(&msg, keyInfo)
if err != nil { if err != nil {
return MakeJSONResponse(err) return MakeJSONResponse(err)
} }

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-waku/waku/v2/utils"
) )
@ -22,10 +22,10 @@ func wakuMessageSymmetricEncoding(messageJSON string, symmetricKey string, optio
return msg, err return msg, err
} }
payload := node.Payload{ payload := payload.Payload{
Data: msg.Payload, Data: msg.Payload,
Key: &node.KeyInfo{ Key: &payload.KeyInfo{
Kind: node.Symmetric, Kind: payload.Symmetric,
}, },
} }
@ -60,10 +60,10 @@ func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optiona
return msg, err return msg, err
} }
payload := node.Payload{ payload := payload.Payload{
Data: msg.Payload, Data: msg.Payload,
Key: &node.KeyInfo{ Key: &payload.KeyInfo{
Kind: node.Asymmetric, Kind: payload.Asymmetric,
}, },
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-waku/waku/v2/utils"
) )
@ -42,7 +43,7 @@ func TestBasicSendingReceiving(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
value := <-sub.C value := <-sub.C
payload, err := node.DecodePayload(value.Message(), &node.KeyInfo{Kind: node.None}) payload, err := payload.DecodePayload(value.Message(), &payload.KeyInfo{Kind: payload.None})
require.NoError(t, err) require.NoError(t, err)
require.Contains(t, string(payload.Data), "test") require.Contains(t, string(payload.Data), "test")
@ -53,9 +54,9 @@ func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) erro
var version uint32 = 0 var version uint32 = 0
var timestamp int64 = utils.GetUnixEpoch() var timestamp int64 = utils.GetUnixEpoch()
p := new(node.Payload) p := new(payload.Payload)
p.Data = []byte(wakuNode.ID() + ": " + msgContent) p.Data = []byte(wakuNode.ID() + ": " + msgContent)
p.Key = &node.KeyInfo{Kind: node.None} p.Key = &payload.KeyInfo{Kind: payload.None}
payload, err := p.Encode(version) payload, err := p.Encode(version)
if err != nil { if err != nil {

View File

@ -15,11 +15,20 @@ import (
"github.com/waku-org/go-waku/waku/persistence" "github.com/waku-org/go-waku/waku/persistence"
"github.com/waku-org/go-waku/waku/persistence/sqlite" "github.com/waku-org/go-waku/waku/persistence/sqlite"
"github.com/waku-org/go-waku/waku/v2/protocol/filter" "github.com/waku-org/go-waku/waku/v2/protocol/filter"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/relay" "github.com/waku-org/go-waku/waku/v2/protocol/relay"
"github.com/waku-org/go-waku/waku/v2/protocol/store" "github.com/waku-org/go-waku/waku/v2/protocol/store"
"github.com/waku-org/go-waku/waku/v2/utils" "github.com/waku-org/go-waku/waku/v2/utils"
) )
func createTestMsg(version uint32) *pb.WakuMessage {
message := new(pb.WakuMessage)
message.Payload = []byte{0, 1, 2}
message.Version = version
message.Timestamp = 123456
return message
}
func TestWakuNode2(t *testing.T) { func TestWakuNode2(t *testing.T) {
hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0") hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")

View File

@ -1,4 +1,4 @@
package node package payload
import ( import (
"crypto/aes" "crypto/aes"
@ -14,7 +14,6 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/waku-org/go-waku/waku/v2/noise"
"github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/pb"
) )
@ -450,26 +449,3 @@ func bytesToUintLittleEndian(b []byte) (res uint64) {
} }
return res return res
} }
// Decodes a WakuMessage to a PayloadV2
// Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
func DecodePayloadV2(message *pb.WakuMessage) (*noise.PayloadV2, error) {
if message.Version != 2 {
return nil, errors.New("wrong message version while decoding payload")
}
return noise.DeserializePayloadV2(message.Payload)
}
// Encodes a PayloadV2 to a WakuMessage
// Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
func EncodePayloadV2(payload2 *noise.PayloadV2) (*pb.WakuMessage, error) {
serializedPayload2, err := payload2.Serialize()
if err != nil {
return nil, err
}
return &pb.WakuMessage{
Payload: serializedPayload2,
Version: 2,
}, nil
}

View File

@ -1,4 +1,4 @@
package node package payload
import ( import (
"crypto/aes" "crypto/aes"

View File

@ -1,4 +1,4 @@
package node package payload
import ( import (
"fmt" "fmt"

View File

@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/waku-org/go-waku/waku/v2/node" "github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol" "github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb" "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/relay" "github.com/waku-org/go-waku/waku/v2/protocol/relay"
@ -120,14 +121,14 @@ func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *Symmetr
return fmt.Errorf("invalid symmetric key: %w", err) return fmt.Errorf("invalid symmetric key: %w", err)
} }
keyInfo := new(node.KeyInfo) keyInfo := new(payload.KeyInfo)
keyInfo.Kind = node.Symmetric keyInfo.Kind = payload.Symmetric
keyInfo.SymKey = symKeyBytes keyInfo.SymKey = symKeyBytes
msg := args.Message.toProto() msg := args.Message.toProto()
msg.Version = 1 msg.Version = 1
err = node.EncodeWakuMessage(msg, keyInfo) err = payload.EncodeWakuMessage(msg, keyInfo)
if err != nil { if err != nil {
return err return err
} }
@ -147,8 +148,8 @@ func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *Symmetr
} }
func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *AsymmetricMessageArgs, reply *bool) error { func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *AsymmetricMessageArgs, reply *bool) error {
keyInfo := new(node.KeyInfo) keyInfo := new(payload.KeyInfo)
keyInfo.Kind = node.Asymmetric keyInfo.Kind = payload.Asymmetric
pubKeyBytes, err := utils.DecodeHexString(args.PublicKey) pubKeyBytes, err := utils.DecodeHexString(args.PublicKey)
if err != nil { if err != nil {
@ -165,7 +166,7 @@ func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *Asymme
msg := args.Message.toProto() msg := args.Message.toProto()
msg.Version = 1 msg.Version = 1
err = node.EncodeWakuMessage(msg, keyInfo) err = payload.EncodeWakuMessage(msg, keyInfo)
if err != nil { if err != nil {
return err return err
} }
@ -203,8 +204,8 @@ func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *Symmetr
var decodedMessages []*pb.WakuMessage var decodedMessages []*pb.WakuMessage
for _, msg := range messages { for _, msg := range messages {
err := node.DecodeWakuMessage(msg, &node.KeyInfo{ err := payload.DecodeWakuMessage(msg, &payload.KeyInfo{
Kind: node.Symmetric, Kind: payload.Symmetric,
SymKey: symKeyBytes, SymKey: symKeyBytes,
}) })
if err != nil { if err != nil {
@ -239,8 +240,8 @@ func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *Asymme
var decodedMessages []*pb.WakuMessage var decodedMessages []*pb.WakuMessage
for _, msg := range messages { for _, msg := range messages {
err := node.DecodeWakuMessage(msg, &node.KeyInfo{ err := payload.DecodeWakuMessage(msg, &payload.KeyInfo{
Kind: node.Asymmetric, Kind: payload.Asymmetric,
PrivKey: privKey, PrivKey: privKey,
}) })
if err != nil { if err != nil {