diff --git a/contractabi.nimble b/contractabi.nimble index 323c465..5acf781 100644 --- a/contractabi.nimble +++ b/contractabi.nimble @@ -4,4 +4,5 @@ description = "ABI Encoding for Ethereum contracts" license = "MIT" requires "stint" +requires "stew" requires "upraises >= 0.1.0 & < 0.2.0" diff --git a/contractabi/encoding.nim b/contractabi/encoding.nim index ae03b01..7174d7e 100644 --- a/contractabi/encoding.nim +++ b/contractabi/encoding.nim @@ -1,5 +1,6 @@ import pkg/stint import pkg/upraises +import pkg/stew/byteutils export stint @@ -110,6 +111,9 @@ func encode[T](encoder: var AbiEncoder, value: seq[T]) = encoder.finishTuple() encoder.setDynamic() +func encode(encoder: var AbiEncoder, value: string) = + encoder.encode(value.toBytes) + func write*[T](encoder: var AbiEncoder, value: T) = var writer = AbiEncoder.init() writer.encode(value) diff --git a/tests/testEncoding.nim b/tests/testEncoding.nim index 993b51e..03d001b 100644 --- a/tests/testEncoding.nim +++ b/tests/testEncoding.nim @@ -1,6 +1,7 @@ import std/unittest import pkg/stint +import pkg/stew/byteutils import contractabi import ./examples @@ -154,5 +155,8 @@ suite "ABI encoding": AbiEncoder.encode(32'u8) & # offset in tuple AbiEncoder.encode(a) + test "encodes strings as UTF-8 byte sequence": + check AbiEncoder.encode("hello!☺") == AbiEncoder.encode("hello!☺".toBytes) + # https://medium.com/b2expand/abi-encoding-explanation-4f470927092d # https://docs.soliditylang.org/en/v0.8.1/abi-spec.html#formal-specification-of-the-encoding