feat: mute

This commit is contained in:
codytseng
2025-01-19 14:40:05 +08:00
parent 34ff0cd314
commit cbae26e492
26 changed files with 564 additions and 45 deletions

View File

@@ -48,6 +48,20 @@ export class BunkerSigner implements ISigner {
})
}
async nip04Encrypt(pubkey: string, plainText: string) {
if (!this.signer) {
throw new Error('Not logged in')
}
return await this.signer.nip04Encrypt(pubkey, plainText)
}
async nip04Decrypt(pubkey: string, cipherText: string) {
if (!this.signer) {
throw new Error('Not logged in')
}
return await this.signer.nip04Decrypt(pubkey, cipherText)
}
getClientSecretKey() {
return bytesToHex(this.clientSecretKey)
}

View File

@@ -37,6 +37,8 @@ type TNostrContext = {
publish: (draftEvent: TDraftEvent, additionalRelayUrls?: string[]) => Promise<Event>
signHttpAuth: (url: string, method: string) => Promise<string>
signEvent: (draftEvent: TDraftEvent) => Promise<Event>
nip04Encrypt: (pubkey: string, plainText: string) => Promise<string>
nip04Decrypt: (pubkey: string, cipherText: string) => Promise<string>
checkLogin: <T>(cb?: () => T) => Promise<T | void>
getRelayList: (pubkey: string) => Promise<TRelayList>
updateRelayListEvent: (relayListEvent: Event) => void
@@ -299,6 +301,14 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
return 'Nostr ' + btoa(JSON.stringify(event))
}
const nip04Encrypt = async (pubkey: string, plainText: string) => {
return signer?.nip04Encrypt(pubkey, plainText) ?? ''
}
const nip04Decrypt = async (pubkey: string, cipherText: string) => {
return signer?.nip04Decrypt(pubkey, cipherText) ?? ''
}
const checkLogin = async <T,>(cb?: () => T): Promise<T | void> => {
if (signer) {
return cb && cb()
@@ -349,6 +359,8 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
removeAccount,
publish,
signHttpAuth,
nip04Encrypt,
nip04Decrypt,
checkLogin,
signEvent,
getRelayList,

View File

@@ -23,4 +23,24 @@ export class Nip07Signer implements ISigner {
async signEvent(draftEvent: TDraftEvent) {
return await this.signer.signEvent(draftEvent)
}
async nip04Encrypt(pubkey: string, plainText: string) {
if (!this.signer) {
throw new Error('Not logged in')
}
if (!this.signer.nip04?.encrypt) {
throw new Error('The extension you are using does not support nip04 encryption')
}
return await this.signer.nip04.encrypt(pubkey, plainText)
}
async nip04Decrypt(pubkey: string, cipherText: string) {
if (!this.signer) {
throw new Error('Not logged in')
}
if (!this.signer.nip04?.decrypt) {
throw new Error('The extension you are using does not support nip04 decryption')
}
return await this.signer.nip04.decrypt(pubkey, cipherText)
}
}

View File

@@ -1,5 +1,5 @@
import { ISigner, TDraftEvent } from '@/types'
import { finalizeEvent, getPublicKey as nGetPublicKey, nip19 } from 'nostr-tools'
import { finalizeEvent, getPublicKey as nGetPublicKey, nip04, nip19 } from 'nostr-tools'
export class NsecSigner implements ISigner {
private privkey: Uint8Array | null = null
@@ -38,4 +38,18 @@ export class NsecSigner implements ISigner {
return null
}
}
async nip04Encrypt(pubkey: string, plainText: string) {
if (!this.privkey) {
throw new Error('Not logged in')
}
return nip04.encrypt(this.privkey, pubkey, plainText)
}
async nip04Decrypt(pubkey: string, cipherText: string) {
if (!this.privkey) {
throw new Error('Not logged in')
}
return nip04.decrypt(this.privkey, pubkey, cipherText)
}
}