Relax JSON to UInt256 length constraint, refactor err msgs

This commit is contained in:
coffeepots 2018-11-28 22:04:57 +00:00
parent b57427e3ee
commit f6cd02ff3f
1 changed files with 9 additions and 7 deletions

View File

@ -175,45 +175,47 @@ 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
func invalidMsg(name: string): string = "When marshalling from JSON, parameter \"" & name & "\" is not valid"
proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) = proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
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:
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum hex quantity \"" & hexStr & "\"") raise newException(ValueError, invalidMsg(argName) & " as an Ethereum hex quantity \"" & hexStr & "\"")
result = hexStr.hexQuantityStr result = hexStr.hexQuantityStr
proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) = proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) =
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:
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as Ethereum data \"" & hexStr & "\"") raise newException(ValueError, invalidMsg(argName) & " as Ethereum data \"" & hexStr & "\"")
result = hexStr.hexDataStr result = hexStr.hexDataStr
proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) = proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) =
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:
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum address \"" & hexStr & "\"") raise newException(ValueError, invalidMsg(argName) & "\" as an Ethereum address \"" & hexStr & "\"")
result = hexStr.EthAddressStr result = hexStr.EthAddressStr
proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) = proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) =
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, invalidMsg(argName) & " as an Ethereum hash \"" & hexStr & "\"")
result = hexStr.EthHashStr result = hexStr.EthHashStr
proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentityStr) = proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentityStr) =
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, invalidMsg(argName) & " as a Whisper identity \"" & hexStr & "\"")
result = hexStr.WhisperIdentityStr result = hexStr.WhisperIdentityStr
proc fromJson*(n: JsonNode, argName: string, result: var UInt256) = proc fromJson*(n: JsonNode, argName: string, result: var UInt256) =
n.kind.expect(JString, argName) n.kind.expect(JString, argName)
let hexStr = n.getStr() let hexStr = n.getStr()
if not hexStr.isValidEthHash: # Same format as hash if hexStr.len <= 66 and hexStr.isValidHexData:
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as a UInt256 \"" & hexStr & "\"") raise newException(ValueError, invalidMsg(argName) & " as a UInt256 \"" & hexStr & "\"")
result = readUintBE[256](hexToPaddedByteArray[32](hexStr)) result = readUintBE[256](hexToPaddedByteArray[32](hexStr))