2020-05-22 17:04:52 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2020-10-28 18:51:38 +00:00
|
|
|
std/[tables, json, typetraits],
|
2020-06-03 13:52:02 +00:00
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
# Nimble packages
|
2020-06-03 13:52:02 +00:00
|
|
|
stew/byteutils, ssz/types,
|
2020-05-22 17:04:52 +00:00
|
|
|
json_rpc/jsonmarshal,
|
|
|
|
|
|
|
|
# Local modules
|
2020-10-28 18:51:38 +00:00
|
|
|
spec/[datatypes, crypto, digest]
|
|
|
|
|
|
|
|
proc toJsonHex(data: openArray[byte]): string =
|
|
|
|
# Per the eth2 API spec, hex arrays are printed with leading 0x
|
|
|
|
"0x" & toHex(data)
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var ValidatorPubKey) =
|
2020-06-29 17:30:19 +00:00
|
|
|
n.kind.expect(JString, argName)
|
2020-10-28 18:51:38 +00:00
|
|
|
result = initPubKey(ValidatorPubKey.fromHex(n.getStr()).tryGet().initPubKey())
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
proc `%`*(pubkey: ValidatorPubKey): JsonNode =
|
2020-10-28 18:51:38 +00:00
|
|
|
newJString(toJsonHex(toRaw(pubkey)))
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var List) =
|
|
|
|
fromJson(n, argName, asSeq result)
|
|
|
|
|
|
|
|
proc `%`*(list: List): JsonNode = %(asSeq(list))
|
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var BitList) =
|
|
|
|
fromJson(n, argName, seq[byte](BitSeq(result)))
|
|
|
|
|
|
|
|
proc `%`*(bitlist: BitList): JsonNode = %(seq[byte](BitSeq(bitlist)))
|
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var ValidatorSig) =
|
2020-06-29 17:30:19 +00:00
|
|
|
n.kind.expect(JString, argName)
|
2020-05-22 17:04:52 +00:00
|
|
|
result = ValidatorSig.fromHex(n.getStr()).tryGet()
|
|
|
|
|
|
|
|
proc `%`*(value: ValidatorSig): JsonNode =
|
2020-10-28 18:51:38 +00:00
|
|
|
newJString(toJsonHex(toRaw(value)))
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2020-05-27 17:06:28 +00:00
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var Version) =
|
2020-06-29 17:30:19 +00:00
|
|
|
n.kind.expect(JString, argName)
|
2020-05-27 17:06:28 +00:00
|
|
|
hexToByteArray(n.getStr(), array[4, byte](result))
|
|
|
|
|
|
|
|
proc `%`*(value: Version): JsonNode =
|
2020-10-28 18:51:38 +00:00
|
|
|
newJString(toJsonHex(distinctBase(value)))
|
2020-05-27 17:06:28 +00:00
|
|
|
|
2020-10-07 13:02:54 +00:00
|
|
|
template genFromJsonForIntType(T: untyped) =
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var T) =
|
2020-05-22 17:04:52 +00:00
|
|
|
n.kind.expect(JInt, argName)
|
2020-10-28 18:51:38 +00:00
|
|
|
let asInt = n.getBiggestInt()
|
2020-10-07 13:02:54 +00:00
|
|
|
# signed -> unsigned conversions are unchecked
|
|
|
|
# https://github.com/nim-lang/RFCs/issues/175
|
|
|
|
if asInt < 0:
|
|
|
|
raise newException(
|
|
|
|
ValueError, "JSON-RPC input is an unexpected negative value")
|
|
|
|
result = T(asInt)
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
genFromJsonForIntType(Epoch)
|
|
|
|
genFromJsonForIntType(Slot)
|
|
|
|
genFromJsonForIntType(CommitteeIndex)
|
2020-09-14 11:13:30 +00:00
|
|
|
genFromJsonForIntType(ValidatorIndex)
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2020-10-28 18:51:38 +00:00
|
|
|
proc `%`*(value: GraffitiBytes): JsonNode =
|
|
|
|
newJString(toJsonHex(distinctBase(value)))
|
2020-06-29 17:30:19 +00:00
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, value: var GraffitiBytes) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
value = GraffitiBytes.init n.getStr()
|
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
proc `%`*(value: CommitteeIndex): JsonNode =
|
2020-10-28 18:51:38 +00:00
|
|
|
newJInt(value.BiggestInt)
|
2020-09-14 11:13:30 +00:00
|
|
|
|
|
|
|
proc `%`*(value: ValidatorIndex): JsonNode =
|
2020-10-28 18:51:38 +00:00
|
|
|
newJInt(value.BiggestInt)
|
|
|
|
|
|
|
|
proc `%`*(value: Eth2Digest): JsonNode =
|
|
|
|
newJString(toJsonHex(value.data))
|
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var Eth2Digest) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
hexToByteArray(n.getStr(), result.data)
|