mirror of https://github.com/status-im/go-waku.git
refactor: move payload handling to its own package
This commit is contained in:
parent
17eb12e62c
commit
5740a6d571
|
@ -22,6 +22,7 @@ import (
|
|||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"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/pb"
|
||||
"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
|
||||
}
|
||||
|
||||
func extractPubKeyAndSignature(payload *node.DecodedPayload) (pubkey string, signature string) {
|
||||
func extractPubKeyAndSignature(payload *payload.DecodedPayload) (pubkey string, signature string) {
|
||||
pkBytes := crypto.FromECDSAPub(payload.PubKey)
|
||||
if len(pkBytes) != 0 {
|
||||
pubkey = hexutil.Encode(pkBytes)
|
||||
|
@ -376,8 +377,8 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string {
|
|||
return MakeJSONResponse(errors.New("unsupported wakumessage version"))
|
||||
}
|
||||
|
||||
keyInfo := &node.KeyInfo{
|
||||
Kind: node.Symmetric,
|
||||
keyInfo := &payload.KeyInfo{
|
||||
Kind: payload.Symmetric,
|
||||
}
|
||||
|
||||
keyInfo.SymKey, err = utils.DecodeHexString(symmetricKey)
|
||||
|
@ -385,7 +386,7 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string {
|
|||
return MakeJSONResponse(err)
|
||||
}
|
||||
|
||||
payload, err := node.DecodePayload(&msg, keyInfo)
|
||||
payload, err := payload.DecodePayload(&msg, keyInfo)
|
||||
if err != nil {
|
||||
return MakeJSONResponse(err)
|
||||
}
|
||||
|
@ -420,8 +421,8 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string {
|
|||
return MakeJSONResponse(errors.New("unsupported wakumessage version"))
|
||||
}
|
||||
|
||||
keyInfo := &node.KeyInfo{
|
||||
Kind: node.Asymmetric,
|
||||
keyInfo := &payload.KeyInfo{
|
||||
Kind: payload.Asymmetric,
|
||||
}
|
||||
|
||||
keyBytes, err := utils.DecodeHexString(privateKey)
|
||||
|
@ -434,7 +435,7 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string {
|
|||
return MakeJSONResponse(err)
|
||||
}
|
||||
|
||||
payload, err := node.DecodePayload(&msg, keyInfo)
|
||||
payload, err := payload.DecodePayload(&msg, keyInfo)
|
||||
if err != nil {
|
||||
return MakeJSONResponse(err)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
|
||||
"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/utils"
|
||||
)
|
||||
|
@ -22,10 +22,10 @@ func wakuMessageSymmetricEncoding(messageJSON string, symmetricKey string, optio
|
|||
return msg, err
|
||||
}
|
||||
|
||||
payload := node.Payload{
|
||||
payload := payload.Payload{
|
||||
Data: msg.Payload,
|
||||
Key: &node.KeyInfo{
|
||||
Kind: node.Symmetric,
|
||||
Key: &payload.KeyInfo{
|
||||
Kind: payload.Symmetric,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -60,10 +60,10 @@ func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optiona
|
|||
return msg, err
|
||||
}
|
||||
|
||||
payload := node.Payload{
|
||||
payload := payload.Payload{
|
||||
Data: msg.Payload,
|
||||
Key: &node.KeyInfo{
|
||||
Kind: node.Asymmetric,
|
||||
Key: &payload.KeyInfo{
|
||||
Kind: payload.Asymmetric,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
|
||||
"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/utils"
|
||||
)
|
||||
|
@ -42,7 +43,7 @@ func TestBasicSendingReceiving(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
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.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 timestamp int64 = utils.GetUnixEpoch()
|
||||
|
||||
p := new(node.Payload)
|
||||
p := new(payload.Payload)
|
||||
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)
|
||||
if err != nil {
|
||||
|
|
|
@ -15,11 +15,20 @@ import (
|
|||
"github.com/waku-org/go-waku/waku/persistence"
|
||||
"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/pb"
|
||||
"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/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) {
|
||||
hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package node
|
||||
package payload
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
|
@ -14,7 +14,6 @@ import (
|
|||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -450,26 +449,3 @@ func bytesToUintLittleEndian(b []byte) (res uint64) {
|
|||
}
|
||||
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
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package node
|
||||
package payload
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
|
@ -1,4 +1,4 @@
|
|||
package node
|
||||
package payload
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"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"
|
||||
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
||||
"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)
|
||||
}
|
||||
|
||||
keyInfo := new(node.KeyInfo)
|
||||
keyInfo.Kind = node.Symmetric
|
||||
keyInfo := new(payload.KeyInfo)
|
||||
keyInfo.Kind = payload.Symmetric
|
||||
keyInfo.SymKey = symKeyBytes
|
||||
|
||||
msg := args.Message.toProto()
|
||||
msg.Version = 1
|
||||
|
||||
err = node.EncodeWakuMessage(msg, keyInfo)
|
||||
err = payload.EncodeWakuMessage(msg, keyInfo)
|
||||
if err != nil {
|
||||
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 {
|
||||
keyInfo := new(node.KeyInfo)
|
||||
keyInfo.Kind = node.Asymmetric
|
||||
keyInfo := new(payload.KeyInfo)
|
||||
keyInfo.Kind = payload.Asymmetric
|
||||
|
||||
pubKeyBytes, err := utils.DecodeHexString(args.PublicKey)
|
||||
if err != nil {
|
||||
|
@ -165,7 +166,7 @@ func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *Asymme
|
|||
msg := args.Message.toProto()
|
||||
msg.Version = 1
|
||||
|
||||
err = node.EncodeWakuMessage(msg, keyInfo)
|
||||
err = payload.EncodeWakuMessage(msg, keyInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -203,8 +204,8 @@ func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *Symmetr
|
|||
|
||||
var decodedMessages []*pb.WakuMessage
|
||||
for _, msg := range messages {
|
||||
err := node.DecodeWakuMessage(msg, &node.KeyInfo{
|
||||
Kind: node.Symmetric,
|
||||
err := payload.DecodeWakuMessage(msg, &payload.KeyInfo{
|
||||
Kind: payload.Symmetric,
|
||||
SymKey: symKeyBytes,
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -239,8 +240,8 @@ func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *Asymme
|
|||
|
||||
var decodedMessages []*pb.WakuMessage
|
||||
for _, msg := range messages {
|
||||
err := node.DecodeWakuMessage(msg, &node.KeyInfo{
|
||||
Kind: node.Asymmetric,
|
||||
err := payload.DecodeWakuMessage(msg, &payload.KeyInfo{
|
||||
Kind: payload.Asymmetric,
|
||||
PrivKey: privKey,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue