mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 08:03:14 +00:00
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:
parent
82e9b7e429
commit
763a758b0a
@ -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())
|
||||
|
||||
|
||||
@ -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.}
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user