**Motivations:** - Keep dependencies up to date for security and features - Automate dependency updates in deployment script - Fix compatibility issues with major version updates (React 19, Next.js 16, nostr-tools 2.x) **Root causes:** - Dependencies were outdated - Deployment script did not update dependencies before deploying - Major version updates introduced breaking API changes **Correctifs:** - Updated all dependencies to latest versions using npm-check-updates - Modified deploy.sh to run npm-check-updates before installing dependencies - Fixed nostr-tools 2.x API changes (generatePrivateKey -> generateSecretKey, signEvent -> finalizeEvent, verifySignature -> verifyEvent) - Fixed React 19 ref types to accept null - Fixed JSX namespace issues (JSX.Element -> React.ReactElement) - Added proper types for event callbacks - Fixed SimplePool.sub typing issues with type assertions **Evolutions:** - Deployment script now automatically updates dependencies to latest versions before deploying - All dependencies updated to latest versions (Next.js 14->16, React 18->19, nostr-tools 1->2, etc.) **Pages affectées:** - package.json - deploy.sh - lib/keyManagement.ts - lib/nostr.ts - lib/nostrRemoteSigner.ts - lib/zapVerification.ts - lib/platformTrackingEvents.ts - lib/sponsoringTracking.ts - lib/articlePublisherHelpersVerification.ts - lib/contentDeliveryVerification.ts - lib/paymentPollingZapReceipt.ts - lib/nostrPrivateMessages.ts - lib/nostrSubscription.ts - lib/nostrZapVerification.ts - lib/markdownRenderer.tsx - components/AuthorFilter.tsx - components/AuthorFilterButton.tsx - components/UserArticlesList.tsx - types/nostr-tools-extended.ts
82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import type { EventTemplate, Event } from 'nostr-tools'
|
|
import { finalizeEvent } from 'nostr-tools'
|
|
import { hexToBytes } from 'nostr-tools/utils'
|
|
import { nostrAuthService } from './nostrAuth'
|
|
import { nostrService } from './nostr'
|
|
|
|
/**
|
|
* Remote signer using Alby (NIP-07)
|
|
* Alby exposes window.nostr API for signing events
|
|
*/
|
|
export class NostrRemoteSigner {
|
|
/**
|
|
* Sign an event template using Alby (window.nostr)
|
|
*/
|
|
private buildUnsignedEvent(eventTemplate: EventTemplate, pubkey: string): EventTemplate & { pubkey: string } {
|
|
return {
|
|
pubkey,
|
|
...eventTemplate,
|
|
created_at: eventTemplate.created_at ?? Math.floor(Date.now() / 1000),
|
|
}
|
|
}
|
|
|
|
private async signWithAlby(unsignedEvent: EventTemplate & { pubkey: string }): Promise<Event | null> {
|
|
if (typeof window === 'undefined' || !window.nostr) {
|
|
return null
|
|
}
|
|
try {
|
|
const signedEvent = await window.nostr.signEvent({
|
|
kind: unsignedEvent.kind,
|
|
created_at: unsignedEvent.created_at,
|
|
tags: unsignedEvent.tags,
|
|
content: unsignedEvent.content,
|
|
})
|
|
return signedEvent as Event
|
|
} catch (e) {
|
|
console.error('Error signing with Alby:', e)
|
|
throw new Error('Failed to sign event with Alby extension')
|
|
}
|
|
}
|
|
|
|
private signWithPrivateKey(unsignedEvent: EventTemplate & { pubkey: string }): Event {
|
|
const privateKey = nostrService.getPrivateKey()
|
|
if (!privateKey) {
|
|
throw new Error('Alby extension required for signing. Please install and connect Alby browser extension.')
|
|
}
|
|
const secretKey = hexToBytes(privateKey)
|
|
return finalizeEvent(unsignedEvent, secretKey)
|
|
}
|
|
|
|
async signEvent(eventTemplate: EventTemplate): Promise<Event | null> {
|
|
const pubkey = nostrService.getPublicKey()
|
|
if (!pubkey) {
|
|
throw new Error('Public key required for signing. Please connect with Alby.')
|
|
}
|
|
|
|
const unsignedEvent = this.buildUnsignedEvent(eventTemplate, pubkey)
|
|
const albySigned = await this.signWithAlby(unsignedEvent)
|
|
if (albySigned) {
|
|
return albySigned
|
|
}
|
|
|
|
return this.signWithPrivateKey(unsignedEvent)
|
|
}
|
|
|
|
/**
|
|
* Check if remote signing is available
|
|
*/
|
|
isAvailable(): boolean {
|
|
const state = nostrAuthService.getState()
|
|
return state.connected && !!state.pubkey
|
|
}
|
|
|
|
/**
|
|
* Check if Alby is available
|
|
*/
|
|
isAlbyAvailable(): boolean {
|
|
return typeof window !== 'undefined' && typeof window.nostr !== 'undefined'
|
|
}
|
|
}
|
|
|
|
export const nostrRemoteSigner = new NostrRemoteSigner()
|