From 469870f2700d97f3e045a6397697047498fcbaf3 Mon Sep 17 00:00:00 2001 From: AriZhong Date: Thu, 28 Apr 2022 19:26:20 +0800 Subject: [PATCH] add private key import --- lib/core/provider/shared_provider.dart | 36 ++++++++++--------- .../dialogs/add_wallet_dialog.dart | 12 ++++--- .../begin/presentation/pages/begin_page.dart | 17 ++++++--- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/lib/core/provider/shared_provider.dart b/lib/core/provider/shared_provider.dart index f05f1e4..2d205d3 100644 --- a/lib/core/provider/shared_provider.dart +++ b/lib/core/provider/shared_provider.dart @@ -267,21 +267,25 @@ class ImportWalletNotifier extends StateNotifier { HomePage.show(context); } - Future importWallet() async { - // state = const ConnectWalletState.loading(); - // isConnectWallet = true; - // if (isConnectWallet) { - // String address = ''; - // final EtherAmount etherAmount = EtherAmount.zero(); - // final EthWallet wallet = EthWallet( - // address: address, - // importMethod: WalletImportMethod.metamask, - // ethers: etherAmount.getValueInUnit(EtherUnit.ether), - // ); - - // state = ConnectWalletState.data(wallet: wallet); - // } else { - // state = const ConnectWalletState.error(msg: '無法匯入錢包,請再試一次'); - // } + Future importWallet(String privateKey) async { + state = const ConnectWalletState.loading(); + + try { + final private = EthPrivateKey.fromHex(privateKey); + final address = await private.extractAddress(); + isConnectWallet = true; + print('Eth Address: $address'); + + final WalletInfo wallet = WalletInfo( + address: address.toString(), + importMethod: WalletImportMethod.privateKey, + privateKey: privateKey, + ); + + state = ConnectWalletState.data(walletInfo: wallet); + } catch (e) { + isConnectWallet = false; + state = const ConnectWalletState.error(msg: '無法匯入錢包,請再試一次'); + } } } diff --git a/lib/pages/begin/presentation/dialogs/add_wallet_dialog.dart b/lib/pages/begin/presentation/dialogs/add_wallet_dialog.dart index 6336802..a86e6aa 100644 --- a/lib/pages/begin/presentation/dialogs/add_wallet_dialog.dart +++ b/lib/pages/begin/presentation/dialogs/add_wallet_dialog.dart @@ -12,8 +12,8 @@ class AddWalletDialog extends StatelessWidget { Key? key, }) : super(key: key); - static Future show(BuildContext context) async { - await showDialog( + static Future show(BuildContext context) async { + return showDialog( context: context, builder: (context) { return AddWalletDialog(); @@ -51,8 +51,6 @@ class AddWalletDialog extends StatelessWidget { TextSelection.fromPosition(TextPosition(offset: privateKeyEditController.text.length)); } - void _onConfirm() {} - @override Widget build(BuildContext context) { return Dialog( @@ -127,7 +125,11 @@ class AddWalletDialog extends StatelessWidget { CommonTextField(textEditingController: privateKeyEditController), const SizedBox(height: 24.0), CommonButton( - onPress: () => _onConfirm(), + onPress: () { + if (privateKeyEditController.text.isNotEmpty) { + Navigator.maybePop(context, privateKeyEditController.text); + } + }, padding: const EdgeInsets.symmetric(vertical: 20.0), color: CustomTheme.secondColor, child: const Text( diff --git a/lib/pages/begin/presentation/pages/begin_page.dart b/lib/pages/begin/presentation/pages/begin_page.dart index a51dbb2..d5571c3 100644 --- a/lib/pages/begin/presentation/pages/begin_page.dart +++ b/lib/pages/begin/presentation/pages/begin_page.dart @@ -6,6 +6,7 @@ import '../../../../core/provider/shared_provider.dart'; import '../../../../core/util/theme.dart'; import '../../data/states/begin_state.dart'; import '../../domain/providers/begin_provider.dart'; +import '../dialogs/add_wallet_dialog.dart'; import '../widgets/common_button.dart'; import 'home_page.dart'; @@ -16,7 +17,8 @@ class BeginPage extends StatelessWidget { required BuildContext context, required WidgetRef ref, }) { - if (ref.read(walletConnectedProvider.notifier).isWalletConnected) { + if (ref.read(walletConnectedProvider.notifier).isWalletConnected || + ref.read(importWalletProvider.notifier).isConnectWallet) { HomePage.show(context); } else { Fluttertoast.showToast(msg: '請先連結或匯入錢包'); @@ -37,7 +39,9 @@ class BeginPage extends StatelessWidget { ref.listen( importWalletProvider, (previous, next) { - if (next is ConnectWalletError) { + if (next is ConnectWalletData) { + ref.read(walletConnectedProvider.notifier).addWallet(next.walletInfo); + } else if (next is ConnectWalletError) { Fluttertoast.showToast(msg: next.msg); } }, @@ -96,7 +100,6 @@ class BeginPage extends StatelessWidget { children: [ Consumer(builder: (context, ref, _) { listenConnectWallet(ref); - return CommonButton( onPress: () => ref.read(connectWalletProvider.notifier).connectWallet(), color: const Color.fromRGBO(255, 255, 255, 1), @@ -135,9 +138,13 @@ class BeginPage extends StatelessWidget { const SizedBox(height: 18), Consumer(builder: (context, ref, _) { listenImportWallet(ref); - return CommonButton( - onPress: () => ref.read(importWalletProvider.notifier).importWallet(), + onPress: () async { + final privateKey = await AddWalletDialog.show(context) as String?; + if (privateKey != null) { + ref.read(importWalletProvider.notifier).importWallet(privateKey); + } + }, color: CustomTheme.secondColor, child: Padding( padding: const EdgeInsets.symmetric(vertical: 8),