diff --git a/nimbus/rpc/hexstrings.nim b/nimbus/rpc/hexstrings.nim index 77fd37022..332b85191 100644 --- a/nimbus/rpc/hexstrings.nim +++ b/nimbus/rpc/hexstrings.nim @@ -27,10 +27,10 @@ import eth_common/eth_types, stint, byteutils, nimcrypto type HexQuantityStr* = distinct string HexDataStr* = distinct string - EthAddressStr* = distinct string # Same as HexDataStr but must be less <= 20 bytes - EthHashStr* = distinct string # Same as HexDataStr but must be exactly 32 bytes - WhisperIdentity* = distinct string # 60 bytes - HexStrings = HexQuantityStr | HexDataStr | EthAddressStr | EthHashStr | WhisperIdentity + EthAddressStr* = distinct string # Same as HexDataStr but must be less <= 20 bytes + EthHashStr* = distinct string # Same as HexDataStr but must be exactly 32 bytes + WhisperIdentityStr* = distinct string # 60 bytes + HexStrings = HexQuantityStr | HexDataStr | EthAddressStr | EthHashStr | WhisperIdentityStr func len*(value: HexStrings): int = value.string.len @@ -141,28 +141,16 @@ proc ethHashStr*(value: string): EthHashStr {.inline.} = value.validateHashStr result = value.EthHashStr -proc whisperIdentity*(value: string): WhisperIdentity {.inline.} = +proc whisperIdentity*(value: string): WhisperIdentityStr {.inline.} = value.validateWhisperIdentity - result = value.WhisperIdentity + result = value.WhisperIdentityStr # Converters for use in RPC import json from json_rpc/rpcserver import expect -proc `%`*(value: HexQuantityStr): JsonNode = - result = %(value.string) - -proc `%`*(value: HexDataStr): JsonNode = - result = %(value.string) - -proc `%`*(value: EthAddressStr): JsonNode = - result = %(value.string) - -proc `%`*(value: EthHashStr): JsonNode = - result = %(value.string) - -proc `%`*(value: WhisperIdentity): JsonNode = +proc `%`*(value: HexStrings): JsonNode = result = %(value.string) # Overloads to support expected representation of hex data @@ -179,7 +167,7 @@ proc `%`*(value: Hash256): JsonNode = proc `%`*(value: UInt256): JsonNode = result = %("0x" & value.toString) -proc `%`*(value: openarray[byte]): JsonNode = +proc `%`*(value: WhisperIdentity): JsonNode = result = %("0x" & byteutils.toHex(value)) proc `%`*(value: ref BloomFilter): JsonNode = @@ -188,7 +176,6 @@ proc `%`*(value: ref BloomFilter): JsonNode = # Marshalling from JSON to Nim types that includes format checking proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) = - # Note that '0x' is stripped after validation n.kind.expect(JString, argName) let hexStr = n.getStr() if not hexStr.isValidHexQuantity: @@ -196,7 +183,6 @@ proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) = result = hexStr.hexQuantityStr proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) = - # Note that '0x' is stripped after validation n.kind.expect(JString, argName) let hexStr = n.getStr() if not hexStr.isValidHexData: @@ -204,7 +190,6 @@ proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) = result = hexStr.hexDataStr proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) = - # Note that '0x' is stripped after validation n.kind.expect(JString, argName) let hexStr = n.getStr() if not hexStr.isValidEthAddress: @@ -212,18 +197,16 @@ proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) = result = hexStr.EthAddressStr proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) = - # Note that '0x' is stripped after validation n.kind.expect(JString, argName) let hexStr = n.getStr() if not hexStr.isValidEthHash: raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum hash \"" & hexStr & "\"") result = hexStr.EthHashStr -proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentity) = - # Note that '0x' is stripped after validation +proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentityStr) = n.kind.expect(JString, argName) let hexStr = n.getStr() if not hexStr.isValidWhisperIdentity: raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as a Whisper identity \"" & hexStr & "\"") - result = hexStr.WhisperIdentity + result = hexStr.WhisperIdentityStr