Method: Eth::Eip712#encode_data

Defined in:
lib/eth/eip712.rb

#encode_data(primary_type, data, types) ⇒ String

Recursively ABI-encodes all data and types according to EIP-712.

Parameters:

  • primary_type (String)

    the primary type which we want to encode.

  • data (Array)

    the data in the data structure we want to encode.

  • types (Array)

    all existing types in the data structure.

Returns:

  • (String)

    an ABI-encoded representation of the data and the types.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/eth/eip712.rb', line 111

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]
    if type.end_with?("]")
      encoded_types.push type
      encoded_values.push encode_array(type, value, types)
    elsif type == "string" || type == "bytes" || !types[type.to_sym].nil?
      encoded_types.push "bytes32"
      encoded_values.push encode_value(type, value, types)
    else
      encoded_types.push type
      encoded_values.push value
    end
  end

  # all data is abi-encoded
  return Abi.encode encoded_types, encoded_values
end