encodeField method

dynamic encodeField(
  1. String name,
  2. String type,
  3. dynamic value,
  4. Map<String, dynamic> types,
)

Implementation

encodeField(
  String name,
  String type,
  dynamic value,
  Map<String, dynamic> types,
) {
  if (types[type] != null) {
    dynamic _a =
        '0x0000000000000000000000000000000000000000000000000000000000000000';
    if (value != null) {
      var _cc = encodeData(type, value, types);
      var _bb = Uint8List.fromList(HEX.decode(_cc));
      _a = sha3_256(Uint8List.fromList(_bb));
    }

    return [
      'bytes32',
      // eslint-disable-line no-eq-null
      _a,
    ];
  }

  if (value == null) {
    throw Exception("missing value for field $name of type $type");
  }

  if (type == 'bytes') {
    if (value is String) {
      return [
        'bytes32',
        sha3_256((Uint8List.fromList(HEX.decode(strip0x(value))))),
      ];
    } else {
      throw Exception("not recognized type for $name with type $type");
    }
  }

  if (type == 'string') {
    // convert string to byteArray - prevents _crypto from interpreting strings like '0xabcd' as hex
    final _val = Uint8List.fromList(utf8.encode(value));
    return ['bytes32', sha3_256(_val)];
  }

  if (type.lastIndexOf(']') == type.length - 1) {
    final parsedType = type.substring(0, type.lastIndexOf('['));
    final typeValuePairs = value.map(
      (item) => encodeField(name, parsedType, item, types),
    );
    return [
      'bytes32',
      sha3_256(
        (_abiHelper.rawEncode(
          // MARK: 0 to get the types and 1 to get the values
          typeValuePairs.map((t) => t[0]).toList(),
          typeValuePairs.map((t) => t[1]).toList(),
        )),
      ),
    ];
  }

  return [type, value];
}