Upgrade nip55 plugin

This commit is contained in:
Jon Staab
2026-02-23 09:49:07 -08:00
parent 3b6f74b24b
commit 2bf0808fc0
4 changed files with 66 additions and 76 deletions
+47 -60
View File
@@ -11,43 +11,21 @@ export const getNip55 = async (): Promise<AppInfo[]> => {
export class Nip55Signer implements ISigner {
#lock = Promise.resolve()
#plugin = NostrSignerPlugin
#packageName: string
#packageNameSet = false
#npub?: string
#publicKey?: string
constructor(packageName: string, publicKey?: string) {
this.#packageName = packageName
constructor(
readonly packageName: string,
publicKey?: string,
) {
if (publicKey) {
this.#publicKey = publicKey
this.#npub = nip19.npubEncode(publicKey)
}
this.#initialize()
}
#initialize() {
if (!this.#packageNameSet) {
void this.#plugin.setPackageName({packageName: this.#packageName}).then(() => {
this.#packageNameSet = true
})
}
}
#then = async <T>(f: (signer: typeof NostrSignerPlugin) => T | Promise<T>): Promise<T> => {
const promise = this.#lock.then(async () => {
if (!this.#packageNameSet) {
try {
await this.#plugin.setPackageName({packageName: this.#packageName})
this.#packageNameSet = true
} catch (error) {
this.#packageNameSet = false
throw error
}
}
return f(this.#plugin)
})
const promise = this.#lock.then(() => f(this.#plugin))
this.#lock = promise.then(() => Promise.resolve())
@@ -57,15 +35,11 @@ export class Nip55Signer implements ISigner {
getPubkey = async (): Promise<string> => {
return this.#then(async signer => {
if (!this.#publicKey || !this.#npub) {
try {
const {npub} = await signer.getPublicKey()
const {data} = nip19.decode(npub)
const {npub} = await signer.getPublicKey(this.packageName)
const {data} = nip19.decode(npub)
this.#npub = npub
this.#publicKey = data as string
} catch (error) {
throw new Error("Failed to get public key")
}
this.#npub = npub
this.#publicKey = data as string
}
return this.#publicKey
})
@@ -77,11 +51,12 @@ export class Nip55Signer implements ISigner {
const hashedEvent = hash(own(template, pubkey))
return this.#then(async signer => {
const {event: json} = await signer.signEvent({
eventJson: JSON.stringify({sig: "", ...hashedEvent}),
eventId: hashedEvent.id,
npub: this.#npub!,
})
const {event: json} = await signer.signEvent(
this.packageName,
JSON.stringify({sig: "", ...hashedEvent}),
hashedEvent.id,
this.#npub!,
)
return JSON.parse(json) as SignedEvent
})
@@ -96,11 +71,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey()
}
return this.#then(async signer => {
const {result} = await signer.nip04Encrypt({
pubKey: recipientPubKey,
plainText: message,
npub: this.#npub!,
})
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip04Encrypt(
this.packageName,
message,
id,
recipientPubKey,
this.#npub!,
)
return result
})
},
@@ -110,11 +88,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey()
}
return this.#then(async signer => {
const {result} = await signer.nip04Decrypt({
pubKey: senderPubKey,
encryptedText: message,
npub: this.#npub!,
})
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip04Decrypt(
this.packageName,
message,
id,
senderPubKey,
this.#npub!,
)
return result
})
},
@@ -127,11 +108,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey()
}
return this.#then(async signer => {
const {result} = await signer.nip44Encrypt({
pubKey: recipientPubKey,
plainText: message,
npub: this.#npub!,
})
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip44Encrypt(
this.packageName,
message,
id,
recipientPubKey,
this.#npub!,
)
return result
})
},
@@ -141,11 +125,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey()
}
return this.#then(async signer => {
const {result} = await signer.nip44Decrypt({
pubKey: senderPubKey,
encryptedText: message,
npub: this.#npub!,
})
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip44Decrypt(
this.packageName,
message,
id,
senderPubKey,
this.#npub!,
)
return result
})
},