Skip to content

Commit

Permalink
eth/abi: allow encoding address types (#242)
Browse files Browse the repository at this point in the history
* eth/abi: allow encoding address types

* eth/abi: add test
  • Loading branch information
q9f committed Jun 16, 2023
1 parent 4b09acc commit 6f19a28
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/eth/abi/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ def hash(arg, type)

# Properly encodes addresses.
def address(arg)
if arg.is_a? Integer
if arg.is_a? Address

# from checksummed address with 0x prefix
Util.zpad_hex arg.to_s[2..-1]
elsif arg.is_a? Integer

# address from integer
Util.zpad_int arg
Expand All @@ -267,11 +271,11 @@ def address(arg)
Util.zpad arg, 32
elsif arg.size == 40

# address from hexadecimal address with 0x prefix
# address from hexadecimal address
Util.zpad_hex arg
elsif arg.size == 42 and arg[0, 2] == "0x"

# address from hexadecimal address
# address from hexadecimal address with 0x prefix
Util.zpad_hex arg[2..-1]
else
raise EncodingError, "Could not parse address: #{arg}"
Expand Down
1 change: 1 addition & 0 deletions spec/eth/abi/encoder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
expect(Abi::Encoder.type t_address, "\xff" * 20).to eq Util.zpad("\xff" * 20, 32)
expect(Abi::Encoder.type t_address, "ff" * 20).to eq Util.zpad("\xff" * 20, 32)
expect(Abi::Encoder.type t_address, "0x" + "ff" * 20).to eq Util.zpad("\xff" * 20, 32)
expect(Abi::Encoder.type t_address, Address.new("0x" + "ff" * 20)).to eq Util.zpad("\xff" * 20, 32)
end

it "can encode primitive types" do
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/ethereum/tests
Submodule tests updated 40 files
+20 −45 BlockchainTests/GeneralStateTests/Shanghai/stEIP3860-limitmeterinitcode/creationTxInitCodeSizeLimit.json
+44 −182 BlockchainTests/GeneralStateTests/stCreateTest/CreateTransactionHighNonce.json
+35,940 −102,364 BlockchainTests/GeneralStateTests/stEIP1559/intrinsic.json
+21 −97 BlockchainTests/GeneralStateTests/stEIP1559/lowFeeCap.json
+60 −212 BlockchainTests/GeneralStateTests/stEIP1559/lowGasLimit.json
+42 −194 BlockchainTests/GeneralStateTests/stEIP1559/lowGasPriceOldTypes.json
+72 −300 BlockchainTests/GeneralStateTests/stEIP1559/outOfFunds.json
+144 −600 BlockchainTests/GeneralStateTests/stEIP1559/outOfFundsOldTypes.json
+21 −97 BlockchainTests/GeneralStateTests/stEIP1559/tipTooHigh.json
+21 −97 BlockchainTests/GeneralStateTests/stEIP1559/transactionIntinsicBug.json
+16 −44 BlockchainTests/GeneralStateTests/stEIP1559/typeTwoBerlin.json
+180 −636 BlockchainTests/GeneralStateTests/stEIP1559/valCausesOOF.json
+94 −352 BlockchainTests/GeneralStateTests/stEIP3607/transactionCollidingWithNonEmptyAccount_calls.json
+22 −91 BlockchainTests/GeneralStateTests/stEIP3607/transactionCollidingWithNonEmptyAccount_callsItself.json
+376 −1,408 BlockchainTests/GeneralStateTests/stEIP3607/transactionCollidingWithNonEmptyAccount_init.json
+22 −91 BlockchainTests/GeneralStateTests/stEIP3607/transactionCollidingWithNonEmptyAccount_send.json
+160 −608 BlockchainTests/GeneralStateTests/stExample/accessListExample.json
+72 −324 BlockchainTests/GeneralStateTests/stExample/basefeeExample.json
+72 −324 BlockchainTests/GeneralStateTests/stExample/eip1559.json
+30 −120 BlockchainTests/GeneralStateTests/stExample/invalidTr.json
+68 −68 BlockchainTests/GeneralStateTests/stExample/solidityExample.json
+96 −248 BlockchainTests/GeneralStateTests/stSpecialTest/eoaEmpty.json
+108 −436 BlockchainTests/GeneralStateTests/stTransactionTest/HighGasPrice.json
+96 −424 BlockchainTests/GeneralStateTests/stTransactionTest/ValueOverflow.json
+4,627 −0 EIPTests/BlockchainTests/StateTests/stEIP1153-transientStorage/transStorageOK.json
+4,250 −0 EIPTests/BlockchainTests/StateTests/stEIP1153-transientStorage/transStorageReset.json
+69 −71 EIPTests/BlockchainTests/StateTests/stEOF/stEIP3540/EOF1_Calls.json
+342 −0 EIPTests/StateTests/stEIP1153-transientStorage/transStorageOK.json
+437 −0 EIPTests/StateTests/stEIP1153-transientStorage/transStorageReset.json
+7 −7 EIPTests/StateTests/stEOF/stEIP3540/EOF1_Calls.json
+4 −4 GeneralStateTests/stExample/accessListExample.json
+10 −10 GeneralStateTests/stExample/solidityExample.json
+17 −17 GeneralStateTests/stTransactionTest/HighGasPrice.json
+4 −4 GeneralStateTests/stTransactionTest/ValueOverflow.json
+1,057 −0 src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/transStorageOKFiller.yml
+395 −0 src/EIPTestsFiller/StateTests/stEIP1153-transientStorage/transStorageResetFiller.yml
+2 −2 src/EIPTestsFiller/StateTests/stEOF/stEIP3540/EOF1_CallsFiller.yml
+6 −21 src/GeneralStateTestsFiller/stExample/accessListExampleFiller.yml
+7 −15 src/GeneralStateTestsFiller/stTransactionTest/HighGasPriceFiller.yml
+5 −13 src/GeneralStateTestsFiller/stTransactionTest/ValueOverflowFiller.yml

0 comments on commit 6f19a28

Please sign in to comment.