Skip to content

Commit

Permalink
eth/abi: fix handling of hex values for byte strings (q9f#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
q9f authored and mculp committed Jul 28, 2022
1 parent 2ec5311 commit cb1b268
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/eth/abi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ def encode_fixed(arg, type)
# Properly encodes byte-strings.
def encode_bytes(arg, type)
raise EncodingError, "Expecting String: #{arg}" unless arg.instance_of? String

if Util.is_prefixed? arg
arg = Util.remove_hex_prefix arg
arg = Util.hex_to_bin arg
end

if type.sub_type.empty?
size = Util.zpad_int arg.size
padding = Constant::BYTE_ZERO * (Util.ceil32(arg.size) - arg.size)
Expand Down
4 changes: 4 additions & 0 deletions spec/eth/abi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@
expect(Abi.encode_type t_address, "0x" + "ff" * 20).to eq Util.zpad("\xff" * 20, 32)
end

it "can handle hex-strings for bytes types" do
expect(Abi.encode ["bytes4"], ["0x80ac58cd"]).to eq "\x80\xACX\xCD#{"\x00" * 28}"
end

it "can decode types" do

# https://github.com/cryptape/ruby-ethereum-abi/blob/90d4fa3fc6b568581165eaacdc506b9b9b49e520/test/abi_test.rb#L105
Expand Down

0 comments on commit cb1b268

Please sign in to comment.