2023-02-17 03:35:22 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2023-11-07 19:48:43 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-11-10 18:31:36 +00:00
|
|
|
"github.com/waku-org/go-waku/cmd/waku/server"
|
2023-02-17 03:35:22 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2023-11-07 19:48:43 +00:00
|
|
|
rlnpb "github.com/waku-org/go-waku/waku/v2/protocol/rln/pb"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
2023-02-17 03:35:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RateLimitProof struct {
|
2023-02-17 15:00:46 +00:00
|
|
|
Proof Base64URLByte `json:"proof,omitempty"`
|
|
|
|
MerkleRoot Base64URLByte `json:"merkle_root,omitempty"`
|
|
|
|
Epoch Base64URLByte `json:"epoch,omitempty"`
|
|
|
|
ShareX Base64URLByte `json:"share_x,omitempty"`
|
|
|
|
ShareY Base64URLByte `json:"share_y,omitempty"`
|
|
|
|
Nullifier Base64URLByte `json:"nullifier,omitempty"`
|
|
|
|
RlnIdentifier Base64URLByte `json:"rln_identifier,omitempty"`
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RPCWakuMessage struct {
|
2023-11-10 18:31:36 +00:00
|
|
|
Payload server.Base64URLByte `json:"payload,omitempty"`
|
|
|
|
ContentTopic string `json:"contentTopic,omitempty"`
|
|
|
|
Version uint32 `json:"version"`
|
|
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
|
|
RateLimitProof *RateLimitProof `json:"rateLimitProof,omitempty"`
|
|
|
|
Ephemeral bool `json:"ephemeral,omitempty"`
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
func ProtoToRPC(input *pb.WakuMessage) (*RPCWakuMessage, error) {
|
2023-02-17 03:35:22 +00:00
|
|
|
if input == nil {
|
2023-11-07 19:48:43 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := input.Validate(); err != nil {
|
|
|
|
return nil, err
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpcWakuMsg := &RPCWakuMessage{
|
|
|
|
Payload: input.Payload,
|
|
|
|
ContentTopic: input.ContentTopic,
|
2023-11-07 19:48:43 +00:00
|
|
|
Version: input.GetVersion(),
|
|
|
|
Timestamp: input.GetTimestamp(),
|
|
|
|
Ephemeral: input.GetEphemeral(),
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if input.RateLimitProof != nil {
|
2023-11-07 19:48:43 +00:00
|
|
|
rateLimitProof := &rlnpb.RateLimitProof{}
|
|
|
|
err := proto.Unmarshal(input.RateLimitProof, rateLimitProof)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-17 03:35:22 +00:00
|
|
|
rpcWakuMsg.RateLimitProof = &RateLimitProof{
|
2023-11-07 19:48:43 +00:00
|
|
|
Proof: rateLimitProof.Proof,
|
|
|
|
MerkleRoot: rateLimitProof.MerkleRoot,
|
|
|
|
Epoch: rateLimitProof.Epoch,
|
|
|
|
ShareX: rateLimitProof.ShareX,
|
|
|
|
ShareY: rateLimitProof.ShareY,
|
|
|
|
Nullifier: rateLimitProof.Nullifier,
|
|
|
|
RlnIdentifier: rateLimitProof.RlnIdentifier,
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
return rpcWakuMsg, nil
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
func (r *RPCWakuMessage) toProto() (*pb.WakuMessage, error) {
|
2023-02-17 03:35:22 +00:00
|
|
|
if r == nil {
|
2023-11-07 19:48:43 +00:00
|
|
|
return nil, errors.New("wakumessage is missing")
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &pb.WakuMessage{
|
|
|
|
Payload: r.Payload,
|
|
|
|
ContentTopic: r.ContentTopic,
|
2023-11-07 19:48:43 +00:00
|
|
|
Version: proto.Uint32(r.Version),
|
|
|
|
Timestamp: proto.Int64(r.Timestamp),
|
|
|
|
Ephemeral: proto.Bool(r.Ephemeral),
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.RateLimitProof != nil {
|
2023-11-07 19:48:43 +00:00
|
|
|
rateLimitProof := &rlnpb.RateLimitProof{
|
2023-02-17 03:35:22 +00:00
|
|
|
Proof: r.RateLimitProof.Proof,
|
|
|
|
MerkleRoot: r.RateLimitProof.MerkleRoot,
|
|
|
|
Epoch: r.RateLimitProof.Epoch,
|
|
|
|
ShareX: r.RateLimitProof.ShareX,
|
|
|
|
ShareY: r.RateLimitProof.ShareY,
|
|
|
|
Nullifier: r.RateLimitProof.Nullifier,
|
|
|
|
RlnIdentifier: r.RateLimitProof.RlnIdentifier,
|
|
|
|
}
|
2023-11-07 19:48:43 +00:00
|
|
|
|
|
|
|
b, err := proto.Marshal(rateLimitProof)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.RateLimitProof = b
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
return msg, nil
|
2023-02-17 03:35:22 +00:00
|
|
|
}
|