mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
this simplifies the code and makes the API more intuitive. we used to need the event emitter thing because we were subscribing to the same relay to check if the event had been published, but that is not necessary now that we assume an OK response will always come. closes https://github.com/nbd-wtf/nostr-tools/issues/262
35 lines
741 B
TypeScript
35 lines
741 B
TypeScript
import {Kind, type EventTemplate, type Event} from './event.ts'
|
|
import {Relay} from './relay.ts'
|
|
|
|
/**
|
|
* Authenticate via NIP-42 flow.
|
|
*
|
|
* @example
|
|
* const sign = window.nostr.signEvent
|
|
* relay.on('auth', challenge =>
|
|
* authenticate({ relay, sign, challenge })
|
|
* )
|
|
*/
|
|
export const authenticate = async ({
|
|
challenge,
|
|
relay,
|
|
sign
|
|
}: {
|
|
challenge: string
|
|
relay: Relay
|
|
sign: <K extends number = number>(
|
|
e: EventTemplate<K>
|
|
) => Promise<Event<K>> | Event<K>
|
|
}): Promise<void> => {
|
|
const e: EventTemplate = {
|
|
kind: Kind.ClientAuth,
|
|
created_at: Math.floor(Date.now() / 1000),
|
|
tags: [
|
|
['relay', relay.url],
|
|
['challenge', challenge]
|
|
],
|
|
content: ''
|
|
}
|
|
return relay.auth(await sign(e))
|
|
}
|