Skip to content

Commit

Permalink
add private key import
Browse files Browse the repository at this point in the history
  • Loading branch information
AriZhong committed Apr 28, 2022
1 parent 298446a commit 469870f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
36 changes: 20 additions & 16 deletions lib/core/provider/shared_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,25 @@ class ImportWalletNotifier extends StateNotifier<ConnectWalletState> {
HomePage.show(context);
}

Future<void> 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<void> 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: '無法匯入錢包,請再試一次');
}
}
}
12 changes: 7 additions & 5 deletions lib/pages/begin/presentation/dialogs/add_wallet_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class AddWalletDialog extends StatelessWidget {
Key? key,
}) : super(key: key);

static Future<void> show(BuildContext context) async {
await showDialog<AddWalletDialog>(
static Future<dynamic> show(BuildContext context) async {
return showDialog<dynamic>(
context: context,
builder: (context) {
return AddWalletDialog();
Expand Down Expand Up @@ -51,8 +51,6 @@ class AddWalletDialog extends StatelessWidget {
TextSelection.fromPosition(TextPosition(offset: privateKeyEditController.text.length));
}

void _onConfirm() {}

@override
Widget build(BuildContext context) {
return Dialog(
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 12 additions & 5 deletions lib/pages/begin/presentation/pages/begin_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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: '請先連結或匯入錢包');
Expand All @@ -37,7 +39,9 @@ class BeginPage extends StatelessWidget {
ref.listen<ConnectWalletState>(
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);
}
},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 469870f

Please sign in to comment.