From 72f9b482ef79e49dd91d600af7904bb5d78b1b0e Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 11 Feb 2024 21:52:06 -0300 Subject: [PATCH] nip46: NostrConnectAdmin kind and handle "auth_url". --- kinds.ts | 1 + nip46.ts | 22 +++++++++------------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/kinds.ts b/kinds.ts index 8594400..f2ad9da 100644 --- a/kinds.ts +++ b/kinds.ts @@ -78,6 +78,7 @@ export const ClientAuth = 22242 export const NWCWalletRequest = 23194 export const NWCWalletResponse = 23195 export const NostrConnect = 24133 +export const NostrConnectAdmin = 24134 export const HTTPAuth = 27235 export const Followsets = 30000 export const Genericlists = 30001 diff --git a/nip46.ts b/nip46.ts index b9e24ac..7b83b27 100644 --- a/nip46.ts +++ b/nip46.ts @@ -4,7 +4,7 @@ import { AbstractSimplePool, SubCloser } from './abstract-pool.ts' import { decrypt, encrypt } from './nip04.ts' import { NIP05_REGEX } from './nip05.ts' import { SimplePool } from './pool.ts' -import { Handlerinformation, NostrConnect } from './kinds.ts' +import { Handlerinformation, NostrConnect, NostrConnectAdmin } from './kinds.ts' var _fetch: any @@ -87,7 +87,7 @@ export class BunkerSigner { * @param remotePubkey - An optional remote public key. This is the key you want to sign as. * @param secretKey - An optional key pair. */ - public constructor(clientSecretKey: Uint8Array, bp: BunkerPointer) { + public constructor(clientSecretKey: Uint8Array, bp: BunkerPointer, params: { onauth?: (url: string) => void } = {}) { this.pool = new SimplePool() this.secretKey = clientSecretKey this.relays = bp.relays @@ -102,13 +102,18 @@ export class BunkerSigner { this.subCloser = this.pool.subscribeMany( this.relays, - [{ kinds: [NostrConnect, 24134], '#p': [getPublicKey(this.secretKey)] }], + [{ kinds: [NostrConnect, NostrConnectAdmin], '#p': [getPublicKey(this.secretKey)] }], { async onevent(event: NostrEvent) { const decryptedContent = await decrypt(clientSecretKey, event.pubkey, event.content) const parsedContent = JSON.parse(decryptedContent) const { id, result, error } = parsedContent + if (result === 'auth_url') { + params.onauth?.(error) + return + } + let handler = listeners[id] if (handler) { if (error) handler.reject(error) @@ -143,7 +148,7 @@ export class BunkerSigner { // the request event const verifiedEvent: VerifiedEvent = finalizeEvent( { - kind: method === 'create_account' ? 24134 : NostrConnect, + kind: method === 'create_account' ? NostrConnectAdmin : NostrConnect, tags: [['p', this.remotePubkey]], content: encryptedContent, created_at: Math.floor(Date.now() / 1000), @@ -154,15 +159,6 @@ export class BunkerSigner { // setup callback listener this.listeners[id] = { resolve, reject } - // Build auth_url handler - // const authHandler = (response: Response) => { - // if (response.result) { - // this.emit('authChallengeSuccess', response) - // } else { - // this.emit('authChallengeError', response.error) - // } - // } - // publish the event await Promise.any(this.pool.publish(this.relays, verifiedEvent)) } catch (err) {