Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Eth:Tx.decode for transaction with s length < 64 chars #148

Merged
merged 2 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/eth/tx/eip1559.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def decode(hex)

# recover sender address
v = Chain.to_v recovery_id, chain_id
public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v.to_s(16)}", chain_id)
public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v.to_s(16)}", chain_id)
address = Util.public_key_to_address(public_key).to_s
@sender = Tx.sanitize_address address
end
Expand Down
2 changes: 1 addition & 1 deletion lib/eth/tx/eip2930.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def decode(hex)

# recover sender address
v = Chain.to_v recovery_id, chain_id
public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v.to_s(16)}", chain_id)
public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v.to_s(16)}", chain_id)
address = Util.public_key_to_address(public_key).to_s
@sender = Tx.sanitize_address address
end
Expand Down
2 changes: 1 addition & 1 deletion lib/eth/tx/legacy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def decode(hex)
unless chain_id.nil?

# recover sender address
public_key = Signature.recover(unsigned_hash, "#{r}#{s}#{v}", chain_id)
public_key = Signature.recover(unsigned_hash, "#{r.rjust(64, "0")}#{s.rjust(64, "0")}#{v}", chain_id)
address = Util.public_key_to_address(public_key).to_s
@sender = Tx.sanitize_address address
else
Expand Down
13 changes: 13 additions & 0 deletions spec/eth/tx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,17 @@
expect(tx.chain_id).to eq 4901
end
end

describe ".decode transaction with small s" do
it "transaction with s with length 62" do
raw = "0xf86b820e8485012a05f200831e848094ffe811714ab35360b67ee195ace7c10d93f89d8c80844e71d92d8194a07b8f34a8fb85d850b3be4fc0330382e125e4216df5598c6d2c3bc47954684cf99f35ef53ee007c2f705eca91448b5c86e81d10f659ad868409bac8197bba9814"
tx = Tx.decode raw
expect(tx.sender).to eq Util.remove_hex_prefix Address.new("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266").to_s
expect(tx.destination).to eq Util.remove_hex_prefix "0xffe811714ab35360b67ee195ace7c10d93f89d8c"
expect(tx.amount).to eq 0
expect(tx.hash).to eq Util.remove_hex_prefix "0x061bff624de0bdd20f557c02b6fbab92ca436871ff31f69ffdd6dc830a8e9709"
expect(tx.signature_v).to eq "94"
expect(tx.chain_id).to eq 56
end
end
end