Skip to content

Commit

Permalink
feat: improve nostr connect flow
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed May 31, 2024
1 parent 6676b4e commit d5b1593
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
20 changes: 17 additions & 3 deletions packages/system/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Metadata } from "@lume/types";
import { commands } from "./commands";
import { Result, commands } from "./commands";

export class NostrAccount {
static async getAccounts() {
Expand All @@ -13,7 +13,14 @@ export class NostrAccount {
}

static async loadAccount(npub: string) {
const query = await commands.loadAccount(npub);
const bunker: string = localStorage.getItem(`${npub}_bunker`);
let query: Result<boolean, string>;

if (bunker?.length && bunker?.startsWith("bunker://")) {
query = await commands.loadAccount(npub, bunker);
} else {
query = await commands.loadAccount(npub, null);
}

if (query.status === "ok") {
return query.data;
Expand Down Expand Up @@ -65,7 +72,14 @@ export class NostrAccount {
const connect = await commands.connectRemoteAccount(uri);

if (connect.status === "ok") {
return connect.data;
const npub = connect.data;
const parsed = new URL(uri);
parsed.searchParams.delete("secret");

// save connection string
localStorage.setItem(`${npub}_bunker`, parsed.toString());

return npub;
} else {
throw new Error(connect.error);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/system/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ try {
else return { status: "error", error: e as any };
}
},
async loadAccount(npub: string) : Promise<Result<boolean, string>> {
async loadAccount(npub: string, bunker: string | null) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("load_account", { npub }) };
return { status: "ok", data: await TAURI_INVOKE("load_account", { npub, bunker }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
Expand Down
32 changes: 28 additions & 4 deletions src-tauri/src/nostr/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,36 @@ pub async fn save_account(
#[specta::specta]
pub async fn load_account(
npub: &str,
bunker: Option<&str>,
state: State<'_, Nostr>,
app: tauri::AppHandle,
) -> Result<bool, String> {
let client = &state.client;
let keyring = Entry::new(npub, "nostr_secret").unwrap();

if let Ok(password) = keyring.get_password() {
let keys = Keys::parse(password).expect("Secret Key is modified, please check again.");
let signer = NostrSigner::Keys(keys);
match bunker {
Some(uri) => {
let app_keys = Keys::parse(password).expect("Secret Key is modified, please check again.");

match NostrConnectURI::parse(uri) {
Ok(bunker_uri) => {
match Nip46Signer::new(bunker_uri, app_keys, Duration::from_secs(30), None).await {
Ok(signer) => client.set_signer(Some(signer.into())).await,
Err(err) => return Err(err.to_string()),
}
}
Err(err) => return Err(err.to_string()),
}
}
None => {
let keys = Keys::parse(password).expect("Secret Key is modified, please check again.");
let signer = NostrSigner::Keys(keys);

// Update signer
client.set_signer(Some(signer)).await;
// Update signer
client.set_signer(Some(signer)).await;
}
}

// Verify signer
let signer = client.signer().await.unwrap();
Expand Down Expand Up @@ -207,14 +225,20 @@ pub async fn connect_remote_account(uri: &str, state: State<'_, Nostr>) -> Resul
match NostrConnectURI::parse(uri) {
Ok(bunker_uri) => {
let app_keys = Keys::generate();
let app_secret = app_keys.secret_key().unwrap().to_string();

// Get remote user
let remote_user = bunker_uri.signer_public_key().unwrap();
let remote_npub = remote_user.to_bech32().unwrap();

match Nip46Signer::new(bunker_uri, app_keys, Duration::from_secs(120), None).await {
Ok(signer) => {
let keyring = Entry::new(&remote_npub, "nostr_secret").unwrap();
let _ = keyring.set_password(&app_secret);

// Update signer
let _ = client.set_signer(Some(signer.into())).await;

Ok(remote_npub.into())
}
Err(err) => Err(err.to_string()),
Expand Down

0 comments on commit d5b1593

Please sign in to comment.