mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-11 21:04:11 +00:00
Add WhisperIdentity string, clean up len func
This commit is contained in:
parent
df88326cfd
commit
fec18e4ec2
@ -17,6 +17,7 @@
|
|||||||
* ref EthAddress
|
* ref EthAddress
|
||||||
* Hash256
|
* Hash256
|
||||||
* UInt256
|
* UInt256
|
||||||
|
* seq[byte]
|
||||||
* openArray[seq]
|
* openArray[seq]
|
||||||
* ref BloomFilter
|
* ref BloomFilter
|
||||||
]#
|
]#
|
||||||
@ -28,11 +29,10 @@ type
|
|||||||
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
|
||||||
|
HexStrings = HexQuantityStr | HexDataStr | EthAddressStr | EthHashStr | WhisperIdentity
|
||||||
|
|
||||||
func len*(quantity: HexQuantityStr): int = quantity.string.len
|
func len*(value: HexStrings): int = value.string.len
|
||||||
func len*(data: HexDataStr): int = data.string.len
|
|
||||||
func len*(data: EthAddressStr): int = data.string.len
|
|
||||||
func len*(data: EthHashStr): int = data.string.len
|
|
||||||
|
|
||||||
# Hex validation
|
# Hex validation
|
||||||
|
|
||||||
@ -91,11 +91,17 @@ func isValidEthHash*(value: string): bool =
|
|||||||
# TODO: Allow shorter hashes (pad with zeros) for convenience?
|
# TODO: Allow shorter hashes (pad with zeros) for convenience?
|
||||||
result = value.len == 66 and value.isValidHexData
|
result = value.len == 66 and value.isValidHexData
|
||||||
|
|
||||||
|
func isValidWhisperIdentity*(value: string): bool =
|
||||||
|
# 60 bytes for WhisperIdentity plus "0x"
|
||||||
|
# TODO: Are the HexData constratins applicable to Whisper identities?
|
||||||
|
result = value.len == 122 and value.isValidHexData
|
||||||
|
|
||||||
const
|
const
|
||||||
SInvalidQuantity = "Invalid hex quantity format for Ethereum"
|
SInvalidQuantity = "Invalid hex quantity format for Ethereum"
|
||||||
SInvalidData = "Invalid hex data format for Ethereum"
|
SInvalidData = "Invalid hex data format for Ethereum"
|
||||||
SInvalidAddress = "Invalid address format for Ethereum"
|
SInvalidAddress = "Invalid address format for Ethereum"
|
||||||
SInvalidHash = "Invalid hash format for Ethereum"
|
SInvalidHash = "Invalid hash format for Ethereum"
|
||||||
|
SInvalidWhisperIdentity = "Invalid format for whisper identity"
|
||||||
|
|
||||||
proc validateHexQuantity*(value: string) {.inline.} =
|
proc validateHexQuantity*(value: string) {.inline.} =
|
||||||
if unlikely(not value.isValidHexQuantity):
|
if unlikely(not value.isValidHexQuantity):
|
||||||
@ -113,6 +119,10 @@ proc validateHashStr*(value: string) {.inline.} =
|
|||||||
if unlikely(not value.isValidEthHash):
|
if unlikely(not value.isValidEthHash):
|
||||||
raise newException(ValueError, SInvalidHash & ": " & value)
|
raise newException(ValueError, SInvalidHash & ": " & value)
|
||||||
|
|
||||||
|
proc validateWhisperIdentity*(value: string) {.inline.} =
|
||||||
|
if unlikely(not value.isValidWhisperIdentity):
|
||||||
|
raise newException(ValueError, SInvalidWhisperIdentity & ": " & value)
|
||||||
|
|
||||||
# Initialisation
|
# Initialisation
|
||||||
|
|
||||||
proc hexQuantityStr*(value: string): HexQuantityStr {.inline.} =
|
proc hexQuantityStr*(value: string): HexQuantityStr {.inline.} =
|
||||||
@ -131,6 +141,10 @@ proc ethHashStr*(value: string): EthHashStr {.inline.} =
|
|||||||
value.validateHashStr
|
value.validateHashStr
|
||||||
result = value.EthHashStr
|
result = value.EthHashStr
|
||||||
|
|
||||||
|
proc whisperIdentity*(value: string): WhisperIdentity {.inline.} =
|
||||||
|
value.validateWhisperIdentity
|
||||||
|
result = value.WhisperIdentity
|
||||||
|
|
||||||
# Converters for use in RPC
|
# Converters for use in RPC
|
||||||
|
|
||||||
import json
|
import json
|
||||||
@ -148,6 +162,9 @@ proc `%`*(value: EthAddressStr): JsonNode =
|
|||||||
proc `%`*(value: EthHashStr): JsonNode =
|
proc `%`*(value: EthHashStr): JsonNode =
|
||||||
result = %(value.string)
|
result = %(value.string)
|
||||||
|
|
||||||
|
proc `%`*(value: WhisperIdentity): JsonNode =
|
||||||
|
result = %(value.string)
|
||||||
|
|
||||||
# Overloads to support expected representation of hex data
|
# Overloads to support expected representation of hex data
|
||||||
|
|
||||||
proc `%`*(value: EthAddress): JsonNode =
|
proc `%`*(value: EthAddress): JsonNode =
|
||||||
@ -162,8 +179,8 @@ proc `%`*(value: Hash256): JsonNode =
|
|||||||
proc `%`*(value: UInt256): JsonNode =
|
proc `%`*(value: UInt256): JsonNode =
|
||||||
result = %("0x" & value.toString)
|
result = %("0x" & value.toString)
|
||||||
|
|
||||||
proc `%`*(value: openArray[seq]): JsonNode =
|
proc `%`*(value: openarray[byte]): JsonNode =
|
||||||
result = %("0x" & value.toHex)
|
result = %("0x" & byteutils.toHex(value))
|
||||||
|
|
||||||
proc `%`*(value: ref BloomFilter): JsonNode =
|
proc `%`*(value: ref BloomFilter): JsonNode =
|
||||||
result = %("0x" & toHex[256](value[]))
|
result = %("0x" & toHex[256](value[]))
|
||||||
@ -202,3 +219,11 @@ proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) =
|
|||||||
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) =
|
||||||
|
# Note that '0x' is stripped after validation
|
||||||
|
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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user