Skip to content

Commit

Permalink
fix: cannot import ncryptsec
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Aug 29, 2024
1 parent d128af1 commit 74d8bf2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
42 changes: 22 additions & 20 deletions src-tauri/src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,40 @@ pub async fn create_account(

#[tauri::command]
#[specta::specta]
pub async fn import_account(
key: String,
password: Option<String>,
state: State<'_, Nostr>,
) -> Result<String, String> {
let client = &state.client;
let secret_key = SecretKey::from_bech32(key).map_err(|err| err.to_string())?;
let keys = Keys::new(secret_key.clone());
let npub = keys.public_key().to_bech32().unwrap();
pub async fn import_account(key: String, password: String) -> Result<String, String> {
let (npub, enc_bech32) = match key.starts_with("ncryptsec") {
true => {
let enc = EncryptedSecretKey::from_bech32(key).map_err(|err| err.to_string())?;
let enc_bech32 = enc.to_bech32().map_err(|err| err.to_string())?;
let secret_key = enc.to_secret_key(password).map_err(|err| err.to_string())?;
let keys = Keys::new(secret_key);
let npub = keys.public_key().to_bech32().unwrap();

(npub, enc_bech32)
}
false => {
let secret_key = SecretKey::from_bech32(key).map_err(|err| err.to_string())?;
let keys = Keys::new(secret_key.clone());
let npub = keys.public_key().to_bech32().unwrap();

let enc_bech32 = match password {
Some(pw) => {
let enc = EncryptedSecretKey::new(&secret_key, pw, 16, KeySecurity::Medium)
let enc = EncryptedSecretKey::new(&secret_key, password, 16, KeySecurity::Medium)
.map_err(|err| err.to_string())?;

enc.to_bech32().map_err(|err| err.to_string())?
let enc_bech32 = enc.to_bech32().map_err(|err| err.to_string())?;

(npub, enc_bech32)
}
None => secret_key.to_bech32().map_err(|err| err.to_string())?,
};

let keyring = Entry::new("Lume Secret Storage", &npub).map_err(|e| e.to_string())?;

let account = Account {
password: enc_bech32,
nostr_connect: None,
};
let j = serde_json::to_string(&account).map_err(|e| e.to_string())?;
let _ = keyring.set_password(&j);

let signer = NostrSigner::Keys(keys);

// Update client's signer
client.set_signer(Some(signer)).await;
let pwd = serde_json::to_string(&account).map_err(|e| e.to_string())?;
keyring.set_password(&pwd).map_err(|e| e.to_string())?;

Ok(npub)
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async createAccount(name: string, about: string, picture: string, password: stri
else return { status: "error", error: e as any };
}
},
async importAccount(key: string, password: string | null) : Promise<Result<string, string>> {
async importAccount(key: string, password: string) : Promise<Result<string, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("import_account", { key, password }) };
} catch (e) {
Expand Down
6 changes: 4 additions & 2 deletions src/routes/auth/import.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ function Screen() {
</button>
</div>
</div>
{key.length && !key.startsWith("ncryptsec") ? (
{key.length ? (
<div className="flex flex-col gap-1">
<label
htmlFor="password"
className="text-sm font-medium text-neutral-800 dark:text-neutral-200"
>
Set password to secure your key
{!key.startsWith("ncryptsec")
? "Set password to secure your key"
: "Enter password to decrypt your key"}
</label>
<input
name="password"
Expand Down

0 comments on commit 74d8bf2

Please sign in to comment.