go-waku/waku/v2/rpc/private.go

259 lines
6.2 KiB
Go
Raw Normal View History

package rpc
import (
"crypto/ecdsa"
"crypto/rand"
"fmt"
"net/http"
2022-06-13 18:30:35 +00:00
"strings"
2021-12-09 09:07:08 +00:00
"sync"
2022-06-13 18:30:35 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/go-waku/waku/v2/node"
2022-06-13 18:30:35 +00:00
"github.com/status-im/go-waku/waku/v2/protocol"
2021-12-08 08:52:27 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/pb"
2022-06-13 18:30:35 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/relay"
2022-09-14 19:19:04 +00:00
"github.com/status-im/go-waku/waku/v2/utils"
"go.uber.org/zap"
)
type PrivateService struct {
node *node.WakuNode
log *zap.Logger
2021-12-09 09:07:08 +00:00
2022-06-13 18:30:35 +00:00
messages map[string][]*pb.WakuMessage
messagesMutex sync.RWMutex
2021-12-09 09:07:08 +00:00
2022-06-13 18:30:35 +00:00
runner *runnerService
}
2022-06-13 18:30:35 +00:00
type SymmetricKeyReply string
type KeyPairReply struct {
PrivateKey string `json:"privateKey"`
2022-06-13 18:30:35 +00:00
PublicKey string `json:"publicKey"`
}
2021-12-08 08:52:27 +00:00
type SymmetricMessageArgs struct {
Topic string `json:"topic"`
2022-06-13 18:30:35 +00:00
Message RPCWakuMessage `json:"message"`
2021-12-08 08:52:27 +00:00
SymKey string `json:"symkey"`
}
type AsymmetricMessageArgs struct {
Topic string `json:"topic"`
2022-06-13 18:30:35 +00:00
Message RPCWakuMessage `json:"message"`
2021-12-08 08:52:27 +00:00
PublicKey string `json:"publicKey"`
}
type SymmetricMessagesArgs struct {
Topic string `json:"topic"`
SymKey string `json:"symkey"`
}
type AsymmetricMessagesArgs struct {
Topic string `json:"topic"`
PrivateKey string `json:"privateKey"`
}
func NewPrivateService(node *node.WakuNode, log *zap.Logger) *PrivateService {
2022-06-13 18:30:35 +00:00
p := &PrivateService{
node: node,
messages: make(map[string][]*pb.WakuMessage),
log: log.Named("private"),
2021-12-08 08:52:27 +00:00
}
2022-06-13 18:30:35 +00:00
p.runner = newRunnerService(node.Broadcaster(), p.addEnvelope)
return p
}
func (p *PrivateService) addEnvelope(envelope *protocol.Envelope) {
p.messagesMutex.Lock()
defer p.messagesMutex.Unlock()
if _, ok := p.messages[envelope.PubsubTopic()]; !ok {
p.messages[envelope.PubsubTopic()] = make([]*pb.WakuMessage, 0)
}
p.messages[envelope.PubsubTopic()] = append(p.messages[envelope.PubsubTopic()], envelope.Message())
2021-12-08 08:52:27 +00:00
}
func (p *PrivateService) GetV1SymmetricKey(req *http.Request, args *Empty, reply *SymmetricKeyReply) error {
key := [32]byte{}
_, err := rand.Read(key[:])
if err != nil {
return err
}
2022-06-13 18:30:35 +00:00
*reply = SymmetricKeyReply(hexutil.Encode(key[:]))
return nil
}
func (p *PrivateService) GetV1AsymmetricKeypair(req *http.Request, args *Empty, reply *KeyPairReply) error {
privateKey, err := crypto.GenerateKey()
if err != nil {
return err
}
privateKeyBytes := crypto.FromECDSA(privateKey)
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
2022-06-13 18:30:35 +00:00
reply.PrivateKey = hexutil.Encode(privateKeyBytes[:])
reply.PublicKey = hexutil.Encode(publicKeyBytes[:])
return nil
}
2021-12-08 08:52:27 +00:00
func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *SymmetricMessageArgs, reply *SuccessReply) error {
2022-09-14 19:19:04 +00:00
symKeyBytes, err := utils.DecodeHexString(args.SymKey)
2022-06-13 18:30:35 +00:00
if err != nil {
return fmt.Errorf("invalid symmetric key: %w", err)
}
2021-12-08 08:52:27 +00:00
keyInfo := new(node.KeyInfo)
keyInfo.Kind = node.Symmetric
2022-06-13 18:30:35 +00:00
keyInfo.SymKey = symKeyBytes
msg := args.Message.toProto()
msg.Version = 1
2021-12-08 08:52:27 +00:00
2022-06-13 18:30:35 +00:00
err = node.EncodeWakuMessage(msg, keyInfo)
2021-12-08 08:52:27 +00:00
if err != nil {
2022-06-14 15:36:34 +00:00
return err
2021-12-09 09:07:08 +00:00
}
2022-06-13 18:30:35 +00:00
topic := args.Topic
if topic == "" {
topic = relay.DefaultWakuTopic
}
_, err = p.node.Relay().PublishToTopic(req.Context(), msg, topic)
2021-12-09 09:07:08 +00:00
if err != nil {
2022-06-14 15:36:34 +00:00
return err
2021-12-08 08:52:27 +00:00
}
2022-06-14 15:36:34 +00:00
*reply = true
2021-12-08 08:52:27 +00:00
return nil
}
2022-06-13 18:30:35 +00:00
func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *AsymmetricMessageArgs, reply *bool) error {
2021-12-08 08:52:27 +00:00
keyInfo := new(node.KeyInfo)
keyInfo.Kind = node.Asymmetric
2022-06-13 18:30:35 +00:00
2022-09-14 19:19:04 +00:00
pubKeyBytes, err := utils.DecodeHexString(args.PublicKey)
2021-12-08 08:52:27 +00:00
if err != nil {
return fmt.Errorf("public key cannot be decoded: %v", err)
}
pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return fmt.Errorf("public key cannot be unmarshalled: %v", err)
}
2022-06-13 18:30:35 +00:00
2021-12-08 08:52:27 +00:00
keyInfo.PubKey = *pubKey
2022-06-13 18:30:35 +00:00
msg := args.Message.toProto()
msg.Version = 1
err = node.EncodeWakuMessage(msg, keyInfo)
2021-12-08 08:52:27 +00:00
if err != nil {
2022-06-13 18:30:35 +00:00
return err
2021-12-09 09:07:08 +00:00
}
2022-06-13 18:30:35 +00:00
topic := args.Topic
if topic == "" {
topic = relay.DefaultWakuTopic
2021-12-09 09:07:08 +00:00
}
2022-06-13 18:30:35 +00:00
_, err = p.node.Relay().PublishToTopic(req.Context(), msg, topic)
if err != nil {
return err
2021-12-08 08:52:27 +00:00
}
2022-06-13 18:30:35 +00:00
*reply = true
2021-12-08 08:52:27 +00:00
return nil
}
2021-12-08 08:52:27 +00:00
func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *SymmetricMessagesArgs, reply *MessagesReply) error {
2022-06-13 18:30:35 +00:00
p.messagesMutex.Lock()
defer p.messagesMutex.Unlock()
if _, ok := p.messages[args.Topic]; !ok {
p.messages[args.Topic] = make([]*pb.WakuMessage, 0)
}
2022-09-14 19:19:04 +00:00
symKeyBytes, err := utils.DecodeHexString(args.SymKey)
2022-06-13 18:30:35 +00:00
if err != nil {
return fmt.Errorf("invalid symmetric key: %w", err)
}
messages := make([]*pb.WakuMessage, len(p.messages[args.Topic]))
copy(messages, p.messages[args.Topic])
p.messages[args.Topic] = make([]*pb.WakuMessage, 0)
var decodedMessages []*pb.WakuMessage
for _, msg := range messages {
err := node.DecodeWakuMessage(msg, &node.KeyInfo{
Kind: node.Symmetric,
SymKey: symKeyBytes,
})
if err != nil {
continue
}
decodedMessages = append(decodedMessages, msg)
}
2021-12-09 09:07:08 +00:00
2022-06-13 18:30:35 +00:00
for i := range decodedMessages {
*reply = append(*reply, ProtoWakuMessageToRPCWakuMessage(decodedMessages[i]))
2021-12-09 09:07:08 +00:00
}
return nil
}
2021-12-08 08:52:27 +00:00
func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *AsymmetricMessagesArgs, reply *MessagesReply) error {
2022-06-13 18:30:35 +00:00
p.messagesMutex.Lock()
defer p.messagesMutex.Unlock()
2021-12-09 09:07:08 +00:00
2022-06-13 18:30:35 +00:00
if _, ok := p.messages[args.Topic]; !ok {
p.messages[args.Topic] = make([]*pb.WakuMessage, 0)
}
messages := make([]*pb.WakuMessage, len(p.messages[args.Topic]))
copy(messages, p.messages[args.Topic])
p.messages[args.Topic] = make([]*pb.WakuMessage, 0)
privKey, err := crypto.HexToECDSA(strings.TrimPrefix(args.PrivateKey, "0x"))
if err != nil {
return fmt.Errorf("invalid asymmetric key: %w", err)
}
var decodedMessages []*pb.WakuMessage
for _, msg := range messages {
err := node.DecodeWakuMessage(msg, &node.KeyInfo{
Kind: node.Asymmetric,
PrivKey: privKey,
})
if err != nil {
continue
}
decodedMessages = append(decodedMessages, msg)
}
for i := range decodedMessages {
*reply = append(*reply, ProtoWakuMessageToRPCWakuMessage(decodedMessages[i]))
2021-12-09 09:07:08 +00:00
}
return nil
}
2022-06-13 18:30:35 +00:00
func (p *PrivateService) Start() {
p.runner.Start()
}
func (p *PrivateService) Stop() {
p.runner.Stop()
}