Add simple validation for hex strings to web3_sha3

This commit is contained in:
coffeepots 2018-06-15 20:35:49 +01:00
parent d789ca6fee
commit a5467063b7

View File

@ -1,4 +1,4 @@
import ../rpcserver, nimcrypto, json, stint, strutils, ethtypes, stintjson import ../rpcserver, nimcrypto, json, stint, strutils, ethtypes, stintjson, ethutils
#[ #[
For details on available RPC calls, see: https://github.com/ethereum/wiki/wiki/JSON-RPC For details on available RPC calls, see: https://github.com/ethereum/wiki/wiki/JSON-RPC
@ -38,10 +38,10 @@ proc addEthRpcs*(server: RpcServer) =
## Returns the SHA3 result of the given string. ## Returns the SHA3 result of the given string.
# TODO: Capture error on malformed input # TODO: Capture error on malformed input
var rawData: seq[byte] var rawData: seq[byte]
if data.len > 2 and data[0] == '0' and data[1] in ['x', 'X']: if data.validateHexData:
rawData = data[2..data.high].fromHex rawData = data[2..data.high].fromHex
else: else:
rawData = data.fromHex raise newException(ValueError, "Invalid hex format")
# data will have 0x prefix # data will have 0x prefix
result = "0x" & $keccak_256.digest(rawData) result = "0x" & $keccak_256.digest(rawData)