Skip to content

Commit

Permalink
fix on eth_utils to detect address and data from params
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Feb 12, 2024
1 parent ae3b605 commit fc6a894
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## 2.2.0-beta03
## 2.2.0-beta02
## 2.2.0-beta04

- Improvements on example wallet

Expand Down
6 changes: 5 additions & 1 deletion example/wallet/lib/dependencies/chains/evm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class EVMService {

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

final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = EthUtils.getDataFromParamsList(parameters);
Expand Down Expand Up @@ -114,6 +115,7 @@ class EVMService {

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

final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = EthUtils.getDataFromParamsList(parameters);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 25 additions & 11 deletions example/wallet/lib/utils/eth_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> decoded = hex.decode(
Expand All @@ -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<String, dynamic> getTransactionFromParams(dynamic params) {
final param = (params as List<dynamic>).first;
return param as Map<String, dynamic>;
static Map<String, dynamic>? getTransactionFromParams(dynamic params) {
final address = getAddressFromParamsList(params);
final param = params.firstWhere(
(p) => p != address,
orElse: () => null,
);
return param as Map<String, dynamic>?;
}
}
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit fc6a894

Please sign in to comment.