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