Abi.encode VS abi.encodePacked

Hi, I’m trying to understand how to produce a sign of a message, looking at for instance the EIP-712.
Online I found an interesting demo (https://weijiekoh.github.io/eip712-signing-demo/index.html) but I didn’t understand when use the function ABI.encode and abi.encodePacked before hashing function.

Can you explain me the logic behind that?

Thank you very much

1 Like

Hi @bb_terk,

See the Solidity documentation for 0.5 breaking changes for the difference between abi.encode and abi.encodePacked

The ABI encoder now properly pads byte arrays and strings from calldata (msg.data and external function parameters) when used in external function calls and in abi.encode. For unpadded encoding, use abi.encodePacked.

abi.encodePacked is a non-standard packed mode

Through abi.encodePacked() , Solidity supports a non-standard packed mode where:

  • types shorter than 32 bytes are neither zero padded nor sign extended and
  • dynamic types are encoded in-place and without the length.
  • array elements are padded, but still encoded in-place

Furthermore, structs as well as nested arrays are not supported.

I am not sure why the tutorial they give uses a mix of abi.encode and abi.encodePacked rather than just using abi.encode

You could look at OpenZeppelin Contracts ECDSA to verify signed messages.

1 Like

Thank you for the answer.

Looking at the example in EIP-712 (https://github.com/ethereum/EIPs/blob/master/assets/eip-712/Example.js), I see that dynamic arrays/strings are not padded, instead of static types that are encoded with padding. Maybe the reason is that a dynamic array could be always different from another one, instead of a static type that could have the same rappresentation in memory. I don’t find another reason for that.

I’ll try OpenZeppeling sign methods, but for meta-transactions there is some implementations in Openzeppeling Library (other than Gas Network Station)? I am studying eip-712 because I would like to sign externally a message.

But more in general thank you again :slight_smile:

1 Like

Hi @bb_terk,

To see ECDSA being used, have a look at the GSN Strategy:

There is also a tutorial: Advanced GSN: GSNRecipientSignature.sol

1 Like

Thank you, I used the recover function correctly…but I don’t understand why is used encodePacked format instead of encode format :slight_smile:

1 Like