feat: pow

This commit is contained in:
codytseng
2025-09-08 23:20:03 +08:00
parent 95f4b207d5
commit 4cc16d5e58
24 changed files with 191 additions and 67 deletions

View File

@@ -7,7 +7,12 @@ import {
createRelayListDraftEvent,
createSeenNotificationsAtDraftEvent
} from '@/lib/draft-event'
import { getLatestEvent, getReplaceableEventIdentifier, isProtectedEvent } from '@/lib/event'
import {
getLatestEvent,
getReplaceableEventIdentifier,
isProtectedEvent,
minePow
} from '@/lib/event'
import { getProfileFromEvent, getRelayListFromEvent } from '@/lib/event-metadata'
import { formatPubkey, pubkeyToNpub } from '@/lib/pubkey'
import client from '@/services/client.service'
@@ -594,12 +599,22 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
return event as VerifiedEvent
}
const publish = async (draftEvent: TDraftEvent, options: TPublishOptions = {}) => {
const publish = async (
draftEvent: TDraftEvent,
{ minPow = 0, ...options }: TPublishOptions = {}
) => {
if (!account || !signer || account.signerType === 'npub') {
throw new Error('You need to login first')
}
const event = await signEvent(draftEvent)
const draft = JSON.parse(JSON.stringify(draftEvent)) as TDraftEvent
let event: VerifiedEvent
if (minPow > 0) {
const unsignedEvent = await minePow({ ...draft, pubkey: account.pubkey }, minPow)
event = await signEvent(unsignedEvent)
} else {
event = await signEvent(draft)
}
if (event.kind !== kinds.Application && event.pubkey !== account.pubkey) {
const eventAuthor = await client.fetchProfile(event.pubkey)