Authorization for entering a private key has been set up.

This commit is contained in:
Alex Vasilev 2023-05-22 23:04:20 +03:00
parent d25e5074f6
commit a5fc25ddec
4 changed files with 171 additions and 128 deletions

View File

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:drifter/pages/login_screen/login_screen.dart';
import 'package:drifter/pages/main_screen/main_screen_widget.dart'; import 'package:drifter/pages/main_screen/main_screen_widget.dart';
import 'package:drifter/pages/splash_screen/splash_screen.dart'; import 'package:drifter/pages/splash_screen/splash_screen.dart';
import 'package:drifter/theme/app_colors.dart'; import 'package:drifter/theme/app_colors.dart';
@ -25,7 +26,10 @@ class MyApp extends StatelessWidget {
unselectedItemColor: Colors.grey, unselectedItemColor: Colors.grey,
), ),
), ),
home: const Splash(), routes: {
'/': (context) => const Splash(),
'/login': (context) => const LoginScreen(),
},
); );
} }
} }

View File

@ -8,8 +8,10 @@ import 'package:drifter/pages/profile_screen/widgets/message_snack_bar.dart';
import 'package:drifter/pages/profile_screen/widgets/ok_button_widget.dart'; import 'package:drifter/pages/profile_screen/widgets/ok_button_widget.dart';
import 'package:drifter/theme/app_colors.dart'; import 'package:drifter/theme/app_colors.dart';
import 'package:drifter/utilities/assets.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_svg/svg.dart';
import 'package:nostr_tools/nostr_tools.dart'; import 'package:nostr_tools/nostr_tools.dart';
class LoginScreen extends StatefulWidget { class LoginScreen extends StatefulWidget {
@ -25,126 +27,143 @@ class LoginScreenState extends State<LoginScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListView( return Scaffold(
children: [ appBar: AppBar(
SizedBox( title: Row(
height: 200, // mainAxisAlignment: MainAxisAlignment.center,
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [ children: [
const Text( SvgPicture.asset(
'Enter your private key', Assets.svg.drifterIcon,
style: TextStyle( height: 30,
fontSize: 20, width: 30,
), alignment: Alignment.centerLeft,
),
const SizedBox(
width: 125,
),
const Text(
"Login",
style: TextStyle(
color: AppColors.mainAccent,
),
// textAlign: TextAlign.center,
), ),
const SizedBox(height: 30),
TextFormField(
decoration: const InputDecoration(
labelText: 'Enter nsec or hex',
border: OutlineInputBorder(),
),
maxLength: 64,
controller: keyController,
key: formKey,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your private key';
}
try {
bool isValidHexKey =
Nostr.instance.keysService.isValidPrivateKey(value);
bool isValidNSec = value.trim().startsWith('nsec') &&
Nostr.instance.keysService.isValidPrivateKey(Nostr
.instance.keysService
.decodeNsecKeyToPrivateKey(value));
if (!(isValidHexKey || isValidNSec)) {
return 'Your private key is not valid.';
}
} on ChecksumVerificationException catch (e) {
return e.message;
} catch (e) {
return 'Error: $e';
}
return null;
}),
], ],
), ),
centerTitle: true,
), ),
SizedBox(height: 20), body: ListView(
Padding( children: [
padding: const EdgeInsets.all(16.0), const SizedBox(
child: ElevatedButton( height: 200,
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(AppColors.background)),
child: const Text(
'Login',
), ),
onPressed: () { Padding(
if (formKey.currentState!.validate()) { padding: const EdgeInsets.all(16.0),
String privateKeyHex = keyController.text.trim(); child: Column(
String publicKeyHex; children: [
String nsecKey; const Text(
String npubKey; 'Enter your private key',
style: TextStyle(
fontSize: 20,
),
),
const SizedBox(height: 30),
TextFormField(
decoration: const InputDecoration(
labelText: 'Enter nsec or hex',
border: OutlineInputBorder(),
),
maxLength: 64,
controller: keyController,
key: formKey,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your private key';
}
try {
bool isValidHexKey = Nostr.instance.keysService
.isValidPrivateKey(value);
bool isValidNSec = value.trim().startsWith('nsec') &&
Nostr.instance.keysService.isValidPrivateKey(Nostr
.instance.keysService
.decodeNsecKeyToPrivateKey(value));
if (!(isValidHexKey || isValidNSec)) {
return 'Your private key is not valid.';
}
} on ChecksumVerificationException catch (e) {
return e.message;
} catch (e) {
return 'Error: $e';
}
return null;
}),
],
),
),
SizedBox(height: 20),
Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(AppColors.background)),
child: const Text(
'Login',
),
onPressed: () {
if (formKey.currentState!.validate()) {
String privateKeyHex = keyController.text.trim();
String publicKeyHex;
String nsecKey;
String npubKey;
// if (privateKeyHex.startsWith('nsec')) { // if (privateKeyHex.startsWith('nsec')) {
// nsecKey = privateKeyHex; // nsecKey = privateKeyHex;
// final decoded = Nostr.instance.keysService // final decoded = Nostr.instance.keysService
// .decodeNsecKeyToPrivateKey(privateKeyHex); // .decodeNsecKeyToPrivateKey(privateKeyHex);
// privateKeyHex = decoded; // privateKeyHex = decoded;
// publicKeyHex = Nostr.instance.keysService // publicKeyHex = Nostr.instance.keysService
// .derivePublicKey(privateKey: 'privateKeyHex'); // .derivePublicKey(privateKey: 'privateKeyHex');
// npubKey = Nostr.instance.keysService // npubKey = Nostr.instance.keysService
// .encodePublicKeyToNpub(publicKeyHex); // .encodePublicKeyToNpub(publicKeyHex);
// } else { // } else {
// publicKeyHex = Nostr.instance.keysService // publicKeyHex = Nostr.instance.keysService
// .derivePublicKey(privateKey: 'privateKeyHex'); // .derivePublicKey(privateKey: 'privateKeyHex');
// nsecKey = Nostr.instance.keysService // nsecKey = Nostr.instance.keysService
// .encodePrivateKeyToNsec(privateKeyHex); // .encodePrivateKeyToNsec(privateKeyHex);
// npubKey = Nostr.instance.keysService // npubKey = Nostr.instance.keysService
// .encodePublicKeyToNpub(publicKeyHex); // .encodePublicKeyToNpub(publicKeyHex);
// }
if (privateKeyHex.startsWith('nsec')) {
final decoded = nip19.decode(privateKeyHex);
privateKeyHex = decoded['data'];
publicKeyHex = keyGenerator.getPublicKey(privateKeyHex);
nsecKey = nip19.nsecEncode(privateKeyHex);
npubKey = nip19.npubEncode(publicKeyHex);
} else {
publicKeyHex = keyGenerator.getPublicKey(privateKeyHex);
nsecKey = nip19.nsecEncode(privateKeyHex);
npubKey = nip19.npubEncode(publicKeyHex);
}
ProfileScreenState()
.addKeyToStorage(
privateKeyHex, publicKeyHex, nsecKey, npubKey)
.then((keysAdded) {
if (keysAdded) {
keyController.clear();
ScaffoldMessenger.of(context).showSnackBar(
MessageSnackBar(label: 'Congratulations! Keys Stored!'),
);
// setState() {
// Keys.privateKey = privateKeyHex;
// Keys.publicKey = publicKeyHex;
// Keys.nsecKey = nsecKey;
// Keys.npubKey = npubKey;
// Keys.keysExist = true;
// } // }
if (privateKeyHex.startsWith('nsec')) {
final decoded = nip19.decode(privateKeyHex);
privateKeyHex = decoded['data'];
publicKeyHex = keyGenerator.getPublicKey(privateKeyHex);
nsecKey = nip19.nsecEncode(privateKeyHex);
npubKey = nip19.npubEncode(publicKeyHex);
} else {
publicKeyHex = keyGenerator.getPublicKey(privateKeyHex);
nsecKey = nip19.nsecEncode(privateKeyHex);
npubKey = nip19.npubEncode(publicKeyHex);
}
try {
ProfileScreenState().addKeyToStorage(
privateKeyHex, publicKeyHex, nsecKey, npubKey);
keyController.clear();
ScaffoldMessenger.of(context).showSnackBar(
MessageSnackBar(label: 'Congratulations! Keys Stored!'),
);
Navigator.of(context).pop(true);
} catch (e) {}
} else {
formKey.currentState?.setState(() {});
} }
}); },
} else { ),
formKey.currentState?.setState(() {}); )
} ],
}, ));
),
)
],
);
} }
} }

