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 'package:drifter/pages/login_screen/login_screen.dart';
import 'package:drifter/pages/main_screen/main_screen_widget.dart';
import 'package:drifter/pages/splash_screen/splash_screen.dart';
import 'package:drifter/theme/app_colors.dart';
@ -25,7 +26,10 @@ class MyApp extends StatelessWidget {
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/theme/app_colors.dart';
import 'package:drifter/utilities/assets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_svg/svg.dart';
import 'package:nostr_tools/nostr_tools.dart';
class LoginScreen extends StatefulWidget {
@ -25,126 +27,143 @@ class LoginScreenState extends State<LoginScreen> {
@override
Widget build(BuildContext context) {
return ListView(
children: [
SizedBox(
height: 200,
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
return Scaffold(
appBar: AppBar(
title: Row(
// mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Enter your private key',
style: TextStyle(
fontSize: 20,
),
SvgPicture.asset(
Assets.svg.drifterIcon,
height: 30,
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),
Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(AppColors.background)),
child: const Text(
'Login',
body: ListView(
children: [
const SizedBox(
height: 200,
),
onPressed: () {
if (formKey.currentState!.validate()) {
String privateKeyHex = keyController.text.trim();
String publicKeyHex;
String nsecKey;
String npubKey;
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
const Text(
'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')) {
// nsecKey = privateKeyHex;
// final decoded = Nostr.instance.keysService
// .decodeNsecKeyToPrivateKey(privateKeyHex);
// privateKeyHex = decoded;
// publicKeyHex = Nostr.instance.keysService
// .derivePublicKey(privateKey: 'privateKeyHex');
// npubKey = Nostr.instance.keysService
// .encodePublicKeyToNpub(publicKeyHex);
// } else {
// publicKeyHex = Nostr.instance.keysService
// .derivePublicKey(privateKey: 'privateKeyHex');
// nsecKey = Nostr.instance.keysService
// .encodePrivateKeyToNsec(privateKeyHex);
// npubKey = Nostr.instance.keysService
// .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')) {
// nsecKey = privateKeyHex;
// final decoded = Nostr.instance.keysService
// .decodeNsecKeyToPrivateKey(privateKeyHex);
// privateKeyHex = decoded;
// publicKeyHex = Nostr.instance.keysService
// .derivePublicKey(privateKey: 'privateKeyHex');
// npubKey = Nostr.instance.keysService
// .encodePublicKeyToNpub(publicKeyHex);
// } else {
// publicKeyHex = Nostr.instance.keysService
// .derivePublicKey(privateKey: 'privateKeyHex');
// nsecKey = Nostr.instance.keysService
// .encodePrivateKeyToNsec(privateKeyHex);
// npubKey = Nostr.instance.keysService
// .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);
}
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/profile_screen/profile_screen.dart';
import 'package:drifter/theme/app_colors.dart';
import 'package:drifter/main.dart';
import 'package:drifter/utilities/assets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
@ -59,7 +60,7 @@ class _MainScreenWidgetState extends State<MainScreenWidget> {
HomeScreen(),
MessageScreen(),
ProfileScreen(),
LoginScreen(),
// LoginScreen(),
],
),
bottomNavigationBar: BottomNavigationBar(
@ -77,10 +78,10 @@ class _MainScreenWidgetState extends State<MainScreenWidget> {
icon: Icon(Icons.person),
label: 'Profile',
),
BottomNavigationBarItem(
icon: Icon(Icons.login),
label: 'Login',
),
// BottomNavigationBarItem(
// icon: Icon(Icons.login),
// label: 'Login',
// ),
],
onTap: onSelectedtap,
),

View File

@ -1,6 +1,7 @@
import 'package:dart_nostr/dart_nostr.dart';
import 'package:drifter/models/keys.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/keys_option_modal_bottom_sheet.dart';
import 'package:drifter/pages/profile_screen/widgets/message_snack_bar.dart';
@ -174,16 +175,34 @@ class ProfileScreenState extends State<ProfileScreen> {
// 'Keys',
// ),
// )
: ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(AppColors.background)),
onPressed: () {
modalBottomSheet();
},
child: const Text(
'Generate Keys',
),
: Row(
children: [
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
AppColors.background)),
onPressed: () {
modalBottomSheet();
},
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
? Row(