From 88ab097a61990173ec01417e515e0b6095de8436 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Tue, 14 Aug 2018 20:22:04 +0100 Subject: [PATCH] Add json converters for byte array and UInt256 types to hex string --- nimbus/rpc/hexstrings.nim | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/nimbus/rpc/hexstrings.nim b/nimbus/rpc/hexstrings.nim index 1fd089589..e80da1d78 100644 --- a/nimbus/rpc/hexstrings.nim +++ b/nimbus/rpc/hexstrings.nim @@ -10,6 +10,8 @@ ## This module implements the Ethereum hexadecimal string formats for JSON ## See: https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding +import eth_common/eth_types, stint, byteutils + type HexQuantityStr* = distinct string HexDataStr* = distinct string @@ -135,6 +137,17 @@ proc `%`*(value: EthAddressStr): JsonNode = proc `%`*(value: EthHashStr): JsonNode = result = %(value.string) +# Overloads to support expected representation of hex data + +proc `%`*(value: EthAddress): JsonNode = + result = %("0x" & value.toHex) + +proc `%`*(value: UInt256): JsonNode = + result = %("0x" & value.toString) + +proc `%`*(value: openArray[seq]): JsonNode = + result = %("0x" & value.toHex) + proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) = # Note that '0x' is stripped after validation n.kind.expect(JString, argName)