Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception happening during transaction parsing #265

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading