From c91d666f358c7dec50568bc225f132b0ad95c34b Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 18 Nov 2021 14:21:36 +0100 Subject: [PATCH] feat: Add private service Plus add missing methods in other services as not implemented yet --- waku/v2/rpc/filter.go | 5 +++ waku/v2/rpc/private.go | 70 +++++++++++++++++++++++++++++++++++++ waku/v2/rpc/private_test.go | 47 +++++++++++++++++++++++++ waku/v2/rpc/relay.go | 5 +++ waku/v2/rpc/rpc_type.go | 3 ++ waku/v2/rpc/waku_rpc.go | 5 +++ 6 files changed, 135 insertions(+) create mode 100644 waku/v2/rpc/private.go create mode 100644 waku/v2/rpc/private_test.go diff --git a/waku/v2/rpc/filter.go b/waku/v2/rpc/filter.go index b8a3e870..1470aaeb 100644 --- a/waku/v2/rpc/filter.go +++ b/waku/v2/rpc/filter.go @@ -1,6 +1,7 @@ package rpc import ( + "fmt" "net/http" "github.com/status-im/go-waku/waku/v2/node" @@ -59,3 +60,7 @@ func (f *FilterService) DeleteV1Subscription(req *http.Request, args *FilterCont reply.Success = true return nil } + +func (f *FilterService) GetV1Messages(req *http.Request, args *Empty, reply *Empty) error { + return fmt.Errorf("not implemented") +} diff --git a/waku/v2/rpc/private.go b/waku/v2/rpc/private.go new file mode 100644 index 00000000..8189b672 --- /dev/null +++ b/waku/v2/rpc/private.go @@ -0,0 +1,70 @@ +package rpc + +import ( + "crypto/ecdsa" + "crypto/rand" + "encoding/hex" + "fmt" + "net/http" + + "github.com/ethereum/go-ethereum/crypto" + "github.com/status-im/go-waku/waku/v2/node" +) + +type PrivateService struct { + node *node.WakuNode +} + +type SymmetricKeyReply struct { + Key string `json:"key"` +} + +type KeyPairReply struct { + PrivateKey string `json:"privateKey"` + PulicKey string `json:"publicKey"` +} + +func (p *PrivateService) GetV1SymmetricKey(req *http.Request, args *Empty, reply *SymmetricKeyReply) error { + key := [32]byte{} + _, err := rand.Read(key[:]) + if err != nil { + return err + } + reply.Key = hex.EncodeToString(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) + reply.PrivateKey = hex.EncodeToString(privateKeyBytes[:]) + reply.PulicKey = hex.EncodeToString(publicKeyBytes[:]) + return nil +} + +func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *FilterContentFilterArgs, reply *SuccessReply) error { + return fmt.Errorf("not implemented") +} + +func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *FilterContentFilterArgs, reply *SuccessReply) error { + return fmt.Errorf("not implemented") +} + +func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *FilterContentFilterArgs, reply *SuccessReply) error { + return fmt.Errorf("not implemented") +} + +func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *FilterContentFilterArgs, reply *SuccessReply) error { + return fmt.Errorf("not implemented") +} diff --git a/waku/v2/rpc/private_test.go b/waku/v2/rpc/private_test.go new file mode 100644 index 00000000..912ca9e1 --- /dev/null +++ b/waku/v2/rpc/private_test.go @@ -0,0 +1,47 @@ +package rpc + +import ( + "context" + "testing" + + "github.com/status-im/go-waku/waku/v2/node" + "github.com/stretchr/testify/require" +) + +func makePrivateService(t *testing.T) *PrivateService { + n, err := node.New(context.Background(), node.WithWakuRelay()) + require.NoError(t, err) + err = n.Start() + require.NoError(t, err) + + return &PrivateService{n} +} + +func TestGetV1SymmetricKey(t *testing.T) { + d := makePrivateService(t) + defer d.node.Stop() + + var reply SymmetricKeyReply + err := d.GetV1SymmetricKey( + makeRequest(t), + &Empty{}, + &reply, + ) + require.NoError(t, err) + require.NotEmpty(t, reply.Key) +} + +func TestGetV1AsymmetricKeypair(t *testing.T) { + d := makePrivateService(t) + defer d.node.Stop() + + var reply KeyPairReply + err := d.GetV1AsymmetricKeypair( + makeRequest(t), + &Empty{}, + &reply, + ) + require.NoError(t, err) + require.NotEmpty(t, reply.PrivateKey) + require.NotEmpty(t, reply.PulicKey) +} diff --git a/waku/v2/rpc/relay.go b/waku/v2/rpc/relay.go index fd1a52da..c4123a46 100644 --- a/waku/v2/rpc/relay.go +++ b/waku/v2/rpc/relay.go @@ -1,6 +1,7 @@ package rpc import ( + "fmt" "net/http" "github.com/status-im/go-waku/waku/v2/node" @@ -62,3 +63,7 @@ func (r *RelayService) DeleteV1Subscription(req *http.Request, args *TopicsArgs, reply.Success = true return nil } + +func (r *RelayService) GetV1Messages(req *http.Request, args *Empty, reply *Empty) error { + return fmt.Errorf("not implemented") +} diff --git a/waku/v2/rpc/rpc_type.go b/waku/v2/rpc/rpc_type.go index f2d8c10a..82576d18 100644 --- a/waku/v2/rpc/rpc_type.go +++ b/waku/v2/rpc/rpc_type.go @@ -4,3 +4,6 @@ type SuccessReply struct { Success bool `json:"success,omitempty"` Error string `json:"error,omitempty"` } + +type Empty struct { +} diff --git a/waku/v2/rpc/waku_rpc.go b/waku/v2/rpc/waku_rpc.go index debfbc07..ec2f54fd 100644 --- a/waku/v2/rpc/waku_rpc.go +++ b/waku/v2/rpc/waku_rpc.go @@ -48,6 +48,11 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc { log.Error(err) } + err = s.RegisterService(&PrivateService{node}, "Private") + if err != nil { + log.Error(err) + } + mux := http.NewServeMux() mux.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) { t := time.Now()