Option to import encrypted private key

This commit is contained in:
Mike Dilger 2023-01-20 18:16:04 +13:00
parent 50c1612651
commit d1fcdeca3b
3 changed files with 39 additions and 17 deletions

View File

@ -11,7 +11,8 @@ use crate::tags::{
};
use minion::Minion;
use nostr_types::{
Event, EventKind, Id, IdHex, PreEvent, PrivateKey, PublicKey, PublicKeyHex, Tag, Unixtime, Url,
EncryptedPrivateKey, Event, EventKind, Id, IdHex, PreEvent, PrivateKey, PublicKey,
PublicKeyHex, Tag, Unixtime, Url,
};
use relay_picker::{BestRelay, RelayPicker};
use std::collections::HashMap;
@ -410,22 +411,33 @@ impl Overlord {
GLOBALS.signer.read().await.save_through_settings().await?;
}
ToOverlordMessage::ImportPriv(mut import_priv, mut password) => {
let maybe_pk1 = PrivateKey::try_from_bech32_string(&import_priv);
let maybe_pk2 = PrivateKey::try_from_hex_string(&import_priv);
import_priv.zeroize();
if maybe_pk1.is_err() && maybe_pk2.is_err() {
password.zeroize();
*GLOBALS.status_message.write().await =
"Private key not recognized.".to_owned();
} else {
let privkey = maybe_pk1.unwrap_or_else(|_| maybe_pk2.unwrap());
if import_priv.starts_with("ncryptsec") {
let epk = EncryptedPrivateKey(import_priv);
GLOBALS.signer.write().await.set_encrypted_private_key(epk);
GLOBALS
.signer
.write()
.await
.set_private_key(privkey, &password)?;
.unlock_encrypted_private_key(&password)?;
password.zeroize();
GLOBALS.signer.read().await.save_through_settings().await?;
} else {
let maybe_pk1 = PrivateKey::try_from_bech32_string(&import_priv);
let maybe_pk2 = PrivateKey::try_from_hex_string(&import_priv);
import_priv.zeroize();
if maybe_pk1.is_err() && maybe_pk2.is_err() {
password.zeroize();
*GLOBALS.status_message.write().await =
"Private key not recognized.".to_owned();
} else {
let privkey = maybe_pk1.unwrap_or_else(|_| maybe_pk2.unwrap());
GLOBALS
.signer
.write()
.await
.set_private_key(privkey, &password)?;
password.zeroize();
GLOBALS.signer.read().await.save_through_settings().await?;
}
}
}
ToOverlordMessage::ImportPub(pubstr) => {

View File

@ -72,9 +72,10 @@ impl Signer {
} else if let Some(epk) = &self.encrypted {
self.private = Some(epk.decrypt(pass)?);
// If older version, re-encrypt with new version at default 2^18 rounds
if let Some(private) = &self.private {
// it will
// it will be
// If older version, re-encrypt with new version at default 2^18 rounds
if epk.version()? < 2 {
self.encrypted = Some(private.export_encrypted(pass, DEFAULT_LOG_N)?);
// and eventually save
@ -84,6 +85,10 @@ impl Signer {
}
});
}
if self.public.is_none() {
self.public = Some(private.public_key());
}
}
Ok(())

View File

@ -184,12 +184,13 @@ fn offer_import_priv_key(app: &mut GossipUi, ui: &mut Ui) {
ui.label("Enter private key");
ui.add(
TextEdit::singleline(&mut app.import_priv)
.hint_text("nsec1 or hex")
.hint_text("ncryptsec1, nsec1, or hex")
.desired_width(f32::INFINITY)
.password(true),
);
});
ui.horizontal(|ui| {
ui.label("Enter a password to keep it encrypted under");
ui.label("Enter a password for the private key");
ui.add(TextEdit::singleline(&mut app.password).password(true));
});
if ui.button("import").clicked() {
@ -234,7 +235,11 @@ fn offer_import_pub_key(app: &mut GossipUi, ui: &mut Ui) {
} else {
ui.horizontal_wrapped(|ui| {
ui.label("Enter your public key");
ui.add(TextEdit::singleline(&mut app.import_pub).hint_text("npub1 or hex"));
ui.add(
TextEdit::singleline(&mut app.import_pub)
.hint_text("npub1 or hex")
.desired_width(f32::INFINITY),
);
if ui.button("Import a Public Key").clicked() {
let _ = GLOBALS
.to_overlord