Fix marshalling of message payloads (#428)

Fix marshalling of message payloads
This commit is contained in:
Hanno Cornelius 2021-03-22 17:13:56 +02:00 committed by GitHub
parent 62b824c387
commit 2fe6935623
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View File

@ -138,7 +138,10 @@ proc `%`*(value: waku_protocol.Topic): JsonNode =
result = %("0x" & value.toHex)
proc `%`*(value: seq[byte]): JsonNode =
result = %("0x" & value.toHex)
if value.len > 0:
result = %("0x" & value.toHex)
else:
result = newJArray()
# Helpers for the fromJson procs
@ -210,12 +213,6 @@ proc fromJson*(n: JsonNode, argName: string, result: var waku_protocol.Topic) =
# Following procs currently required only for testing, the `createRpcSigs` macro
# requires it as it will convert the JSON results back to the original Nim
# types, but it needs the `fromJson` calls for those specific Nim types to do so
proc fromJson*(n: JsonNode, argName: string, result: var seq[byte]) =
n.kind.expect(JString, argName)
let hexStr = n.getStr()
if not hexStr.isValidHexData:
raise newException(ValueError, invalidMsg(argName) & " as a hex data \"" & hexStr & "\"")
result = hexToSeqByte(hexStr)
proc fromJson*(n: JsonNode, argName: string, result: var Hash256) =
n.kind.expect(JString, argName)

View File

@ -1,5 +1,5 @@
import
std/options,
std/[options, json, sequtils],
eth/keys,
../../../v1/node/rpc/hexstrings,
../../protocol/waku_store/waku_store_types,
@ -8,6 +8,16 @@ import
export hexstrings
## Json marshalling
proc `%`*(value: WakuMessage): JsonNode =
## This ensures that seq[byte] fields are marshalled to hex-format JStrings
## (as defined in `hexstrings.nim`) rather than the default JArray[JInt]
let jObj = newJObject()
for k, v in value.fieldPairs:
jObj[k] = %v
return jObj
## Conversion tools
## Since the Waku v2 JSON-RPC API has its own defined types,
## we need to convert between these and the types for the Nim API