Skip to content

Return Ethers.Transaction.Unknown for unregistered transaction types - #308

Merged
alisinabh merged 10 commits into
mainfrom
feat/unknown-transaction-types
Nov 16, 2026
Merged

alisinabh merged 10 commits into
mainfrom
feat/unknown-transaction-types

Conversation

@alisinabh

@alisinabh alisinabh commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Ethers.Transaction.from_rpc_map/1 (and so Ethers.get_transaction/2 and the batch :get_transaction action) returned {:error, :unsupported_type} for any transaction type without a registered module, and Ethers.Transaction.decode/1 returned {:error, :unsupported_transaction_type}. On L2s such types are common (OP-stack deposit transactions, type 0x7e, are in every block), so code that scans blocks, looks up arbitrary hashes or reads raw transactions could not handle them.

Such transactions now come back as Ethers.Transaction.Unknown, which keeps them undecoded.

The struct

@enforce_keys [:type_id]
defstruct [:type_id, :rpc_map, :raw_payload, :metadata]
  • type_id: the numeric transaction type
  • rpc_map: the JSON-RPC transaction object exactly as the node returned it, set by from_rpc_map/1
  • raw_payload: the EIP-2718 TransactionPayload (the bytes after the type byte), undecoded, set by decode/1
  • metadata: an Ethers.Transaction.Metadata (block hash, block number, transaction index) read from the RPC map, the same struct Ethers.Transaction.Signed carries for registered types; nil when decoded from bytes

Design decisions

  • Bare minimum. Future transaction types cannot be anticipated, so no other field is decoded: hash, from, to, etc. are read from rpc_map (e.g. transaction.rpc_map["hash"]).
  • Two source fields, not one. rpc_map and raw_payload each have a single type; exactly one is set depending on where the transaction came from.
  • decode/1. A first byte in 0x01..0x7f without a registered module returns {:ok, %Unknown{type_id: byte, raw_payload: rest}} without validating the payload. A 0x00 first byte still returns {:error, :unsupported_transaction_type}: type 0 is legacy, which has no type byte (geth rejects it too).
  • Encoding and hashing. An Unknown with raw_payload encodes back to <<type_id, raw_payload::binary>> and transaction_hash/2 returns the keccak hash of those bytes, as for any EIP-2718 transaction. One with only rpc_map raises ArgumentError for both (the hash message points at rpc_map["hash"]). to_rpc_map/1 raises for both.
  • Signing fails clearly. Ethers.Transaction.Protocol is implemented with the real type_id/1 and type_envelope/1 (<<type_id>> for 0x00..0x7f), and to_rlp_list/2 raises Unsupported transaction type 0x7E: Ethers.Transaction.Unknown cannot be signed or converted to an RLP list, which is what Ethers.Signer.Local hits. Ethers.Signer.JsonRPC fails on to_rpc_map/1.
  • Type matching. The RPC type is parsed as an integer quantity and matched against each registered module's type_id/0. Types registered through the compile-time :transaction_types config take precedence over Unknown. Registered types decode exactly as before.
  • Ethers.Transaction.t() now includes Ethers.Transaction.Unknown.t(); t_payload() is unchanged.

Breaking change

  • Ethers.Transaction.from_rpc_map/1, Ethers.get_transaction/2 and the batch :get_transaction action return {:ok, %Ethers.Transaction.Unknown{}} for an unregistered transaction type instead of {:error, :unsupported_type}.
  • {:error, :unsupported_type} is no longer returned: a type that is not a hex quantity now returns {:error, :invalid_type}.
  • Ethers.Transaction.decode/1 returns {:ok, %Ethers.Transaction.Unknown{}} for an unregistered type byte instead of {:error, :unsupported_transaction_type} (still returned for a 0x00 type byte).

Edge inputs for the RPC type:

type Before After
"0x7e" (unregistered) {:error, :unsupported_type} {:ok, %Unknown{type_id: 0x7E}}
"0x0002" {:error, :unsupported_type} Eip1559
"0x" Legacy {:error, :invalid_type}
"0xzz" raised ArgumentError {:error, :invalid_type}
2, "7e" (not 0x hex) {:error, :unsupported_type} {:error, :invalid_type}
missing Legacy Legacy

Documented in CHANGELOG.md and guides/upgrading.md.

