mirror of https://github.com/status-im/go-waku.git
feat: private get messages
This commit is contained in:
parent
1b0e03c07b
commit
12593bb503
|
@ -53,7 +53,7 @@ Examples of usage of go-waku as a library can be found in the examples folder. T
|
|||
|[13/WAKU2-STORE](https://rfc.vac.dev/spec/13)|✔|
|
||||
|[14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14)|✔|
|
||||
|[15/WAKU2-BRIDGE](https://rfc.vac.dev/spec/15)|⛔|
|
||||
|[16/WAKU2-RPC](https://rfc.vac.dev/spec/16)|🚧|
|
||||
|[16/WAKU2-RPC](https://rfc.vac.dev/spec/16)|✔|
|
||||
|[17/WAKU2-RLNRELAY](https://rfc.vac.dev/spec/17)||
|
||||
|[18/WAKU2-SWAP](https://rfc.vac.dev/spec/18)|🚧|
|
||||
|[21/WAKU2-FTSTORE](https://rfc.vac.dev/spec/21)|✔|
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/status-im/go-waku/waku/v2/node"
|
||||
|
@ -14,6 +15,12 @@ import (
|
|||
|
||||
type PrivateService struct {
|
||||
node *node.WakuNode
|
||||
|
||||
symmetricMessages map[string][]*pb.WakuMessage
|
||||
symmetricMessagesMutex sync.RWMutex
|
||||
|
||||
asymmetricMessages map[string][]*pb.WakuMessage
|
||||
asymmetricMessagesMutex sync.RWMutex
|
||||
}
|
||||
|
||||
type SymmetricKeyReply struct {
|
||||
|
@ -49,7 +56,9 @@ type AsymmetricMessagesArgs struct {
|
|||
|
||||
func NewPrivateService(node *node.WakuNode) *PrivateService {
|
||||
return &PrivateService{
|
||||
node: node,
|
||||
node: node,
|
||||
symmetricMessages: make(map[string][]*pb.WakuMessage),
|
||||
asymmetricMessages: make(map[string][]*pb.WakuMessage),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,10 +100,23 @@ func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *Symmetr
|
|||
if err != nil {
|
||||
reply.Error = err.Error()
|
||||
reply.Success = false
|
||||
} else {
|
||||
reply.Success = true
|
||||
return nil
|
||||
}
|
||||
err = p.node.Publish(req.Context(), &args.Message)
|
||||
if err != nil {
|
||||
reply.Error = err.Error()
|
||||
reply.Success = false
|
||||
return nil
|
||||
}
|
||||
|
||||
p.symmetricMessagesMutex.Lock()
|
||||
defer p.symmetricMessagesMutex.Unlock()
|
||||
if _, ok := p.symmetricMessages[args.Topic]; !ok {
|
||||
p.symmetricMessages[args.Topic] = make([]*pb.WakuMessage, 0)
|
||||
}
|
||||
p.symmetricMessages[args.Topic] = append(p.symmetricMessages[args.Topic], &args.Message)
|
||||
|
||||
reply.Success = true
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -116,17 +138,48 @@ func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *Asymme
|
|||
if err != nil {
|
||||
reply.Error = err.Error()
|
||||
reply.Success = false
|
||||
} else {
|
||||
reply.Success = true
|
||||
return nil
|
||||
}
|
||||
err = p.node.Publish(req.Context(), &args.Message)
|
||||
if err != nil {
|
||||
reply.Error = err.Error()
|
||||
reply.Success = false
|
||||
return nil
|
||||
}
|
||||
|
||||
p.asymmetricMessagesMutex.Lock()
|
||||
defer p.asymmetricMessagesMutex.Unlock()
|
||||
if _, ok := p.asymmetricMessages[args.Topic]; !ok {
|
||||
p.asymmetricMessages[args.Topic] = make([]*pb.WakuMessage, 0)
|
||||
}
|
||||
p.asymmetricMessages[args.Topic] = append(p.asymmetricMessages[args.Topic], &args.Message)
|
||||
|
||||
reply.Success = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *SymmetricMessagesArgs, reply *MessagesReply) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
p.symmetricMessagesMutex.Lock()
|
||||
defer p.symmetricMessagesMutex.Unlock()
|
||||
|
||||
if _, ok := p.symmetricMessages[args.Topic]; !ok {
|
||||
return fmt.Errorf("topic %s not subscribed", args.Topic)
|
||||
}
|
||||
|
||||
reply.Messages = p.symmetricMessages[args.Topic]
|
||||
p.symmetricMessages[args.Topic] = make([]*pb.WakuMessage, 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PrivateService) GetV1AsymmetricMessages(req *http.Request, args *AsymmetricMessagesArgs, reply *MessagesReply) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
p.asymmetricMessagesMutex.Lock()
|
||||
defer p.asymmetricMessagesMutex.Unlock()
|
||||
|
||||
if _, ok := p.asymmetricMessages[args.Topic]; !ok {
|
||||
return fmt.Errorf("topic %s not subscribed", args.Topic)
|
||||
}
|
||||
|
||||
reply.Messages = p.asymmetricMessages[args.Topic]
|
||||
p.asymmetricMessages[args.Topic] = make([]*pb.WakuMessage, 0)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -10,12 +10,12 @@ import (
|
|||
)
|
||||
|
||||
func makePrivateService(t *testing.T) *PrivateService {
|
||||
n, err := node.New(context.Background(), node.WithWakuRelay())
|
||||
n, err := node.New(context.Background(), node.WithWakuRelayAndMinPeers(0))
|
||||
require.NoError(t, err)
|
||||
err = n.Start()
|
||||
require.NoError(t, err)
|
||||
|
||||
return &PrivateService{n}
|
||||
return NewPrivateService(n)
|
||||
}
|
||||
|
||||
func TestGetV1SymmetricKey(t *testing.T) {
|
||||
|
@ -82,3 +82,60 @@ func TestPostV1AsymmetricMessage(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.True(t, reply.Success)
|
||||
}
|
||||
|
||||
func TestGetV1SymmetricMessages(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)
|
||||
|
||||
var getReply MessagesReply
|
||||
err = d.GetV1SymmetricMessages(
|
||||
makeRequest(t),
|
||||
&SymmetricMessagesArgs{Topic: "test", SymKey: "abc"},
|
||||
&getReply,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, getReply.Messages, 1)
|
||||
}
|
||||
|
||||
func TestGetV1AsymmetricMessages(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)
|
||||
|
||||
var getReply MessagesReply
|
||||
err = d.GetV1AsymmetricMessages(
|
||||
makeRequest(t),
|
||||
&AsymmetricMessagesArgs{
|
||||
Topic: "test",
|
||||
PrivateKey: "045ded6a56c88173e87a88c55b96956964b1bd3351b5fcb70950a4902fbc1bc0ceabb0ac846c3a4b8f2f6024c0e19f0a7f6a4865035187de5463f34012304fc7c5",
|
||||
},
|
||||
&getReply,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, getReply.Messages, 1)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue