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", () => ({
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")
})
})
+2 -2
View File
@@ -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"
+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
})
},
+5 -5
View File
@@ -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