From 57e27f8654626f5c0b547c605fe065c17d946d04 Mon Sep 17 00:00:00 2001 From: Alfreedom <00tango.bromine@icloud.com> Date: Mon, 4 Mar 2024 09:57:32 +0100 Subject: [PATCH] minor change --- example/dapp/lib/utils/crypto/eip155.dart | 10 ++++++++- .../lib/dependencies/bip39/bip39_base.dart | 6 ++++-- .../lib/dependencies/chains/evm_service.dart | 21 +++++++------------ lib/apis/auth_api/utils/auth_signature.dart | 12 +++-------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/example/dapp/lib/utils/crypto/eip155.dart b/example/dapp/lib/utils/crypto/eip155.dart index 2205db83..d6373561 100644 --- a/example/dapp/lib/utils/crypto/eip155.dart +++ b/example/dapp/lib/utils/crypto/eip155.dart @@ -1,4 +1,8 @@ import 'dart:convert'; +// ignore: depend_on_referenced_packages +import 'package:convert/convert.dart'; + +import 'package:flutter/foundation.dart'; import 'package:intl/intl.dart'; import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart'; @@ -161,12 +165,16 @@ class EIP155 { required String address, required String message, }) async { + final bytes = utf8.encode(message); + final encoded = '0x${hex.encode(bytes)}'; + debugPrint('personalSign $encoded'); + return await web3App.request( topic: topic, chainId: chainId, request: SessionRequestParams( method: methods[EIP155Methods.personalSign]!, - params: [message, address], + params: [encoded, address], ), ); } diff --git a/example/wallet/lib/dependencies/bip39/bip39_base.dart b/example/wallet/lib/dependencies/bip39/bip39_base.dart index f446a987..34619d3a 100644 --- a/example/wallet/lib/dependencies/bip39/bip39_base.dart +++ b/example/wallet/lib/dependencies/bip39/bip39_base.dart @@ -53,8 +53,10 @@ Uint8List _randomBytes(int size) { return bytes; } -String generateMnemonic( - {int strength = 128, RandomBytes randomBytes = _randomBytes}) { +String generateMnemonic({ + int strength = 128, + RandomBytes randomBytes = _randomBytes, +}) { assert(strength % 32 == 0); final entropy = randomBytes(strength ~/ 8); return entropyToMnemonic(hex.encode(entropy)); diff --git a/example/wallet/lib/dependencies/chains/evm_service.dart b/example/wallet/lib/dependencies/chains/evm_service.dart index b62b9631..d7fcf5ee 100644 --- a/example/wallet/lib/dependencies/chains/evm_service.dart +++ b/example/wallet/lib/dependencies/chains/evm_service.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; // ignore: depend_on_referenced_packages import 'package:http/http.dart' as http; @@ -93,15 +92,13 @@ class EVMService { final keys = GetIt.I().getKeysForChain( chainSupported.chainId, ); - final credentials = EthPrivateKey.fromHex(keys[0].privateKey); - final signature = hex.encode( - credentials.signPersonalMessageToUint8List( - Uint8List.fromList(utf8.encode(message)), - ), + final signature = EthSigUtil.signPersonalMessage( + privateKey: keys[0].privateKey, + message: utf8.encode(message), ); - response = response.copyWith(result: '0x$signature'); + response = response.copyWith(result: signature); } catch (e) { debugPrint('[$runtimeType] personalSign error $e'); response = response.copyWith( @@ -138,15 +135,13 @@ class EVMService { final keys = GetIt.I().getKeysForChain( chainSupported.chainId, ); - final credentials = EthPrivateKey.fromHex(keys[0].privateKey); - final signature = hex.encode( - credentials.signPersonalMessageToUint8List( - Uint8List.fromList(utf8.encode(message)), - ), + final signature = EthSigUtil.signPersonalMessage( + privateKey: keys[0].privateKey, + message: utf8.encode(message), ); - response = response.copyWith(result: '0x$signature'); + response = response.copyWith(result: signature); } catch (e) { debugPrint('[$runtimeType] ethSign error $e'); response = response.copyWith( diff --git a/lib/apis/auth_api/utils/auth_signature.dart b/lib/apis/auth_api/utils/auth_signature.dart index 52937580..fa706de2 100644 --- a/lib/apis/auth_api/utils/auth_signature.dart +++ b/lib/apis/auth_api/utils/auth_signature.dart @@ -48,9 +48,7 @@ class AuthSignature { // Get the sig bytes // print(sig); final sigBytes = Uint8List.fromList( - hex.decode( - sig.substring(2), - ), + hex.decode(sig.substring(2)), ); // If the sig bytes aren't 65 bytes long, throw an error @@ -60,15 +58,11 @@ class AuthSignature { // Get the r and s values from the sig bytes final r = BigInt.parse( - hex.encode( - sigBytes.sublist(0, 32), - ), + hex.encode(sigBytes.sublist(0, 32)), radix: 16, ); final s = BigInt.parse( - hex.encode( - sigBytes.sublist(32, 64), - ), + hex.encode(sigBytes.sublist(32, 64)), radix: 16, ); // print(sigBytes[64]);