Skip to content

Commit

Permalink
feat: allow logging in with nip05
Browse files Browse the repository at this point in the history
  • Loading branch information
verbiricha committed Jan 18, 2024
1 parent 91237b2 commit 75091d3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/badges/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const metadata = {
metadataBase: new URL('https://badges.page'),
metadataBase: new URL("https://badges.page"),
title: "Badges",
description: "Create, collect and award badges",
};
Expand Down
6 changes: 5 additions & 1 deletion apps/emojis/ui/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { theme } from "./theme";

const cacheAdapter = new NDKCacheAdapterDexie({ dbName: "emojis" });
const ndk = new NDK({
explicitRelayUrls: ["wss://nos.lol", "wss://relay.nostr.band", "wss://frens.nostr1.com"],
explicitRelayUrls: [
"wss://nos.lol",
"wss://relay.nostr.band",
"wss://frens.nostr1.com",
],
outboxRelayUrls: ["wss://purplepag.es"],
enableOutboxModel: true,
cacheAdapter,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function LoginBunker({ onLogin }: LoginProps) {
placeholder={formatMessage({
id: "ngine.bunker-url-placeholder",
description: "Bunker URL placeholder",
defaultMessage: "bunker://",
defaultMessage: "bunker:// URL or nostr address",
})}
value={url}
onChange={(ev) => setUrl(ev.target.value)}
Expand Down
70 changes: 49 additions & 21 deletions packages/core/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const NgineProvider = ({
const signer = new NDKPrivateKeySigner(session.privkey);
ndk.signer = signer;
} else if (session?.method === "nip46" && session.bunker) {
const { privkey, pubkey, relays } = session.bunker;
const { privkey, relays } = session.bunker;
const localSigner = new NDKPrivateKeySigner(privkey);
const bunkerNDK = new NDK({ explicitRelayUrls: relays });
bunkerNDK.connect().then(() => {
Expand All @@ -175,6 +175,9 @@ export const NgineProvider = ({
session.pubkey,
localSigner,
);
signer.on("authUrl", (url) => {
window.open(url, "auth", "width=600,height=600");
});
ndk.signer = signer;
});
}
Expand All @@ -194,30 +197,55 @@ export const NgineProvider = ({
return user;
}

async function nip46Login(url: string) {
const asURL = new URL(url);
const relays = asURL.searchParams.getAll("relay");
const pubkey = asURL.pathname.replace(/^\/\//, "");
const bunkerNDK = new NDK({
explicitRelayUrls: relays,
});
await bunkerNDK.connect();
const localSigner = NDKPrivateKeySigner.generate();
const signer = new NDKNip46Signer(bunkerNDK, pubkey, localSigner);
const user = await signer.blockUntilReady();
if (user) {
ndk.signer = signer;
setSession({
method: "nip46",
pubkey: user.pubkey,
bunker: {
privkey: localSigner.privateKey as string,
async function getNostrConnectSettings(url: string) {
if (url.includes("bunker://")) {
const asURL = new URL(url);
const relays = asURL.searchParams.getAll("relay");
const pubkey = asURL.pathname.replace(/^\/\//, "");
return { relays, pubkey };
} else {
const user = await NDKUser.fromNip05(url, ndk);
if (user) {
const pubkey = user.hexpubkey;
const relays =
user.nip46Urls?.length > 0
? user.nip46Urls
: ["wss://relay.nsecbunker.com"];
return {
pubkey,
relays,
},
};
}
}
}

async function nip46Login(url: string) {
const settings = await getNostrConnectSettings(url);
if (settings) {
const { pubkey, relays } = settings;
const bunkerNDK = new NDK({
explicitRelayUrls: relays,
});
await bunkerNDK.connect();
const localSigner = NDKPrivateKeySigner.generate();
const signer = new NDKNip46Signer(bunkerNDK, pubkey, localSigner);
signer.on("authUrl", (url) => {
window.open(url, "auth", "width=600,height=600");
});
const user = await signer.blockUntilReady();
if (user) {
ndk.signer = signer;
setSession({
method: "nip46",
pubkey,
bunker: {
privkey: localSigner.privateKey as string,
relays,
},
});
}
return user;
}
return user;
}

async function npubLogin(pubkey: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lang.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "A button for logging in with a nsecbunker"
},
"ngine.bunker-url-placeholder": {
"defaultMessage": "bunker://",
"defaultMessage": "bunker:// URL or nostr address",
"description": "Bunker URL placeholder"
},
"ngine.comment-label": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ngine.bookmarks-count": "Bookmarks ({count})",
"ngine.bunker-login": "Log in",
"ngine.bunker-url-placeholder": "bunker://",
"ngine.bunker-url-placeholder": "bunker:// URL or nostr address",
"ngine.comment-label": "Comment",
"ngine.comment-placeholder": "Type your reply here",
"ngine.copy": "Copy",
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export interface Session {
privkey?: string;
bunker?: {
privkey: string;
pubkey: string;
relays: string[];
};
}
Expand Down

2 comments on commit 75091d3

@vercel
Copy link

@vercel vercel bot commented on 75091d3 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 75091d3 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.