mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-02-02 15:24:01 +00:00
Add EthHashStr validation
This commit is contained in:
parent
d1f283b004
commit
4c38ede9c9
@ -13,11 +13,13 @@
|
||||
type
|
||||
HexQuantityStr* = distinct string
|
||||
HexDataStr* = distinct string
|
||||
EthAddressStr* = 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
|
||||
|
||||
func len*(data: HexQuantityStr): int = data.string.len
|
||||
func len*(quantity: HexQuantityStr): int = quantity.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
|
||||
|
||||
@ -67,12 +69,20 @@ func isValidHexData*(value: string): bool =
|
||||
|
||||
func isValidEthAddress*(value: string): bool =
|
||||
# 20 bytes for EthAddress plus "0x"
|
||||
# Addresses are allowed to be shorter than 20 bytes for convenience
|
||||
result = value.len <= 42 and value.isValidHexData
|
||||
|
||||
func isValidEthHash*(value: string): bool =
|
||||
# 32 bytes for EthAddress plus "0x"
|
||||
# Currently hashes are required to be exact lengths
|
||||
# TODO: Allow shorter hashes (pad with zeros) for convenience?
|
||||
result = value.len == 66 and value.isValidHexData
|
||||
|
||||
const
|
||||
SInvalidQuantity = "Invalid hex quantity format for Ethereum"
|
||||
SInvalidData = "Invalid hex data format for Ethereum"
|
||||
SInvalidAddress = "Invalid address format for Ethereum"
|
||||
SInvalidHash = "Invalid hash format for Ethereum"
|
||||
|
||||
proc validateHexQuantity*(value: string) {.inline.} =
|
||||
if unlikely(not value.isValidHexQuantity):
|
||||
@ -86,6 +96,10 @@ proc validateHexAddressStr*(value: string) {.inline.} =
|
||||
if unlikely(not value.isValidEthAddress):
|
||||
raise newException(ValueError, SInvalidAddress & ": " & value)
|
||||
|
||||
proc validateHashStr*(value: string) {.inline.} =
|
||||
if unlikely(not value.isValidEthHash):
|
||||
raise newException(ValueError, SInvalidHash & ": " & value)
|
||||
|
||||
# Initialisation
|
||||
|
||||
proc hexQuantityStr*(value: string): HexQuantityStr {.inline.} =
|
||||
@ -100,6 +114,10 @@ proc ethAddressStr*(value: string): EthAddressStr {.inline.} =
|
||||
value.validateHexAddressStr
|
||||
result = value.EthAddressStr
|
||||
|
||||
proc ethHashStr*(value: string): EthHashStr {.inline.} =
|
||||
value.validateHashStr
|
||||
result = value.EthHashStr
|
||||
|
||||
# Converters for use in RPC
|
||||
|
||||
import json
|
||||
@ -114,6 +132,9 @@ proc `%`*(value: HexDataStr): JsonNode =
|
||||
proc `%`*(value: EthAddressStr): JsonNode =
|
||||
result = %(value.string)
|
||||
|
||||
proc `%`*(value: EthHashStr): JsonNode =
|
||||
result = %(value.string)
|
||||
|
||||
proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
|
||||
# Note that '0x' is stripped after validation
|
||||
n.kind.expect(JString, argName)
|
||||
@ -138,3 +159,11 @@ proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) =
|
||||
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum address \"" & hexStr & "\"")
|
||||
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user