Avatar
DJ_Bitcoin
b0a8fbfd49b1c76b74d1db0f611bcc6def19ef9944f10ec8e3545caed4b34c5d
Introducing the one-of-a-kind Bitcoin DJ, a trailblazer in the world of electronic dance music! With a deep-rooted passion for both digital innovation and electrifying beats, this cutting-edge artist is poised to revolutionize the way we experience music and community engagement.

👉0005 Tech House - Bitcoin Maxi👈

https://cdn.satellite.earth/3086318ca609f041810e086ae7d39f8a7731684218fb8804b185f6ec5fb79ec7.webm

🆔TRACKLIST🆔

00:00:00 1 Dennis Cruz - New Life (Original Mix)

00:06:29 2 Chris IDH - About You (Dennis Cruz Remix)

00:10:54 3 D-Nox & Beckers - La Plata (Tiger Stripes Remix)

00:17:11 4 Bimas - Who Is The Singer (Nikola Gala)

00:23:43 5 DJ Le Roi - I Get Deep feat. Roland Clark (DJ T. Remix)

00:31:08 6 Deca & Dence - Ancient Lines (Original Mix)

00:35:45 7 Elfgrin - Ghost Poet (Lars Wickinger Remix)

00:41:05 8 Brett Gould, Malia Belle - Can't You See (Original Mix)

00:46:29 9 Daniel Muscas - Dun Da (Original Mix)

00:51:56 10 Bontan - The First Time (Original Mix)

00:57:40 11 Emanuel Satie - Now You Know (Original Mix)

01:03:54 12 Dachshund - Only (Original Mix)

01:08:59 13 Brett Gould, Andy MacDougall - Magnetic

01:14:26 14 Cuartero - Mama ()

01:19:38 15 Dario D'Attis - Solera (Original Mix)

We are still dancing :party:

Do you fancy a dance party? Now DJ for about 3 hours. #tunestr

nostr:naddr1qqjrqcfhxv6xzd3j95ex2efn956x2dpe943rvvn9956nwd3hxejn2dp5x9jn2qgwwaehxw309ahx7uewd3hkctcpr9mhxue69uhhyetvv9ujuumwdae8gtnnda3kjctv9uq32amnwvaz7tmjv4kxz7fwv3sk6atn9e5k7tcpzamhxue69uhhyetvv9ujumn0wd68ytnzv9hxgtcpz9mhxue69uhkummnw3ezumrpdejz7qg7waehxw309ahx7um5wgkhqatz9emk2mrvdaexgetj9ehx2ap0qyghwumn8ghj7mn0wd68ytnhd9hx2tcpz4mhxue69uhhyetvv9ujumn0wd68ytnzvuhsz9thwden5te0dehhxarj9ehhsarj9ejx2a30qgsv73dxhgfk8tt76gf6q788zrfyz9dwwgwfk3aar6l5gk82a76v9fgrqsqqqan89c8pcc

nostr:npub1rs2ytg24ayt05c6rcxxqv07tz8gdmwt9ukzvjt7ts8x5twync4ushuc4u6

It would be kind of brilliant if you could use Sats4AI with the llmproxy. Is that already possible? Or can I put it together myself?

https://docs.litellm.ai/docs/simple_proxy

We're celebrating USB-C for everyone!

nostr:naddr1qvzqqqrkvupzpn6956apxcad0mfp8grcuugdysg44eepex68h50t73zcathmfs49qy88wumn8ghj7mn0wvhxcmmv9uq32amnwvaz7tmjv4kxz7fwv3sk6atn9e5k7tcqy3jn2c35x5ersv3dxfjkvdedx33njved8qurze3dv5crvv35xuuxxdfsxcusxk3u7v

👉0003 Bass House - Quantum Computing👈

https://cdn.satellite.earth/102c517b4650d8468f99a44bddc95f45e10a763c1e5f16d4d8e6fcf00110519d.webm

🆔TRACKLIST🆔

00:00:00 1 Yellow Claw, €URO TRA$H - The Function feat. Sky Sky (Original Mix)

00:02:54 2 Blawan - Lavender

00:07:23 3 Mike Cervello, Curbi - The Sub (Original Mix)

00:10:23 4 Mike Cervello, Moksi - The Treble (Original Mix)

00:13:01 5 Moksi - Ring Ding Ding (Original Mix)

00:16:01 6 Yellow Claw, Breaking Beattz, €URO TRA$H - Six Figures feat. Almondmilkhunni (Original Mix)

00:19:15 7 Odd Mob, ShockOne - Follow Me (Odd Mob Remix)

00:22:42 8 Odd Mob, Birthdayy Partyy - Underground (Extended Mix)

00:24:55 9 Curbi - Fuck It Up (Original Mix)

00:28:10 10 €URO TRA$H - No Tinder feat. Ramengvrl (Ootoro Remix)

00:30:21 11 Matroda, Bleu Clair - Disco Tool (David Herrlich Remix)

00:32:38 12 Curbi - What You Like (Original Mix)

00:35:02 13 Hasse de Moor, Curbi - Alcoholic (Extended Mix)

00:38:31 14 Mike Cervello, Curbi - The Funktion (Original Mix)

00:42:23 15 David Herrlich - My Rhythm (Extended Mix)

00:46:19 16 - Albin Myers & Jonth - 2 STEP (Extended)

00:49:11 17 Odd Mob, Basura Boyz - Guest List feat. Basura Boyz (Extended Mix)

00:51:45 18 Boys Noize, Skrillex - Fine Day Anthem (Extended Mix)

00:55:05 19 Curbi - Jaw Drop (Original Mix)

00:57:41 20 Rufus / Rufus Du Sol - Innerbloom (What So Not Remix)

01:01:38 21 Moksi - There is Life (Original Mix)

👉0002 Bass House - Copy Me👈

https://cdn.satellite.earth/7856cbabf0c1f7c13c280f9b783578652744b576a8d054f4c3a870813b545042.webm

🆔TRACKLIST🆔

00:00:00 1 Beam, Skrillex - Selecta (Original Mix)

00:02:20 2 Moksi, Nostalgix - Gimme Bass (Original Mix)

00:06:09 3 Giant, Dot N Life - Moving On (Extended Mix)

00:09:27 4 Space Ducks - Basic (Extended Mix)

00:12:19 5 Ship Wrek - Dance With Me (Original Mix)

00:14:06 6 Walker & Royce - My Stop

00:20:38 7 Eats Everything - Hevvie

00:25:43 8 Scuba - The Hope

00:28:58 9 Anti Up - Hey Pablo (Original Mix)

00:31:40 10 BOMBAYS - GODDESS (Original Mix)

00:35:19 11 Phlegmatic Dogs - Keepmastik (Original Mix)

00:39:11 12 Skrillex, Bobby Raps - Leave Me Like This (Original Mix)

00:40:59 13 Yellow Claw, €URO TRA$H - Broke Bitch feat. Lil Asian Thiccie (Original Mix)

00:43:32 14 Zxx - Get Down

00:48:54 15 Lex Loofah - Music Makes You...

00:57:07 16 Moksi - Calm Down feat. Digitzz and Emy Perez (Original Mix)

01:00:41 17 illusionize, Breaking Beattz - Dv (Original Mix)

01:04:03 18 Big Pineapple, King Topher - Friends (Dot N Life Extended Remix)

01:07:06 19 Matroda - Throw Your Hands (Original Mix)

01:11:02 20 Yellow Claw, €URO TRA$H - 1F2F feat. OGAQUAFINA (Original Mix)

👉0004 Techno - Node👈

https://cdn.satellite.earth/ea9d5154c0f612eb21f418e9f5de125d98285c0f1c0bd5cb5f9be8638990f164.webm

🆔TRACKLIST🆔

00:00:00 1 Kaiserdisco - Eris (Original Mix)

00:07:10 2 Enrico Sangiuliano - Dutch Kiss (Straight Mix)

00:13:41 3 Oliver Carloni - Genome (Original Mix)

00:19:50 4 [ Wex 10 ] - Tv Room (Roberto Capuano Remix)

00:23:54 5 Reboot - Just Hang On (Kölsch Remix)

00:31:19 6 Reset Robot - She

00:36:49 7 Mr Beat - Nasty (Original Mix)

00:41:57 8 Roman Lindau - Beast

00:46:33 9 Enrico Sangiuliano, Secret Cinema - Alone With The Bitch

00:52:34 10 Joshua Calleja - Flow (Original Mix)

00:58:27 11 Andrew Meller - Insomnia - Original Mix

01:03:55 12 Samuel L Session - Four To The Floor (Pig&Dan Remix)

👉0003 Deep House - Pokey👈

https://cdn.satellite.earth/a69962234f93d95888314852118679c6fd6fc35735f1ad32de7743280a07be0a.webm

🆔TRACKLIST🆔

00:00:00 1 Dusky, Janai - Lost In You feat. Janai (Andre Crom Remix)

00:06:24 2 Mann & Klamm - Morning Ride

00:12:16 3 Florian Kruse, Hendrik Burkhard - Moments (Original Mix)

00:18:08 4 Jackie Queens, Cornelius SA - Feel It Now feat. Jackie Queens (Original Mix)

00:23:12 5 Masaje - Celebrate (Dole & Kom Remix)

00:28:56 6 Marsh - Black Mountain (Original Mix)

00:34:32 7 Djuma Soundsystem, Westerby, Djuma Soundsystem & Westerby - Koma Kobache (Ost & Kjex Remix)

00:40:56 8 Dole & Kom - Phara Oh (Original Mix)

00:48:16 9 Modular Project - Leaving (Acid Pauli Remix)

00:53:14 10 Nala - Sun Is Hot (Original Mix)

00:57:51 11 Newman (NYC) - Breath Of The Souls (Original Mix)

01:04:47 12 Dusky - Mr Man (Original Mix)

01:10:55 13 Nacho Portichuelo - Eternal (Original Mix)

From 9b84454f53ff248a1390e9ec7a676093f7669697 Mon Sep 17 00:00:00 2001

From: fsociety

Date: Wed, 2 Oct 2024 00:33:36 +0200

Subject: [PATCH] feat: integrate nostr-login for user authentication

This commit integrates nostr-login for user authentication and removes the previous method. It includes changes in the package.json, Navbar.svelte, users.ts and the creation of a new file nostr-login/index.ts. This new method provides a more streamlined and efficient user authentication process.

---

package.json | 1 +

src/lib/components/Navbar.svelte | 45 +++++++++++++++++----------------------------

src/lib/signers/nostr-login/index.ts | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

src/lib/stores/users.ts | 72 +++++++++++++++++++++++-------------------------------------------------

src/lib/wrappers/Navbar.svelte | 21 ++-------------------

yarn.lock | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------

6 files changed, 240 insertions(+), 109 deletions(-)

create mode 100644 src/lib/signers/nostr-login/index.ts

diff --git a/package.json b/package.json

index 8db6542..2539883 100644

--- a/package.json

+++ b/package.json

@@ -34,6 +34,7 @@

"eslint-config-prettier": "^9.1.0",

"eslint-plugin-prettier": "^5.2.1",

"eslint-plugin-svelte": "^2.44.0",

+ "nostr-login": "^1.6.6",

"postcss": "^8.4.47",

"prettier": "^3.3.3",

"prettier-plugin-svelte": "^3.2.6",

diff --git a/src/lib/components/Navbar.svelte b/src/lib/components/Navbar.svelte

index e66e96d..d5d3d3c 100644

--- a/src/lib/components/Navbar.svelte

+++ b/src/lib/components/Navbar.svelte

@@ -1,13 +1,28 @@

@@ -35,34 +50,8 @@

class="menu dropdown-content z-[1] -mr-4 rounded-box bg-base-400 p-2 shadow"

