Add WhisperIdentityStr

This commit is contained in:
coffeepots 2018-08-21 22:21:30 +01:00 committed by zah
parent fec18e4ec2
commit 3f2f8f83b4
1 changed files with 10 additions and 27 deletions

View File

@ -29,8 +29,8 @@ type
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
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