nostr tools added
This commit is contained in:
@@ -26,6 +26,10 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
Map<String, bool> _connectionStatus = {};
|
||||
List<String> _events = [];
|
||||
bool _isLoading = false;
|
||||
final TextEditingController _nsecController = TextEditingController();
|
||||
final TextEditingController _npubController = TextEditingController();
|
||||
final TextEditingController _hexPrivateKeyController = TextEditingController();
|
||||
bool _showImportFields = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -34,6 +38,14 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
_generateKeyPair();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nsecController.dispose();
|
||||
_npubController.dispose();
|
||||
_hexPrivateKeyController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _loadRelays() {
|
||||
if (widget.nostrService == null) return;
|
||||
|
||||
@@ -50,9 +62,115 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
|
||||
setState(() {
|
||||
_keyPair = widget.nostrService!.generateKeyPair();
|
||||
_nsecController.clear();
|
||||
_npubController.clear();
|
||||
_hexPrivateKeyController.clear();
|
||||
_showImportFields = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _importFromNsec() {
|
||||
final nsec = _nsecController.text.trim();
|
||||
if (nsec.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Please enter an nsec key'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setState(() {
|
||||
_keyPair = NostrKeyPair.fromNsec(nsec);
|
||||
_showImportFields = false;
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Keypair imported from nsec'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Failed to import nsec: ${e.toString()}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _importFromNpub() {
|
||||
final npub = _npubController.text.trim();
|
||||
if (npub.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Please enter an npub key'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setState(() {
|
||||
_keyPair = NostrKeyPair.fromNpub(npub);
|
||||
_showImportFields = false;
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Public key imported from npub (note: cannot sign events without private key)'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Failed to import npub: ${e.toString()}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _importFromHexPrivateKey() {
|
||||
final hexKey = _hexPrivateKeyController.text.trim();
|
||||
if (hexKey.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Please enter a hex private key'),
|
||||
backgroundColor: Colors.orange,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setState(() {
|
||||
_keyPair = NostrKeyPair.fromHexPrivateKey(hexKey);
|
||||
_showImportFields = false;
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Keypair imported from hex private key'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Failed to import hex key: ${e.toString()}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _connectToRelay(String relayUrl) async {
|
||||
if (widget.nostrService == null) return;
|
||||
|
||||
@@ -149,6 +267,21 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
if (_keyPair!.privateKey.isEmpty) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Cannot publish: No private key available. Import nsec or hex private key.'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a test event
|
||||
final event = NostrEvent.create(
|
||||
@@ -237,28 +370,112 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Keypair',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'Keypair',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
TextButton.icon(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_showImportFields = !_showImportFields;
|
||||
});
|
||||
},
|
||||
icon: Icon(_showImportFields ? Icons.visibility_off : Icons.import_export),
|
||||
label: Text(_showImportFields ? 'Hide Import' : 'Import Key'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (_keyPair != null) ...[
|
||||
Text(
|
||||
'Public Key: ${_keyPair!.publicKey.substring(0, 16)}...',
|
||||
style: const TextStyle(fontSize: 12, fontFamily: 'monospace'),
|
||||
const Text(
|
||||
'Public Key:',
|
||||
style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Private Key: ${_keyPair!.privateKey.substring(0, 16)}...',
|
||||
style: const TextStyle(fontSize: 12, fontFamily: 'monospace'),
|
||||
SelectableText(
|
||||
_keyPair!.publicKey,
|
||||
style: const TextStyle(fontSize: 11, fontFamily: 'monospace'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (_keyPair!.privateKey.isNotEmpty) ...[
|
||||
const Text(
|
||||
'Private Key:',
|
||||
style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
|
||||
),
|
||||
SelectableText(
|
||||
_keyPair!.privateKey,
|
||||
style: const TextStyle(fontSize: 11, fontFamily: 'monospace'),
|
||||
),
|
||||
] else ...[
|
||||
const Text(
|
||||
'Private Key: (not available - imported from npub only)',
|
||||
style: TextStyle(fontSize: 12, color: Colors.orange),
|
||||
),
|
||||
],
|
||||
] else ...[
|
||||
const Text('No keypair generated'),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
if (_showImportFields) ...[
|
||||
const Divider(),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Import Key',
|
||||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _nsecController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'nsec (private key)',
|
||||
hintText: 'nsec1...',
|
||||
border: OutlineInputBorder(),
|
||||
helperText: 'Enter your nsec private key',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _importFromNsec,
|
||||
child: const Text('Import from nsec'),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _npubController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'npub (public key)',
|
||||
hintText: 'npub1...',
|
||||
border: OutlineInputBorder(),
|
||||
helperText: 'Enter npub to view only (cannot sign events)',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _importFromNpub,
|
||||
child: const Text('Import from npub'),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _hexPrivateKeyController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Hex Private Key',
|
||||
hintText: '64 hex characters',
|
||||
border: OutlineInputBorder(),
|
||||
helperText: 'Enter private key in hex format (64 characters)',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _importFromHexPrivateKey,
|
||||
child: const Text('Import from Hex'),
|
||||
),
|
||||
const Divider(),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
ElevatedButton.icon(
|
||||
onPressed: _generateKeyPair,
|
||||
icon: const Icon(Icons.refresh),
|
||||
|
||||
@@ -24,8 +24,10 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
final TextEditingController _userIdController = TextEditingController();
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
final TextEditingController _nostrKeyController = TextEditingController();
|
||||
bool _isLoading = false;
|
||||
bool _useFirebaseAuth = false;
|
||||
bool _useNostrLogin = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -41,6 +43,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
_userIdController.dispose();
|
||||
_emailController.dispose();
|
||||
_passwordController.dispose();
|
||||
_nostrKeyController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -52,6 +55,50 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
});
|
||||
|
||||
try {
|
||||
// Handle Nostr login
|
||||
if (_useNostrLogin) {
|
||||
final nostrKey = _nostrKeyController.text.trim();
|
||||
|
||||
if (nostrKey.isEmpty) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Please enter nsec or npub key'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate format
|
||||
if (!nostrKey.startsWith('nsec') && !nostrKey.startsWith('npub')) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Invalid key format. Expected nsec or npub.'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Login with Nostr
|
||||
await widget.sessionService!.loginWithNostr(nostrKey);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Nostr login successful'),
|
||||
),
|
||||
);
|
||||
setState(() {});
|
||||
widget.onSessionChanged?.call();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle Firebase or regular login
|
||||
if (_useFirebaseAuth && widget.firebaseService != null) {
|
||||
// Use Firebase Auth for authentication
|
||||
final email = _emailController.text.trim();
|
||||
@@ -246,7 +293,56 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text('User ID: ${currentUser.id}'),
|
||||
// Display Nostr profile if available
|
||||
if (currentUser.nostrProfile != null) ...[
|
||||
Row(
|
||||
children: [
|
||||
if (currentUser.nostrProfile!.picture != null)
|
||||
CircleAvatar(
|
||||
radius: 30,
|
||||
backgroundImage: NetworkImage(
|
||||
currentUser.nostrProfile!.picture!,
|
||||
),
|
||||
onBackgroundImageError: (_, __) {},
|
||||
)
|
||||
else
|
||||
const CircleAvatar(
|
||||
radius: 30,
|
||||
child: Icon(Icons.person),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
currentUser.nostrProfile!.name ??
|
||||
currentUser.nostrProfile!.displayName,
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
if (currentUser.nostrProfile!.about != null)
|
||||
Text(
|
||||
currentUser.nostrProfile!.about!,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Divider(),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
Text('User ID: ${currentUser.id.substring(0, currentUser.id.length > 32 ? 32 : currentUser.id.length)}${currentUser.id.length > 32 ? '...' : ''}'),
|
||||
Text('Username: ${currentUser.username}'),
|
||||
Text(
|
||||
'Created: ${DateTime.fromMillisecondsSinceEpoch(currentUser.createdAt).toString().split('.')[0]}',
|
||||
@@ -306,7 +402,39 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 24),
|
||||
if (_useFirebaseAuth) ...[
|
||||
// Login method selector
|
||||
SegmentedButton<bool>(
|
||||
segments: const [
|
||||
ButtonSegment<bool>(
|
||||
value: false,
|
||||
label: Text('Regular'),
|
||||
),
|
||||
ButtonSegment<bool>(
|
||||
value: true,
|
||||
label: Text('Nostr'),
|
||||
),
|
||||
],
|
||||
selected: {_useNostrLogin},
|
||||
onSelectionChanged: (Set<bool> newSelection) {
|
||||
setState(() {
|
||||
_useNostrLogin = newSelection.first;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
if (_useNostrLogin) ...[
|
||||
TextField(
|
||||
controller: _nostrKeyController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nostr Key (nsec or npub)',
|
||||
hintText: 'Enter your nsec or npub key',
|
||||
border: OutlineInputBorder(),
|
||||
helperText: 'Enter your Nostr private key (nsec) or public key (npub)',
|
||||
),
|
||||
maxLines: 3,
|
||||
minLines: 1,
|
||||
),
|
||||
] else if (_useFirebaseAuth) ...[
|
||||
TextField(
|
||||
controller: _emailController,
|
||||
decoration: const InputDecoration(
|
||||
|
||||
Reference in New Issue
Block a user