From 2bf0808fc09bd9f0722d6ed0ce70ea537e4079ab Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 23 Feb 2026 09:49:07 -0800 Subject: [PATCH] Upgrade nip55 plugin --- packages/signer/__tests__/nip55.test.ts | 21 +++-- packages/signer/package.json | 4 +- packages/signer/src/signers/nip55.ts | 107 +++++++++++------------- pnpm-lock.yaml | 10 +-- 4 files changed, 66 insertions(+), 76 deletions(-) diff --git a/packages/signer/__tests__/nip55.test.ts b/packages/signer/__tests__/nip55.test.ts index cd3b6f1..1b56787 100644 --- a/packages/signer/__tests__/nip55.test.ts +++ b/packages/signer/__tests__/nip55.test.ts @@ -6,21 +6,24 @@ import {npubEncode} from "nostr-tools/nip19" vi.mock("nostr-signer-capacitor-plugin", () => ({ NostrSignerPlugin: { - setPackageName: vi.fn().mockResolvedValue(undefined), getPublicKey: vi.fn(() => ({npub: npubEncode("ee".repeat(32))})), - signEvent: vi.fn().mockResolvedValue({ + signEvent: vi.fn(() => ({ event: JSON.stringify({sig: "ee".repeat(64)}), - }), - nip04Encrypt: vi.fn(({plainText}) => ({result: "encrypted:" + plainText})), - nip04Decrypt: vi.fn(({encryptedText}) => ({result: encryptedText.split("encrypted:")[1]})), - nip44Encrypt: vi.fn(({plainText}) => ({result: "encrypted:" + plainText})), - nip44Decrypt: vi.fn(({encryptedText}) => ({result: encryptedText.split("encrypted:")[1]})), + })), + nip04Encrypt: vi.fn((_, plainText: string) => ({result: "encrypted:" + plainText})), + nip04Decrypt: vi.fn((_, encryptedText: string) => ({ + result: encryptedText.split("encrypted:")[1], + })), + nip44Encrypt: vi.fn((_, plainText: string) => ({result: "encrypted:" + plainText})), + nip44Decrypt: vi.fn((_, encryptedText: string) => ({ + result: encryptedText.split("encrypted:")[1], + })), }, })) describe("Nip55Signer", () => { beforeEach(() => { - // Mock NostrSignerPlugin + vi.clearAllMocks() }) testSigner("Nip55Signer", () => new Nip55Signer("test-package")) @@ -29,6 +32,6 @@ describe("Nip55Signer", () => { it("should handle package initialization", async () => { const signer = new Nip55Signer("test-package") await signer.getPubkey() - expect(NostrSignerPlugin.setPackageName).toHaveBeenCalledWith({packageName: "test-package"}) + expect(NostrSignerPlugin.getPublicKey).toHaveBeenCalledWith("test-package") }) }) diff --git a/packages/signer/package.json b/packages/signer/package.json index afc60d0..0e4108f 100644 --- a/packages/signer/package.json +++ b/packages/signer/package.json @@ -25,7 +25,7 @@ "@welshman/lib": "workspace:*", "@welshman/net": "workspace:*", "@welshman/util": "workspace:*", - "nostr-signer-capacitor-plugin": "~0.0.4", + "nostr-signer-capacitor-plugin": "~0.0.5", "nostr-tools": "^2.19.4" }, "devDependencies": { @@ -35,7 +35,7 @@ "@welshman/lib": "workspace:*", "@welshman/net": "workspace:*", "@welshman/util": "workspace:*", - "nostr-signer-capacitor-plugin": "~0.0.4", + "nostr-signer-capacitor-plugin": "~0.0.5", "nostr-tools": "^2.19.4", "rimraf": "~6.0.0", "typescript": "~5.8.0" diff --git a/packages/signer/src/signers/nip55.ts b/packages/signer/src/signers/nip55.ts index 2ac14fe..d97b467 100644 --- a/packages/signer/src/signers/nip55.ts +++ b/packages/signer/src/signers/nip55.ts @@ -11,43 +11,21 @@ export const getNip55 = async (): Promise => { 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 (f: (signer: typeof NostrSignerPlugin) => T | Promise): Promise => { - 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 => { 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 }) }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7b4d7bc..751dca0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -404,8 +404,8 @@ importers: specifier: workspace:* version: link:../util nostr-signer-capacitor-plugin: - specifier: ~0.0.4 - version: 0.0.4(@capacitor/core@7.2.0) + specifier: ~0.0.5 + version: 0.0.5(@capacitor/core@7.2.0) nostr-tools: specifier: ^2.19.4 version: 2.19.4(typescript@5.8.2) @@ -2473,8 +2473,8 @@ packages: prosemirror-view: ^1.39.3 tiptap-markdown: ^0.8.10 - nostr-signer-capacitor-plugin@0.0.4: - resolution: {integrity: sha512-cuoTUULKE6g+CQmFgz3R5yP6OD4o5owvU1LdS+GdO5ElMdaBPlwiBzWSQpl6eYOFCqkGukhnXNEUWhgvUpgymg==} + nostr-signer-capacitor-plugin@0.0.5: + resolution: {integrity: sha512-/EvqWz71HZ5cWmzvfXWTm48AWZtbeZDbOg3vLwXyXPjnIp1DR7Wurww/Mo41ORNu1DNPlqH20l7kIXKO6vR5og==} peerDependencies: '@capacitor/core': ^7.0.0 @@ -5326,7 +5326,7 @@ snapshots: prosemirror-view: 1.41.3 tiptap-markdown: 0.8.10(@tiptap/core@2.11.7(@tiptap/pm@2.11.7)) - nostr-signer-capacitor-plugin@0.0.4(@capacitor/core@7.2.0): + nostr-signer-capacitor-plugin@0.0.5(@capacitor/core@7.2.0): dependencies: '@capacitor/core': 7.2.0