From 14c1e533d64a901ac8bef5e77b1bf7f608643f78 Mon Sep 17 00:00:00 2001 From: artur Date: Wed, 6 Mar 2024 17:10:29 +0300 Subject: [PATCH] Dedup auth_urls, fix nip05 login not showing auth_url, add requested perms to connect/create_account --- src/services/nostr-connect.ts | 23 +++++++++++++++-------- src/views/signin/address/index.tsx | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/services/nostr-connect.ts b/src/services/nostr-connect.ts index 8699f2c39..89990e8e9 100644 --- a/src/services/nostr-connect.ts +++ b/src/services/nostr-connect.ts @@ -28,8 +28,8 @@ export enum NostrConnectMethod { Nip04Decrypt = "nip04_decrypt", } type RequestParams = { - [NostrConnectMethod.Connect]: [string] | [string, string]; - [NostrConnectMethod.CreateAccount]: [string, string] | [string, string, string]; + [NostrConnectMethod.Connect]: [string] | [string, string] | [string, string, string]; + [NostrConnectMethod.CreateAccount]: [string, string] | [string, string, string] | [string, string, string, string]; [NostrConnectMethod.Disconnect]: []; [NostrConnectMethod.GetPublicKey]: []; [NostrConnectMethod.SignEvent]: [string]; @@ -57,6 +57,9 @@ export type NostrConnectErrorResponse = { error: string; }; +// FIXME list all requested perms +const Perms = "nip04_encrypt,nip04_decrypt,sign_event:0,sign_event:1,sign_event:3,sign_event:4,sign_event:6,sign_event:7" + export class NostrConnectClient { sub: NostrMultiSubscription; log = logger.extend("NostrConnectClient"); @@ -107,6 +110,7 @@ export class NostrConnectClient { } private requests = new Map>(); + private auths = new Set(); async handleEvent(event: NostrEvent) { if (this.provider && event.pubkey !== this.provider) return; @@ -122,10 +126,13 @@ export class NostrConnectClient { if (response.error) { this.log("Got Error", response.id, response.result, response.error); if (response.result === "auth_url") { - try { - await this.handleAuthURL(response.error); - } catch (e) { - p.reject(e); + if (!this.auths.has(response.id)) { + this.auths.add(response.id) + try { + await this.handleAuthURL(response.error); + } catch (e) { + p.reject(e); + } } } else p.reject(response); } else if (response.result) { @@ -186,7 +193,7 @@ export class NostrConnectClient { try { const result = await this.makeRequest( NostrConnectMethod.Connect, - token ? [this.publicKey, token] : [this.publicKey], + [this.publicKey, token || '', Perms], ); this.isConnected = true; return result; @@ -203,7 +210,7 @@ export class NostrConnectClient { try { const newPubkey = await this.makeAdminRequest( NostrConnectMethod.CreateAccount, - email ? [name, domain, email] : [name, domain], + [name, domain, email || '', Perms], ); this.pubkey = newPubkey; this.isConnected = true; diff --git a/src/views/signin/address/index.tsx b/src/views/signin/address/index.tsx index 0000472a9..cf24e61cc 100644 --- a/src/views/signin/address/index.tsx +++ b/src/views/signin/address/index.tsx @@ -42,7 +42,7 @@ export default function LoginNostrAddressView() { if (nip05.hasNip46) { setLoading("Connecting..."); const relays = safeRelayUrls(nip05.nip46Relays || rootNip05?.nip46Relays || rootNip05?.relays || nip05.relays); - const client = nostrConnectService.fromHostedBunker(nip05.pubkey, relays, rootNip05?.pubkey); + const client = nostrConnectService.fromHostedBunker(nip05.pubkey, relays); await client.connect(); nostrConnectService.saveClient(client);