encodeData static method

Uint8List encodeData(
  1. TypedData typedData,
  2. String primaryType,
  3. dynamic data
)

Implementation

static Uint8List encodeData(
    TypedData typedData, String primaryType, dynamic data) {
  final types = typedData.types;
  final args = types[primaryType];
  if (args == null || args.length == 0) {
    throw ArgumentError('TypedDataUtils: $primaryType type is not defined');
  }

  List<String> abiTypes = [];
  List<dynamic> abiValues = [];

  final typeHashBytes = typeHash(types, primaryType);

  // Add typehash
  abiTypes.add('bytes32');
  abiValues.add(zeroPad(typeHashBytes, 32));

  // Add field contents
  types[primaryType]!.forEach((TypedDataArgument field) {
    var value = data[field.name];
    if (types[field.type] != null) {
      abiTypes.add('bytes32');
      value = keccak256(encodeData(typedData, field.type, value));
      abiValues.add(value);
    } else if (field.type == 'string' || field.type == 'bytes') {
      abiTypes.add('bytes32');
      if (field.type == 'string') {
        value = zeroPad(keccakUtf8(value), 32);
      } else {
        value = zeroPad(keccak256(value), 32);
      }
      abiValues.add(value);
    } else if (field.type.lastIndexOf(']') == field.type.length - 1) {
      throw 'TODO: Arrays currently uninplemented in encodeData';
    } else {
      abiTypes.add(field.type);
      abiValues.add(value);
    }
  });

  return abiRawEncode(abiTypes, abiValues);
}