- Update NostrConnectService to use nos2x (window.nostr) by default - Fallback to NostrConnect bridge only if nos2x is not available - Update NostrRemoteSigner to use window.nostr.signEvent() for signing - Add TypeScript definitions for NIP-07 window.nostr API - Update documentation to reflect nos2x as primary authentication method - Remove default use.nsec.app bridge URL - All TypeScript checks pass
107 lines
2.9 KiB
TypeScript
107 lines
2.9 KiB
TypeScript
import type { NostrConnectState } from '@/types/nostr'
|
|
import { nostrService } from './nostr'
|
|
|
|
const NOSTRCONNECT_BRIDGE = process.env.NEXT_PUBLIC_NOSTRCONNECT_BRIDGE ?? ''
|
|
|
|
interface MessageData {
|
|
type?: string
|
|
method?: string
|
|
action?: string
|
|
pubkey?: string
|
|
publicKey?: string
|
|
privateKey?: string
|
|
secretKey?: string
|
|
message?: string
|
|
error?: string
|
|
params?: {
|
|
pubkey?: string
|
|
publicKey?: string
|
|
privateKey?: string
|
|
secretKey?: string
|
|
}
|
|
}
|
|
|
|
function handleConnectMessage(
|
|
data: MessageData,
|
|
onSuccess: (pubkey: string, privateKey?: string) => void,
|
|
onError: (error: Error) => void
|
|
): boolean {
|
|
const pubkey = data.pubkey ?? data.publicKey
|
|
const privateKey = data.privateKey ?? data.secretKey
|
|
|
|
if (!pubkey) {
|
|
console.error('No pubkey in message data:', data)
|
|
onError(new Error('No pubkey received'))
|
|
return false
|
|
}
|
|
nostrService.setPublicKey(pubkey)
|
|
if (privateKey) {
|
|
nostrService.setPrivateKey(privateKey)
|
|
}
|
|
|
|
onSuccess(pubkey, privateKey)
|
|
return true
|
|
}
|
|
|
|
function handleAlternativeConnectMessage(
|
|
data: MessageData,
|
|
onSuccess: (pubkey: string, privateKey?: string) => void,
|
|
onError: (error: Error) => void
|
|
): boolean {
|
|
const pubkey =
|
|
data.pubkey ?? data.publicKey ?? data.params?.pubkey ?? data.params?.publicKey
|
|
const privateKey =
|
|
data.privateKey ?? data.secretKey ?? data.params?.privateKey ?? data.params?.secretKey
|
|
|
|
if (!pubkey) {
|
|
console.error('No pubkey in message data:', data)
|
|
onError(new Error('No pubkey received'))
|
|
return false
|
|
}
|
|
nostrService.setPublicKey(pubkey)
|
|
if (privateKey) {
|
|
nostrService.setPrivateKey(privateKey)
|
|
}
|
|
|
|
onSuccess(pubkey, privateKey)
|
|
return true
|
|
}
|
|
|
|
function handleErrorMessage(data: MessageData, onError: (error: Error) => void): void {
|
|
const errorMessage = data.message ?? data.error ?? 'Connection failed'
|
|
console.error('Connection error:', errorMessage)
|
|
onError(new Error(errorMessage))
|
|
}
|
|
|
|
/**
|
|
* Handle NostrConnect connection message
|
|
*/
|
|
export function handleNostrConnectMessage(
|
|
event: MessageEvent,
|
|
_state: NostrConnectState,
|
|
onSuccess: (pubkey: string, privateKey?: string) => void,
|
|
onError: (error: Error) => void
|
|
): void {
|
|
const bridgeOrigin = new URL(NOSTRCONNECT_BRIDGE).origin
|
|
|
|
if (event.origin !== bridgeOrigin) {
|
|
console.warn('Origin mismatch:', event.origin, 'expected:', bridgeOrigin)
|
|
return
|
|
}
|
|
|
|
const data = event.data as MessageData | undefined
|
|
if (!data) {
|
|
return
|
|
}
|
|
|
|
const messageType = data.type ?? data.method ?? data.action
|
|
|
|
if (messageType === 'nostrconnect:connect' || messageType === 'connect') {
|
|
handleConnectMessage(data, onSuccess, onError)
|
|
} else if (messageType === 'nostrconnect:error' || messageType === 'error') {
|
|
handleErrorMessage(data, onError)
|
|
} else if (data.method === 'connect' || data.action === 'connect') {
|
|
handleAlternativeConnectMessage(data, onSuccess, onError)
|
|
}
|
|
}
|