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