From 2fe6935623966d46f621a239e19cc37fabd1735c Mon Sep 17 00:00:00 2001 From: Hanno Cornelius <68783915+jm-clius@users.noreply.github.com> Date: Mon, 22 Mar 2021 17:13:56 +0200 Subject: [PATCH] Fix marshalling of message payloads (#428) Fix marshalling of message payloads --- waku/v1/node/rpc/hexstrings.nim | 11 ++++------- waku/v2/node/jsonrpc/jsonrpc_utils.nim | 12 +++++++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/waku/v1/node/rpc/hexstrings.nim b/waku/v1/node/rpc/hexstrings.nim index bc12e6833..e8f4fdff3 100644 --- a/waku/v1/node/rpc/hexstrings.nim +++ b/waku/v1/node/rpc/hexstrings.nim @@ -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) diff --git a/waku/v2/node/jsonrpc/jsonrpc_utils.nim b/waku/v2/node/jsonrpc/jsonrpc_utils.nim index 1d78242cf..735da3c53 100644 --- a/waku/v2/node/jsonrpc/jsonrpc_utils.nim +++ b/waku/v2/node/jsonrpc/jsonrpc_utils.nim @@ -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