2021-11-30 14:14:57 +00:00
|
|
|
import pkg/stint
|
|
|
|
import pkg/stew/endians2
|
2021-11-30 15:23:24 +00:00
|
|
|
import pkg/stew/byteutils
|
2021-12-01 12:01:28 +00:00
|
|
|
import pkg/questionable/results
|
2021-11-30 14:14:57 +00:00
|
|
|
import pkg/upraises
|
2021-11-30 15:22:52 +00:00
|
|
|
import ./encoding
|
2021-12-09 10:48:14 +00:00
|
|
|
import ./integers
|
2022-01-19 08:16:22 +00:00
|
|
|
import ./address
|
|
|
|
|
|
|
|
export stint
|
|
|
|
export address
|
2021-11-30 14:14:57 +00:00
|
|
|
|
|
|
|
push: {.upraises:[].}
|
|
|
|
|
|
|
|
type
|
|
|
|
AbiDecoder* = object
|
|
|
|
bytes: seq[byte]
|
|
|
|
stack: seq[Tuple]
|
2021-12-01 09:59:31 +00:00
|
|
|
last: int
|
2021-11-30 14:14:57 +00:00
|
|
|
Tuple = object
|
|
|
|
start: int
|
2021-12-01 09:59:31 +00:00
|
|
|
index: int
|
2021-11-30 14:14:57 +00:00
|
|
|
Padding = enum
|
|
|
|
padLeft,
|
|
|
|
padRight
|
|
|
|
UInt = SomeUnsignedInt | StUint
|
2021-12-09 10:48:14 +00:00
|
|
|
Int = SomeSignedInt | StInt
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func read*(decoder: var AbiDecoder, T: type): ?!T
|
2021-12-01 11:09:42 +00:00
|
|
|
|
2021-12-02 10:13:50 +00:00
|
|
|
func init(_: type Tuple, offset: int): Tuple =
|
|
|
|
Tuple(start: offset, index: offset)
|
2021-12-01 09:59:31 +00:00
|
|
|
|
2021-12-01 11:09:42 +00:00
|
|
|
func init(_: type AbiDecoder, bytes: seq[byte], offset=0): AbiDecoder =
|
2021-12-02 10:13:50 +00:00
|
|
|
AbiDecoder(bytes: bytes, stack: @[Tuple.init(offset)])
|
2021-11-30 14:14:57 +00:00
|
|
|
|
|
|
|
func currentTuple(decoder: var AbiDecoder): var Tuple =
|
|
|
|
decoder.stack[^1]
|
|
|
|
|
2021-12-01 09:59:31 +00:00
|
|
|
func index(decoder: var AbiDecoder): var int =
|
|
|
|
decoder.currentTuple.index
|
|
|
|
|
|
|
|
func `index=`(decoder: var AbiDecoder, value: int) =
|
|
|
|
decoder.currentTuple.index = value
|
|
|
|
|
2021-12-02 10:21:06 +00:00
|
|
|
func startTuple(decoder: var AbiDecoder): ?!void =
|
|
|
|
decoder.stack.add(Tuple.init(decoder.index))
|
|
|
|
success()
|
|
|
|
|
|
|
|
func finishTuple(decoder: var AbiDecoder) =
|
|
|
|
doAssert decoder.stack.len > 1, "unable to finish a tuple that hasn't started"
|
|
|
|
let tupl = decoder.stack.pop()
|
|
|
|
decoder.index = tupl.index
|
|
|
|
|
2021-12-01 09:59:31 +00:00
|
|
|
func updateLast(decoder: var AbiDecoder, index: int) =
|
|
|
|
if index > decoder.last:
|
|
|
|
decoder.last = index
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func advance(decoder: var AbiDecoder, amount: int): ?!void =
|
2021-11-30 14:14:57 +00:00
|
|
|
decoder.index += amount
|
2021-12-01 09:59:31 +00:00
|
|
|
decoder.updateLast(decoder.index)
|
2021-12-01 15:34:02 +00:00
|
|
|
if decoder.index <= decoder.bytes.len:
|
|
|
|
success()
|
|
|
|
else:
|
|
|
|
failure "reading past end"
|
|
|
|
|
|
|
|
func skipPadding(decoder: var AbiDecoder, amount: int): ?!void =
|
|
|
|
let index = decoder.index
|
|
|
|
?decoder.advance(amount)
|
|
|
|
for i in index..<index+amount:
|
2021-12-09 09:38:34 +00:00
|
|
|
if decoder.bytes[i] notin [0x00'u8, 0xFF'u8]:
|
2021-12-01 15:34:02 +00:00
|
|
|
return failure "invalid padding found"
|
2021-12-01 12:01:28 +00:00
|
|
|
success()
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func read(decoder: var AbiDecoder, amount: int, padding=padLeft): ?!seq[byte] =
|
2021-11-30 14:14:57 +00:00
|
|
|
let padlen = (32 - amount mod 32) mod 32
|
|
|
|
if padding == padLeft:
|
2021-12-01 15:34:02 +00:00
|
|
|
?decoder.skipPadding(padlen)
|
2021-12-01 12:01:28 +00:00
|
|
|
let index = decoder.index
|
|
|
|
?decoder.advance(amount)
|
|
|
|
result = success decoder.bytes[index..<index+amount]
|
2021-11-30 14:14:57 +00:00
|
|
|
if padding == padRight:
|
2021-12-01 15:34:02 +00:00
|
|
|
?decoder.skipPadding(padlen)
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type UInt): ?!T =
|
|
|
|
success T.fromBytesBE(?decoder.read(sizeof(T)))
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-09 10:48:14 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type Int): ?!T =
|
|
|
|
let unsigned = ?decoder.read(T.unsigned)
|
2021-12-09 09:38:34 +00:00
|
|
|
success cast[T](unsigned)
|
|
|
|
|
2021-12-01 16:16:40 +00:00
|
|
|
template basetype(Range: type range): untyped =
|
|
|
|
when Range isnot SomeUnsignedInt: {.error: "only uint ranges supported".}
|
|
|
|
elif sizeof(Range) == sizeof(uint8): uint8
|
|
|
|
elif sizeof(Range) == sizeof(uint16): uint16
|
|
|
|
elif sizeof(Range) == sizeof(uint32): uint32
|
|
|
|
elif sizeof(Range) == sizeof(uint64): uint64
|
|
|
|
else: {.error "unsupported range type".}
|
|
|
|
|
|
|
|
func decode(decoder: var AbiDecoder, T: type range): ?!T =
|
|
|
|
let bytes = ?decoder.read(sizeof(T))
|
|
|
|
let value = basetype(T).fromBytesBE(bytes)
|
|
|
|
if value in T.low..T.high:
|
|
|
|
success T(value)
|
|
|
|
else:
|
|
|
|
failure "value not in range"
|
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type bool): ?!T =
|
2021-12-01 15:34:02 +00:00
|
|
|
case ?decoder.read(uint8)
|
|
|
|
of 0: success false
|
|
|
|
of 1: success true
|
|
|
|
else: failure "invalid boolean value"
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type enum): ?!T =
|
2021-12-01 16:26:35 +00:00
|
|
|
let value = ?decoder.read(uint64)
|
|
|
|
if value in T.low.uint64..T.high.uint64:
|
|
|
|
success T(value)
|
|
|
|
else:
|
|
|
|
failure "invalid enum value"
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2022-01-19 08:16:22 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type Address): ?!T =
|
|
|
|
var bytes: array[20, byte]
|
|
|
|
bytes[0..<20] =(?decoder.read(20))[0..<20]
|
|
|
|
success T.init(bytes)
|
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode[I](decoder: var AbiDecoder, T: type array[I, byte]): ?!T =
|
|
|
|
var arr: T
|
|
|
|
arr[0..<arr.len] = ?decoder.read(arr.len, padRight)
|
|
|
|
success arr
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type seq[byte]): ?!T =
|
2021-12-02 10:13:50 +00:00
|
|
|
let len = ?decoder.read(uint64)
|
|
|
|
decoder.read(len.int, padRight)
|
2021-11-30 14:14:57 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode[T: tuple](decoder: var AbiDecoder, _: typedesc[T]): ?!T =
|
|
|
|
var tupl: T
|
2021-12-02 10:13:50 +00:00
|
|
|
?decoder.startTuple()
|
2021-12-01 12:01:28 +00:00
|
|
|
for element in tupl.fields:
|
|
|
|
element = ?decoder.read(typeof(element))
|
2021-12-01 10:40:12 +00:00
|
|
|
decoder.finishTuple()
|
2021-12-01 12:01:28 +00:00
|
|
|
success tupl
|
2021-12-01 10:40:12 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode[T](decoder: var AbiDecoder, _: type seq[T]): ?!seq[T] =
|
|
|
|
var sequence: seq[T]
|
|
|
|
let len = ?decoder.read(uint64)
|
2021-12-02 10:13:50 +00:00
|
|
|
?decoder.startTuple()
|
2021-11-30 15:22:52 +00:00
|
|
|
for _ in 0..<len:
|
2021-12-01 12:01:28 +00:00
|
|
|
sequence.add(?decoder.read(T))
|
2021-11-30 15:22:52 +00:00
|
|
|
decoder.finishTuple()
|
2021-12-01 12:01:28 +00:00
|
|
|
success sequence
|
2021-11-30 15:22:52 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode[I,T](decoder: var AbiDecoder, _: type array[I,T]): ?!array[I,T] =
|
|
|
|
var arr: array[I, T]
|
2021-12-02 10:13:50 +00:00
|
|
|
?decoder.startTuple()
|
2021-12-01 12:01:28 +00:00
|
|
|
for i in 0..<arr.len:
|
|
|
|
arr[i] = ?decoder.read(T)
|
2021-11-30 15:22:52 +00:00
|
|
|
decoder.finishTuple()
|
2021-12-01 12:01:28 +00:00
|
|
|
success arr
|
2021-11-30 15:22:52 +00:00
|
|
|
|
2021-12-01 12:01:28 +00:00
|
|
|
func decode(decoder: var AbiDecoder, T: type string): ?!T =
|
|
|
|
success string.fromBytes(?decoder.read(seq[byte]))
|
2021-11-30 15:23:24 +00:00
|
|
|
|
2021-12-02 10:13:50 +00:00
|
|
|
func readOffset(decoder: var AbiDecoder): ?!int =
|
|
|
|
let offset = ?decoder.read(uint64)
|
|
|
|
success decoder.currentTuple.start + offset.int
|
|
|
|
|
|
|
|
func readTail*(decoder: var AbiDecoder, T: type): ?!T =
|
|
|
|
let offset = ?decoder.readOffset()
|
|
|
|
var tailDecoder = AbiDecoder.init(decoder.bytes, offset.int)
|
|
|
|
result = tailDecoder.read(T)
|
|
|
|
decoder.updateLast(tailDecoder.last)
|
|
|
|
|
2021-12-01 16:44:14 +00:00
|
|
|
func read*(decoder: var AbiDecoder, T: type): ?!T =
|
2021-12-02 10:13:50 +00:00
|
|
|
const dynamic = AbiEncoder.isDynamic(typeof(!result))
|
|
|
|
if dynamic and decoder.stack.len > 1:
|
|
|
|
decoder.readTail(T)
|
|
|
|
else:
|
|
|
|
decoder.decode(T)
|
2021-12-02 10:21:06 +00:00
|
|
|
|
|
|
|
func finish(decoder: var AbiDecoder): ?!void =
|
|
|
|
doAssert decoder.stack.len == 1, "not all tuples were finished"
|
|
|
|
doAssert decoder.last mod 32 == 0, "encoding invariant broken"
|
|
|
|
if decoder.last != decoder.bytes.len:
|
|
|
|
failure "unread trailing bytes found"
|
|
|
|
else:
|
|
|
|
success()
|
|
|
|
|
|
|
|
func decode*(_: type AbiDecoder, bytes: seq[byte], T: type): ?!T =
|
|
|
|
var decoder = AbiDecoder.init(bytes)
|
|
|
|
var value = ?decoder.decode(T)
|
|
|
|
?decoder.finish()
|
|
|
|
success value
|