add JSON support for eligibility proof in Lightpush REST API

This commit is contained in:
Sergei Tikhomirov 2025-05-21 14:48:58 +02:00
parent 0e741ca786
commit 0d00fb8810
2 changed files with 37 additions and 2 deletions

View File

@ -1,4 +1,9 @@
import std/options
import
std/[options, strutils],
stew/byteutils,
json_serialization,
json_serialization/std/options,
../waku_api/rest/serdes
# Implementing the RFC:
# https://github.com/vacp2p/rfc/tree/master/content/docs/rfcs/73
@ -10,3 +15,26 @@ type
EligibilityStatus* = object
statusCode*: uint32
statusDesc*: Option[string]
proc writeValue*(
writer: var JsonWriter[RestJson], value: EligibilityProof
) {.raises: [IOError].} =
if value.proofOfPayment.isSome():
writer.writeValue("0x" & value.proofOfPayment.get().toHex())
else:
writer.writeValue("")
proc readValue*(
reader: var JsonReader[RestJson], value: var EligibilityProof
) {.raises: [SerializationError, IOError].} =
let hexStr = reader.readValue(string)
if hexStr.len > 0:
let startIndex = if hexStr.len > 2 and hexStr[0..1] == "0x": 2 else: 0
try:
let bytes = hexToSeqByte(hexStr[startIndex..^1])
value = EligibilityProof(proofOfPayment: some(bytes))
except ValueError as e:
# Either handle the error or re-raise it
raise newException(SerializationError, "Invalid hex string: " & e.msg)
else:
value = EligibilityProof(proofOfPayment: none(seq[byte]))

View File

@ -7,7 +7,7 @@ import
json_serialization/std/options,
presto/[route, client]
import ../../../waku_core, ../relay/types as relay_types, ../serdes
import ../../../waku_core, ../../../incentivization/rpc, ../relay/types as relay_types, ../serdes
export relay_types
@ -17,6 +17,7 @@ type
PushRequest* = object
pubsubTopic*: Option[PubSubTopic]
message*: RelayWakuMessage
eligibilityProof*: Option[EligibilityProof]
PushResponse* = object
statusDesc*: Option[string]
@ -30,6 +31,8 @@ proc writeValue*(
if value.pubsubTopic.isSome():
writer.writeField("pubsubTopic", value.pubsubTopic.get())
writer.writeField("message", value.message)
if value.eligibilityProof.isSome():
writer.writeField("eligibilityProof", value.eligibilityProof.get())
writer.endRecord()
proc readValue*(
@ -38,6 +41,7 @@ proc readValue*(
var
pubsubTopic = none(PubsubTopic)
message = none(RelayWakuMessage)
eligibilityProof = none(EligibilityProof)
var keys = initHashSet[string]()
for fieldName in readObjectFields(reader):
@ -55,6 +59,8 @@ proc readValue*(
pubsubTopic = some(reader.readValue(PubsubTopic))
of "message":
message = some(reader.readValue(RelayWakuMessage))
of "eligibilityProof":
eligibilityProof = some(reader.readValue(EligibilityProof))
else:
unrecognizedFieldWarning(value)
@ -68,6 +74,7 @@ proc readValue*(
else:
some(pubsubTopic.get()),
message: message.get(),
eligibilityProof: eligibilityProof,
)
proc writeValue*(