diff --git a/src/controllers/NostrController.ts b/src/controllers/NostrController.ts index c3d2908..a976955 100644 --- a/src/controllers/NostrController.ts +++ b/src/controllers/NostrController.ts @@ -1,9 +1,9 @@ import { EventTemplate, UnsignedEvent } from 'nostr-tools' -import { WindowNostr } from 'nostr-tools/nip07' import { EventEmitter } from 'tseep' import store from '../store/store' import { SignedEvent } from '../types' import { LoginMethodContext } from '../services/LoginMethodStrategy/loginMethodContext' +import { unixNow } from '../utils' export class NostrController extends EventEmitter { private static instance: NostrController @@ -11,13 +11,6 @@ export class NostrController extends EventEmitter { private constructor() { super() } - private getNostrObject = () => { - if (window.nostr) return window.nostr as WindowNostr - - throw new Error( - `window.nostr object not present. Make sure you have an nostr extension installed/working properly.` - ) - } public static getInstance(): NostrController { if (!NostrController.instance) { @@ -97,23 +90,37 @@ export class NostrController extends EventEmitter { } /** - * Function will capture the public key from the nostr extension or if no extension present - * function wil capture the public key from the local storage + * Function will capture the public key from signedEvent */ capturePublicKey = async (): Promise => { - const nostr = this.getNostrObject() - const pubKey = await nostr.getPublicKey().catch((err: unknown) => { - if (err instanceof Error) { - return Promise.reject(err.message) - } else { - return Promise.reject(JSON.stringify(err)) + try { + const timestamp = unixNow() + const { href } = window.location + + const authEvent: EventTemplate = { + kind: 27235, + tags: [ + ['u', href], + ['method', 'GET'] + ], + content: '', + created_at: timestamp } - }) - if (!pubKey) { - return Promise.reject('Error getting public key, user canceled') + const signedAuthEvent = await this.signEvent(authEvent) + const pubkey = signedAuthEvent.pubkey + + if (!pubkey) { + return Promise.reject('Error getting public key, user canceled') + } + + return Promise.resolve(pubkey) + } catch (error) { + if (error instanceof Error) { + return Promise.reject(error.message) + } else { + return Promise.reject(JSON.stringify(error)) + } } - - return Promise.resolve(pubKey) } }