View File

@ -4,6 +4,7 @@ import 'package:drifter/pages/login_screen/login_screen.dart';
import 'package:drifter/pages/message_screen/message_screen_widget.dart'; import 'package:drifter/pages/message_screen/message_screen_widget.dart';
import 'package:drifter/pages/profile_screen/profile_screen.dart'; import 'package:drifter/pages/profile_screen/profile_screen.dart';
import 'package:drifter/theme/app_colors.dart'; import 'package:drifter/theme/app_colors.dart';
import 'package:drifter/main.dart';
import 'package:drifter/utilities/assets.dart'; import 'package:drifter/utilities/assets.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
@ -59,7 +60,7 @@ class _MainScreenWidgetState extends State<MainScreenWidget> {
HomeScreen(), HomeScreen(),
MessageScreen(), MessageScreen(),
ProfileScreen(), ProfileScreen(),
LoginScreen(), // LoginScreen(),
], ],
), ),
bottomNavigationBar: BottomNavigationBar( bottomNavigationBar: BottomNavigationBar(
@ -77,10 +78,10 @@ class _MainScreenWidgetState extends State<MainScreenWidget> {
icon: Icon(Icons.person), icon: Icon(Icons.person),
label: 'Profile', label: 'Profile',
), ),
BottomNavigationBarItem( // BottomNavigationBarItem(
icon: Icon(Icons.login), // icon: Icon(Icons.login),
label: 'Login', // label: 'Login',
), // ),
], ],
onTap: onSelectedtap, onTap: onSelectedtap,
), ),

