Encode empty transaction fields

addressesencodingraw-transactionrlptransactions

I want to RLP-encode a(n unsigned) transaction. Some fields of the transaction can be empty/null/zero. I want to know how they are encoded, though.

Let's assume the following transaction fields:

signer_nonce = 0
gas_price = 0
gas_limit = 21000
destination = nil
amount = 0
payload = nil

Nonce, gas price, limit, and amount are easily encoded using any RLP-encoder and a value of 0x00 is perfectly fine.

But how do I encode empty fields if I don't have any data or any recipient address?

Eth::Util.bin_to_hex(RLP.encode(nil))
/home/user/.gem/ruby/3.0.0/gems/rlp-0.7.3/lib/rlp/sedes.rb:31:in `infer': Did not find sedes handling type NilClass (TypeError)
    from /home/user/.gem/ruby/3.0.0/gems/rlp-0.7.3/lib/rlp/encode.rb:51:in `encode'
    from (irb):14:in `<main>'
    from /usr/lib/ruby/gems/3.0.0/gems/irb-1.3.6/exe/irb:11:in `<top (required)>'
    from /bin/irb:23:in `load'
    from /bin/irb:23:in `<main>'

I cannot set the address to 0x00 as this would be a valid address afaik. Should I use the empty string byte 0x80? How would I distinguish and empty string from the zero byte 0x80?

Eth::Util.bin_to_hex(RLP.encode(""))
=> "80"
Eth::Util.bin_to_hex(RLP.encode(0))
=> "80"

How to encode empty transaction fields such as empty data or no recipient address?

Best Answer

All of these field have to be provided and should not be nil. There are generally 2 different types when encoding a transaction with RLP: 256-bit Number and a Bytes.

So if we have an empty data we would encode it as an empty bytes (e.g. 0x). In this case an empty string and an empty bytes are the same, as a string would be converted to bytes before RLP encoding.

For the address it is a little bit more tricky. It is defined as a Bytes that has a length of 20. Unless you have a creation transaction then it is an empty bytes.

You can find an example how this is implemented in the Kethereum repositors:

You can find the more formal definition in the Ethereum Yellow Paper at the beginning of page 5.

A list of all fields that should be encoded in a Transaction can for example be found in EIP-1559 for the latest format.

Related Topic