go-waku/mobile/encoding.go

97 lines
1.9 KiB
Go
Raw Normal View History

package gowaku
2022-04-03 00:22:42 +00:00
import (
"encoding/json"
"github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/status-im/go-waku/waku/v2/protocol/pb"
2022-09-14 19:19:04 +00:00
"github.com/status-im/go-waku/waku/v2/utils"
2022-04-03 00:22:42 +00:00
)
func wakuMessage(messageJSON string) (pb.WakuMessage, error) {
var msg pb.WakuMessage
err := json.Unmarshal([]byte(messageJSON), &msg)
msg.Version = 0
return msg, err
}
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
}
payload := node.Payload{
Data: msg.Payload,
Key: &node.KeyInfo{
Kind: node.Symmetric,
2022-04-03 00:22:42 +00:00
},
}
keyBytes, err := utils.DecodeHexString(symmetricKey)
2022-04-03 00:22:42 +00:00
if err != nil {
return msg, err
}
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
}
func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optionalSigningKey string) (pb.WakuMessage, error) {
msg, err := wakuMessage(messageJSON)
if err != nil {
return msg, err
}
payload := node.Payload{
Data: msg.Payload,
Key: &node.KeyInfo{
Kind: node.Asymmetric,
},
}
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
}