Add json converters for byte array and UInt256 types to hex string

This commit is contained in:
coffeepots 2018-08-14 20:22:04 +01:00
parent ee15f4a995
commit 88ab097a61
1 changed files with 13 additions and 0 deletions

View File

@ -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)