feat: Add private service

Plus add missing methods in other services as not implemented yet
This commit is contained in:
Anthony Laibe 2021-11-18 14:21:36 +01:00
parent 38e5fdbe3e
commit c91d666f35
6 changed files with 135 additions and 0 deletions

View File

@ -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")
}

70
waku/v2/rpc/private.go Normal file
View File

@ -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")
}

View File

@ -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)
}

View File

@ -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")
}

View File

@ -4,3 +4,6 @@ type SuccessReply struct {
Success bool `json:"success,omitempty"`
Error string `json:"error,omitempty"`
}
type Empty struct {
}

View File

@ -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()