go-waku/waku/v2/rpc/utils.go

200 lines
4.8 KiB
Go
Raw Normal View History

2022-06-13 18:30:35 +00:00
package rpc
import (
"encoding/hex"
"fmt"
"strings"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
2022-06-13 18:30:35 +00:00
)
// 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 {
2022-10-04 23:15:39 +00:00
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"`
RlnIdentifier HexBytes `json:"rln_identifier,omitempty"`
2022-07-05 21:28:34 +00:00
}
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{
2022-10-04 23:15:39 +00:00
Proof: input.RateLimitProof.Proof,
MerkleRoot: input.RateLimitProof.MerkleRoot,
Epoch: input.RateLimitProof.Epoch,
ShareX: input.RateLimitProof.ShareX,
ShareY: input.RateLimitProof.ShareY,
Nullifier: input.RateLimitProof.Nullifier,
RlnIdentifier: input.RateLimitProof.RlnIdentifier,
2022-07-05 21:28:34 +00:00
}
}
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{
2022-10-04 23:15:39 +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,
2022-07-05 21:28:34 +00:00
}
}
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)
}
2022-09-14 19:19:04 +00:00
decoded, err := utils.DecodeHexString(hexString)
2022-06-13 18:30:35 +00:00
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{
2022-10-04 23:15:39 +00:00
Proof: input.RateLimitProof.Proof,
MerkleRoot: input.RateLimitProof.MerkleRoot,
Epoch: input.RateLimitProof.Epoch,
ShareX: input.RateLimitProof.ShareX,
ShareY: input.RateLimitProof.ShareY,
Nullifier: input.RateLimitProof.Nullifier,
RlnIdentifier: input.RateLimitProof.RlnIdentifier,
2022-07-05 21:28:34 +00:00
}
}
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-09-23 20:40:12 +00:00
Version: r.Version,
2022-06-13 18:30:35 +00:00
}
2022-07-05 21:28:34 +00:00
if r.RateLimitProof != nil {
msg.RateLimitProof = &pb.RateLimitProof{
2022-10-04 23:15:39 +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,
2022-07-05 21:28:34 +00:00
}
}
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)
}
2022-09-14 19:19:04 +00:00
decoded, err := utils.DecodeHexString(hexString)
2022-06-13 18:30:35 +00:00
if err != nil {
return err
}
*h = decoded
return nil
}