Tests and verification

  • RPC fixture: a verbatim eth_getTransactionByHash response for the OP Mainnet L1 attributes deposit transaction 0x47b7a389d253158f3f2cb367dfd23eba2f95f641a3b671651515a673628d7c50 (block 159588978), fetched from the public OP Mainnet RPC. Its fields match the OP-stack deposits specification and op-geth's RPC transaction encoding.
  • Raw fixture: the same transaction encoded as the spec defines it, 0x7e || rlp([sourceHash, from, to, mint, value, gas, isSystemTx, data]) (the public RPC does not expose raw transactions). decode/1 returns it as an Unknown, encode/1 round-trips it, and transaction_hash/2 returns the real OP Mainnet transaction hash.
  • Unit tests also cover string and atom keys, missing or invalid metadata, arbitrary type ids, :invalid_type, payloads that are not validated, the 0x00 type byte, the ArgumentError messages for encode/hash/to_rpc_map/signing, registered types unaffected, and Ethers.get_transaction/2, get_transaction!/2 and Ethers.batch/2 through a stub RPC client.
  • Checked by hand, not in the suite:
    • Against a temporary anvil --network optimism (1.6.0-nightly), a deposit transaction sent with eth_sendTransaction comes back from Ethers.get_transaction/2 and the batch action as an Unknown with the node's map. CI runs a single plain anvil, so no live test was added. (That anvil panics on debug_getRawTransaction for deposits, so the raw-bytes path could only be checked against the spec-encoded fixture.)
    • With a custom 0x7e module registered in :transaction_types (compiled in a scratch copy), from_rpc_map/1 returns the custom struct and other unregistered types still return Unknown.

Known follow-ups

  • Registered types still drop the node-reported hash and from from the RPC map.
  • Ethers.send_transaction/2 and Ethers.sign_transaction/2 take TxData or parameter maps; passing any transaction struct (an Unknown as much as an Eip1559) fails with a Collectable protocol error, and a type: Ethers.Transaction.Unknown override fails with an UndefinedFunctionError, as any module that is not a transaction type does today.

Closes #292

🤖 Generated with Claude Code

https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6

alisinabh and others added 4 commits November 15, 2026 18:13
Ethers.Transaction.from_rpc_map/1, and so Ethers.get_transaction/2 and
the batch :get_transaction action, returned {:error, :unsupported_type}
for any transaction type without a registered module. On L2s such types
are common (OP-stack deposit transactions are in every block), so code
scanning blocks or looking up arbitrary hashes could not read them.

Such transactions now decode into the read-only Ethers.Transaction.Unknown
passthrough struct: the numeric type id, the common fields (hash, from,
to, nonce, value, gas, input, chain_id) and block metadata decoded where
present, and the original RPC map in raw for type specific fields. Fields
with values that cannot be decoded are left nil instead of failing the
lookup. Encoding, hashing and to_rpc_map/1 raise an ArgumentError naming
the type. Registered types decode as before, and a type that is not a hex
quantity returns {:error, :invalid_type}.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Align Ethers.Transaction.Unknown with the library rule that every
returned address is EIP-55 checksummed: accept the 0X prefix like
Ethers.Utils.maybe_checksum_address/1 does, and test that from and to
come back checksummed whatever casing the node uses. Malformed values
still leave the field nil, with the original value kept in raw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
@alisinabh
alisinabh force-pushed the feat/unknown-transaction-types branch from fb9c4ad to 55390c4 Compare November 15, 2026 23:16
Comment thread lib/ethers/transaction/unknown.ex Resolved
Comment thread lib/ethers/transaction/unknown.ex Resolved
alisinabh and others added 6 commits November 15, 2026 20:27
Since future transaction types cannot be anticipated, Unknown no longer
guesses at common fields. It keeps only the type id, block metadata and
the transaction as it was read:

- rpc_map: the JSON-RPC object, unchanged, from from_rpc_map/1
- raw_payload: the EIP-2718 TransactionPayload, undecoded, from decode/1

Transaction.decode/1 now returns an Unknown for a first byte in
0x01..0x7f without a registered module instead of
{:error, :unsupported_transaction_type}; a 0x00 type byte is still
rejected since legacy transactions have no type byte. An Unknown with
raw_payload encodes back to its bytes and hashes like any EIP-2718
transaction. One read from an RPC map still raises ArgumentError on
encode/1 and transaction_hash/2, to_rpc_map/1 raises for both, and
signing raises instead of producing a transaction with an unknown
signature layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012hqvX8DbbP3mgXy5hVEDc6
@alisinabh
alisinabh merged commit 79817d1 into main Nov 16, 2026
4 checks passed
@alisinabh
alisinabh deleted the feat/unknown-transaction-types branch November 16, 2026 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return a passthrough struct for unknown transaction types (L2 deposit transactions etc.)

1 participant