I wanted to sign a message but encountered this error. I get the message in my application as a .json and I need to convert it to Eip712TypedData in order to sign it using this awesome package.
I wrote this code in order to transform the JSON into the variable:
import 'dart:convert';
import 'package:on_chain/on_chain.dart';
Eip712TypedData jsonToEip712TypedData(String jsonString) {
final jsonData = json.decode(jsonString);
final Map<String, dynamic> typesJson = jsonData['types'];
final Map<String, List<Eip712TypeDetails>> parsedTypes = {};
typesJson.forEach((typeName, typeFields) {
final fields = List<Map<String, dynamic>>.from(typeFields);
final List<Eip712TypeDetails> fieldsList = fields.map((field) {
return Eip712TypeDetails(
name: field['name'] as String,
type: field['type'] as String,
);
}).toList();
parsedTypes[typeName] = fieldsList;
});
final domainData = jsonData['domain'] as Map<String, dynamic>;
final domain = {
"name": domainData['name'],
"version": domainData['version'],
"chainId": BigInt.parse(domainData['chainId'].toString()),
"verifyingContract": domainData['verifyingContract'],
};
final messageData = jsonData['message'] as Map<String, dynamic>;
return Eip712TypedData(
types: parsedTypes,
primaryType: jsonData['primaryType'] as String,
domain: domain,
message: messageData,
);
}
But I receive an error in:
final Map<String, dynamic> typesJson = jsonData['types'];
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
How can I solve this problem? Are there other ways to sign this message? Here is the JSON I receive:
{ "primaryType": "Order",
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"Order": [
{
"name": "salt",
"type": "uint256"
},
{
"name": "makerAsset",
"type": "address"
},
{
"name": "takerAsset",
"type": "address"
},
{
"name": "maker",
"type": "address"
},
{
"name": "receiver",
"type": "address"
},
{
"name": "allowedSender",
"type": "address"
},
{
"name": "makingAmount",
"type": "uint256"
},
{
"name": "takingAmount",
"type": "uint256"
},
{
"name": "offsets",
"type": "uint256"
},
{
"name": "interactions",
"type": "bytes"
}
]
},
"domain": {
"name": "1inch Aggregation Router",
"version": "5",
"chainId": 56,
"verifyingContract": "0x11111...960582"
},
"message": {
"salt": "4613...485",
"makerAsset": "0x55...955",
"takerAsset": "0xbb4c...c095c",
"maker": "0x04...f8ff0",
"receiver": "0x0eee00...63e7e4",
"allowedSender": "0x1d0ae...7ac63",
"makingAmount": "1000000000000000000",
"takingAmount": "913207530647947",
"offsets": "0x3800...00000000000",
"interactions": "0x63592c2b0000000000000000000000000000000000000000000000000000000065ff21820eee00137d807...000000000000000000000000000000000000000000000000000000000000000000000ffffffff39"
}
}
Thank you so much in advance for your help and for creating and maintaining this fantastic package.