Skip to content

Commit

Permalink
Fix exception happening during transaction parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Feb 10, 2024
1 parent 5679966 commit ae3b605
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions example/wallet/lib/dependencies/chains/evm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class EVMService {
'eth_signTransaction': ethSignTransaction,
'eth_sendTransaction': ethSendTransaction,
'eth_signTypedData': ethSignTypedData,
'eth_signTypedData_v4': ethSignTypedData,
'eth_signTypedData_v4': ethSignTypedDataV4,
'wallet_switchEthereumChain': switchChain,
'wallet_addEthereumChain': addChain,
// add whatever method/handler you want to support
Expand Down Expand Up @@ -162,7 +162,7 @@ class EVMService {
debugPrint('[$runtimeType] ethSignTypedData request: $parameters');

final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = parameters[1] as String;
final data = EthUtils.getDataFromParamsList(parameters);
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
Expand All @@ -176,7 +176,7 @@ class EVMService {
final signature = EthSigUtil.signTypedData(
privateKey: keys[0].privateKey,
jsonData: data,
version: TypedDataVersion.V4,
version: TypedDataVersion.V1,
);
response = response.copyWith(result: signature);
} catch (e) {
Expand All @@ -199,6 +199,47 @@ class EVMService {
);
}

Future<void> ethSignTypedDataV4(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] ethSignTypedDataV4 request: $parameters');

final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = EthUtils.getDataFromParamsList(parameters);
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
);
if (await requestApproval(data)) {
try {
final keys = GetIt.I<IKeyService>().getKeysForChain(
chainSupported.chainId,
);

final signature = EthSigUtil.signTypedData(
privateKey: keys[0].privateKey,
jsonData: data,
version: TypedDataVersion.V4,
);
response = response.copyWith(result: signature);
} catch (e) {
debugPrint('[$runtimeType] ethSignTypedDataV4 error $e');
response = response.copyWith(
error: JsonRpcError(code: 0, message: e.toString()),
);
}
} else {
response = response.copyWith(
error: const JsonRpcError(code: 5001, message: 'User rejected method'),
);
}

_goBackToDapp(topic, response.result ?? response.error);

return _web3Wallet.respondSessionRequest(
topic: topic,
response: response,
);
}

Future<dynamic> ethSignTransaction(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] ethSignTransaction request: $parameters');
final pRequest = _web3Wallet.pendingRequests.getAll().first;
Expand All @@ -207,8 +248,8 @@ class EVMService {
jsonrpc: '2.0',
);

final tJson = parameters[0] as Map<String, dynamic>;
final result = await approveTransaction(tJson);
final data = EthUtils.getTransactionFromParams(parameters);
final result = await approveTransaction(data);
if (result is Transaction) {
try {
// Load the private key
Expand Down Expand Up @@ -259,8 +300,8 @@ class EVMService {
jsonrpc: '2.0',
);

final tJson = parameters[0] as Map<String, dynamic>;
final result = await approveTransaction(tJson);
final data = EthUtils.getTransactionFromParams(parameters);
final result = await approveTransaction(data);
if (result is Transaction) {
try {
// Load the private key
Expand Down

0 comments on commit ae3b605

Please sign in to comment.