nim-contract-abi/contractabi/decoding.nim

127 lines
3.8 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
2021-11-30 14:14:57 +00:00
import pkg/upraises
2021-11-30 15:22:52 +00:00
import ./encoding
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
dynamic: bool
Padding = enum
padLeft,
padRight
UInt = SomeUnsignedInt | StUint
func init(_: type Tuple, offset: int, dynamic: bool): Tuple =
Tuple(start: offset, index: offset, dynamic: dynamic)
2021-11-30 14:14:57 +00:00
func init*(_: type AbiDecoder, bytes: seq[byte], offset=0): AbiDecoder =
AbiDecoder(bytes: bytes, stack: @[Tuple.init(offset, dynamic=false)])
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 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) =
decoder.index += amount
decoder.updateLast(decoder.index)
2021-11-30 14:14:57 +00:00
func read(decoder: var AbiDecoder, amount: int, padding = padLeft): seq[byte] =
let padlen = (32 - amount mod 32) mod 32
if padding == padLeft:
decoder.advance(padlen)
result = decoder.bytes[decoder.index..<decoder.index+amount]
decoder.advance(amount)
if padding == padRight:
decoder.advance(padlen)
func read*(decoder: var AbiDecoder, T: type): T =
decoder.decode(T)
func decode*(decoder: var AbiDecoder, T: type UInt): T =
2021-11-30 14:14:57 +00:00
T.fromBytesBE(decoder.read(sizeof(T)))
func decode*(decoder: var AbiDecoder, T: type bool): T =
2021-11-30 14:14:57 +00:00
decoder.read(uint8) != 0
func decode*(decoder: var AbiDecoder, T: type enum): T =
2021-11-30 14:14:57 +00:00
T(decoder.read(uint64))
func decode*[I](decoder: var AbiDecoder, T: type array[I, byte]): T =
result[0..<result.len] = decoder.read(result.len, padRight)
2021-11-30 14:14:57 +00:00
func readOffset(decoder: var AbiDecoder): int =
let offset = decoder.read(uint64)
decoder.currentTuple.start + offset.int
func readTail(decoder: var AbiDecoder, T: type seq[byte]): T =
let offset = decoder.readOffset()
var tailDecoder = AbiDecoder.init(decoder.bytes, offset)
result = tailDecoder.read(T)
decoder.updateLast(tailDecoder.index)
2021-11-30 14:14:57 +00:00
func decode*(decoder: var AbiDecoder, T: type seq[byte]): T =
2021-11-30 14:14:57 +00:00
if decoder.currentTuple.dynamic:
decoder.readTail(T)
else:
let len = decoder.read(uint64).int
decoder.read(len, padRight)
2021-11-30 14:14:57 +00:00
func startTuple*(decoder: var AbiDecoder, dynamic: bool) =
var start: int
if decoder.currentTuple.dynamic and dynamic:
start = decoder.readOffset()
else:
start = decoder.index
decoder.stack.add(Tuple.init(start, dynamic))
2021-11-30 14:14:57 +00:00
func finishTuple*(decoder: var AbiDecoder) =
doAssert decoder.stack.len > 1, "unable to finish a tuple that hasn't started"
let tupl = decoder.stack.pop()
if not tupl.dynamic:
decoder.index = tupl.index
2021-11-30 14:14:57 +00:00
func finish*(decoder: var AbiDecoder) =
doAssert decoder.stack.len == 1, "not all tuples were finished"
doAssert decoder.last == decoder.bytes.len, "unread trailing bytes found"
doAssert decoder.last mod 32 == 0, "encoding variant broken"
2021-11-30 14:14:57 +00:00
func decode*[T](decoder: var AbiDecoder, _: type seq[T]): seq[T] =
2021-11-30 15:22:52 +00:00
let len = decoder.read(uint64)
decoder.startTuple(dynamic=true)
for _ in 0..<len:
result.add(decoder.read(T))
decoder.finishTuple()
func decode*[I,T](decoder: var AbiDecoder, _: type array[I,T]): array[I,T] =
2021-11-30 15:22:52 +00:00
const dynamic = AbiEncoder.isDynamic(T)
decoder.startTuple(dynamic)
for i in 0..<result.len:
result[i] = decoder.read(T)
decoder.finishTuple()
func decode*(decoder: var AbiDecoder, T: type string): T =
2021-11-30 15:23:24 +00:00
string.fromBytes(decoder.read(seq[byte]))
2021-11-30 14:14:57 +00:00
func decode*(_: type AbiDecoder, bytes: seq[byte], T: type): T =
var decoder = AbiDecoder.init(bytes)
result = decoder.decode(T)
2021-11-30 14:14:57 +00:00
decoder.finish()