From 81d52cb3cb56bf662e67ffefb714268516510fc9 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Mon, 18 Jun 2018 18:31:11 +0100 Subject: [PATCH] Add validated hex string type --- tests/ethhexstrings.nim | 151 ++++++++++++++++++++++++++++++++++++++++ tests/ethprocs.nim | 11 ++- tests/ethutils.nim | 43 ------------ tests/testethcalls.nim | 2 +- 4 files changed, 156 insertions(+), 51 deletions(-) create mode 100644 tests/ethhexstrings.nim delete mode 100644 tests/ethutils.nim diff --git a/tests/ethhexstrings.nim b/tests/ethhexstrings.nim new file mode 100644 index 0000000..50529bf --- /dev/null +++ b/tests/ethhexstrings.nim @@ -0,0 +1,151 @@ +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] + +proc encodeQuantity*(value: SomeUnsignedInt): string = + var hValue = value.toHex.stripLeadingZeros + result = "0x" & hValue + +template hasHexHeader*(value: string | HexDataStr | HexQuantityStr): bool = + template strVal: untyped = value.string + if strVal[0] == '0' and strVal[1] in {'x', 'X'} and strVal.len > 2: true + else: false + +template isHexChar*(c: char): bool = + if c notin {'0'..'9'} and + c notin {'a'..'f'} and + c notin {'A'..'F'}: false + else: true + +proc validate*(value: HexQuantityStr): bool = + template strVal: untyped = value.string + if not value.hasHexHeader: + return false + # No leading zeros + if strVal[2] == '0': return false + for i in 2.. string: + server.rpc("web3_sha3") do(data: HexDataStr) -> HexDataStr: ## Returns Keccak-256 (not the standardized SHA3-256) of the given data. ## ## data: the data to convert into a SHA3 hash. ## Returns the SHA3 result of the given string. # TODO: Capture error on malformed input var rawData: seq[byte] - if data.validateHexData: - rawData = data[2..data.high].fromHex - else: - raise newException(ValueError, "Invalid hex format") + rawData = data.string.fromHex # data will have 0x prefix - result = "0x" & $keccak_256.digest(rawData) + result = hexDataStr "0x" & $keccak_256.digest(rawData) server.rpc("net_version") do() -> string: ## Returns string of the current network id: diff --git a/tests/ethutils.nim b/tests/ethutils.nim deleted file mode 100644 index 22e8a61..0000000 --- a/tests/ethutils.nim +++ /dev/null @@ -1,43 +0,0 @@ -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] - -proc encodeQuantity*(value: SomeUnsignedInt): string = - var hValue = value.toHex.stripLeadingZeros - result = "0x" & hValue - -template hasHexHeader*(value: string): bool = - if value[0] == '0' and value[1] in {'x', 'X'} and value.len > 2: true - else: false - -template isHexChar*(c: char): bool = - if c notin {'0'..'9'} and - c notin {'a'..'f'} and - c notin {'A'..'F'}: false - else: true - -proc validateHexQuantity*(value: string): bool = - if not value.hasHexHeader: - return false - # No leading zeros - if value[2] == '0': return false - for i in 2..