feat: 💨

This commit is contained in:
codytseng
2025-02-07 22:56:00 +08:00
parent 5d21172017
commit ec19b9cbfe
6 changed files with 130 additions and 72 deletions

View File

@@ -39,9 +39,10 @@ type TNostrContext = {
options?: { additionalRelayUrls?: string[]; specifiedRelayUrls?: string[] }
) => Promise<Event>
signHttpAuth: (url: string, method: string) => Promise<string>
signEvent: (draftEvent: TDraftEvent) => Promise<Event>
signEvent: (draftEvent: TDraftEvent) => Promise<VerifiedEvent>
nip04Encrypt: (pubkey: string, plainText: string) => Promise<string>
nip04Decrypt: (pubkey: string, cipherText: string) => Promise<string>
startLogin: () => void
checkLogin: <T>(cb?: () => T) => Promise<T | void>
getRelayList: (pubkey: string) => Promise<TRelayList>
updateRelayListEvent: (relayListEvent: Event) => void
@@ -158,6 +159,14 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
client.initUserIndexFromFollowings(account.pubkey)
}, [account])
useEffect(() => {
if (signer) {
client.signer = signer.signEvent.bind(signer)
} else {
client.signer = undefined
}
}, [signer])
const hasNostrLoginHash = () => {
return window.location.hash && window.location.hash.startsWith('#nostr-login')
}
@@ -336,8 +345,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
: (relayList?.write ?? [])
.concat(additionalRelayUrls ?? [])
.concat(client.getDefaultRelayUrls()),
event,
{ signer: signEvent }
event
)
return event
}
@@ -415,6 +423,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
signHttpAuth,
nip04Encrypt,
nip04Decrypt,
startLogin: () => setOpenLoginDialog(true),
checkLogin,
signEvent,
getRelayList,

View File

@@ -23,20 +23,18 @@ export class NsecSigner implements ISigner {
}
async getPublicKey() {
if (!this.pubkey) {
throw new Error('Not logged in')
}
return this.pubkey
}
async signEvent(draftEvent: TDraftEvent) {
if (!this.privkey) {
return null
throw new Error('Not logged in')
}
try {
return finalizeEvent(draftEvent, this.privkey)
} catch (error) {
console.error(error)
return null
}
return finalizeEvent(draftEvent, this.privkey)
}
async nip04Encrypt(pubkey: string, plainText: string) {