View File

@ -1,6 +1,7 @@
import 'package:dart_nostr/dart_nostr.dart'; import 'package:dart_nostr/dart_nostr.dart';
import 'package:drifter/models/keys.dart'; import 'package:drifter/models/keys.dart';
import 'package:drifter/pages/profile_screen/widgets/delete_keys_dialog.dart'; import 'package:drifter/pages/profile_screen/widgets/delete_keys_dialog.dart';
import 'package:drifter/main.dart';
import 'package:drifter/pages/profile_screen/widgets/key_exist_dialog.dart'; import 'package:drifter/pages/profile_screen/widgets/key_exist_dialog.dart';
import 'package:drifter/pages/profile_screen/widgets/keys_option_modal_bottom_sheet.dart'; import 'package:drifter/pages/profile_screen/widgets/keys_option_modal_bottom_sheet.dart';
import 'package:drifter/pages/profile_screen/widgets/message_snack_bar.dart'; import 'package:drifter/pages/profile_screen/widgets/message_snack_bar.dart';
@ -174,16 +175,34 @@ class ProfileScreenState extends State<ProfileScreen> {
// 'Keys', // 'Keys',
// ), // ),
// ) // )
: ElevatedButton( : Row(
style: ButtonStyle( children: [
backgroundColor: ElevatedButton(
MaterialStateProperty.all(AppColors.background)), style: ButtonStyle(
onPressed: () { backgroundColor: MaterialStateProperty.all(
modalBottomSheet(); AppColors.background)),
}, onPressed: () {
child: const Text( modalBottomSheet();
'Generate Keys', },
), child: const Text(
'Generate Keys',
),
),
SizedBox(width: 100),
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
AppColors.background)),
onPressed: () {
Navigator.pushNamed(context, '/login').then((_) {
initState();
});
},
child: const Text(
'Login',
),
),
],
), ),
Keys.keysExist Keys.keysExist
? Row( ? Row(