Use new nip55 plugin

This commit is contained in:
Jon Staab
2026-02-23 15:10:36 -08:00
parent 2bf0808fc0
commit 6d5197b47c
5 changed files with 52 additions and 52 deletions
+1
View File
@@ -6,6 +6,7 @@ import {npubEncode} from "nostr-tools/nip19"
vi.mock("nostr-signer-capacitor-plugin", () => ({
NostrSignerPlugin: {
setPackageName: vi.fn(() => Promise.resolve()),
getPublicKey: vi.fn(() => ({npub: npubEncode("ee".repeat(32))})),
signEvent: vi.fn(() => ({
event: JSON.stringify({sig: "ee".repeat(64)}),
+37 -43
View File
@@ -1,6 +1,5 @@
import {NostrSignerPlugin, AppInfo} from "nostr-signer-capacitor-plugin"
import * as nip19 from "nostr-tools/nip19"
import {SignedEvent, StampedEvent, hash, own} from "@welshman/util"
import {SignedEvent, StampedEvent, hash, own, Pubkey} from "@welshman/util"
import {signWithOptions, SignOptions, ISigner} from "../util.js"
export const getNip55 = async (): Promise<AppInfo[]> => {
@@ -9,23 +8,19 @@ export const getNip55 = async (): Promise<AppInfo[]> => {
}
export class Nip55Signer implements ISigner {
#lock = Promise.resolve()
#plugin = NostrSignerPlugin
#npub?: string
#publicKey?: string
#pubkey?: string
#lock: Promise<unknown>
constructor(
readonly packageName: string,
publicKey?: string,
pubkey?: string,
) {
if (publicKey) {
this.#publicKey = publicKey
this.#npub = nip19.npubEncode(publicKey)
}
this.#pubkey = pubkey
this.#lock = NostrSignerPlugin.setPackageName(packageName)
}
#then = async <T>(f: (signer: typeof NostrSignerPlugin) => T | Promise<T>): Promise<T> => {
const promise = this.#lock.then(() => f(this.#plugin))
#then = async <T>(f: () => T | Promise<T>): Promise<T> => {
const promise = this.#lock.then(f)
this.#lock = promise.then(() => Promise.resolve())
@@ -33,15 +28,14 @@ export class Nip55Signer implements ISigner {
}
getPubkey = async (): Promise<string> => {
return this.#then(async signer => {
if (!this.#publicKey || !this.#npub) {
const {npub} = await signer.getPublicKey(this.packageName)
const {data} = nip19.decode(npub)
return this.#then(async () => {
if (!this.#pubkey) {
const {npub} = await NostrSignerPlugin.getPublicKey(this.packageName)
this.#npub = npub
this.#publicKey = data as string
this.#pubkey = Pubkey.from(npub).toString()
}
return this.#publicKey
return this.#pubkey
})
}
@@ -50,12 +44,12 @@ export class Nip55Signer implements ISigner {
this.getPubkey().then(pubkey => {
const hashedEvent = hash(own(template, pubkey))
return this.#then(async signer => {
const {event: json} = await signer.signEvent(
return this.#then(async () => {
const {event: json} = await NostrSignerPlugin.signEvent(
this.packageName,
JSON.stringify({sig: "", ...hashedEvent}),
hashedEvent.id,
this.#npub!,
this.#pubkey!,
)
return JSON.parse(json) as SignedEvent
@@ -66,35 +60,35 @@ export class Nip55Signer implements ISigner {
nip04 = {
encrypt: async (recipientPubKey: string, message: string): Promise<string> => {
const myNpub = this.#npub
if (!myNpub) {
const myPubkey = this.#pubkey
if (!myPubkey) {
await this.getPubkey()
}
return this.#then(async signer => {
return this.#then(async () => {
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip04Encrypt(
const {result} = await NostrSignerPlugin.nip04Encrypt(
this.packageName,
message,
id,
recipientPubKey,
this.#npub!,
this.#pubkey!,
)
return result
})
},
decrypt: async (senderPubKey: string, message: string): Promise<string> => {
const myNpub = this.#npub
if (!myNpub) {
const myPubkey = this.#pubkey
if (!myPubkey) {
await this.getPubkey()
}
return this.#then(async signer => {
return this.#then(async () => {
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip04Decrypt(
const {result} = await NostrSignerPlugin.nip04Decrypt(
this.packageName,
message,
id,
senderPubKey,
this.#npub!,
this.#pubkey!,
)
return result
})
@@ -103,35 +97,35 @@ export class Nip55Signer implements ISigner {
nip44 = {
encrypt: async (recipientPubKey: string, message: string): Promise<string> => {
const myNpub = this.#npub
if (!myNpub) {
const myPubkey = this.#pubkey
if (!myPubkey) {
await this.getPubkey()
}
return this.#then(async signer => {
return this.#then(async () => {
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip44Encrypt(
const {result} = await NostrSignerPlugin.nip44Encrypt(
this.packageName,
message,
id,
recipientPubKey,
this.#npub!,
this.#pubkey!,
)
return result
})
},
decrypt: async (senderPubKey: string, message: string): Promise<string> => {
const myNpub = this.#npub
if (!myNpub) {
const myPubkey = this.#pubkey
if (!myPubkey) {
await this.getPubkey()
}
return this.#then(async signer => {
return this.#then(async () => {
const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
const {result} = await signer.nip44Decrypt(
const {result} = await NostrSignerPlugin.nip44Decrypt(
this.packageName,
message,
id,
senderPubKey,
this.#npub!,
this.#pubkey!,
)
return result
})