From fc6a894eb83b37a0c5ba283b44dce5a097962ecd Mon Sep 17 00:00:00 2001 From: Alfreedom <00tango.bromine@icloud.com> Date: Mon, 12 Feb 2024 14:58:08 +0100 Subject: [PATCH] fix on eth_utils to detect address and data from params --- CHANGELOG.md | 3 +- .../lib/dependencies/chains/evm_service.dart | 6 +++- example/wallet/lib/utils/eth_utils.dart | 36 +++++++++++++------ lib/src/version.dart | 2 +- pubspec.yaml | 2 +- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd5054c..efb3b826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,4 @@ -## 2.2.0-beta03 -## 2.2.0-beta02 +## 2.2.0-beta04 - Improvements on example wallet diff --git a/example/wallet/lib/dependencies/chains/evm_service.dart b/example/wallet/lib/dependencies/chains/evm_service.dart index 95e0e4a3..a97f83b9 100644 --- a/example/wallet/lib/dependencies/chains/evm_service.dart +++ b/example/wallet/lib/dependencies/chains/evm_service.dart @@ -68,6 +68,7 @@ class EVMService { Future personalSign(String topic, dynamic parameters) async { debugPrint('[$runtimeType] personalSign request: $parameters'); + // message, address final pRequest = _web3Wallet.pendingRequests.getAll().first; final data = EthUtils.getDataFromParamsList(parameters); @@ -114,6 +115,7 @@ class EVMService { Future ethSign(String topic, dynamic parameters) async { debugPrint('[$runtimeType] ethSign request: $parameters'); + // address, message final pRequest = _web3Wallet.pendingRequests.getAll().first; final data = EthUtils.getDataFromParamsList(parameters); @@ -176,7 +178,7 @@ class EVMService { final signature = EthSigUtil.signTypedData( privateKey: keys[0].privateKey, jsonData: data, - version: TypedDataVersion.V1, + version: TypedDataVersion.V4, ); response = response.copyWith(result: signature); } catch (e) { @@ -249,6 +251,7 @@ class EVMService { ); final data = EthUtils.getTransactionFromParams(parameters); + if (data == null) return; final result = await approveTransaction(data); if (result is Transaction) { try { @@ -301,6 +304,7 @@ class EVMService { ); final data = EthUtils.getTransactionFromParams(parameters); + if (data == null) return; final result = await approveTransaction(data); if (result is Transaction) { try { diff --git a/example/wallet/lib/utils/eth_utils.dart b/example/wallet/lib/utils/eth_utils.dart index f561aa0b..4bcb448b 100644 --- a/example/wallet/lib/utils/eth_utils.dart +++ b/example/wallet/lib/utils/eth_utils.dart @@ -4,6 +4,11 @@ import 'package:convert/convert.dart'; import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart'; class EthUtils { + static final addressRegEx = RegExp( + r'^0x[a-fA-F0-9]{40}$', + caseSensitive: false, + ); + static String getUtf8Message(String maybeHex) { if (maybeHex.startsWith('0x')) { final List decoded = hex.decode( @@ -15,26 +20,35 @@ class EthUtils { return maybeHex; } - static String getAddressFromParamsList(dynamic params) { + static dynamic getAddressFromParamsList(dynamic params) { return (params as List).firstWhere((p) { try { - EthereumAddress.fromHex(p); - return true; + if (addressRegEx.hasMatch(p)) { + EthereumAddress.fromHex(p); + return true; + } + return false; } catch (e) { return false; } - }); + }, orElse: () => null); } static dynamic getDataFromParamsList(dynamic params) { - return (params as List).firstWhere((p) { - final address = getAddressFromParamsList(params); - return p != address; - }); + final address = getAddressFromParamsList(params); + final param = (params as List).firstWhere( + (p) => p != address, + orElse: () => null, + ); + return param; } - static Map getTransactionFromParams(dynamic params) { - final param = (params as List).first; - return param as Map; + static Map? getTransactionFromParams(dynamic params) { + final address = getAddressFromParamsList(params); + final param = params.firstWhere( + (p) => p != address, + orElse: () => null, + ); + return param as Map?; } } diff --git a/lib/src/version.dart b/lib/src/version.dart index a2527669..d092ca47 100644 --- a/lib/src/version.dart +++ b/lib/src/version.dart @@ -1,2 +1,2 @@ // Generated code. Do not modify. -const packageVersion = '2.2.0-beta03'; +const packageVersion = '2.2.0-beta04'; diff --git a/pubspec.yaml b/pubspec.yaml index 3fc0022a..137e5ad8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: walletconnect_flutter_v2 description: This repository contains oficial implementation of WalletConnect v2 protocols for Flutter applications. The communications protocol for web3. -version: 2.2.0-beta03 +version: 2.2.0-beta04 repository: https://github.com/WalletConnect/WalletConnectFlutterV2 environment: