encodeValue static method
Encodes a value according to its type in the context of EIP-712 structured data. The method handles array types, struct types, strings, and bytes. Returns a Tuple containing the encoded type and data.
Implementation
static Tuple<String, dynamic> encodeValue(
Eip712TypedData typedData, String type, dynamic data) {
final isArray = _extractArrayType(type);
if (isArray != null) {
if (data is! List) {
throw SolidityAbiException('Invalid data provided for array codec.',
details: {'input': data});
}
if (isArray.item2 > 0 && data.length != isArray.item2) {
throw SolidityAbiException(
'Invalid array length: expected ${isArray.item2}, but got ${data.length}',
details: {'input': data},
);
}
final encodedData = data
.map((item) => encodeValue(typedData, isArray.item1, item))
.toList();
final List<String> types = encodedData.map((item) => item.item1).toList();
final List<dynamic> values =
encodedData.map((item) => item.item2).toList();
return Tuple(bytes32TypeName,
QuickCrypto.keccack256Hash(EIP712Utils.abiEncode(types, values)));
}
if (typedData.types[type] != null) {
return Tuple(bytes32TypeName, structHash(typedData, type, data));
}
if (type == 'string' || type == 'bytes') {
final List<int> bytesData =
type == 'string' ? StringUtils.encode(data) : data;
return Tuple(bytes32TypeName, QuickCrypto.keccack256Hash(bytesData));
}
return Tuple(type, data);
}