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
+12 -9
View File
@@ -6,21 +6,24 @@ import {npubEncode} from "nostr-tools/nip19"
vi.mock("nostr-signer-capacitor-plugin", () => ({ vi.mock("nostr-signer-capacitor-plugin", () => ({
NostrSignerPlugin: { NostrSignerPlugin: {
setPackageName: vi.fn().mockResolvedValue(undefined),
getPublicKey: vi.fn(() => ({npub: npubEncode("ee".repeat(32))})), getPublicKey: vi.fn(() => ({npub: npubEncode("ee".repeat(32))})),
signEvent: vi.fn().mockResolvedValue({ signEvent: vi.fn(() => ({
event: JSON.stringify({sig: "ee".repeat(64)}), event: JSON.stringify({sig: "ee".repeat(64)}),
}), })),
nip04Encrypt: vi.fn(({plainText}) => ({result: "encrypted:" + plainText})), nip04Encrypt: vi.fn((_, plainText: string) => ({result: "encrypted:" + plainText})),
nip04Decrypt: vi.fn(({encryptedText}) => ({result: encryptedText.split("encrypted:")[1]})), nip04Decrypt: vi.fn((_, encryptedText: string) => ({
nip44Encrypt: vi.fn(({plainText}) => ({result: "encrypted:" + plainText})), result: encryptedText.split("encrypted:")[1],
nip44Decrypt: vi.fn(({encryptedText}) => ({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", () => { describe("Nip55Signer", () => {
beforeEach(() => { beforeEach(() => {
// Mock NostrSignerPlugin vi.clearAllMocks()
}) })
testSigner("Nip55Signer", () => new Nip55Signer("test-package")) testSigner("Nip55Signer", () => new Nip55Signer("test-package"))
@@ -29,6 +32,6 @@ describe("Nip55Signer", () => {
it("should handle package initialization", async () => { it("should handle package initialization", async () => {
const signer = new Nip55Signer("test-package") const signer = new Nip55Signer("test-package")
await signer.getPubkey() await signer.getPubkey()
expect(NostrSignerPlugin.setPackageName).toHaveBeenCalledWith({packageName: "test-package"}) expect(NostrSignerPlugin.getPublicKey).toHaveBeenCalledWith("test-package")
}) })
}) })
+2 -2
View File
@@ -25,7 +25,7 @@
"@welshman/lib": "workspace:*", "@welshman/lib": "workspace:*",
"@welshman/net": "workspace:*", "@welshman/net": "workspace:*",
"@welshman/util": "workspace:*", "@welshman/util": "workspace:*",
"nostr-signer-capacitor-plugin": "~0.0.4", "nostr-signer-capacitor-plugin": "~0.0.5",
"nostr-tools": "^2.19.4" "nostr-tools": "^2.19.4"
}, },
"devDependencies": { "devDependencies": {
@@ -35,7 +35,7 @@
"@welshman/lib": "workspace:*", "@welshman/lib": "workspace:*",
"@welshman/net": "workspace:*", "@welshman/net": "workspace:*",
"@welshman/util": "workspace:*", "@welshman/util": "workspace:*",
"nostr-signer-capacitor-plugin": "~0.0.4", "nostr-signer-capacitor-plugin": "~0.0.5",
"nostr-tools": "^2.19.4", "nostr-tools": "^2.19.4",
"rimraf": "~6.0.0", "rimraf": "~6.0.0",
"typescript": "~5.8.0" "typescript": "~5.8.0"
+47 -60
View File
@@ -11,43 +11,21 @@ export const getNip55 = async (): Promise<AppInfo[]> => {
export class Nip55Signer implements ISigner { export class Nip55Signer implements ISigner {
#lock = Promise.resolve() #lock = Promise.resolve()
#plugin = NostrSignerPlugin #plugin = NostrSignerPlugin
#packageName: string
#packageNameSet = false
#npub?: string #npub?: string
#publicKey?: string #publicKey?: string
constructor(packageName: string, publicKey?: string) { constructor(
this.#packageName = packageName readonly packageName: string,
publicKey?: string,
) {
if (publicKey) { if (publicKey) {
this.#publicKey = publicKey this.#publicKey = publicKey
this.#npub = nip19.npubEncode(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> => { #then = async <T>(f: (signer: typeof NostrSignerPlugin) => T | Promise<T>): Promise<T> => {
const promise = this.#lock.then(async () => { const promise = this.#lock.then(() => f(this.#plugin))
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)
})
this.#lock = promise.then(() => Promise.resolve()) this.#lock = promise.then(() => Promise.resolve())
@@ -57,15 +35,11 @@ export class Nip55Signer implements ISigner {
getPubkey = async (): Promise<string> => { getPubkey = async (): Promise<string> => {
return this.#then(async signer => { return this.#then(async signer => {
if (!this.#publicKey || !this.#npub) { if (!this.#publicKey || !this.#npub) {
try { const {npub} = await signer.getPublicKey(this.packageName)
const {npub} = await signer.getPublicKey() const {data} = nip19.decode(npub)
const {data} = nip19.decode(npub)
this.#npub = npub this.#npub = npub
this.#publicKey = data as string this.#publicKey = data as string
} catch (error) {
throw new Error("Failed to get public key")
}
} }
return this.#publicKey return this.#publicKey
}) })
@@ -77,11 +51,12 @@ export class Nip55Signer implements ISigner {
const hashedEvent = hash(own(template, pubkey)) const hashedEvent = hash(own(template, pubkey))
return this.#then(async signer => { return this.#then(async signer => {
const {event: json} = await signer.signEvent({ const {event: json} = await signer.signEvent(
eventJson: JSON.stringify({sig: "", ...hashedEvent}), this.packageName,
eventId: hashedEvent.id, JSON.stringify({sig: "", ...hashedEvent}),
npub: this.#npub!, hashedEvent.id,
}) this.#npub!,
)
return JSON.parse(json) as SignedEvent return JSON.parse(json) as SignedEvent
}) })
@@ -96,11 +71,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey() await this.getPubkey()
} }
return this.#then(async signer => { return this.#then(async signer => {
const {result} = await signer.nip04Encrypt({ const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
pubKey: recipientPubKey, const {result} = await signer.nip04Encrypt(
plainText: message, this.packageName,
npub: this.#npub!, message,
}) id,
recipientPubKey,
this.#npub!,
)
return result return result
}) })
}, },
@@ -110,11 +88,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey() await this.getPubkey()
} }
return this.#then(async signer => { return this.#then(async signer => {
const {result} = await signer.nip04Decrypt({ const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
pubKey: senderPubKey, const {result} = await signer.nip04Decrypt(
encryptedText: message, this.packageName,
npub: this.#npub!, message,
}) id,
senderPubKey,
this.#npub!,
)
return result return result
}) })
}, },
@@ -127,11 +108,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey() await this.getPubkey()
} }
return this.#then(async signer => { return this.#then(async signer => {
const {result} = await signer.nip44Encrypt({ const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
pubKey: recipientPubKey, const {result} = await signer.nip44Encrypt(
plainText: message, this.packageName,
npub: this.#npub!, message,
}) id,
recipientPubKey,
this.#npub!,
)
return result return result
}) })
}, },
@@ -141,11 +125,14 @@ export class Nip55Signer implements ISigner {
await this.getPubkey() await this.getPubkey()
} }
return this.#then(async signer => { return this.#then(async signer => {
const {result} = await signer.nip44Decrypt({ const id = `${Date.now()}-${Math.random().toString(36).slice(2)}`
pubKey: senderPubKey, const {result} = await signer.nip44Decrypt(
encryptedText: message, this.packageName,
npub: this.#npub!, message,
}) id,
senderPubKey,
this.#npub!,
)
return result return result
}) })
}, },
+5 -5
View File
@@ -404,8 +404,8 @@ importers:
specifier: workspace:* specifier: workspace:*
version: link:../util version: link:../util
nostr-signer-capacitor-plugin: nostr-signer-capacitor-plugin:
specifier: ~0.0.4 specifier: ~0.0.5
version: 0.0.4(@capacitor/core@7.2.0) version: 0.0.5(@capacitor/core@7.2.0)
nostr-tools: nostr-tools:
specifier: ^2.19.4 specifier: ^2.19.4
version: 2.19.4(typescript@5.8.2) version: 2.19.4(typescript@5.8.2)
@@ -2473,8 +2473,8 @@ packages:
prosemirror-view: ^1.39.3 prosemirror-view: ^1.39.3
tiptap-markdown: ^0.8.10 tiptap-markdown: ^0.8.10
nostr-signer-capacitor-plugin@0.0.4: nostr-signer-capacitor-plugin@0.0.5:
resolution: {integrity: sha512-cuoTUULKE6g+CQmFgz3R5yP6OD4o5owvU1LdS+GdO5ElMdaBPlwiBzWSQpl6eYOFCqkGukhnXNEUWhgvUpgymg==} resolution: {integrity: sha512-/EvqWz71HZ5cWmzvfXWTm48AWZtbeZDbOg3vLwXyXPjnIp1DR7Wurww/Mo41ORNu1DNPlqH20l7kIXKO6vR5og==}
peerDependencies: peerDependencies:
'@capacitor/core': ^7.0.0 '@capacitor/core': ^7.0.0
@@ -5326,7 +5326,7 @@ snapshots:
prosemirror-view: 1.41.3 prosemirror-view: 1.41.3
tiptap-markdown: 0.8.10(@tiptap/core@2.11.7(@tiptap/pm@2.11.7)) 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: dependencies:
'@capacitor/core': 7.2.0 '@capacitor/core': 7.2.0