Add json converters for byte array and UInt256 types to hex string
This commit is contained in:
parent
ee15f4a995
commit
88ab097a61
|
@ -10,6 +10,8 @@
|
||||||
## This module implements the Ethereum hexadecimal string formats for JSON
|
## This module implements the Ethereum hexadecimal string formats for JSON
|
||||||
## See: https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
|
## See: https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
|
||||||
|
|
||||||
|
import eth_common/eth_types, stint, byteutils
|
||||||
|
|
||||||
type
|
type
|
||||||
HexQuantityStr* = distinct string
|
HexQuantityStr* = distinct string
|
||||||
HexDataStr* = distinct string
|
HexDataStr* = distinct string
|
||||||
|
@ -135,6 +137,17 @@ proc `%`*(value: EthAddressStr): JsonNode =
|
||||||
proc `%`*(value: EthHashStr): JsonNode =
|
proc `%`*(value: EthHashStr): JsonNode =
|
||||||
result = %(value.string)
|
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) =
|
proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
|
||||||
# Note that '0x' is stripped after validation
|
# Note that '0x' is stripped after validation
|
||||||
n.kind.expect(JString, argName)
|
n.kind.expect(JString, argName)
|
||||||
|
|
Loading…
Reference in New Issue