nim-contract-abi/contractabi/decoding.nim

192 lines
5.5 KiB
Nim
Raw Normal View History

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
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
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]
last: int
2021-11-30 14:14:57 +00:00
Tuple = object
start: int
index: int
2021-11-30 14:14:57 +00:00
Padding = enum
padLeft,
padRight
UInt = SomeUnsignedInt | StUint
Int = SomeSignedInt | StInt
2021-11-30 14:14:57 +00:00
func read*(decoder: var AbiDecoder, T: type): ?!T
func init(_: type Tuple, offset: int): Tuple =
Tuple(start: offset, index: offset)
func init(_: type AbiDecoder, bytes: seq[byte], offset=0): AbiDecoder =
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]
func index(decoder: var AbiDecoder): var int =
decoder.currentTuple.index
func `index=`(decoder: var AbiDecoder, value: int) =
decoder.currentTuple.index = value
func startTuple*(decoder: var AbiDecoder) =
decoder.stack.add(Tuple.init(decoder.index))
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
func updateLast(decoder: var AbiDecoder, index: int) =
if index > decoder.last:
decoder.last = index
2021-11-30 14:14:57 +00:00
func advance(decoder: var AbiDecoder, amount: int): ?!void =
2021-11-30 14:14:57 +00:00
decoder.index += amount
decoder.updateLast(decoder.index)
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]:
return failure "invalid padding found"
success()
2021-11-30 14:14:57 +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:
?decoder.skipPadding(padlen)
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:
?decoder.skipPadding(padlen)
2021-11-30 14:14:57 +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
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)
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"
func decode(decoder: var AbiDecoder, T: type bool): ?!T =
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
func decode(decoder: var AbiDecoder, T: type enum): ?!T =
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)
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
func decode(decoder: var AbiDecoder, T: type seq[byte]): ?!T =
let len = ?decoder.read(uint64)
decoder.read(len.int, padRight)
2021-11-30 14:14:57 +00:00
func decode[T: tuple](decoder: var AbiDecoder, _: typedesc[T]): ?!T =
var tupl: T
decoder.startTuple()
for element in tupl.fields:
element = ?decoder.read(typeof(element))
2021-12-01 10:40:12 +00:00
decoder.finishTuple()
success tupl
2021-12-01 10:40:12 +00:00
func decode[T](decoder: var AbiDecoder, _: type seq[T]): ?!seq[T] =
var sequence: seq[T]
let len = ?decoder.read(uint64)
decoder.startTuple()
2021-11-30 15:22:52 +00:00
for _ in 0..<len:
sequence.add(?decoder.read(T))
2021-11-30 15:22:52 +00:00
decoder.finishTuple()
success sequence
2021-11-30 15:22:52 +00:00
func decode[I,T](decoder: var AbiDecoder, _: type array[I,T]): ?!array[I,T] =
var arr: array[I, T]
decoder.startTuple()
for i in 0..<arr.len:
arr[i] = ?decoder.read(T)
2021-11-30 15:22:52 +00:00
decoder.finishTuple()
success arr
2021-11-30 15:22:52 +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
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)
func read*(decoder: var AbiDecoder, T: type): ?!T =
const dynamic = AbiEncoder.isDynamic(typeof(!result))
if dynamic and decoder.stack.len > 1:
decoder.readTail(T)
else:
decoder.decode(T)
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