2022-04-12 08:12:14 -04:00
|
|
|
package gowaku
|
2022-04-02 20:22:42 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2022-12-15 20:47:14 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/payload"
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-04-02 20:22:42 -04:00
|
|
|
)
|
|
|
|
|
2023-02-06 18:16:20 -04:00
|
|
|
func wakuMessage(messageJSON string) (*pb.WakuMessage, error) {
|
|
|
|
var msg *pb.WakuMessage
|
2022-04-02 20:22:42 -04:00
|
|
|
err := json.Unmarshal([]byte(messageJSON), &msg)
|
|
|
|
msg.Version = 0
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
2023-02-06 18:16:20 -04:00
|
|
|
func wakuMessageSymmetricEncoding(messageJSON string, symmetricKey string, optionalSigningKey string) (*pb.WakuMessage, error) {
|
2022-04-02 20:22:42 -04:00
|
|
|
msg, err := wakuMessage(messageJSON)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:47:14 -04:00
|
|
|
payload := payload.Payload{
|
2022-04-02 20:22:42 -04:00
|
|
|
Data: msg.Payload,
|
2022-12-15 20:47:14 -04:00
|
|
|
Key: &payload.KeyInfo{
|
|
|
|
Kind: payload.Symmetric,
|
2022-04-02 20:22:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-14 09:19:17 -05:00
|
|
|
keyBytes, err := utils.DecodeHexString(symmetricKey)
|
2022-04-02 20:22:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
2022-10-14 09:19:17 -05:00
|
|
|
payload.Key.SymKey = keyBytes
|
2022-04-02 20:22:42 -04:00
|
|
|
|
|
|
|
if optionalSigningKey != "" {
|
2022-09-14 15:19:04 -04:00
|
|
|
signingKeyBytes, err := utils.DecodeHexString(optionalSigningKey)
|
2022-04-02 20:22:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Key.PrivKey, err = crypto.ToECDSA(signingKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Version = 1
|
|
|
|
msg.Payload, err = payload.Encode(1)
|
|
|
|
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
2023-02-06 18:16:20 -04:00
|
|
|
func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optionalSigningKey string) (*pb.WakuMessage, error) {
|
2022-04-02 20:22:42 -04:00
|
|
|
msg, err := wakuMessage(messageJSON)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:47:14 -04:00
|
|
|
payload := payload.Payload{
|
2022-04-02 20:22:42 -04:00
|
|
|
Data: msg.Payload,
|
2022-12-15 20:47:14 -04:00
|
|
|
Key: &payload.KeyInfo{
|
|
|
|
Kind: payload.Asymmetric,
|
2022-04-02 20:22:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-14 15:19:04 -04:00
|
|
|
keyBytes, err := utils.DecodeHexString(publicKey)
|
2022-04-02 20:22:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Key.PubKey, err = unmarshalPubkey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if optionalSigningKey != "" {
|
2022-09-14 15:19:04 -04:00
|
|
|
signingKeyBytes, err := utils.DecodeHexString(optionalSigningKey)
|
2022-04-02 20:22:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.Key.PrivKey, err = crypto.ToECDSA(signingKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return msg, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Version = 1
|
|
|
|
msg.Payload, err = payload.Encode(1)
|
|
|
|
|
|
|
|
return msg, err
|
|
|
|
}
|