2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-01-15 18:42:40 +00:00
|
|
|
import
|
|
|
|
strformat,
|
2018-05-30 16:11:15 +00:00
|
|
|
errors, constants, stint, eth_common
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
template validateCanonicalAddress*(value: EthAddress, title: string = "Value") =
|
|
|
|
# TODO: This needs to be removed
|
|
|
|
discard
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-06 19:20:06 +00:00
|
|
|
proc validateGte*(value: Int256 | int, minimum: int, title: string = "Value") =
|
|
|
|
if value.i256 < minimum.i256:
|
|
|
|
raise newException(ValidationError,
|
|
|
|
&"{title} {value} is not greater than or equal to {minimum}")
|
|
|
|
|
|
|
|
proc validateGt*(value: Int256 | int, minimum: int, title: string = "Value") =
|
|
|
|
if value.i256 <= minimum.i256:
|
|
|
|
raise newException(ValidationError,
|
|
|
|
&"{title} {value} is not greater than {minimum}")
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-02-06 19:20:06 +00:00
|
|
|
proc validateLength*[T](values: seq[T], size: int) =
|
|
|
|
if values.len != size:
|
2018-01-17 14:16:00 +00:00
|
|
|
raise newException(ValidationError,
|
2018-02-06 19:20:06 +00:00
|
|
|
&"seq expected {size} len, got {values.len}")
|
2018-01-17 14:16:00 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc validateLte*(value: UInt256 | int, maximum: int, title: string = "Value") =
|
|
|
|
if value.u256 > maximum.u256:
|
2018-01-30 18:12:05 +00:00
|
|
|
raise newException(ValidationError,
|
2018-02-06 19:20:06 +00:00
|
|
|
&"{title} {value} is not less or equal to {maximum}")
|
2018-01-30 18:12:05 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc validateLt*(value: UInt256 | int, maximum: int, title: string = "Value") =
|
|
|
|
if value.u256 >= maximum.u256:
|
2018-01-15 18:42:40 +00:00
|
|
|
raise newException(ValidationError,
|
2018-02-06 19:20:06 +00:00
|
|
|
&"{title} {value} is not less than {maximum}")
|
2018-01-17 14:16:00 +00:00
|
|
|
|
2018-02-06 19:20:06 +00:00
|
|
|
proc validateStackItem*(value: string) =
|
|
|
|
if value.len > 32:
|
2018-01-17 14:16:00 +00:00
|
|
|
raise newException(ValidationError,
|
2018-02-06 19:20:06 +00:00
|
|
|
&"Invalid stack item: expected 32 bytes, got {value.len}: value is {value}")
|
2018-02-20 17:27:43 +00:00
|
|
|
|
|
|
|
proc validateStackItem*(value: string | seq[byte]) =
|
|
|
|
if value.len > 32:
|
|
|
|
raise newException(ValidationError,
|
|
|
|
&"Invalid stack item: expected 32 bytes, got {value.len}: value is {value}")
|