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-07 12:41:54 +00:00
|
|
|
errors, constants, stint
|
2018-01-15 18:42:40 +00:00
|
|
|
|
2018-01-31 12:57:05 +00:00
|
|
|
proc validateCanonicalAddress*(value: string, title: string = "Value") =
|
2018-01-24 13:31:24 +00:00
|
|
|
# TODO
|
|
|
|
if false: #len(value) != 20:
|
2018-01-15 18:42:40 +00:00
|
|
|
raise newException(ValidationError,
|
2018-02-06 19:20:06 +00:00
|
|
|
&"{title} {value} is not a valid canonical address")
|
|
|
|
|
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}")
|