Class TypedData

java.lang.Object
com.codename1.crypto.evm.TypedData

public final class TypedData extends Object

EIP-712 typed structured data: the eth_signTypedData_v4 document permits, orders and meta-transactions are signed as.

String jsonFromTheDapp = "{...}";           // the eth_signTypedData_v4 argument
byte[] privateKey = SecureRandom.bytes(32);
TypedData doc = TypedData.fromJson(jsonFromTheDapp);
byte[] signature = doc.sign(privateKey);      // r || s || v

The document is the JSON a dapp sends: types, primaryType, domain and message. When types omits EIP712Domain, it is derived from the domain fields present, as wallets do.

  • Constructor Details

    • TypedData

      public TypedData(Map<String, List<String[]>> types, String primaryType, Map<String,Object> domain, Map<String,Object> message)

      A document from its parts.

      Parameters
      • types: struct name to its members, each {name, type}, in declaration order

      • primaryType: the struct message is an instance of

      • domain: the EIP712Domain values

      • message: the values of the primary struct

  • Method Details

    • fromJson

      public static TypedData fromJson(String json)

      Reads the eth_signTypedData_v4 JSON document.

      Throws
      • IllegalArgumentException: for malformed JSON or a document missing a part
    • hash

      public byte[] hash()
      The 32-byte digest a wallet signs: keccak256(0x1901 || domainSeparator || hashStruct(message)).
    • domainSeparator

      public byte[] domainSeparator()
      hashStruct(EIP712Domain, domain).
    • messageHash

      public byte[] messageHash()
      The hashStruct of the message.
    • sign

      public byte[] sign(byte[] privateKey)
      Signs the document, answering r || s || v with v 27 or 28.
    • recover

      public EvmAddress recover(byte[] signature65)
      The address that signed the document.
    • encodeType

      public String encodeType(String type)
      The encodeType string of a struct, such as Mail(Person from,Person to,string contents)Person(string name,address wallet).
    • hashStruct

      public byte[] hashStruct(String type, Map<String,Object> value)
      keccak256(typeHash || encodeData(value)) of a struct.