module Eth::Eip712

def encode_data(primary_type, data, types)

Returns:
  • (String) - an ABI-encoded representation of the data and the types.

Parameters:
  • types (Array) -- all existing types in the data structure.
  • data (Array) -- the data in the data structure we want to encode.
  • primary_type (String) -- the primary type which we want to encode.
def encode_data(primary_type, data, types)
  # first data field is the type hash
  encoded_types = ["bytes32"]
  encoded_values = [hash_type(primary_type, types)]
  # adds field contents
  types[primary_type.to_sym].each do |field|
    value = data[field[:name].to_sym]
    type = field[:type]
    raise NotImplementedError, "Arrays currently unimplemented for EIP-712." if type.end_with? "]"
    if type == "string" or type == "bytes"
      encoded_types.push "bytes32"
      encoded_values.push Util.keccak256 value
    elsif !types[type.to_sym].nil?
      encoded_types.push "bytes32"
      value = encode_data type, value, types
      encoded_values.push Util.keccak256 value
    else
      encoded_types.push type
      encoded_values.push value
    end
  end
  # all data is abi-encoded
  return Abi.encode encoded_types, encoded_values
end