nimbus-eth1/nimbus/rpc/hexstrings.nim

112 lines
3.4 KiB
Nim
Raw Normal View History

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
type
HexQuantityStr* = distinct string
HexDataStr* = distinct string
# 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]
func encodeQuantity*(value: SomeUnsignedInt): string {.inline.} =
2018-07-27 17:02:02 +00:00
var hValue = value.toHex.stripLeadingZeros
result = "0x" & hValue
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
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
func isValidHexQuantity*(value: string): bool =
if not value.hasHexHeader:
2018-07-27 17:02:02 +00:00
return false
# No leading zeros (but allow 0x0)
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
func isValidHexData*(value: string): bool =
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
const
SInvalidQuantity = "Invalid hex quantity format for Ethereum"
SInvalidData = "Invalid hex data format for Ethereum"
proc validateHexQuantity*(value: string) {.inline.} =
if unlikely(not value.isValidHexQuantity):
raise newException(ValueError, SInvalidQuantity & ": " & value)
proc validateHexData*(value: string) {.inline.} =
if unlikely(not value.isValidHexData):
raise newException(ValueError, SInvalidData & ": " & value)
2018-07-27 17:02:02 +00:00
# Initialisation
proc hexQuantityStr*(value: string): HexQuantityStr {.inline.} =
value.validateHexQuantity
result = value.HexQuantityStr
2018-07-27 17:02:02 +00:00
proc hexDataStr*(value: string): HexDataStr {.inline.} =
value.validateHexData
result = value.HexDataStr
2018-07-27 17:02:02 +00:00
# Converters for use in RPC
import json
from json_rpc/rpcserver import expect
proc `%`*(value: HexQuantityStr): JsonNode =
result = %(value.string)
proc `%`*(value: HexDataStr): 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)
let hexStr = n.getStr()
if not hexStr.isValidHexQuantity:
2018-07-31 16:46:40 +00:00
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as an Ethereum hex quantity \"" & hexStr & "\"")
result = hexStr.hexQuantityStr
2018-07-27 17:02:02 +00:00
proc fromJson*(n: JsonNode, argName: string, result: var HexDataStr) =
# Note that '0x' is stripped after validation
n.kind.expect(JString, argName)
let hexStr = n.getStr()
if not hexStr.isValidHexData:
2018-07-31 16:46:40 +00:00
raise newException(ValueError, "Parameter \"" & argName & "\" is not valid as a Ethereum data \"" & hexStr & "\"")
result = hexStr.hexDataStr
2018-07-27 17:02:02 +00:00