From 651e324285aede494d09780626b45749626fe892 Mon Sep 17 00:00:00 2001 From: Alfreedom <00tango.bromine@icloud.com> Date: Thu, 8 Feb 2024 12:25:54 +0100 Subject: [PATCH] use respondSessionRequest() --- CHANGELOG.md | 1 + .../lib/dependencies/chains/evm_service.dart | 174 ++++++++++++------ example/wallet/lib/models/chain_data.dart | 8 + .../lib/utils/namespace_model_builder.dart | 2 +- .../wc_connection_widget_info.dart | 33 ++-- lib/src/version.dart | 2 +- pubspec.yaml | 2 +- 7 files changed, 146 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dab9ec3..4bd5054c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +## 2.2.0-beta03 ## 2.2.0-beta02 - 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 80e4b9af..a8ff39b5 100644 --- a/example/wallet/lib/dependencies/chains/evm_service.dart +++ b/example/wallet/lib/dependencies/chains/evm_service.dart @@ -43,6 +43,7 @@ class EVMService { event: event, ); } + // Supported methods Map methodsHandlers = { 'personal_sign': personalSign, @@ -65,13 +66,16 @@ class EVMService { } } - Future personalSign(String topic, dynamic parameters) async { + Future personalSign(String topic, dynamic parameters) async { debugPrint('[$runtimeType] personalSign request: $parameters'); - dynamic result; - // final pRequest = _web3Wallet.pendingRequests.getAll().first; + final pRequest = _web3Wallet.pendingRequests.getAll().first; final data = EthUtils.getDataFromParamsList(parameters); final message = EthUtils.getUtf8Message(data.toString()); + var response = JsonRpcResponse( + id: pRequest.id, + jsonrpc: '2.0', + ); if (await requestApproval(message)) { try { @@ -81,33 +85,43 @@ class EVMService { ); final credentials = EthPrivateKey.fromHex(keys[0].privateKey); - final String signature = hex.encode( + final signature = hex.encode( credentials.signPersonalMessageToUint8List( Uint8List.fromList(utf8.encode(message)), ), ); - result = '0x$signature'; + response = response.copyWith(result: '0x$signature'); } catch (e) { debugPrint('[$runtimeType] personalSign error $e'); - result = JsonRpcError(code: 0, message: e.toString()); + response = response.copyWith( + error: JsonRpcError(code: 0, message: e.toString()), + ); } } else { - result = const JsonRpcError(code: 5001, message: 'User rejected method'); + response = response.copyWith( + error: const JsonRpcError(code: 5001, message: 'User rejected method'), + ); } - _goBackToDapp(topic, result); + _goBackToDapp(topic, response.result ?? response.error); - return result; + return _web3Wallet.respondSessionRequest( + topic: topic, + response: response, + ); } - Future ethSign(String topic, dynamic parameters) async { + Future ethSign(String topic, dynamic parameters) async { debugPrint('[$runtimeType] ethSign request: $parameters'); - dynamic result; - // final pRequest = _web3Wallet.pendingRequests.getAll().first; + final pRequest = _web3Wallet.pendingRequests.getAll().first; final data = EthUtils.getDataFromParamsList(parameters); final message = EthUtils.getUtf8Message(data.toString()); + var response = JsonRpcResponse( + id: pRequest.id, + jsonrpc: '2.0', + ); if (await requestApproval(message)) { try { @@ -123,74 +137,79 @@ class EVMService { ), ); - result = '0x$signature'; + response = response.copyWith(result: '0x$signature'); } catch (e) { debugPrint('[$runtimeType] ethSign error $e'); - result = JsonRpcError(code: 0, message: e.toString()); + response = response.copyWith( + error: JsonRpcError(code: 0, message: e.toString()), + ); } } else { - result = const JsonRpcError(code: 5001, message: 'User rejected method'); + response = response.copyWith( + error: const JsonRpcError(code: 5001, message: 'User rejected method'), + ); } - _goBackToDapp(topic, result); + _goBackToDapp(topic, response.result ?? response.error); - return result; + return _web3Wallet.respondSessionRequest( + topic: topic, + response: response, + ); } - Future ethSignTypedData(String topic, dynamic parameters) async { + Future ethSignTypedData(String topic, dynamic parameters) async { debugPrint('[$runtimeType] ethSignTypedData request: $parameters'); - dynamic result; - // final pRequest = _web3Wallet.pendingRequests.getAll().first; + final pRequest = _web3Wallet.pendingRequests.getAll().first; final data = parameters[1] as String; + var response = JsonRpcResponse( + id: pRequest.id, + jsonrpc: '2.0', + ); if (await requestApproval(data)) { try { final keys = GetIt.I().getKeysForChain( chainSupported.chainId, ); - result = EthSigUtil.signTypedData( + final signature = EthSigUtil.signTypedData( privateKey: keys[0].privateKey, jsonData: data, version: TypedDataVersion.V4, ); + response = response.copyWith(result: signature); } catch (e) { debugPrint('[$runtimeType] ethSignTypedData error $e'); - result = JsonRpcError(code: 0, message: e.toString()); + response = response.copyWith( + error: JsonRpcError(code: 0, message: e.toString()), + ); } } else { - result = const JsonRpcError(code: 5001, message: 'User rejected method'); + response = response.copyWith( + error: const JsonRpcError(code: 5001, message: 'User rejected method'), + ); } - _goBackToDapp(topic, result); - - return result; - } + _goBackToDapp(topic, response.result ?? response.error); - Future switchChain(String topic, dynamic parameters) async { - debugPrint('received switchChain request: $topic $parameters'); - final params = (parameters as List).first as Map; - final hexChainId = params['chainId'].toString().replaceFirst('0x', ''); - final chainId = int.parse(hexChainId, radix: 16); - final web3wallet = _web3WalletService.getWeb3Wallet(); - await web3wallet.emitSessionEvent( + return _web3Wallet.respondSessionRequest( topic: topic, - chainId: 'eip155:$chainId', - event: SessionEventParams( - name: 'chainChanged', - data: chainId, - ), + response: response, ); } Future ethSignTransaction(String topic, dynamic parameters) async { debugPrint('[$runtimeType] ethSignTransaction request: $parameters'); - dynamic result; + final pRequest = _web3Wallet.pendingRequests.getAll().first; + var response = JsonRpcResponse( + id: pRequest.id, + jsonrpc: '2.0', + ); - // final pRequest = _web3Wallet.pendingRequests.getAll().first; final tJson = parameters[0] as Map; - final transaction = await approveTransaction(tJson); - if (transaction != null) { + final result = await approveTransaction(tJson); + if (result is Transaction) { try { // Load the private key final keys = GetIt.I().getKeysForChain( @@ -202,37 +221,47 @@ class EVMService { final signature = await ethClient.signTransaction( credentials, - transaction, + result, chainId: int.parse(chainId), ); // Sign the transaction final signedTx = hex.encode(signature); - result = '0x$signedTx'; + response = response.copyWith(result: '0x$signedTx'); } on RPCError catch (e) { debugPrint('[$runtimeType] ethSignTransaction error $e'); - result = JsonRpcError(code: e.errorCode, message: e.message); + response = response.copyWith( + error: JsonRpcError(code: e.errorCode, message: e.message), + ); } catch (e) { debugPrint('[$runtimeType] ethSignTransaction error $e'); - result = JsonRpcError(code: 0, message: e.toString()); + response = response.copyWith( + error: JsonRpcError(code: 0, message: e.toString()), + ); } } else { - result = const JsonRpcError(code: 5001, message: 'User rejected method'); + response = response.copyWith(error: result as JsonRpcError); } - _goBackToDapp(topic, result); + _goBackToDapp(topic, response.result ?? response.error); - return result; + return _web3Wallet.respondSessionRequest( + topic: topic, + response: response, + ); } Future ethSendTransaction(String topic, dynamic parameters) async { debugPrint('[$runtimeType] ethSendTransaction request: $parameters'); - dynamic result; + final pRequest = _web3Wallet.pendingRequests.getAll().first; + var response = JsonRpcResponse( + id: pRequest.id, + jsonrpc: '2.0', + ); - // final pRequest = _web3Wallet.pendingRequests.getAll().first; final tJson = parameters[0] as Map; - final transaction = await approveTransaction(tJson); - if (transaction is Transaction) { + final result = await approveTransaction(tJson); + if (result is Transaction) { try { // Load the private key final keys = GetIt.I().getKeysForChain( @@ -244,25 +273,48 @@ class EVMService { final signedTx = await ethClient.sendTransaction( credentials, - transaction, + result, chainId: int.parse(chainId), ); - result = '0x$signedTx'; + response = response.copyWith(result: '0x$signedTx'); } on RPCError catch (e) { debugPrint('[$runtimeType] ethSendTransaction error $e'); - result = JsonRpcError(code: e.errorCode, message: e.message); + response = response.copyWith( + error: JsonRpcError(code: e.errorCode, message: e.message), + ); } catch (e) { debugPrint('[$runtimeType] ethSendTransaction error $e'); - result = JsonRpcError(code: 0, message: e.toString()); + response = response.copyWith( + error: JsonRpcError(code: 0, message: e.toString()), + ); } } else { - result = transaction as JsonRpcError; + response = response.copyWith(error: result as JsonRpcError); } - _goBackToDapp(topic, result); + _goBackToDapp(topic, response.result ?? response.error); - return result; + return _web3Wallet.respondSessionRequest( + topic: topic, + response: response, + ); + } + + Future switchChain(String topic, dynamic parameters) async { + debugPrint('received switchChain request: $topic $parameters'); + final params = (parameters as List).first as Map; + final hexChainId = params['chainId'].toString().replaceFirst('0x', ''); + final chainId = int.parse(hexChainId, radix: 16); + final web3wallet = _web3WalletService.getWeb3Wallet(); + await web3wallet.emitSessionEvent( + topic: topic, + chainId: 'eip155:$chainId', + event: SessionEventParams( + name: 'chainChanged', + data: chainId, + ), + ); } void _goBackToDapp(String topic, dynamic result) { diff --git a/example/wallet/lib/models/chain_data.dart b/example/wallet/lib/models/chain_data.dart index c42baaa6..11cd168a 100644 --- a/example/wallet/lib/models/chain_data.dart +++ b/example/wallet/lib/models/chain_data.dart @@ -43,6 +43,14 @@ class ChainData { color: Colors.orange, rpc: ['https://api.avax.network/ext/bc/C/rpc'], ), + const ChainMetadata( + type: ChainType.eip155, + chainId: 'eip155:56', + name: 'BNB Smart Chain Mainnet', + logo: '/chain-logos/eip155-56.png', + color: Colors.orange, + rpc: ['https://bsc-dataseed1.bnbchain.org'], + ), const ChainMetadata( type: ChainType.eip155, chainId: 'eip155:42220', diff --git a/example/wallet/lib/utils/namespace_model_builder.dart b/example/wallet/lib/utils/namespace_model_builder.dart index b0b3728e..798015e9 100644 --- a/example/wallet/lib/utils/namespace_model_builder.dart +++ b/example/wallet/lib/utils/namespace_model_builder.dart @@ -82,7 +82,7 @@ class ConnectionWidgetBuilder { } models.add( WCConnectionModel( - title: StringConstants.events, + title: '${StringConstants.events} (Tap to send)', elements: ns.events, elementActions: actions, ), diff --git a/example/wallet/lib/widgets/wc_connection_widget/wc_connection_widget_info.dart b/example/wallet/lib/widgets/wc_connection_widget/wc_connection_widget_info.dart index 20537660..9d5cfc70 100644 --- a/example/wallet/lib/widgets/wc_connection_widget/wc_connection_widget_info.dart +++ b/example/wallet/lib/widgets/wc_connection_widget/wc_connection_widget_info.dart @@ -51,24 +51,34 @@ class WCConnectionWidgetInfo extends StatelessWidget { } Widget _buildElement(String text) { - return InkWell( - onTap: model.elementActions != null ? model.elementActions![text] : null, - child: Container( - decoration: BoxDecoration( - color: StyleConstants.layerColor4, - borderRadius: BorderRadius.circular( - StyleConstants.linear16, - ), + return ElevatedButton( + onPressed: + model.elementActions != null ? model.elementActions![text] : null, + style: ButtonStyle( + elevation: model.elementActions != null + ? MaterialStateProperty.all(4.0) + : MaterialStateProperty.all(0.0), + padding: MaterialStateProperty.all(const EdgeInsets.all(0.0)), + visualDensity: VisualDensity.compact, + backgroundColor: MaterialStateProperty.all( + StyleConstants.layerColor4, + ), + overlayColor: MaterialStateProperty.all(Colors.white), + shape: MaterialStateProperty.resolveWith( + (states) { + return RoundedRectangleBorder( + borderRadius: BorderRadius.circular(StyleConstants.linear16), + ); + }, ), - // margin: const EdgeInsets.all(2), + ), + child: Container( padding: const EdgeInsets.all( StyleConstants.linear8, ), child: Text( text, style: StyleConstants.layerTextStyle4, - maxLines: 10, - overflow: TextOverflow.ellipsis, ), ), ); @@ -78,7 +88,6 @@ class WCConnectionWidgetInfo extends StatelessWidget { return Text( model.text!, style: StyleConstants.layerTextStyle3, - // textAlign: TextAlign.center, ); } } diff --git a/lib/src/version.dart b/lib/src/version.dart index dec81e91..a2527669 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-beta02'; +const packageVersion = '2.2.0-beta03'; diff --git a/pubspec.yaml b/pubspec.yaml index 5d0c9ca6..3fc0022a 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-beta02 +version: 2.2.0-beta03 repository: https://github.com/WalletConnect/WalletConnectFlutterV2 environment: