relay: add support for NIP42 authentication

This commit is contained in:
Lynn Zenn
2023-04-18 15:10:12 +02:00
committed by fiatjaf_
parent 26e35d50e0
commit 6e58fe371c
4 changed files with 94 additions and 11 deletions

42
nip42.ts Normal file
View File

@@ -0,0 +1,42 @@
import {EventTemplate, Event, Kind} from './event'
import {Relay} from './relay'
/**
* 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: (e: EventTemplate) => Promise<Event>
}): Promise<void> => {
const e: EventTemplate = {
kind: Kind.ClientAuth,
created_at: Math.floor(Date.now() / 1000),
tags: [
['relay', relay.url],
['challenge', challenge]
],
content: ''
}
const sub = relay.publish(await sign(e), 'AUTH')
return new Promise((resolve, reject) => {
sub.on('ok', function ok() {
sub.off('ok', ok)
resolve()
})
sub.on('failed', function fail(reason: string) {
sub.off('failed', fail)
reject(reason)
})
})
}