From bfcb0992d437d82f75362f813d15233aac43db4f Mon Sep 17 00:00:00 2001 From: Pablo Fernandez
Date: Tue, 2 Jan 2024 14:46:24 +0000 Subject: [PATCH] allow using nip05 to sign from the client too --- OAUTH-LIKE-FLOW.md | 2 +- src/client.ts | 13 ++++++++++++- src/daemon/web/registration-validations.ts | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/OAUTH-LIKE-FLOW.md b/OAUTH-LIKE-FLOW.md index 9b071e7..19ad412 100644 --- a/OAUTH-LIKE-FLOW.md +++ b/OAUTH-LIKE-FLOW.md @@ -41,7 +41,7 @@ In the background, the bunker will have configured the requested NIP-05 mapping ``` ## NIP-89 -Bunkers supporting user registration can announce themselves using NIP-89's `kind:31990`. Clients SHOULD validate that the 31990 is from a pubkey that owns the root NIP-05 of the domain. +Bunkers supporting user registration can announce themselves using NIP-89's `kind:31990`. Clients MUST validate that the 31990 is from a pubkey that owns the root NIP-05 of the domain. ```json { diff --git a/src/client.ts b/src/client.ts index 5741cc0..dee08cb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -85,7 +85,18 @@ function loadPrivateKey(): string | undefined { remoteUser = u; remotePubkey = remoteUser.pubkey; } else { - remoteUser = new NDKUser({npub: remotePubkey}); + // check if we have a @ so we try to get the npub from nip05 + if (remotePubkey.includes('@')) { + const u = await NDKUser.fromNip05(remotePubkey); + if (!u) { + console.log(`Invalid nip05 ${remotePubkey}`); + process.exit(1); + } + remoteUser = u; + remotePubkey = remoteUser.pubkey; + } else { + remoteUser = new NDKUser({npub: remotePubkey}); + } } ndk = await createNDK(); diff --git a/src/daemon/web/registration-validations.ts b/src/daemon/web/registration-validations.ts index 7c5affa..182b65c 100644 --- a/src/daemon/web/registration-validations.ts +++ b/src/daemon/web/registration-validations.ts @@ -19,7 +19,7 @@ export async function validateRegistration(request, record) { if (!email.includes("@")) throw new Error("Invalid email address"); // validate email uniqueness (if one was provided) - const emailRecord = await prisma.user.findUnique({ where: { email } }); + const emailRecord = await prisma.user.findFirst({ where: { email } }); if (emailRecord) throw new Error("Email already exists"); } } \ No newline at end of file