Dedup auth_urls, fix nip05 login not showing auth_url, add requested perms to connect/create_account

This commit is contained in:
artur
2024-03-06 17:10:29 +03:00
parent fe0e913a77
commit 14c1e533d6
2 changed files with 16 additions and 9 deletions

View File

@@ -28,8 +28,8 @@ export enum NostrConnectMethod {
Nip04Decrypt = "nip04_decrypt", Nip04Decrypt = "nip04_decrypt",
} }
type RequestParams = { type RequestParams = {
[NostrConnectMethod.Connect]: [string] | [string, string]; [NostrConnectMethod.Connect]: [string] | [string, string] | [string, string, string];
[NostrConnectMethod.CreateAccount]: [string, string] | [string, string, string]; [NostrConnectMethod.CreateAccount]: [string, string] | [string, string, string] | [string, string, string, string];
[NostrConnectMethod.Disconnect]: []; [NostrConnectMethod.Disconnect]: [];
[NostrConnectMethod.GetPublicKey]: []; [NostrConnectMethod.GetPublicKey]: [];
[NostrConnectMethod.SignEvent]: [string]; [NostrConnectMethod.SignEvent]: [string];
@@ -57,6 +57,9 @@ export type NostrConnectErrorResponse = {
error: string; 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 { export class NostrConnectClient {
sub: NostrMultiSubscription; sub: NostrMultiSubscription;
log = logger.extend("NostrConnectClient"); log = logger.extend("NostrConnectClient");
@@ -107,6 +110,7 @@ export class NostrConnectClient {
} }
private requests = new Map<string, Deferred<any>>(); private requests = new Map<string, Deferred<any>>();
private auths = new Set<string>();
async handleEvent(event: NostrEvent) { async handleEvent(event: NostrEvent) {
if (this.provider && event.pubkey !== this.provider) return; if (this.provider && event.pubkey !== this.provider) return;
@@ -122,10 +126,13 @@ export class NostrConnectClient {
if (response.error) { if (response.error) {
this.log("Got Error", response.id, response.result, response.error); this.log("Got Error", response.id, response.result, response.error);
if (response.result === "auth_url") { if (response.result === "auth_url") {
try { if (!this.auths.has(response.id)) {
await this.handleAuthURL(response.error); this.auths.add(response.id)
} catch (e) { try {
p.reject(e); await this.handleAuthURL(response.error);
} catch (e) {
p.reject(e);
}
} }
} else p.reject(response); } else p.reject(response);
} else if (response.result) { } else if (response.result) {
@@ -186,7 +193,7 @@ export class NostrConnectClient {
try { try {
const result = await this.makeRequest( const result = await this.makeRequest(
NostrConnectMethod.Connect, NostrConnectMethod.Connect,
token ? [this.publicKey, token] : [this.publicKey], [this.publicKey, token || '', Perms],
); );
this.isConnected = true; this.isConnected = true;
return result; return result;
@@ -203,7 +210,7 @@ export class NostrConnectClient {
try { try {
const newPubkey = await this.makeAdminRequest( const newPubkey = await this.makeAdminRequest(
NostrConnectMethod.CreateAccount, NostrConnectMethod.CreateAccount,
email ? [name, domain, email] : [name, domain], [name, domain, email || '', Perms],
); );
this.pubkey = newPubkey; this.pubkey = newPubkey;
this.isConnected = true; this.isConnected = true;

View File

@@ -42,7 +42,7 @@ export default function LoginNostrAddressView() {
if (nip05.hasNip46) { if (nip05.hasNip46) {
setLoading("Connecting..."); setLoading("Connecting...");
const relays = safeRelayUrls(nip05.nip46Relays || rootNip05?.nip46Relays || rootNip05?.relays || nip05.relays); 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(); await client.connect();
nostrConnectService.saveClient(client); nostrConnectService.saveClient(client);