>

  • -

    -

    -

  • -

    -

    - on:click={() => {

    - logout()

    - }}>Logout

    - >

    -

  • - {:else if nip07_plugin === undefined}

    -

    - {:else if nip07_plugin}

    -

    - on:click={() => {

    - login_function()

    - }}

    - class="btn btn-ghost btn-sm normal-case">Login

    - >

    - {:else}

    -

    - on:click={() => {

    - singup_function()

    - }}

    - class="btn btn-ghost btn-sm normal-case">Sign up

    - >

    {/if}

    diff --git a/src/lib/signers/nostr-login/index.ts b/src/lib/signers/nostr-login/index.ts

    new file mode 100644

    index 0000000..563a8db

    --- /dev/null

    +++ b/src/lib/signers/nostr-login/index.ts

    @@ -0,0 +1,111 @@

    +import NDK, { NDKUser, NDKRelay } from '@nostr-dev-kit/ndk'

    +import type { NDKSigner, NostrEvent } from '@nostr-dev-kit/ndk'

    +

    +export class NDKNostrLoginSigner implements NDKSigner {

    + private _userPromise: Promise | undefined

    +

    + blockUntilReady(): Promise {

    + return new Promise((resolve) => {

    + const eventHandler = (event: Event) => {

    + document.removeEventListener('nlAuth', eventHandler)

    + const customEvent = event as CustomEvent<{ pubkey: string }>

    + resolve(new NDKUser({ pubkey: customEvent.detail.pubkey }))

    + }

    +

    + document.addEventListener('nlAuth', eventHandler)

    + })

    + }

    +

    + user(): Promise {

    + if (!this._userPromise) {

    + this._userPromise = this.blockUntilReady()

    + }

    +

    + return this._userPromise

    + }

    +

    + async sign(event: NostrEvent): Promise {

    + if (!window.nostr) {

    + throw new Error('window.nostr is undefined')

    + }

    + const signedEvent = await window.nostr.signEvent(event)

    + return signedEvent.sig

    + }

    +

    + async relays?(ndk?: NDK): Promise {

    + const relays = (await window.nostr?.getRelays?.()) || {}

    +

    + const activeRelays: string[] = []

    + for (const url of Object.keys(relays)) {

    + // Currently only respects relays that are both readable and writable.

    + if (relays[url].read && relays[url].write) {

    + activeRelays.push(url)

    + }

    + }

    + return activeRelays.map(

    + (url) => new NDKRelay(url, ndk?.relayAuthDefaultPolicy, ndk)

    + )

    + }

    +

    + async encrypt(

    + recipient: NDKUser,

    + value: string,

    + type: string = 'nip04'

    + ): Promise {

    + if (type === 'nip44') {

    + return this.nip44Encrypt(recipient, value)

    + } else {

    + return this.nip04Encrypt(recipient, value)

    + }

    + }

    +

    + async decrypt(

    + sender: NDKUser,

    + value: string,

    + type: string = 'nip04'

    + ): Promise {

    + if (type === 'nip44') {

    + return this.nip44Decrypt(sender, value)

    + } else {

    + return this.nip04Decrypt(sender, value)

    + }

    + }

    +

    + private async nip44Encrypt(

    + recipient: NDKUser,

    + value: string

    + ): Promise {

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + if (!window.nostr || !window.nostr.nip44) {

    + throw new Error('window.nostr or window.nostr.nip44 is undefined')

    + }

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + return await window.nostr.nip44.encrypt(recipient.pubkey, value)

    + }

    +

    + private async nip04Encrypt(

    + recipient: NDKUser,

    + value: string

    + ): Promise {

    + if (!window.nostr || !window.nostr.nip04) {

    + throw new Error('window.nostr or window.nostr.nip04 is undefined')

    + }

    + return await window.nostr.nip04.encrypt(recipient.pubkey, value)

    + }

    +

    + private async nip44Decrypt(sender: NDKUser, value: string): Promise {

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + if (!window.nostr || !window.nostr.nip44) {

    + throw new Error('window.nostr or window.nostr.nip44 is undefined')

    + }

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + return await window.nostr.nip44.decrypt(sender.pubkey, value)

    + }

    +

    + private async nip04Decrypt(sender: NDKUser, value: string): Promise {

    + if (!window.nostr || !window.nostr.nip04) {

    + throw new Error('window.nostr or window.nostr.nip04 is undefined')

    + }

    + return await window.nostr.nip04.decrypt(sender.pubkey, value)

    + }

    +}

    diff --git a/src/lib/stores/users.ts b/src/lib/stores/users.ts

    index 608ffed..40ed744 100644

    --- a/src/lib/stores/users.ts

    +++ b/src/lib/stores/users.ts

    @@ -2,13 +2,12 @@ import {

    defaults as user_defaults,

    type UserObject,

    } from '$lib/components/users/type'

    -import {

    - getRelayListForUser,

    - NDKNip07Signer,

    - NDKRelayList,

    -} from '@nostr-dev-kit/ndk'

    +import { getRelayListForUser, NDKRelayList } from '@nostr-dev-kit/ndk'

    import { get, writable, type Unsubscriber, type Writable } from 'svelte/store'

    import { ndk } from './ndk'

    +import { init as initNostrLogin } from 'nostr-login'

    +import { NDKNostrLoginSigner } from '$lib/signers/nostr-login'

    +import type { NostrLoginAuthOptions } from 'nostr-login/dist/types'

    export const users: { [hexpubkey: string]: Writable } = {}

    @@ -85,31 +84,7 @@ export const returnUser = async (hexpubkey: string): Promise => {

    })

    }

    -// nip07_plugin is set in Navbar component

    -export const nip07_plugin: Writable = writable(undefined)

    -

    -export const checkForNip07Plugin = () => {

    - if (window.nostr) {

    - nip07_plugin.set(true)

    - if (localStorage.getItem('nip07pubkey')) login()

    - } else {

    - let timerId: NodeJS.Timeout | undefined = undefined

    - const intervalId = setInterval(() => {

    - if (window.nostr) {

    - clearTimeout(timerId)

    - clearInterval(intervalId)

    - nip07_plugin.set(true)

    - if (localStorage.getItem('nip07pubkey')) login()

    - }

    - }, 100)

    - timerId = setTimeout(() => {

    - clearInterval(intervalId)

    - nip07_plugin.set(false)

    - }, 5000)

    - }

    -}

    -

    -const signer = new NDKNip07Signer(2000)

    +const signer = new NDKNostrLoginSigner()

    export const logged_in_user: Writable =

    writable(undefined)

    @@ -118,24 +93,24 @@ export const login = async (): Promise => {

    return new Promise(async (res, rej) => {

    const user = get(logged_in_user)

    if (user) return res()

    - if (get(nip07_plugin)) {

    - try {

    - const ndk_user = await signer.blockUntilReady()

    - localStorage.setItem('nip07pubkey', ndk_user.pubkey)

    - logged_in_user.set({

    - ...user_defaults,

    - hexpubkey: ndk_user.pubkey,

    - })

    - ndk.signer = signer

    - ensureUser(ndk_user.pubkey).subscribe((user) => {

    - logged_in_user.set({ ...user })

    - })

    - return res()

    - } catch (e) {

    - alert(e)

    - rej()

    - }

    - } else {

    + try {

    + ndk.signer = signer

    + initNostrLogin({

    + onAuth: async (_npub: string, options: NostrLoginAuthOptions) => {

    + const pubkey =

    + typeof options.pubkey === 'string' ? options.pubkey : ''

    + logged_in_user.set({

    + ...user_defaults,

    + hexpubkey: pubkey,

    + })

    + ensureUser(pubkey).subscribe((user) => {

    + logged_in_user.set({ ...user })

    + })

    + return res()

    + },

    + })

    + } catch (e) {

    + alert(e)

    rej()

    }

    })

    @@ -143,7 +118,6 @@ export const login = async (): Promise => {

    export const logout = async (): Promise => {

    logged_in_user.set(undefined)

    - localStorage.removeItem('nip07pubkey')

    ndk.signer = undefined

    }

    diff --git a/src/lib/wrappers/Navbar.svelte b/src/lib/wrappers/Navbar.svelte

    index 4b63bc7..1f7146f 100644

    --- a/src/lib/wrappers/Navbar.svelte

    +++ b/src/lib/wrappers/Navbar.svelte

    @@ -1,23 +1,6 @@

    -

    - logged_in_user={$logged_in_user}

    - nip07_plugin={$nip07_plugin}

    - login_function={login}

    - {singup_function}

    -/>

    +

    diff --git a/yarn.lock b/yarn.lock

    index f75be09..f976ab4 100644

    --- a/yarn.lock

    +++ b/yarn.lock

    @@ -566,6 +566,13 @@ __metadata:

    languageName: node

    linkType: hard

    +"@noble/ciphers@npm:0.2.0":

    + version: 0.2.0

    + resolution: "@noble/ciphers@npm:0.2.0"

    + checksum: 10c0/57dea65c32741df20a1ac24f365d616a558527109d778c1bec877f20b28875a26b80097bce51ae19529f3792ccf8285fe73839ff404733e32a27a6ebf60edd2c

    + languageName: node

    + linkType: hard

    +

    "@noble/ciphers@npm:^0.5.1":

    version: 0.5.3

    resolution: "@noble/ciphers@npm:0.5.3"

    @@ -573,6 +580,15 @@ __metadata:

    languageName: node

    linkType: hard

    +"@noble/curves@npm:1.1.0, @noble/curves@npm:~1.1.0":

    + version: 1.1.0

    + resolution: "@noble/curves@npm:1.1.0"

    + dependencies:

    + "@noble/hashes": "npm:1.3.1"

    + checksum: 10c0/81115c3ebfa7e7da2d7e18d44d686f98dc6d35dbde3964412c05707c92d0994a01545bc265d5c0bc05c8c49333f75b99c9acef6750f5a79b3abcc8e0546acf88

    + languageName: node

    + linkType: hard

    +

    "@noble/curves@npm:1.2.0":

    version: 1.2.0

    resolution: "@noble/curves@npm:1.2.0"

    @@ -582,7 +598,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/curves@npm:^1.4.0":

    +"@noble/curves@npm:^1.4.0, @noble/curves@npm:^1.6.0":

    version: 1.6.0

    resolution: "@noble/curves@npm:1.6.0"

    dependencies:

    @@ -591,15 +607,6 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/curves@npm:~1.1.0":

    - version: 1.1.0

    - resolution: "@noble/curves@npm:1.1.0"

    - dependencies:

    - "@noble/hashes": "npm:1.3.1"

    - checksum: 10c0/81115c3ebfa7e7da2d7e18d44d686f98dc6d35dbde3964412c05707c92d0994a01545bc265d5c0bc05c8c49333f75b99c9acef6750f5a79b3abcc8e0546acf88

    - languageName: node

    - linkType: hard

    -

    "@noble/hashes@npm:1.3.1":

    version: 1.3.1

    resolution: "@noble/hashes@npm:1.3.1"

    @@ -614,7 +621,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.3.1":

    +"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.5.0":

    version: 1.5.0

    resolution: "@noble/hashes@npm:1.5.0"

    checksum: 10c0/1b46539695fbfe4477c0822d90c881a04d4fa2921c08c552375b444a48cac9930cb1ee68de0a3c7859e676554d0f3771999716606dc4d8f826e414c11692cdd9

    @@ -628,7 +635,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/secp256k1@npm:^2.0.0":

    +"@noble/secp256k1@npm:^2.0.0, @noble/secp256k1@npm:^2.1.0":

    version: 2.1.0

    resolution: "@noble/secp256k1@npm:2.1.0"

    checksum: 10c0/b4c7edd2a5ec5acf294546cd06d08dc2a2a2b2ebe34a6da1f2f5104f56983f81dd31c261ad365c6b9757d1c54017fc3363331ee33bba8715ff94c2bc954313cc

    @@ -692,6 +699,25 @@ __metadata:

    languageName: node

    linkType: hard

    +"@nostr-dev-kit/ndk@npm:^2.3.1":

    + version: 2.10.1

    + resolution: "@nostr-dev-kit/ndk@npm:2.10.1"

    + dependencies:

    + "@noble/curves": "npm:^1.6.0"

    + "@noble/hashes": "npm:^1.5.0"

    + "@noble/secp256k1": "npm:^2.1.0"

    + "@scure/base": "npm:^1.1.9"

    + debug: "npm:^4.3.6"

    + light-bolt11-decoder: "npm:^3.2.0"

    + nostr-tools: "npm:^2.7.1"

    + tseep: "npm:^1.2.2"

    + typescript-lru-cache: "npm:^2.0.0"

    + utf8-buffer: "npm:^1.0.0"

    + websocket-polyfill: "npm:^0.0.3"

    + checksum: 10c0/b73cd0e9f036e60893d0470d778c5e672795f93308edca389d42c9487267e95df208b2b2353b20eb766ae606cfd1c328d03a1e43d69351e27159843cdb619cc9

    + languageName: node

    + linkType: hard

    +

    "@npmcli/agent@npm:^2.0.0":

    version: 2.2.2

    resolution: "@npmcli/agent@npm:2.2.2"

    @@ -854,7 +880,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@scure/base@npm:^1.1.1, @scure/base@npm:~1.1.0":

    +"@scure/base@npm:^1.1.1, @scure/base@npm:^1.1.9, @scure/base@npm:~1.1.0":

    version: 1.1.9

    resolution: "@scure/base@npm:1.1.9"

    checksum: 10c0/77a06b9a2db8144d22d9bf198338893d77367c51b58c72b99df990c0a11f7cadd066d4102abb15e3ca6798d1529e3765f55c4355742465e49aed7a0c01fe76e8

    @@ -3857,6 +3883,7 @@ __metadata:

    eslint-plugin-prettier: "npm:^5.2.1"

    eslint-plugin-svelte: "npm:^2.44.0"

    highlight.js: "npm:^11.10.0"

    + nostr-login: "npm:^1.6.6"

    nostr-tools: "npm:^2.7.2"

    parse-diff: "npm:^0.11.1"

    postcss: "npm:^8.4.47"

    @@ -4492,6 +4519,15 @@ __metadata:

    languageName: node

    linkType: hard

    +"light-bolt11-decoder@npm:^3.2.0":

    + version: 3.2.0

    + resolution: "light-bolt11-decoder@npm:3.2.0"

    + dependencies:

    + "@scure/base": "npm:1.1.1"

    + checksum: 10c0/65c1514b40b3b7fd5f4f0b40412bab35b0135f20f6b4929f5616bc5693f759fa8636d2393ba71fa782b2d9204315015e044628736ee7aea72f71939b0eb486f1

    + languageName: node

    + linkType: hard

    +

    "lilconfig@npm:^2.0.5, lilconfig@npm:^2.1.0":

    version: 2.1.0

    resolution: "lilconfig@npm:2.1.0"

    @@ -5030,6 +5066,36 @@ __metadata:

    languageName: node

    linkType: hard

    +"nostr-login@npm:^1.6.6":

    + version: 1.6.6

    + resolution: "nostr-login@npm:1.6.6"

    + dependencies:

    + "@nostr-dev-kit/ndk": "npm:^2.3.1"

    + nostr-tools: "npm:^1.17.0"

    + tseep: "npm:^1.2.1"

    + checksum: 10c0/f521f906eef377321406838d2ab3be314c9755cbbad90328f32cd65fc5b035bb85729116bc5da313445c399aafb9f1bc12833a789f440e58f12619e84d9ff15f

    + languageName: node

    + linkType: hard

    +

    +"nostr-tools@npm:^1.17.0":

    + version: 1.17.0

    + resolution: "nostr-tools@npm:1.17.0"

    + dependencies:

    + "@noble/ciphers": "npm:0.2.0"

    + "@noble/curves": "npm:1.1.0"

    + "@noble/hashes": "npm:1.3.1"

    + "@scure/base": "npm:1.1.1"

    + "@scure/bip32": "npm:1.3.1"

    + "@scure/bip39": "npm:1.2.1"

    + peerDependencies:

    + typescript: ">=5.0.0"

    + peerDependenciesMeta:

    + typescript:

    + optional: true

    + checksum: 10c0/b52732df3e403ef3c73a41fe1dea89accbff91597b231d811d577c35a9bd9307651de65ec7fbcc9989aef4c35e9c6b1005200fbbfec45544dcd64f928bbfc476

    + languageName: node

    + linkType: hard

    +

    "nostr-tools@npm:^2.7.1, nostr-tools@npm:^2.7.2":

    version: 2.7.2

    resolution: "nostr-tools@npm:2.7.2"

    @@ -6638,6 +6704,13 @@ __metadata:

    languageName: node

    linkType: hard

    +"tseep@npm:^1.2.1, tseep@npm:^1.2.2":

    + version: 1.3.1

    + resolution: "tseep@npm:1.3.1"

    + checksum: 10c0/01b68f1f0e1084ff78bed9f28d83a595b4b6256caecd8c245af62716037dc4e87c9916c90d5d5cd4a2935f7d438a1117b2703dbf8abc79f087bc176a6f247ed6

    + languageName: node

    + linkType: hard

    +

    "tslib@npm:^2.0.1, tslib@npm:^2.6.2, tslib@npm:^2.7.0":

    version: 2.7.0

    resolution: "tslib@npm:2.7.0"

    --

    libgit2 1.8.1

    I test switching accounts with the refactored version.

    From 9b84454f53ff248a1390e9ec7a676093f7669697 Mon Sep 17 00:00:00 2001

    From: fsociety

    Date: Wed, 2 Oct 2024 00:33:36 +0200

    Subject: [PATCH] feat: integrate nostr-login for user authentication

    This commit integrates nostr-login for user authentication and removes the previous method. It includes changes in the package.json, Navbar.svelte, users.ts and the creation of a new file nostr-login/index.ts. This new method provides a more streamlined and efficient user authentication process.

    ---

    package.json | 1 +

    src/lib/components/Navbar.svelte | 45 +++++++++++++++++----------------------------

    src/lib/signers/nostr-login/index.ts | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    src/lib/stores/users.ts | 72 +++++++++++++++++++++++-------------------------------------------------

    src/lib/wrappers/Navbar.svelte | 21 ++-------------------

    yarn.lock | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------

    6 files changed, 240 insertions(+), 109 deletions(-)

    create mode 100644 src/lib/signers/nostr-login/index.ts

    diff --git a/package.json b/package.json

    index 8db6542..2539883 100644

    --- a/package.json

    +++ b/package.json

    @@ -34,6 +34,7 @@

    "eslint-config-prettier": "^9.1.0",

    "eslint-plugin-prettier": "^5.2.1",

    "eslint-plugin-svelte": "^2.44.0",

    + "nostr-login": "^1.6.6",

    "postcss": "^8.4.47",

    "prettier": "^3.3.3",

    "prettier-plugin-svelte": "^3.2.6",

    diff --git a/src/lib/components/Navbar.svelte b/src/lib/components/Navbar.svelte

    index e66e96d..d5d3d3c 100644

    --- a/src/lib/components/Navbar.svelte

    +++ b/src/lib/components/Navbar.svelte

    @@ -1,13 +1,28 @@

    @@ -35,34 +50,8 @@

    class="menu dropdown-content z-[1] -mr-4 rounded-box bg-base-400 p-2 shadow"

    >

  • -

    -

    -

  • -

    -

    - on:click={() => {

    - logout()

    - }}>Logout

    - >

    -

  • - {:else if nip07_plugin === undefined}

    -

    - {:else if nip07_plugin}

    -

    - on:click={() => {

    - login_function()

    - }}

    - class="btn btn-ghost btn-sm normal-case">Login

    - >

    - {:else}

    -

    - on:click={() => {

    - singup_function()

    - }}

    - class="btn btn-ghost btn-sm normal-case">Sign up

    - >

    {/if}

    diff --git a/src/lib/signers/nostr-login/index.ts b/src/lib/signers/nostr-login/index.ts

    new file mode 100644

    index 0000000..563a8db

    --- /dev/null

    +++ b/src/lib/signers/nostr-login/index.ts

    @@ -0,0 +1,111 @@

    +import NDK, { NDKUser, NDKRelay } from '@nostr-dev-kit/ndk'

    +import type { NDKSigner, NostrEvent } from '@nostr-dev-kit/ndk'

    +

    +export class NDKNostrLoginSigner implements NDKSigner {

    + private _userPromise: Promise | undefined

    +

    + blockUntilReady(): Promise {

    + return new Promise((resolve) => {

    + const eventHandler = (event: Event) => {

    + document.removeEventListener('nlAuth', eventHandler)

    + const customEvent = event as CustomEvent<{ pubkey: string }>

    + resolve(new NDKUser({ pubkey: customEvent.detail.pubkey }))

    + }

    +

    + document.addEventListener('nlAuth', eventHandler)

    + })

    + }

    +

    + user(): Promise {

    + if (!this._userPromise) {

    + this._userPromise = this.blockUntilReady()

    + }

    +

    + return this._userPromise

    + }

    +

    + async sign(event: NostrEvent): Promise {

    + if (!window.nostr) {

    + throw new Error('window.nostr is undefined')

    + }

    + const signedEvent = await window.nostr.signEvent(event)

    + return signedEvent.sig

    + }

    +

    + async relays?(ndk?: NDK): Promise {

    + const relays = (await window.nostr?.getRelays?.()) || {}

    +

    + const activeRelays: string[] = []

    + for (const url of Object.keys(relays)) {

    + // Currently only respects relays that are both readable and writable.

    + if (relays[url].read && relays[url].write) {

    + activeRelays.push(url)

    + }

    + }

    + return activeRelays.map(

    + (url) => new NDKRelay(url, ndk?.relayAuthDefaultPolicy, ndk)

    + )

    + }

    +

    + async encrypt(

    + recipient: NDKUser,

    + value: string,

    + type: string = 'nip04'

    + ): Promise {

    + if (type === 'nip44') {

    + return this.nip44Encrypt(recipient, value)

    + } else {

    + return this.nip04Encrypt(recipient, value)

    + }

    + }

    +

    + async decrypt(

    + sender: NDKUser,

    + value: string,

    + type: string = 'nip04'

    + ): Promise {

    + if (type === 'nip44') {

    + return this.nip44Decrypt(sender, value)

    + } else {

    + return this.nip04Decrypt(sender, value)

    + }

    + }

    +

    + private async nip44Encrypt(

    + recipient: NDKUser,

    + value: string

    + ): Promise {

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + if (!window.nostr || !window.nostr.nip44) {

    + throw new Error('window.nostr or window.nostr.nip44 is undefined')

    + }

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + return await window.nostr.nip44.encrypt(recipient.pubkey, value)

    + }

    +

    + private async nip04Encrypt(

    + recipient: NDKUser,

    + value: string

    + ): Promise {

    + if (!window.nostr || !window.nostr.nip04) {

    + throw new Error('window.nostr or window.nostr.nip04 is undefined')

    + }

    + return await window.nostr.nip04.encrypt(recipient.pubkey, value)

    + }

    +

    + private async nip44Decrypt(sender: NDKUser, value: string): Promise {

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + if (!window.nostr || !window.nostr.nip44) {

    + throw new Error('window.nostr or window.nostr.nip44 is undefined')

    + }

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + return await window.nostr.nip44.decrypt(sender.pubkey, value)

    + }

    +

    + private async nip04Decrypt(sender: NDKUser, value: string): Promise {

    + if (!window.nostr || !window.nostr.nip04) {

    + throw new Error('window.nostr or window.nostr.nip04 is undefined')

    + }

    + return await window.nostr.nip04.decrypt(sender.pubkey, value)

    + }

    +}

    diff --git a/src/lib/stores/users.ts b/src/lib/stores/users.ts

    index 608ffed..40ed744 100644

    --- a/src/lib/stores/users.ts

    +++ b/src/lib/stores/users.ts

    @@ -2,13 +2,12 @@ import {

    defaults as user_defaults,

    type UserObject,

    } from '$lib/components/users/type'

    -import {

    - getRelayListForUser,

    - NDKNip07Signer,

    - NDKRelayList,

    -} from '@nostr-dev-kit/ndk'

    +import { getRelayListForUser, NDKRelayList } from '@nostr-dev-kit/ndk'

    import { get, writable, type Unsubscriber, type Writable } from 'svelte/store'

    import { ndk } from './ndk'

    +import { init as initNostrLogin } from 'nostr-login'

    +import { NDKNostrLoginSigner } from '$lib/signers/nostr-login'

    +import type { NostrLoginAuthOptions } from 'nostr-login/dist/types'

    export const users: { [hexpubkey: string]: Writable } = {}

    @@ -85,31 +84,7 @@ export const returnUser = async (hexpubkey: string): Promise => {

    })

    }

    -// nip07_plugin is set in Navbar component

    -export const nip07_plugin: Writable = writable(undefined)

    -

    -export const checkForNip07Plugin = () => {

    - if (window.nostr) {

    - nip07_plugin.set(true)

    - if (localStorage.getItem('nip07pubkey')) login()

    - } else {

    - let timerId: NodeJS.Timeout | undefined = undefined

    - const intervalId = setInterval(() => {

    - if (window.nostr) {

    - clearTimeout(timerId)

    - clearInterval(intervalId)

    - nip07_plugin.set(true)

    - if (localStorage.getItem('nip07pubkey')) login()

    - }

    - }, 100)

    - timerId = setTimeout(() => {

    - clearInterval(intervalId)

    - nip07_plugin.set(false)

    - }, 5000)

    - }

    -}

    -

    -const signer = new NDKNip07Signer(2000)

    +const signer = new NDKNostrLoginSigner()

    export const logged_in_user: Writable =

    writable(undefined)

    @@ -118,24 +93,24 @@ export const login = async (): Promise => {

    return new Promise(async (res, rej) => {

    const user = get(logged_in_user)

    if (user) return res()

    - if (get(nip07_plugin)) {

    - try {

    - const ndk_user = await signer.blockUntilReady()

    - localStorage.setItem('nip07pubkey', ndk_user.pubkey)

    - logged_in_user.set({

    - ...user_defaults,

    - hexpubkey: ndk_user.pubkey,

    - })

    - ndk.signer = signer

    - ensureUser(ndk_user.pubkey).subscribe((user) => {

    - logged_in_user.set({ ...user })

    - })

    - return res()

    - } catch (e) {

    - alert(e)

    - rej()

    - }

    - } else {

    + try {

    + ndk.signer = signer

    + initNostrLogin({

    + onAuth: async (_npub: string, options: NostrLoginAuthOptions) => {

    + const pubkey =

    + typeof options.pubkey === 'string' ? options.pubkey : ''

    + logged_in_user.set({

    + ...user_defaults,

    + hexpubkey: pubkey,

    + })

    + ensureUser(pubkey).subscribe((user) => {

    + logged_in_user.set({ ...user })

    + })

    + return res()

    + },

    + })

    + } catch (e) {

    + alert(e)

    rej()

    }

    })

    @@ -143,7 +118,6 @@ export const login = async (): Promise => {

    export const logout = async (): Promise => {

    logged_in_user.set(undefined)

    - localStorage.removeItem('nip07pubkey')

    ndk.signer = undefined

    }

    diff --git a/src/lib/wrappers/Navbar.svelte b/src/lib/wrappers/Navbar.svelte

    index 4b63bc7..1f7146f 100644

    --- a/src/lib/wrappers/Navbar.svelte

    +++ b/src/lib/wrappers/Navbar.svelte

    @@ -1,23 +1,6 @@

    -

    - logged_in_user={$logged_in_user}

    - nip07_plugin={$nip07_plugin}

    - login_function={login}

    - {singup_function}

    -/>

    +

    diff --git a/yarn.lock b/yarn.lock

    index f75be09..f976ab4 100644

    --- a/yarn.lock

    +++ b/yarn.lock

    @@ -566,6 +566,13 @@ __metadata:

    languageName: node

    linkType: hard

    +"@noble/ciphers@npm:0.2.0":

    + version: 0.2.0

    + resolution: "@noble/ciphers@npm:0.2.0"

    + checksum: 10c0/57dea65c32741df20a1ac24f365d616a558527109d778c1bec877f20b28875a26b80097bce51ae19529f3792ccf8285fe73839ff404733e32a27a6ebf60edd2c

    + languageName: node

    + linkType: hard

    +

    "@noble/ciphers@npm:^0.5.1":

    version: 0.5.3

    resolution: "@noble/ciphers@npm:0.5.3"

    @@ -573,6 +580,15 @@ __metadata:

    languageName: node

    linkType: hard

    +"@noble/curves@npm:1.1.0, @noble/curves@npm:~1.1.0":

    + version: 1.1.0

    + resolution: "@noble/curves@npm:1.1.0"

    + dependencies:

    + "@noble/hashes": "npm:1.3.1"

    + checksum: 10c0/81115c3ebfa7e7da2d7e18d44d686f98dc6d35dbde3964412c05707c92d0994a01545bc265d5c0bc05c8c49333f75b99c9acef6750f5a79b3abcc8e0546acf88

    + languageName: node

    + linkType: hard

    +

    "@noble/curves@npm:1.2.0":

    version: 1.2.0

    resolution: "@noble/curves@npm:1.2.0"

    @@ -582,7 +598,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/curves@npm:^1.4.0":

    +"@noble/curves@npm:^1.4.0, @noble/curves@npm:^1.6.0":

    version: 1.6.0

    resolution: "@noble/curves@npm:1.6.0"

    dependencies:

    @@ -591,15 +607,6 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/curves@npm:~1.1.0":

    - version: 1.1.0

    - resolution: "@noble/curves@npm:1.1.0"

    - dependencies:

    - "@noble/hashes": "npm:1.3.1"

    - checksum: 10c0/81115c3ebfa7e7da2d7e18d44d686f98dc6d35dbde3964412c05707c92d0994a01545bc265d5c0bc05c8c49333f75b99c9acef6750f5a79b3abcc8e0546acf88

    - languageName: node

    - linkType: hard

    -

    "@noble/hashes@npm:1.3.1":

    version: 1.3.1

    resolution: "@noble/hashes@npm:1.3.1"

    @@ -614,7 +621,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.3.1":

    +"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.5.0":

    version: 1.5.0

    resolution: "@noble/hashes@npm:1.5.0"

    checksum: 10c0/1b46539695fbfe4477c0822d90c881a04d4fa2921c08c552375b444a48cac9930cb1ee68de0a3c7859e676554d0f3771999716606dc4d8f826e414c11692cdd9

    @@ -628,7 +635,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/secp256k1@npm:^2.0.0":

    +"@noble/secp256k1@npm:^2.0.0, @noble/secp256k1@npm:^2.1.0":

    version: 2.1.0

    resolution: "@noble/secp256k1@npm:2.1.0"

    checksum: 10c0/b4c7edd2a5ec5acf294546cd06d08dc2a2a2b2ebe34a6da1f2f5104f56983f81dd31c261ad365c6b9757d1c54017fc3363331ee33bba8715ff94c2bc954313cc

    @@ -692,6 +699,25 @@ __metadata:

    languageName: node

    linkType: hard

    +"@nostr-dev-kit/ndk@npm:^2.3.1":

    + version: 2.10.1

    + resolution: "@nostr-dev-kit/ndk@npm:2.10.1"

    + dependencies:

    + "@noble/curves": "npm:^1.6.0"

    + "@noble/hashes": "npm:^1.5.0"

    + "@noble/secp256k1": "npm:^2.1.0"

    + "@scure/base": "npm:^1.1.9"

    + debug: "npm:^4.3.6"

    + light-bolt11-decoder: "npm:^3.2.0"

    + nostr-tools: "npm:^2.7.1"

    + tseep: "npm:^1.2.2"

    + typescript-lru-cache: "npm:^2.0.0"

    + utf8-buffer: "npm:^1.0.0"

    + websocket-polyfill: "npm:^0.0.3"

    + checksum: 10c0/b73cd0e9f036e60893d0470d778c5e672795f93308edca389d42c9487267e95df208b2b2353b20eb766ae606cfd1c328d03a1e43d69351e27159843cdb619cc9

    + languageName: node

    + linkType: hard

    +

    "@npmcli/agent@npm:^2.0.0":

    version: 2.2.2

    resolution: "@npmcli/agent@npm:2.2.2"

    @@ -854,7 +880,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@scure/base@npm:^1.1.1, @scure/base@npm:~1.1.0":

    +"@scure/base@npm:^1.1.1, @scure/base@npm:^1.1.9, @scure/base@npm:~1.1.0":

    version: 1.1.9

    resolution: "@scure/base@npm:1.1.9"

    checksum: 10c0/77a06b9a2db8144d22d9bf198338893d77367c51b58c72b99df990c0a11f7cadd066d4102abb15e3ca6798d1529e3765f55c4355742465e49aed7a0c01fe76e8

    @@ -3857,6 +3883,7 @@ __metadata:

    eslint-plugin-prettier: "npm:^5.2.1"

    eslint-plugin-svelte: "npm:^2.44.0"

    highlight.js: "npm:^11.10.0"

    + nostr-login: "npm:^1.6.6"

    nostr-tools: "npm:^2.7.2"

    parse-diff: "npm:^0.11.1"

    postcss: "npm:^8.4.47"

    @@ -4492,6 +4519,15 @@ __metadata:

    languageName: node

    linkType: hard

    +"light-bolt11-decoder@npm:^3.2.0":

    + version: 3.2.0

    + resolution: "light-bolt11-decoder@npm:3.2.0"

    + dependencies:

    + "@scure/base": "npm:1.1.1"

    + checksum: 10c0/65c1514b40b3b7fd5f4f0b40412bab35b0135f20f6b4929f5616bc5693f759fa8636d2393ba71fa782b2d9204315015e044628736ee7aea72f71939b0eb486f1

    + languageName: node

    + linkType: hard

    +

    "lilconfig@npm:^2.0.5, lilconfig@npm:^2.1.0":

    version: 2.1.0

    resolution: "lilconfig@npm:2.1.0"

    @@ -5030,6 +5066,36 @@ __metadata:

    languageName: node

    linkType: hard

    +"nostr-login@npm:^1.6.6":

    + version: 1.6.6

    + resolution: "nostr-login@npm:1.6.6"

    + dependencies:

    + "@nostr-dev-kit/ndk": "npm:^2.3.1"

    + nostr-tools: "npm:^1.17.0"

    + tseep: "npm:^1.2.1"

    + checksum: 10c0/f521f906eef377321406838d2ab3be314c9755cbbad90328f32cd65fc5b035bb85729116bc5da313445c399aafb9f1bc12833a789f440e58f12619e84d9ff15f

    + languageName: node

    + linkType: hard

    +

    +"nostr-tools@npm:^1.17.0":

    + version: 1.17.0

    + resolution: "nostr-tools@npm:1.17.0"

    + dependencies:

    + "@noble/ciphers": "npm:0.2.0"

    + "@noble/curves": "npm:1.1.0"

    + "@noble/hashes": "npm:1.3.1"

    + "@scure/base": "npm:1.1.1"

    + "@scure/bip32": "npm:1.3.1"

    + "@scure/bip39": "npm:1.2.1"

    + peerDependencies:

    + typescript: ">=5.0.0"

    + peerDependenciesMeta:

    + typescript:

    + optional: true

    + checksum: 10c0/b52732df3e403ef3c73a41fe1dea89accbff91597b231d811d577c35a9bd9307651de65ec7fbcc9989aef4c35e9c6b1005200fbbfec45544dcd64f928bbfc476

    + languageName: node

    + linkType: hard

    +

    "nostr-tools@npm:^2.7.1, nostr-tools@npm:^2.7.2":

    version: 2.7.2

    resolution: "nostr-tools@npm:2.7.2"

    @@ -6638,6 +6704,13 @@ __metadata:

    languageName: node

    linkType: hard

    +"tseep@npm:^1.2.1, tseep@npm:^1.2.2":

    + version: 1.3.1

    + resolution: "tseep@npm:1.3.1"

    + checksum: 10c0/01b68f1f0e1084ff78bed9f28d83a595b4b6256caecd8c245af62716037dc4e87c9916c90d5d5cd4a2935f7d438a1117b2703dbf8abc79f087bc176a6f247ed6

    + languageName: node

    + linkType: hard

    +

    "tslib@npm:^2.0.1, tslib@npm:^2.6.2, tslib@npm:^2.7.0":

    version: 2.7.0

    resolution: "tslib@npm:2.7.0"

    --

    libgit2 1.8.1

    Avatar
    DJ_Bitcoin 1y ago

    Ok, let's test again. I have changed the signer implementation.

    From 9b84454f53ff248a1390e9ec7a676093f7669697 Mon Sep 17 00:00:00 2001

    From: fsociety

    Date: Wed, 2 Oct 2024 00:33:36 +0200

    Subject: [PATCH] feat: integrate nostr-login for user authentication

    This commit integrates nostr-login for user authentication and removes the previous method. It includes changes in the package.json, Navbar.svelte, users.ts and the creation of a new file nostr-login/index.ts. This new method provides a more streamlined and efficient user authentication process.

    ---

    package.json | 1 +

    src/lib/components/Navbar.svelte | 45 +++++++++++++++++----------------------------

    src/lib/signers/nostr-login/index.ts | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    src/lib/stores/users.ts | 72 +++++++++++++++++++++++-------------------------------------------------

    src/lib/wrappers/Navbar.svelte | 21 ++-------------------

    yarn.lock | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------

    6 files changed, 240 insertions(+), 109 deletions(-)

    create mode 100644 src/lib/signers/nostr-login/index.ts

    diff --git a/package.json b/package.json

    index 8db6542..2539883 100644

    --- a/package.json

    +++ b/package.json

    @@ -34,6 +34,7 @@

    "eslint-config-prettier": "^9.1.0",

    "eslint-plugin-prettier": "^5.2.1",

    "eslint-plugin-svelte": "^2.44.0",

    + "nostr-login": "^1.6.6",

    "postcss": "^8.4.47",

    "prettier": "^3.3.3",

    "prettier-plugin-svelte": "^3.2.6",

    diff --git a/src/lib/components/Navbar.svelte b/src/lib/components/Navbar.svelte

    index e66e96d..d5d3d3c 100644

    --- a/src/lib/components/Navbar.svelte

    +++ b/src/lib/components/Navbar.svelte

    @@ -1,13 +1,28 @@

    @@ -35,34 +50,8 @@

    class="menu dropdown-content z-[1] -mr-4 rounded-box bg-base-400 p-2 shadow"

    >

  • -

    -

    -

  • -

    -

    - on:click={() => {

    - logout()

    - }}>Logout

    - >

    -

  • - {:else if nip07_plugin === undefined}

    -

    - {:else if nip07_plugin}

    -

    - on:click={() => {

    - login_function()

    - }}

    - class="btn btn-ghost btn-sm normal-case">Login

    - >

    - {:else}

    -

    - on:click={() => {

    - singup_function()

    - }}

    - class="btn btn-ghost btn-sm normal-case">Sign up

    - >

    {/if}

    diff --git a/src/lib/signers/nostr-login/index.ts b/src/lib/signers/nostr-login/index.ts

    new file mode 100644

    index 0000000..563a8db

    --- /dev/null

    +++ b/src/lib/signers/nostr-login/index.ts

    @@ -0,0 +1,111 @@

    +import NDK, { NDKUser, NDKRelay } from '@nostr-dev-kit/ndk'

    +import type { NDKSigner, NostrEvent } from '@nostr-dev-kit/ndk'

    +

    +export class NDKNostrLoginSigner implements NDKSigner {

    + private _userPromise: Promise | undefined

    +

    + blockUntilReady(): Promise {

    + return new Promise((resolve) => {

    + const eventHandler = (event: Event) => {

    + document.removeEventListener('nlAuth', eventHandler)

    + const customEvent = event as CustomEvent<{ pubkey: string }>

    + resolve(new NDKUser({ pubkey: customEvent.detail.pubkey }))

    + }

    +

    + document.addEventListener('nlAuth', eventHandler)

    + })

    + }

    +

    + user(): Promise {

    + if (!this._userPromise) {

    + this._userPromise = this.blockUntilReady()

    + }

    +

    + return this._userPromise

    + }

    +

    + async sign(event: NostrEvent): Promise {

    + if (!window.nostr) {

    + throw new Error('window.nostr is undefined')

    + }

    + const signedEvent = await window.nostr.signEvent(event)

    + return signedEvent.sig

    + }

    +

    + async relays?(ndk?: NDK): Promise {

    + const relays = (await window.nostr?.getRelays?.()) || {}

    +

    + const activeRelays: string[] = []

    + for (const url of Object.keys(relays)) {

    + // Currently only respects relays that are both readable and writable.

    + if (relays[url].read && relays[url].write) {

    + activeRelays.push(url)

    + }

    + }

    + return activeRelays.map(

    + (url) => new NDKRelay(url, ndk?.relayAuthDefaultPolicy, ndk)

    + )

    + }

    +

    + async encrypt(

    + recipient: NDKUser,

    + value: string,

    + type: string = 'nip04'

    + ): Promise {

    + if (type === 'nip44') {

    + return this.nip44Encrypt(recipient, value)

    + } else {

    + return this.nip04Encrypt(recipient, value)

    + }

    + }

    +

    + async decrypt(

    + sender: NDKUser,

    + value: string,

    + type: string = 'nip04'

    + ): Promise {

    + if (type === 'nip44') {

    + return this.nip44Decrypt(sender, value)

    + } else {

    + return this.nip04Decrypt(sender, value)

    + }

    + }

    +

    + private async nip44Encrypt(

    + recipient: NDKUser,

    + value: string

    + ): Promise {

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + if (!window.nostr || !window.nostr.nip44) {

    + throw new Error('window.nostr or window.nostr.nip44 is undefined')

    + }

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + return await window.nostr.nip44.encrypt(recipient.pubkey, value)

    + }

    +

    + private async nip04Encrypt(

    + recipient: NDKUser,

    + value: string

    + ): Promise {

    + if (!window.nostr || !window.nostr.nip04) {

    + throw new Error('window.nostr or window.nostr.nip04 is undefined')

    + }

    + return await window.nostr.nip04.encrypt(recipient.pubkey, value)

    + }

    +

    + private async nip44Decrypt(sender: NDKUser, value: string): Promise {

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + if (!window.nostr || !window.nostr.nip44) {

    + throw new Error('window.nostr or window.nostr.nip44 is undefined')

    + }

    + // @ts-expect-error window.nostr.nip44 is not defined in the type definitions

    + return await window.nostr.nip44.decrypt(sender.pubkey, value)

    + }

    +

    + private async nip04Decrypt(sender: NDKUser, value: string): Promise {

    + if (!window.nostr || !window.nostr.nip04) {

    + throw new Error('window.nostr or window.nostr.nip04 is undefined')

    + }

    + return await window.nostr.nip04.decrypt(sender.pubkey, value)

    + }

    +}

    diff --git a/src/lib/stores/users.ts b/src/lib/stores/users.ts

    index 608ffed..40ed744 100644

    --- a/src/lib/stores/users.ts

    +++ b/src/lib/stores/users.ts

    @@ -2,13 +2,12 @@ import {

    defaults as user_defaults,

    type UserObject,

    } from '$lib/components/users/type'

    -import {

    - getRelayListForUser,

    - NDKNip07Signer,

    - NDKRelayList,

    -} from '@nostr-dev-kit/ndk'

    +import { getRelayListForUser, NDKRelayList } from '@nostr-dev-kit/ndk'

    import { get, writable, type Unsubscriber, type Writable } from 'svelte/store'

    import { ndk } from './ndk'

    +import { init as initNostrLogin } from 'nostr-login'

    +import { NDKNostrLoginSigner } from '$lib/signers/nostr-login'

    +import type { NostrLoginAuthOptions } from 'nostr-login/dist/types'

    export const users: { [hexpubkey: string]: Writable } = {}

    @@ -85,31 +84,7 @@ export const returnUser = async (hexpubkey: string): Promise => {

    })

    }

    -// nip07_plugin is set in Navbar component

    -export const nip07_plugin: Writable = writable(undefined)

    -

    -export const checkForNip07Plugin = () => {

    - if (window.nostr) {

    - nip07_plugin.set(true)

    - if (localStorage.getItem('nip07pubkey')) login()

    - } else {

    - let timerId: NodeJS.Timeout | undefined = undefined

    - const intervalId = setInterval(() => {

    - if (window.nostr) {

    - clearTimeout(timerId)

    - clearInterval(intervalId)

    - nip07_plugin.set(true)

    - if (localStorage.getItem('nip07pubkey')) login()

    - }

    - }, 100)

    - timerId = setTimeout(() => {

    - clearInterval(intervalId)

    - nip07_plugin.set(false)

    - }, 5000)

    - }

    -}

    -

    -const signer = new NDKNip07Signer(2000)

    +const signer = new NDKNostrLoginSigner()

    export const logged_in_user: Writable =

    writable(undefined)

    @@ -118,24 +93,24 @@ export const login = async (): Promise => {

    return new Promise(async (res, rej) => {

    const user = get(logged_in_user)

    if (user) return res()

    - if (get(nip07_plugin)) {

    - try {

    - const ndk_user = await signer.blockUntilReady()

    - localStorage.setItem('nip07pubkey', ndk_user.pubkey)

    - logged_in_user.set({

    - ...user_defaults,

    - hexpubkey: ndk_user.pubkey,

    - })

    - ndk.signer = signer

    - ensureUser(ndk_user.pubkey).subscribe((user) => {

    - logged_in_user.set({ ...user })

    - })

    - return res()

    - } catch (e) {

    - alert(e)

    - rej()

    - }

    - } else {

    + try {

    + ndk.signer = signer

    + initNostrLogin({

    + onAuth: async (_npub: string, options: NostrLoginAuthOptions) => {

    + const pubkey =

    + typeof options.pubkey === 'string' ? options.pubkey : ''

    + logged_in_user.set({

    + ...user_defaults,

    + hexpubkey: pubkey,

    + })

    + ensureUser(pubkey).subscribe((user) => {

    + logged_in_user.set({ ...user })

    + })

    + return res()

    + },

    + })

    + } catch (e) {

    + alert(e)

    rej()

    }

    })

    @@ -143,7 +118,6 @@ export const login = async (): Promise => {

    export const logout = async (): Promise => {

    logged_in_user.set(undefined)

    - localStorage.removeItem('nip07pubkey')

    ndk.signer = undefined

    }

    diff --git a/src/lib/wrappers/Navbar.svelte b/src/lib/wrappers/Navbar.svelte

    index 4b63bc7..1f7146f 100644

    --- a/src/lib/wrappers/Navbar.svelte

    +++ b/src/lib/wrappers/Navbar.svelte

    @@ -1,23 +1,6 @@

    -

    - logged_in_user={$logged_in_user}

    - nip07_plugin={$nip07_plugin}

    - login_function={login}

    - {singup_function}

    -/>

    +

    diff --git a/yarn.lock b/yarn.lock

    index f75be09..f976ab4 100644

    --- a/yarn.lock

    +++ b/yarn.lock

    @@ -566,6 +566,13 @@ __metadata:

    languageName: node

    linkType: hard

    +"@noble/ciphers@npm:0.2.0":

    + version: 0.2.0

    + resolution: "@noble/ciphers@npm:0.2.0"

    + checksum: 10c0/57dea65c32741df20a1ac24f365d616a558527109d778c1bec877f20b28875a26b80097bce51ae19529f3792ccf8285fe73839ff404733e32a27a6ebf60edd2c

    + languageName: node

    + linkType: hard

    +

    "@noble/ciphers@npm:^0.5.1":

    version: 0.5.3

    resolution: "@noble/ciphers@npm:0.5.3"

    @@ -573,6 +580,15 @@ __metadata:

    languageName: node

    linkType: hard

    +"@noble/curves@npm:1.1.0, @noble/curves@npm:~1.1.0":

    + version: 1.1.0

    + resolution: "@noble/curves@npm:1.1.0"

    + dependencies:

    + "@noble/hashes": "npm:1.3.1"

    + checksum: 10c0/81115c3ebfa7e7da2d7e18d44d686f98dc6d35dbde3964412c05707c92d0994a01545bc265d5c0bc05c8c49333f75b99c9acef6750f5a79b3abcc8e0546acf88

    + languageName: node

    + linkType: hard

    +

    "@noble/curves@npm:1.2.0":

    version: 1.2.0

    resolution: "@noble/curves@npm:1.2.0"

    @@ -582,7 +598,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/curves@npm:^1.4.0":

    +"@noble/curves@npm:^1.4.0, @noble/curves@npm:^1.6.0":

    version: 1.6.0

    resolution: "@noble/curves@npm:1.6.0"

    dependencies:

    @@ -591,15 +607,6 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/curves@npm:~1.1.0":

    - version: 1.1.0

    - resolution: "@noble/curves@npm:1.1.0"

    - dependencies:

    - "@noble/hashes": "npm:1.3.1"

    - checksum: 10c0/81115c3ebfa7e7da2d7e18d44d686f98dc6d35dbde3964412c05707c92d0994a01545bc265d5c0bc05c8c49333f75b99c9acef6750f5a79b3abcc8e0546acf88

    - languageName: node

    - linkType: hard

    -

    "@noble/hashes@npm:1.3.1":

    version: 1.3.1

    resolution: "@noble/hashes@npm:1.3.1"

    @@ -614,7 +621,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.3.1":

    +"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.5.0":

    version: 1.5.0

    resolution: "@noble/hashes@npm:1.5.0"

    checksum: 10c0/1b46539695fbfe4477c0822d90c881a04d4fa2921c08c552375b444a48cac9930cb1ee68de0a3c7859e676554d0f3771999716606dc4d8f826e414c11692cdd9

    @@ -628,7 +635,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@noble/secp256k1@npm:^2.0.0":

    +"@noble/secp256k1@npm:^2.0.0, @noble/secp256k1@npm:^2.1.0":

    version: 2.1.0

    resolution: "@noble/secp256k1@npm:2.1.0"

    checksum: 10c0/b4c7edd2a5ec5acf294546cd06d08dc2a2a2b2ebe34a6da1f2f5104f56983f81dd31c261ad365c6b9757d1c54017fc3363331ee33bba8715ff94c2bc954313cc

    @@ -692,6 +699,25 @@ __metadata:

    languageName: node

    linkType: hard

    +"@nostr-dev-kit/ndk@npm:^2.3.1":

    + version: 2.10.1

    + resolution: "@nostr-dev-kit/ndk@npm:2.10.1"

    + dependencies:

    + "@noble/curves": "npm:^1.6.0"

    + "@noble/hashes": "npm:^1.5.0"

    + "@noble/secp256k1": "npm:^2.1.0"

    + "@scure/base": "npm:^1.1.9"

    + debug: "npm:^4.3.6"

    + light-bolt11-decoder: "npm:^3.2.0"

    + nostr-tools: "npm:^2.7.1"

    + tseep: "npm:^1.2.2"

    + typescript-lru-cache: "npm:^2.0.0"

    + utf8-buffer: "npm:^1.0.0"

    + websocket-polyfill: "npm:^0.0.3"

    + checksum: 10c0/b73cd0e9f036e60893d0470d778c5e672795f93308edca389d42c9487267e95df208b2b2353b20eb766ae606cfd1c328d03a1e43d69351e27159843cdb619cc9

    + languageName: node

    + linkType: hard

    +

    "@npmcli/agent@npm:^2.0.0":

    version: 2.2.2

    resolution: "@npmcli/agent@npm:2.2.2"

    @@ -854,7 +880,7 @@ __metadata:

    languageName: node

    linkType: hard

    -"@scure/base@npm:^1.1.1, @scure/base@npm:~1.1.0":

    +"@scure/base@npm:^1.1.1, @scure/base@npm:^1.1.9, @scure/base@npm:~1.1.0":

    version: 1.1.9

    resolution: "@scure/base@npm:1.1.9"

    checksum: 10c0/77a06b9a2db8144d22d9bf198338893d77367c51b58c72b99df990c0a11f7cadd066d4102abb15e3ca6798d1529e3765f55c4355742465e49aed7a0c01fe76e8

    @@ -3857,6 +3883,7 @@ __metadata:

    eslint-plugin-prettier: "npm:^5.2.1"

    eslint-plugin-svelte: "npm:^2.44.0"

    highlight.js: "npm:^11.10.0"

    + nostr-login: "npm:^1.6.6"

    nostr-tools: "npm:^2.7.2"

    parse-diff: "npm:^0.11.1"

    postcss: "npm:^8.4.47"

    @@ -4492,6 +4519,15 @@ __metadata:

    languageName: node

    linkType: hard

    +"light-bolt11-decoder@npm:^3.2.0":

    + version: 3.2.0

    + resolution: "light-bolt11-decoder@npm:3.2.0"

    + dependencies:

    + "@scure/base": "npm:1.1.1"

    + checksum: 10c0/65c1514b40b3b7fd5f4f0b40412bab35b0135f20f6b4929f5616bc5693f759fa8636d2393ba71fa782b2d9204315015e044628736ee7aea72f71939b0eb486f1

    + languageName: node

    + linkType: hard

    +

    "lilconfig@npm:^2.0.5, lilconfig@npm:^2.1.0":

    version: 2.1.0

    resolution: "lilconfig@npm:2.1.0"

    @@ -5030,6 +5066,36 @@ __metadata:

    languageName: node

    linkType: hard

    +"nostr-login@npm:^1.6.6":

    + version: 1.6.6

    + resolution: "nostr-login@npm:1.6.6"

    + dependencies:

    + "@nostr-dev-kit/ndk": "npm:^2.3.1"

    + nostr-tools: "npm:^1.17.0"

    + tseep: "npm:^1.2.1"

    + checksum: 10c0/f521f906eef377321406838d2ab3be314c9755cbbad90328f32cd65fc5b035bb85729116bc5da313445c399aafb9f1bc12833a789f440e58f12619e84d9ff15f

    + languageName: node

    + linkType: hard

    +

    +"nostr-tools@npm:^1.17.0":

    + version: 1.17.0

    + resolution: "nostr-tools@npm:1.17.0"

    + dependencies:

    + "@noble/ciphers": "npm:0.2.0"

    + "@noble/curves": "npm:1.1.0"

    + "@noble/hashes": "npm:1.3.1"

    + "@scure/base": "npm:1.1.1"

    + "@scure/bip32": "npm:1.3.1"

    + "@scure/bip39": "npm:1.2.1"

    + peerDependencies:

    + typescript: ">=5.0.0"

    + peerDependenciesMeta:

    + typescript:

    + optional: true

    + checksum: 10c0/b52732df3e403ef3c73a41fe1dea89accbff91597b231d811d577c35a9bd9307651de65ec7fbcc9989aef4c35e9c6b1005200fbbfec45544dcd64f928bbfc476

    + languageName: node

    + linkType: hard

    +

    "nostr-tools@npm:^2.7.1, nostr-tools@npm:^2.7.2":

    version: 2.7.2

    resolution: "nostr-tools@npm:2.7.2"

    @@ -6638,6 +6704,13 @@ __metadata:

    languageName: node

    linkType: hard

    +"tseep@npm:^1.2.1, tseep@npm:^1.2.2":

    + version: 1.3.1

    + resolution: "tseep@npm:1.3.1"

    + checksum: 10c0/01b68f1f0e1084ff78bed9f28d83a595b4b6256caecd8c245af62716037dc4e87c9916c90d5d5cd4a2935f7d438a1117b2703dbf8abc79f087bc176a6f247ed6

    + languageName: node

    + linkType: hard

    +

    "tslib@npm:^2.0.1, tslib@npm:^2.6.2, tslib@npm:^2.7.0":

    version: 2.7.0

    resolution: "tslib@npm:2.7.0"

    --

    libgit2 1.8.1

    Avatar
    DJ_Bitcoin 1y ago

    Ok, I've switched to another account, but there is still a bug. "signer required". Will fix that.

    Avatar

    I am live with a new MELODIC HOUSE DJ set! 🤙

    https://zap.stream/naddr1qqjrqdpexqcnycfh95ersvpn956x2v3c95uxywry95urxv3hxgcrvv3n893xgqgwwaehxw309ahx7uewd3hkctczyr85tf46zd366lkjzws83ecs6fq3ttnjrjd500g7haz936h0knp22qcyqqq8vecxrymfg

    Replying to Avatar TheGrinder

    61k by tomorrow. If not I'll delete this note.

    Avatar

    Hello? 🤙

    nostr:nevent1qvzqqqqqqypzqmn477tj8972x227pax2p77xawwv0xlgtwha644ax7pzpj5wae6wqqsw3fwlgfqy7rkl9gnedesnp2fsd0d8lt98pjgfsnk3elw5q96gwaspw72tf

    Load More