transactions – Role of ABI-Encoding in Digital Signatures and Contract Invocation

abibytecodecontract-invocationsignaturetransactions

I have two questions regarding the ABI.

1. When does it happen?

A digital signature is made by creating a transaction data structure, RLP-encoded serializing the data, hashing it through keccak-256, and signing it with ECDSA using a private key. At which point does ABI-encoding happen?

2. What does it encode?

Does ABI-encoding only happen for the contract method invoking data or also for things like the value, nonce, etc? If latter, does that mean the transaction parameters get RLP-encoded AND ABI-encoded?

Best Answer

The ABI or the Application Binary Interface is the standard to interact with the contracts. EVM uses the ABI encoded data to understand which part of the bytecode to execute.

When does it happen? A contract interaction is just another transaction on ethereum. The payload/what-to-do is in the data field of the transaction. ABI encoding encodes the user's parameters and function signature. Then you put this in the data field of the transaction. And then you sign the transaction as proof of ownership. Then the whole transaction data is RLP encoded.

What does it encode? ABI encoding is used only in contract interactions. It is used to compute the keccak256 hash of the target function selector and then the arguments you want to supply. The process returns raw bytes which are then put into the data field of the transaction.

The whole flow :

Sender :

  • Decide which function to execute at a contract address
  • ABI encode function selector and arguments
  • Put the ABI encoder's output raw bytes in the data field of transaction
  • Put all the other values, to, value and nonce
  • RLP encode these fields
  • Sign the transaction
  • Again RLP encode it and send

After travelling through some wires...

Receiver/Node :

  • RLP decode the received transaction
  • Verify if valid by checking the signature
  • RLP decode
  • Execute! (EVM internally handles the ABI decode and execution)

RLP encoding is the protocol used to communicate over channels i.e your mobile's data or Ethernet or etc.., it is the low level method used to convert data before sending through transmission. Aside from Ethereum, you can also use RLP encoding for something like Mobile communication. It's just another protocol.

Related Topic