Skip to content

Commit

Permalink
eth/abi: raise error if numeric comes as string (q9f#114)
Browse files Browse the repository at this point in the history
* Revert "eth/abi: allow parsing numerics from string inputs (q9f#112)"

This reverts commit bb640c7.

* eth/abi: raise error if numeric comes as string
  • Loading branch information
q9f authored and mculp committed Jul 28, 2022
1 parent 5cdc89a commit 1d34b37
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/eth/abi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def signature(interface)

# Properly encodes unsigned integers.
def encode_uint(arg, type)
arg = arg.to_i if arg.is_a? String
raise ArgumentError, "Don't know how to handle this input." unless arg.is_a? Numeric
raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > Constant::UINT_MAX or arg < Constant::UINT_MIN
real_size = type.sub_type.to_i
i = arg.to_i
Expand All @@ -316,7 +316,7 @@ def encode_uint(arg, type)

# Properly encodes signed integers.
def encode_int(arg, type)
arg = arg.to_i if arg.is_a? String
raise ArgumentError, "Don't know how to handle this input." unless arg.is_a? Numeric
raise ValueOutOfBounds, "Number out of range: #{arg}" if arg > Constant::INT_MAX or arg < Constant::INT_MIN
real_size = type.sub_type.to_i
i = arg.to_i
Expand All @@ -332,13 +332,15 @@ def encode_bool(arg)

# Properly encodes unsigned fixed-point numbers.
def encode_ufixed(arg, type)
raise ArgumentError, "Don't know how to handle this input." unless arg.is_a? Numeric
high, low = type.sub_type.split("x").map(&:to_i)
raise ValueOutOfBounds, arg unless arg >= 0 and arg < 2 ** high
return Util.zpad_int((arg * 2 ** low).to_i)
end

# Properly encodes signed fixed-point numbers.
def encode_fixed(arg, type)
raise ArgumentError, "Don't know how to handle this input." unless arg.is_a? Numeric
high, low = type.sub_type.split("x").map(&:to_i)
raise ValueOutOfBounds, arg unless arg >= -2 ** (high - 1) and arg < 2 ** (high - 1)
i = (arg * 2 ** low).to_i
Expand Down
7 changes: 0 additions & 7 deletions spec/eth/abi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@
end
end

it "can encode numbers from string inputs" do
expect(Abi.encode(["uint256"], ["10000000000000000000000"])).to eq Abi.encode(["uint256"], [10000000000000000000000])
expect(Abi.decode(["uint256"], "00000000000000000000000000000000000000000000021e19e0c9bab2400000")).to eq [10000000000000000000000]
expect(Abi.encode(["int256"], ["20000000000000000000000"])).to eq Abi.encode(["int256"], [20000000000000000000000])
expect(Abi.decode(["int256"], "00000000000000000000000000000000000000000000043c33c1937564800000")).to eq [20000000000000000000000]
end

it "can do encode and decode complex types" do
types = [
"bool",
Expand Down

0 comments on commit 1d34b37

Please sign in to comment.