Name signers after nips

This commit is contained in:
Jon Staab
2024-08-08 11:43:29 -07:00
parent 536c8123f4
commit a8d0f5bc4f
9 changed files with 132 additions and 124 deletions
+53
View File
@@ -0,0 +1,53 @@
import {EventTemplate} from '@welshman/util'
import {hash, own, Sign, ISigner, EncryptionImplementation} from '../util'
export type Nip07 = {
signEvent: Sign
nip04: EncryptionImplementation
nip44: EncryptionImplementation
getPublicKey: () => string | undefined
}
export const getNip07 = () => (window as {nostr?: Nip07}).nostr
export class Nip07Signer implements ISigner {
#lock = Promise.resolve()
#then = async <T>(f: (ext: Nip07) => T | Promise<T>) => {
const promise = this.#lock.then(() => {
const ext = getNip07()
if (!ext) throw new Error("Nip07 is not enabled")
return f(ext)
})
// Recover from errors
this.#lock = promise.then(() => undefined, () => undefined)
return promise
}
getPubkey = async () => getNip07()!.getPublicKey()!
sign = async (template: EventTemplate) => {
const event = hash(own(await this.getPubkey(), template))
return this.#then(ext => ext.signEvent(event))
}
nip04 = {
encrypt: (pubkey: string, message: string) =>
this.#then(ext => ext.nip04.encrypt(pubkey, message)),
decrypt: (pubkey: string, message: string) =>
this.#then(ext => ext.nip04.decrypt(pubkey, message)),
}
nip44 = {
encrypt: (pubkey: string, message: string) =>
this.#then(ext => ext.nip44.encrypt(pubkey, message)),
decrypt: (pubkey: string, message: string) =>
this.#then(ext => ext.nip44.decrypt(pubkey, message)),
}
}