2021-11-18 13:21:36 +00:00
|
|
|
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"
|
2021-11-18 13:21:36 +00:00
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-11-18 13:21:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
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"
|
|
|
|
"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/utils"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-18 13:21:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PrivateService struct {
|
|
|
|
node *node.WakuNode
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2021-12-09 09:07:08 +00:00
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
messages map[string][]*pb.WakuMessage
|
2022-10-20 13:19:23 +00:00
|
|
|
cacheCapacity int
|
2022-06-13 18:30:35 +00:00
|
|
|
messagesMutex sync.RWMutex
|
2021-12-09 09:07:08 +00:00
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
runner *runnerService
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
type SymmetricKeyReply string
|
2021-11-18 13:21:36 +00:00
|
|
|
|
|
|
|
type KeyPairReply struct {
|
|
|
|
PrivateKey string `json:"privateKey"`
|
2022-06-13 18:30:35 +00:00
|
|
|
PublicKey string `json:"publicKey"`
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 08:52:27 +00:00
|
|
|
type SymmetricMessageArgs struct {
|
2023-02-13 12:52:53 +00:00
|
|
|
Topic string `json:"topic"`
|
2023-02-17 03:35:22 +00:00
|
|
|
Message *RPCWakuMessage `json:"message"`
|
2023-02-13 12:52:53 +00:00
|
|
|
SymKey string `json:"symkey"`
|
2021-12-08 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AsymmetricMessageArgs struct {
|
2023-02-13 12:52:53 +00:00
|
|
|
Topic string `json:"topic"`
|
2023-02-17 03:35:22 +00:00
|
|
|
Message *RPCWakuMessage `json:"message"`
|
2023-02-13 12:52:53 +00:00
|
|
|
PublicKey string `json:"publicKey"`
|
2021-12-08 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SymmetricMessagesArgs struct {
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
SymKey string `json:"symkey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AsymmetricMessagesArgs struct {
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
PrivateKey string `json:"privateKey"`
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:19:23 +00:00
|
|
|
func NewPrivateService(node *node.WakuNode, cacheCapacity int, log *zap.Logger) *PrivateService {
|
2022-06-13 18:30:35 +00:00
|
|
|
p := &PrivateService{
|
2022-10-20 13:19:23 +00:00
|
|
|
node: node,
|
|
|
|
cacheCapacity: cacheCapacity,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:19:23 +00:00
|
|
|
// Keep a specific max number of messages per topic
|
|
|
|
if len(p.messages[envelope.PubsubTopic()]) >= p.cacheCapacity {
|
|
|
|
p.messages[envelope.PubsubTopic()] = p.messages[envelope.PubsubTopic()][1:]
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
p.messages[envelope.PubsubTopic()] = append(p.messages[envelope.PubsubTopic()], envelope.Message())
|
2021-12-08 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:21:36 +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[:]))
|
2021-11-18 13:21:36 +00:00
|
|
|
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[:])
|
2021-11-18 13:21:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:14 +00:00
|
|
|
keyInfo := new(payload.KeyInfo)
|
|
|
|
keyInfo.Kind = payload.Symmetric
|
2022-06-13 18:30:35 +00:00
|
|
|
keyInfo.SymKey = symKeyBytes
|
|
|
|
|
2023-02-13 12:52:53 +00:00
|
|
|
msg := args.Message
|
2022-06-13 18:30:35 +00:00
|
|
|
msg.Version = 1
|
2021-12-08 08:52:27 +00:00
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
protoMsg := msg.toProto()
|
|
|
|
|
|
|
|
err = payload.EncodeWakuMessage(protoMsg, 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
|
|
|
|
}
|
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
_, err = p.node.Relay().PublishToTopic(req.Context(), protoMsg, 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
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *AsymmetricMessageArgs, reply *bool) error {
|
2022-12-16 00:47:14 +00:00
|
|
|
keyInfo := new(payload.KeyInfo)
|
|
|
|
keyInfo.Kind = payload.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
|
|
|
|
|
2023-02-13 12:52:53 +00:00
|
|
|
msg := args.Message
|
2022-06-13 18:30:35 +00:00
|
|
|
msg.Version = 1
|
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
protoMsg := msg.toProto()
|
|
|
|
|
|
|
|
err = payload.EncodeWakuMessage(protoMsg, 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
|
|
|
}
|
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
_, err = p.node.Relay().PublishToTopic(req.Context(), protoMsg, topic)
|
2022-06-13 18:30:35 +00:00
|
|
|
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-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-12-16 00:47:14 +00:00
|
|
|
err := payload.DecodeWakuMessage(msg, &payload.KeyInfo{
|
|
|
|
Kind: payload.Symmetric,
|
2022-06-13 18:30:35 +00:00
|
|
|
SymKey: symKeyBytes,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
decodedMessages = append(decodedMessages, msg)
|
|
|
|
}
|
2021-12-09 09:07:08 +00:00
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
for i := range decodedMessages {
|
|
|
|
*reply = append(*reply, ProtoToRPC(decodedMessages[i]))
|
|
|
|
}
|
2021-12-09 09:07:08 +00:00
|
|
|
|
|
|
|
return nil
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-12-16 00:47:14 +00:00
|
|
|
err := payload.DecodeWakuMessage(msg, &payload.KeyInfo{
|
|
|
|
Kind: payload.Asymmetric,
|
2022-06-13 18:30:35 +00:00
|
|
|
PrivKey: privKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
decodedMessages = append(decodedMessages, msg)
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:04:33 +00:00
|
|
|
for _, msg := range decodedMessages {
|
|
|
|
*reply = append(*reply, ProtoToRPC(msg))
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
2021-12-09 09:07:08 +00:00
|
|
|
|
|
|
|
return nil
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
2022-06-13 18:30:35 +00:00
|
|
|
|
|
|
|
func (p *PrivateService) Start() {
|
|
|
|
p.runner.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrivateService) Stop() {
|
|
|
|
p.runner.Stop()
|
|
|
|
}
|