Fix ABI encoding of Ethereum Addresses

They should be encoded as if they were UInt160, which means they
should be left-padded with zeroes, instead of right-padded.
This commit is contained in:
Mark Spanbroek 2021-02-23 10:23:42 +01:00
parent 82e9b7e429
commit 763a758b0a
4 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import pkg/stew/endians2
import pkg/stint
import ./types
type
Abi* = object
@ -61,6 +62,9 @@ proc write*(writer: var AbiWriter, bytes: seq[byte]) =
else:
writer.writeLater(bytes)
proc write*(writer: var AbiWriter, address: EthAddress) =
writer.padleft(address.toArray)
proc startTuple*(writer: var AbiWriter) =
writer.tuples.add(Tuple())

View File

@ -5,4 +5,9 @@ export stint
type
UInt48* = range[0'u64..2'u64^48-1]
EthAddress* = array[20, byte]
EthAddress* = distinct array[20, byte]
proc toArray*(address: EthAddress): array[20, byte] =
array[20, byte](address)
proc `==`*(a, b: EthAddress): bool {.borrow.}

View File

@ -24,6 +24,9 @@ proc example*(_: type UInt256): UInt256 =
proc example*(_: type UInt128): UInt128 =
UInt128.fromBytes(array[16, byte].example)
proc example*(_: type EthAddress): EthAddress =
EthAddress(array[20, byte].example)
proc example*(_: type Channel): Channel =
Channel(
nonce: UInt48.example,

View File

@ -1,5 +1,6 @@
import std/unittest
import pkg/nitro/abi
import pkg/nitro/types
import pkg/stint
import ./examples
@ -61,6 +62,10 @@ suite "ABI encoding":
let bytes33len = Abi.encode(bytes33.len.uint64)
check Abi.encode(bytes33) == bytes33len & bytes33 & 31.zeroes
test "encodes ethereum addresses":
let address = EthAddress.example
check Abi.encode(address) == 12.zeroes & @(address.toArray)
test "encodes tuples":
let a = true
let b = @[1'u8, 2'u8, 3'u8]