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),
|
||||
|
||||
Reference in New Issue
Block a user