diff --git a/waku/incentivization/rpc.nim b/waku/incentivization/rpc.nim index 5223f5b5b..cfcbe2455 100644 --- a/waku/incentivization/rpc.nim +++ b/waku/incentivization/rpc.nim @@ -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])) \ No newline at end of file diff --git a/waku/waku_api/rest/lightpush/types.nim b/waku/waku_api/rest/lightpush/types.nim index 1fb87ab45..e2efa5879 100644 --- a/waku/waku_api/rest/lightpush/types.nim +++ b/waku/waku_api/rest/lightpush/types.nim @@ -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*(