getMessageForSign method
Get a signable message from the typed data, accepts typedData and hash and hash is used when you want the hashed result of the output (keccak hash which ethereum uses)
Implementation
String getMessageForSign({
required Map<String, dynamic> typedData,
bool hash = true,
bool include0x = true,
}) {
Uint8List _primaryTypeHash = Uint8List.fromList([]);
// MARK: step1. sanitize data
final sanitizedData = sanitizeData(typedData);
// MARK: step2. get struct hash of typedData with type EIP712Domain
final _eip712DomainHash = hashStruct(
'EIP712Domain',
sanitizedData['domain'],
sanitizedData['types'],
);
// MARK: step3. get struct hash of typedData with type primaryType
if (sanitizedData['primaryType'] != 'EIP712Domain') {
_primaryTypeHash = hashStruct(
sanitizedData["primaryType"],
sanitizedData['message'],
sanitizedData['types'],
);
}
// MARK: step4. concat `EIP_191_PREFIX` and first and second step to get the message
final message = Uint8List.fromList([
...EIP_191_PREFIX,
..._eip712DomainHash,
..._primaryTypeHash,
]);
if (hash) {
final _res = HEX.encode(sha3_256(message));
return include0x ? "0x$_res" : _res;
}
final _res = HEX.encode(message);
return include0x ? "0x$_res" : _res;
}