2018-07-27 17:02:02 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
## This module implements the Ethereum hexadecimal string formats for JSON
|
|
|
|
## See: https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
|
|
|
|
|
2018-08-21 13:31:45 +00:00
|
|
|
#[
|
|
|
|
Note:
|
|
|
|
The following types are converted to hex strings when marshalled to JSON:
|
|
|
|
* EthAddress
|
|
|
|
* ref EthAddress
|
|
|
|
* Hash256
|
|
|
|
* UInt256
|
2018-08-21 20:50:26 +00:00
|
|
|
* seq[byte]
|
2018-08-21 13:31:45 +00:00
|
|
|
* openArray[seq]
|
|
|
|
* ref BloomFilter
|
|
|
|
]#
|
|
|
|
|
2019-02-05 19:15:50 +00:00
|
|
|
import eth/common/eth_types, stint, byteutils, nimcrypto
|
2018-08-14 19:22:04 +00:00
|
|
|
|
2018-07-27 17:02:02 +00:00
|
|
|
type
|
|
|
|
HexQuantityStr* = distinct string
|
|
|
|
HexDataStr* = distinct string
|
2018-08-21 21:21:30 +00:00
|
|
|
EthAddressStr* = distinct string # Same as HexDataStr but must be less <= 20 bytes
|
|
|
|
EthHashStr* = distinct string # Same as HexDataStr but must be exactly 32 bytes
|
|
|
|
WhisperIdentityStr* = distinct string # 60 bytes
|
|
|
|
HexStrings = HexQuantityStr | HexDataStr | EthAddressStr | EthHashStr | WhisperIdentityStr
|
2018-08-13 16:33:57 +00:00
|
|
|
|
2018-08-24 13:20:57 +00:00
|
|
|
template len*(value: HexStrings): int = value.string.len
|
2018-07-27 17:02:02 +00:00
|
|
|
|
|
|
|
# Hex validation
|
|
|
|
|
|
|
|
template stripLeadingZeros(value: string): string =
|
|
|
|
var cidx = 0
|
|
|
|
# ignore the last character so we retain '0' on zero value
|
|
|
|
while cidx < value.len - 1 and value[cidx] == '0':
|
|
|
|
cidx.inc
|
|
|
|
value[cidx .. ^1]
|
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
func encodeQuantity*(value: SomeUnsignedInt): string {.inline.} =
|
2018-07-27 17:02:02 +00:00
|
|
|
var hValue = value.toHex.stripLeadingZeros
|
|
|
|
result = "0x" & hValue
|
|
|
|
|
2018-08-06 17:08:23 +00:00
|
|
|
template hasHexHeader(value: string): bool =
|
|
|
|
if value.len >= 2 and value[0] == '0' and value[1] in {'x', 'X'}: true
|
2018-07-27 17:02:02 +00:00
|
|
|
else: false
|
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
template isHexChar(c: char): bool =
|
2018-07-27 17:02:02 +00:00
|
|
|
if c notin {'0'..'9'} and
|
|
|
|
c notin {'a'..'f'} and
|
|
|
|
c notin {'A'..'F'}: false
|
|
|
|
else: true
|
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
func isValidHexQuantity*(value: string): bool =
|
2018-08-06 17:08:23 +00:00
|
|
|
if not value.hasHexHeader:
|
2018-07-27 17:02:02 +00:00
|
|
|
return false
|
2018-07-31 17:14:20 +00:00
|
|
|
# No leading zeros (but allow 0x0)
|
2018-08-06 17:08:23 +00:00
|
|
|
if value.len < 3 or (value.len > 3 and value[2] == '0'): return false
|
2018-07-27 17:02:02 +00:00
|
|
|
for i in 2 ..< value.len:
|
|
|
|
let c = value[i]
|
|
|
|
if not c.isHexChar:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
func isValidHexData*(value: string): bool =
|
2018-08-06 17:08:23 +00:00
|
|
|
if not value.hasHexHeader:
|
2018-07-27 17:02:02 +00:00
|
|
|
return false
|
|
|
|
# Must be even number of digits
|
|
|
|
if value.len mod 2 != 0: return false
|
|
|
|
# Leading zeros are allowed
|
|
|
|
for i in 2 ..< value.len:
|
|
|
|
let c = value[i]
|
|
|
|
if not c.isHexChar:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2018-08-13 16:33:57 +00:00
|
|
|
func isValidEthAddress*(value: string): bool =
|
|
|
|
# 20 bytes for EthAddress plus "0x"
|
2018-08-13 17:39:17 +00:00
|
|
|
# Addresses are allowed to be shorter than 20 bytes for convenience
|
2018-08-13 16:33:57 +00:00
|
|
|
result = value.len <= 42 and value.isValidHexData
|
|
|
|
|
2018-08-13 17:39:17 +00:00
|
|
|
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
|
|
|
|
|
2018-08-21 20:50:26 +00:00
|
|
|
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
|
|
|
|
|
2018-07-31 17:14:20 +00:00
|
|
|
const
|
|
|
|
SInvalidQuantity = "Invalid hex quantity format for Ethereum"
|
|
|
|
SInvalidData = "Invalid hex data format for Ethereum"
|
2018-08-13 16:33:57 +00:00
|
|
|
SInvalidAddress = "Invalid address format for Ethereum"
|
2018-08-13 17:39:17 +00:00
|
|
|
SInvalidHash = "Invalid hash format for Ethereum"
|
2018-08-21 20:50:26 +00:00
|
|
|
SInvalidWhisperIdentity = "Invalid format for whisper identity"
|
2018-07-31 17:14:20 +00:00
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
proc validateHexQuantity*(value: string) {.inline.} =
|
|
|
|
if unlikely(not value.isValidHexQuantity):
|
2018-07-31 17:14:20 +00:00
|
|
|
raise newException(ValueError, SInvalidQuantity & ": " & value)
|
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
proc validateHexData*(value: string) {.inline.} =
|
|
|
|
if unlikely(not value.isValidHexData):
|
2018-07-31 17:14:20 +00:00
|
|
|
raise newException(ValueError, SInvalidData & ": " & value)
|
|
|
|
|
2018-08-13 16:33:57 +00:00
|
|
|
proc validateHexAddressStr*(value: string) {.inline.} =
|
|
|
|
if unlikely(not value.isValidEthAddress):
|
|
|
|
raise newException(ValueError, SInvalidAddress & ": " & value)
|
|
|
|
|
2018-08-13 17:39:17 +00:00
|
|
|
proc validateHashStr*(value: string) {.inline.} =
|
|
|
|
if unlikely(not value.isValidEthHash):
|
|
|
|
raise newException(ValueError, SInvalidHash & ": " & value)
|
|
|
|
|
2018-08-21 20:50:26 +00:00
|
|
|
proc validateWhisperIdentity*(value: string) {.inline.} =
|
|
|
|
if unlikely(not value.isValidWhisperIdentity):
|
|
|
|
raise newException(ValueError, SInvalidWhisperIdentity & ": " & value)
|
|
|
|
|
2018-07-27 17:02:02 +00:00
|
|
|
# Initialisation
|
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
proc hexQuantityStr*(value: string): HexQuantityStr {.inline.} =
|
|
|
|
value.validateHexQuantity
|
2018-07-31 17:14:20 +00:00
|
|
|
result = value.HexQuantityStr
|
2018-07-27 17:02:02 +00:00
|
|
|
|
2018-08-06 16:17:58 +00:00
|
|
|
proc hexDataStr*(value: string): HexDataStr {.inline.} =
|
|
|
|
value.validateHexData
|
2018-07-31 17:14:20 +00:00
|
|
|
result = value.HexDataStr
|
2018-07-27 17:02:02 +00:00
|
|
|
|
2018-08-13 16:33:57 +00:00
|
|
|
proc ethAddressStr*(value: string): EthAddressStr {.inline.} =
|
|
|
|
value.validateHexAddressStr
|
|
|
|
result = value.EthAddressStr
|
|
|
|
|
2018-08-13 17:39:17 +00:00
|
|
|
proc ethHashStr*(value: string): EthHashStr {.inline.} =
|
|
|
|
value.validateHashStr
|
|
|
|
result = value.EthHashStr
|
|
|
|
|
2018-08-21 21:21:30 +00:00
|
|
|
proc whisperIdentity*(value: string): WhisperIdentityStr {.inline.} =
|
2018-08-21 20:50:26 +00:00
|
|
|
value.validateWhisperIdentity
|
2018-08-21 21:21:30 +00:00
|
|
|
result = value.WhisperIdentityStr
|
2018-08-21 20:50:26 +00:00
|
|
|
|
2018-07-27 17:02:02 +00:00
|
|
|
# Converters for use in RPC
|
|
|
|
|
|
|
|
import json
|
|
|
|
from json_rpc/rpcserver import expect
|
|
|
|
|
2018-08-21 21:21:30 +00:00
|
|
|
proc `%`*(value: HexStrings): JsonNode =
|
2018-08-21 20:50:26 +00:00
|
|
|
result = %(value.string)
|
|
|
|
|
2018-08-14 19:22:04 +00:00
|
|
|
# Overloads to support expected representation of hex data
|
|
|
|
|
|
|
|
proc `%`*(value: EthAddress): JsonNode =
|
|
|
|
result = %("0x" & value.toHex)
|
|
|
|
|
2018-08-16 16:41:40 +00:00
|
|
|
proc `%`*(value: ref EthAddress): JsonNode =
|
|
|
|
result = %("0x" & value[].toHex)
|
|
|
|
|
2018-08-15 12:13:30 +00:00
|
|
|
proc `%`*(value: Hash256): JsonNode =
|
|
|
|
result = %("0x" & $value)
|
|
|
|
|
2018-08-14 19:22:04 +00:00
|
|
|
proc `%`*(value: UInt256): JsonNode =
|
|
|
|
result = %("0x" & value.toString)
|
|
|
|
|
2018-08-21 21:21:30 +00:00
|
|
|
proc `%`*(value: WhisperIdentity): JsonNode =
|
2018-08-21 20:50:26 +00:00
|
|
|
result = %("0x" & byteutils.toHex(value))
|
2018-08-14 19:22:04 +00:00
|
|
|
|
2018-08-15 13:07:06 +00:00
|
|
|
proc `%`*(value: ref BloomFilter): JsonNode =
|
|
|
|
result = %("0x" & toHex[256](value[]))
|
|
|
|
|
|
|
|
# Marshalling from JSON to Nim types that includes format checking
|
|
|
|
|
2018-11-28 22:04:57 +00:00
|
|
|
func invalidMsg(name: string): string = "When marshalling from JSON, parameter \"" & name & "\" is not valid"
|
|
|
|
|
2018-07-27 17:02:02 +00:00
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
let hexStr = n.getStr()
|
2018-08-06 16:17:58 +00:00
|
|
|
if not hexStr.isValidHexQuantity:
|
2018-11-28 22:04:57 +00:00
|
|
|
raise newException(ValueError, invalidMsg(argName) & " as an Ethereum hex quantity \"" & hexStr & "\"")
|
2018-07-31 16:46:40 +00:00
|
|
|
result = hexStr.hexQuantityStr
|
2018-07-27 17:02:02 +00:00
|
|
|
|
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
let hexStr = n.getStr()
|
2018-08-06 16:17:58 +00:00
|
|
|
if not hexStr.isValidHexData:
|
2018-11-28 22:04:57 +00:00
|
|
|
raise newException(ValueError, invalidMsg(argName) & " as Ethereum data \"" & hexStr & "\"")
|
2018-07-31 16:46:40 +00:00
|
|
|
result = hexStr.hexDataStr
|
2018-07-27 17:02:02 +00:00
|
|
|
|
2018-08-13 16:33:57 +00:00
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var EthAddressStr) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
let hexStr = n.getStr()
|
|
|
|
if not hexStr.isValidEthAddress:
|
2018-11-28 22:04:57 +00:00
|
|
|
raise newException(ValueError, invalidMsg(argName) & "\" as an Ethereum address \"" & hexStr & "\"")
|
2018-08-13 16:33:57 +00:00
|
|
|
result = hexStr.EthAddressStr
|
|
|
|
|
2018-08-13 17:39:17 +00:00
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var EthHashStr) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
let hexStr = n.getStr()
|
|
|
|
if not hexStr.isValidEthHash:
|
2018-11-28 22:04:57 +00:00
|
|
|
raise newException(ValueError, invalidMsg(argName) & " as an Ethereum hash \"" & hexStr & "\"")
|
2018-08-13 17:39:17 +00:00
|
|
|
result = hexStr.EthHashStr
|
|
|
|
|
2018-08-21 21:21:30 +00:00
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var WhisperIdentityStr) =
|
2018-08-21 20:50:26 +00:00
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
let hexStr = n.getStr()
|
|
|
|
if not hexStr.isValidWhisperIdentity:
|
2018-11-28 22:04:57 +00:00
|
|
|
raise newException(ValueError, invalidMsg(argName) & " as a Whisper identity \"" & hexStr & "\"")
|
2018-08-21 21:21:30 +00:00
|
|
|
result = hexStr.WhisperIdentityStr
|
2018-08-21 20:50:26 +00:00
|
|
|
|
2018-11-23 17:21:03 +00:00
|
|
|
proc fromJson*(n: JsonNode, argName: string, result: var UInt256) =
|
|
|
|
n.kind.expect(JString, argName)
|
|
|
|
let hexStr = n.getStr()
|
2018-11-28 22:04:57 +00:00
|
|
|
if hexStr.len <= 66 and hexStr.isValidHexData:
|
|
|
|
raise newException(ValueError, invalidMsg(argName) & " as a UInt256 \"" & hexStr & "\"")
|
2018-11-23 17:21:03 +00:00
|
|
|
result = readUintBE[256](hexToPaddedByteArray[32](hexStr))
|
|
|
|
|