mirror of https://github.com/status-im/go-waku.git
feat: private rpc encrypt message
This commit is contained in:
parent
c45e8a3c31
commit
d0d3271433
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/status-im/go-waku/waku/v2/node"
|
"github.com/status-im/go-waku/waku/v2/node"
|
||||||
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PrivateService struct {
|
type PrivateService struct {
|
||||||
|
@ -24,6 +25,34 @@ type KeyPairReply struct {
|
||||||
PulicKey string `json:"publicKey"`
|
PulicKey string `json:"publicKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SymmetricMessageArgs struct {
|
||||||
|
Topic string `json:"topic"`
|
||||||
|
Message pb.WakuMessage `json:"message"`
|
||||||
|
SymKey string `json:"symkey"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AsymmetricMessageArgs struct {
|
||||||
|
Topic string `json:"topic"`
|
||||||
|
Message pb.WakuMessage `json:"message"`
|
||||||
|
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) *PrivateService {
|
||||||
|
return &PrivateService{
|
||||||
|
node: node,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PrivateService) GetV1SymmetricKey(req *http.Request, args *Empty, reply *SymmetricKeyReply) error {
|
func (p *PrivateService) GetV1SymmetricKey(req *http.Request, args *Empty, reply *SymmetricKeyReply) error {
|
||||||
key := [32]byte{}
|
key := [32]byte{}
|
||||||
_, err := rand.Read(key[:])
|
_, err := rand.Read(key[:])
|
||||||
|
@ -53,18 +82,51 @@ func (p *PrivateService) GetV1AsymmetricKeypair(req *http.Request, args *Empty,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *Empty, reply *SuccessReply) error {
|
func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *SymmetricMessageArgs, reply *SuccessReply) error {
|
||||||
|
keyInfo := new(node.KeyInfo)
|
||||||
|
keyInfo.Kind = node.Symmetric
|
||||||
|
keyInfo.SymKey = []byte(args.SymKey)
|
||||||
|
|
||||||
|
err := node.EncodeWakuMessage(&args.Message, keyInfo)
|
||||||
|
if err != nil {
|
||||||
|
reply.Error = err.Error()
|
||||||
|
reply.Success = false
|
||||||
|
} else {
|
||||||
|
reply.Success = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *AsymmetricMessageArgs, reply *SuccessReply) error {
|
||||||
|
keyInfo := new(node.KeyInfo)
|
||||||
|
keyInfo.Kind = node.Asymmetric
|
||||||
|
pubKeyBytes, err := hex.DecodeString(args.PublicKey)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
keyInfo.PubKey = *pubKey
|
||||||
|
|
||||||
|
err = node.EncodeWakuMessage(&args.Message, keyInfo)
|
||||||
|
if err != nil {
|
||||||
|
reply.Error = err.Error()
|
||||||
|
reply.Success = false
|
||||||
|
} else {
|
||||||
|
reply.Success = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *SymmetricMessagesArgs, reply *MessagesReply) error {
|
||||||
return fmt.Errorf("not implemented")
|
return fmt.Errorf("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *Empty, reply *SuccessReply) error {
|
func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *AsymmetricMessagesArgs, reply *MessagesReply) error {
|
||||||
return fmt.Errorf("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *Empty, reply *SuccessReply) error {
|
|
||||||
return fmt.Errorf("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *Empty, reply *SuccessReply) error {
|
|
||||||
return fmt.Errorf("not implemented")
|
return fmt.Errorf("not implemented")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/status-im/go-waku/waku/v2/node"
|
"github.com/status-im/go-waku/waku/v2/node"
|
||||||
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,7 +32,7 @@ func TestGetV1SymmetricKey(t *testing.T) {
|
||||||
require.NotEmpty(t, reply.Key)
|
require.NotEmpty(t, reply.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetV1AsymmetricKeypair(t *testing.T) {
|
func TestGetV1AsymmetricKey(t *testing.T) {
|
||||||
d := makePrivateService(t)
|
d := makePrivateService(t)
|
||||||
defer d.node.Stop()
|
defer d.node.Stop()
|
||||||
|
|
||||||
|
@ -42,6 +43,42 @@ func TestGetV1AsymmetricKeypair(t *testing.T) {
|
||||||
&reply,
|
&reply,
|
||||||
)
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEmpty(t, reply.PrivateKey)
|
|
||||||
require.NotEmpty(t, reply.PulicKey)
|
require.NotEmpty(t, reply.PulicKey)
|
||||||
|
require.NotEmpty(t, reply.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostV1SymmetricMessage(t *testing.T) {
|
||||||
|
d := makePrivateService(t)
|
||||||
|
defer d.node.Stop()
|
||||||
|
|
||||||
|
var reply SuccessReply
|
||||||
|
err := d.PostV1SymmetricMessage(
|
||||||
|
makeRequest(t),
|
||||||
|
&SymmetricMessageArgs{
|
||||||
|
Topic: "test",
|
||||||
|
Message: pb.WakuMessage{Payload: []byte("test")},
|
||||||
|
SymKey: "abc",
|
||||||
|
},
|
||||||
|
&reply,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, reply.Success)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostV1AsymmetricMessage(t *testing.T) {
|
||||||
|
d := makePrivateService(t)
|
||||||
|
defer d.node.Stop()
|
||||||
|
|
||||||
|
var reply SuccessReply
|
||||||
|
err := d.PostV1AsymmetricMessage(
|
||||||
|
makeRequest(t),
|
||||||
|
&AsymmetricMessageArgs{
|
||||||
|
Topic: "test",
|
||||||
|
Message: pb.WakuMessage{Payload: []byte("test")},
|
||||||
|
PublicKey: "045ded6a56c88173e87a88c55b96956964b1bd3351b5fcb70950a4902fbc1bc0ceabb0ac846c3a4b8f2f6024c0e19f0a7f6a4865035187de5463f34012304fc7c5",
|
||||||
|
},
|
||||||
|
&reply,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, reply.Success)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ type WakuRpc struct {
|
||||||
|
|
||||||
relayService *RelayService
|
relayService *RelayService
|
||||||
filterService *FilterService
|
filterService *FilterService
|
||||||
|
privateService *PrivateService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
||||||
|
@ -53,7 +54,8 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.RegisterService(&PrivateService{node}, "Private")
|
privateService := NewPrivateService(node)
|
||||||
|
err = s.RegisterService(privateService, "Private")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -82,6 +84,7 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc {
|
||||||
server: server,
|
server: server,
|
||||||
relayService: relayService,
|
relayService: relayService,
|
||||||
filterService: filterService,
|
filterService: filterService,
|
||||||
|
privateService: privateService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue