Add pomade signer

This commit is contained in:
Jon Staab
2025-12-23 16:28:10 -08:00
parent e9b1172d15
commit 282b20c460
8 changed files with 361 additions and 21 deletions
+1
View File
@@ -4,3 +4,4 @@ export * from "./signers/nip01.js"
export * from "./signers/nip07.js"
export * from "./signers/nip46.js"
export * from "./signers/nip55.js"
export * from "./signers/pomade.js"
+68
View File
@@ -0,0 +1,68 @@
import * as nt44 from "nostr-tools/nip44"
import type {Client} from "@pomade/core"
import type {StampedEvent} from "@welshman/util"
import {thrower, hexToBytes} from "@welshman/lib"
import {ISigner, SignOptions, signWithOptions} from "../util.js"
export class PomadeSigner implements ISigner {
#pubkey: string
#sharedSecretCache = new Map<string, Uint8Array<ArrayBuffer>>()
constructor(readonly client: Client) {
this.#pubkey = client.userPubkey
}
private getSharedSecret = async (pubkey: string) => {
let sharedSecret = this.#sharedSecretCache.get(pubkey)
if (!sharedSecret) {
const hexSharedSecret = await this.client.getConversationKey(pubkey)
if (hexSharedSecret) {
sharedSecret = hexToBytes(hexSharedSecret)
this.#sharedSecretCache.set(pubkey, sharedSecret)
}
}
return sharedSecret
}
getPubkey = async () => this.#pubkey
sign = (event: StampedEvent, options: SignOptions = {}) => {
const promise = this.client.sign(event).then(r => {
if (!r.event) {
throw new Error(r.messages[0]?.payload.message || "Failed to sign event")
}
return r.event
})
return signWithOptions(promise, options)
}
nip04 = {
encrypt: thrower("PomadeSigner does not support nip44"),
decrypt: thrower("PomadeSigner does not support nip44"),
}
nip44 = {
encrypt: async (pubkey: string, message: string) => {
const sharedSecret = await this.getSharedSecret(pubkey)
if (!sharedSecret) {
throw new Error("Failed to get shared secret")
}
return nt44.v2.encrypt(message, sharedSecret)
},
decrypt: async (pubkey: string, message: string) => {
const sharedSecret = await this.getSharedSecret(pubkey)
if (!sharedSecret) {
throw new Error("Failed to get shared secret")
}
return nt44.v2.decrypt(message, sharedSecret)
},
}
}