94 lines
2.5 KiB
Dart
94 lines
2.5 KiB
Dart
// generate_with_recovery_phrase.dart
|
|
|
|
import 'package:bip39_mnemonic/bip39_mnemonic.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'widgets/key_management.dart';
|
|
|
|
class GenerateWithRecoveryPhrasePage extends StatefulWidget {
|
|
const GenerateWithRecoveryPhrasePage({super.key});
|
|
|
|
@override
|
|
_GenerateWithRecoveryPhrasePageState createState() => _GenerateWithRecoveryPhrasePageState();
|
|
}
|
|
|
|
class _GenerateWithRecoveryPhrasePageState extends State<GenerateWithRecoveryPhrasePage> {
|
|
final KeyManagement _keyManagement = KeyManagement();
|
|
bool _isGenerating = false;
|
|
String? _recoveryPhrase;
|
|
|
|
Future<void> _generateKeyPair() async {
|
|
setState(() {
|
|
_isGenerating = true;
|
|
});
|
|
|
|
try {
|
|
Mnemonic mnemonic = await _keyManagement.generateKeyPairWithRecovery();
|
|
setState(() {
|
|
_recoveryPhrase = mnemonic.toString();
|
|
});
|
|
_showRecoveryPhraseDialog(mnemonic.toString());
|
|
} catch (e) {
|
|
_showErrorDialog('Failed to generate key pair: $e');
|
|
} finally {
|
|
setState(() {
|
|
_isGenerating = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
void _showRecoveryPhraseDialog(String phrase) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Recovery Phrase'),
|
|
content: SingleChildScrollView(
|
|
child: Text(
|
|
'Please write down your recovery phrase and keep it in a safe place. This phrase can be used to restore your key pair.\n\n$phrase',
|
|
style: const TextStyle(fontSize: 16),
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showErrorDialog(String message) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Error'),
|
|
content: Text(message),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
appBar: AppBar(
|
|
title: const Text('Generate Key Pair with Recovery'),
|
|
),
|
|
body: Center(
|
|
child: _isGenerating
|
|
? const CircularProgressIndicator()
|
|
: ElevatedButton(
|
|
onPressed: _generateKeyPair,
|
|
child: const Text('Generate Key Pair'),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|