2022-06-13 18:30:35 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HexBytes is marshalled to a hex string
|
|
|
|
type HexBytes []byte
|
|
|
|
|
|
|
|
// ByteArray is marshalled to a uint8 array
|
|
|
|
type ByteArray []byte
|
|
|
|
|
2022-07-05 21:28:34 +00:00
|
|
|
type RateLimitProof struct {
|
|
|
|
Proof HexBytes `json:"proof,omitempty"`
|
|
|
|
MerkleRoot HexBytes `json:"merkle_root,omitempty"`
|
|
|
|
Epoch HexBytes `json:"epoch,omitempty"`
|
|
|
|
ShareX HexBytes `json:"share_x,omitempty"`
|
|
|
|
ShareY HexBytes `json:"share_y,omitempty"`
|
|
|
|
Nullifier HexBytes `json:"nullifier,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
type RPCWakuMessage struct {
|
2022-07-05 21:28:34 +00:00
|
|
|
Payload ByteArray `json:"payload,omitempty"`
|
|
|
|
ContentTopic string `json:"contentTopic,omitempty"`
|
|
|
|
Version uint32 `json:"version"`
|
|
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
|
|
RateLimitProof *RateLimitProof `json:"rateLimitProof,omitempty"`
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RPCWakuRelayMessage struct {
|
2022-07-05 21:28:34 +00:00
|
|
|
Payload HexBytes `json:"payload,omitempty"`
|
|
|
|
ContentTopic string `json:"contentTopic,omitempty"`
|
|
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
|
|
RateLimitProof *RateLimitProof `json:"rateLimitProof,omitempty"`
|
|
|
|
Version uint32 `json:"version"`
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ProtoWakuMessageToRPCWakuMessage(input *pb.WakuMessage) *RPCWakuMessage {
|
|
|
|
if input == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:28:34 +00:00
|
|
|
rpcWakuMsg := &RPCWakuMessage{
|
2022-06-13 18:30:35 +00:00
|
|
|
Payload: input.Payload,
|
|
|
|
ContentTopic: input.ContentTopic,
|
|
|
|
Version: input.Version,
|
|
|
|
Timestamp: input.Timestamp,
|
|
|
|
}
|
2022-07-05 21:28:34 +00:00
|
|
|
|
|
|
|
if input.RateLimitProof != nil {
|
|
|
|
rpcWakuMsg.RateLimitProof = &RateLimitProof{
|
|
|
|
Proof: input.RateLimitProof.Proof,
|
|
|
|
MerkleRoot: input.RateLimitProof.MerkleRoot,
|
|
|
|
Epoch: input.RateLimitProof.Epoch,
|
|
|
|
ShareX: input.RateLimitProof.ShareX,
|
|
|
|
ShareY: input.RateLimitProof.ShareY,
|
|
|
|
Nullifier: input.RateLimitProof.Nullifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcWakuMsg
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RPCWakuMessage) toProto() *pb.WakuMessage {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:28:34 +00:00
|
|
|
msg := &pb.WakuMessage{
|
2022-06-13 18:30:35 +00:00
|
|
|
Payload: r.Payload,
|
|
|
|
ContentTopic: r.ContentTopic,
|
|
|
|
Version: r.Version,
|
|
|
|
Timestamp: r.Timestamp,
|
|
|
|
}
|
2022-07-05 21:28:34 +00:00
|
|
|
|
|
|
|
if r.RateLimitProof != nil {
|
|
|
|
msg.RateLimitProof = &pb.RateLimitProof{
|
|
|
|
Proof: r.RateLimitProof.Proof,
|
|
|
|
MerkleRoot: r.RateLimitProof.MerkleRoot,
|
|
|
|
Epoch: r.RateLimitProof.Epoch,
|
|
|
|
ShareX: r.RateLimitProof.ShareX,
|
|
|
|
ShareY: r.RateLimitProof.ShareY,
|
|
|
|
Nullifier: r.RateLimitProof.Nullifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u HexBytes) MarshalJSON() ([]byte, error) {
|
|
|
|
var result string
|
|
|
|
if u == nil {
|
|
|
|
result = "null"
|
|
|
|
} else {
|
|
|
|
result = strings.Join(strings.Fields(fmt.Sprintf("%d", u)), ",")
|
|
|
|
}
|
|
|
|
return []byte(result), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HexBytes) UnmarshalText(b []byte) error {
|
|
|
|
hexString := ""
|
|
|
|
if b != nil {
|
|
|
|
hexString = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := hex.DecodeString(hexString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*h = decoded
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProtoWakuMessageToRPCWakuRelayMessage(input *pb.WakuMessage) *RPCWakuRelayMessage {
|
|
|
|
if input == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:28:34 +00:00
|
|
|
rpcMsg := &RPCWakuRelayMessage{
|
2022-06-13 18:30:35 +00:00
|
|
|
Payload: input.Payload,
|
|
|
|
ContentTopic: input.ContentTopic,
|
|
|
|
Timestamp: input.Timestamp,
|
|
|
|
}
|
2022-07-05 21:28:34 +00:00
|
|
|
|
|
|
|
if input.RateLimitProof != nil {
|
|
|
|
rpcMsg.RateLimitProof = &RateLimitProof{
|
|
|
|
Proof: input.RateLimitProof.Proof,
|
|
|
|
MerkleRoot: input.RateLimitProof.MerkleRoot,
|
|
|
|
Epoch: input.RateLimitProof.Epoch,
|
|
|
|
ShareX: input.RateLimitProof.ShareX,
|
|
|
|
ShareY: input.RateLimitProof.ShareY,
|
|
|
|
Nullifier: input.RateLimitProof.Nullifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcMsg
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RPCWakuRelayMessage) toProto() *pb.WakuMessage {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:28:34 +00:00
|
|
|
msg := &pb.WakuMessage{
|
2022-06-13 18:30:35 +00:00
|
|
|
Payload: r.Payload,
|
|
|
|
ContentTopic: r.ContentTopic,
|
|
|
|
Timestamp: r.Timestamp,
|
|
|
|
}
|
2022-07-05 21:28:34 +00:00
|
|
|
|
|
|
|
if r.RateLimitProof != nil {
|
|
|
|
msg.RateLimitProof = &pb.RateLimitProof{
|
|
|
|
Proof: r.RateLimitProof.Proof,
|
|
|
|
MerkleRoot: r.RateLimitProof.MerkleRoot,
|
|
|
|
Epoch: r.RateLimitProof.Epoch,
|
|
|
|
ShareX: r.RateLimitProof.ShareX,
|
|
|
|
ShareY: r.RateLimitProof.ShareY,
|
|
|
|
Nullifier: r.RateLimitProof.Nullifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h ByteArray) MarshalText() ([]byte, error) {
|
|
|
|
if h == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(hex.EncodeToString(h)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ByteArray) UnmarshalText(b []byte) error {
|
|
|
|
hexString := ""
|
|
|
|
if b != nil {
|
|
|
|
hexString = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := hex.DecodeString(hexString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*h = decoded
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|