Skip to content

Commit 87fb8c1

Browse files
encoding.nim int values encoded with 32Byte compatible Ethereum ABI
1 parent c74d9fc commit 87fb8c1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

web3/encoding.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# those terms.
99

1010
import
11-
std/macros,
11+
std/[macros, math],
1212
stint, ./eth_api_types, stew/[assign2, byteutils]
1313

1414
macro makeTypeEnum(): untyped =
@@ -71,7 +71,12 @@ macro makeTypeEnum(): untyped =
7171
makeTypeEnum()
7272

7373
func encode*[bits: static[int]](x: StUint[bits]): seq[byte] =
74-
@(x.toByteArrayBE())
74+
let numTargetBytes = bits div 8
75+
let paddingBytes = 32 - numTargetBytes ## the Ethereum ABI imposes a 32 byte width for every type
76+
let paddingZeros = newSeq[byte](paddingBytes)
77+
let ret = paddingZeros & @(x.toByteArrayBE())
78+
let retHex = ret.toHex()
79+
ret
7580

7681
func encode*[bits: static[int]](x: StInt[bits]): seq[byte] =
7782
@(x.toByteArrayBE())

0 commit comments

Comments
 (0)