diff --git a/src/lib/chat/adapters/nip-17-adapter.ts b/src/lib/chat/adapters/nip-17-adapter.ts index 3e78c0f..4ff1e28 100644 --- a/src/lib/chat/adapters/nip-17-adapter.ts +++ b/src/lib/chat/adapters/nip-17-adapter.ts @@ -168,10 +168,26 @@ export class Nip17Adapter extends ChatProtocolAdapter { throw new Error("No active account or signer"); } - // Check if signer supports NIP-44 encryption - if (!activeSigner.nip44Encrypt) { + // Helper to call NIP-44 encrypt (supports both signer patterns) + const nip44Encrypt = async ( + pubkey: string, + plaintext: string, + ): Promise => { + // Try direct method (PasswordSigner, NostrConnectSigner, etc.) + if (typeof activeSigner.nip44Encrypt === "function") { + return await activeSigner.nip44Encrypt(pubkey, plaintext); + } + + // Try nip44 getter (ExtensionSigner) + if ( + activeSigner.nip44 && + typeof activeSigner.nip44.encrypt === "function" + ) { + return await activeSigner.nip44.encrypt(pubkey, plaintext); + } + throw new Error("Signer does not support NIP-44 encryption"); - } + }; const recipientPubkey = conversation.participants.find( (p) => p.pubkey !== activePubkey, @@ -230,10 +246,7 @@ export class Nip17Adapter extends ChatProtocolAdapter { // Step 2: Create the seal (kind 13) // Encrypt the rumor with conversation key (sender → recipient) const rumorJSON = JSON.stringify(rumor); - const encryptedRumor = await activeSigner.nip44Encrypt( - recipientPubkey, - rumorJSON, - ); + const encryptedRumor = await nip44Encrypt(recipientPubkey, rumorJSON); // Sign the seal const sealDraft = { diff --git a/src/services/gift-wrap.ts b/src/services/gift-wrap.ts index 5dc3457..ef048c7 100644 --- a/src/services/gift-wrap.ts +++ b/src/services/gift-wrap.ts @@ -568,17 +568,30 @@ class GiftWrapManager { throw new Error("Gift wrap not addressed to this pubkey"); } + // Helper to call NIP-44 decrypt (supports both signer patterns) + const nip44Decrypt = async ( + pubkey: string, + ciphertext: string, + ): Promise => { + // Try direct method (PasswordSigner, NostrConnectSigner, etc.) + if (typeof signer.nip44Decrypt === "function") { + return await signer.nip44Decrypt(pubkey, ciphertext); + } + + // Try nip44 getter (ExtensionSigner) + if (signer.nip44 && typeof signer.nip44.decrypt === "function") { + return await signer.nip44.decrypt(pubkey, ciphertext); + } + + throw new Error("Signer does not support NIP-44 decryption"); + }; + // Step 1: Decrypt the gift wrap to get the seal (kind 13) // The gift wrap is encrypted with the conversation key between // the random ephemeral key (giftWrap.pubkey) and our key (recipientPubkey) let sealJSON: string; try { - // Check if signer has nip44Decrypt capability - if (!signer.nip44Decrypt) { - throw new Error("Signer does not support NIP-44 decryption"); - } - - sealJSON = await signer.nip44Decrypt(giftWrap.pubkey, giftWrap.content); + sealJSON = await nip44Decrypt(giftWrap.pubkey, giftWrap.content); } catch (error) { throw new Error(`Failed to decrypt gift wrap: ${error}`); } @@ -601,11 +614,7 @@ class GiftWrapManager { // the sender (seal.pubkey) and us (recipientPubkey) let rumorJSON: string; try { - if (!signer.nip44Decrypt) { - throw new Error("Signer does not support NIP-44 decryption"); - } - - rumorJSON = await signer.nip44Decrypt(seal.pubkey, seal.content); + rumorJSON = await nip44Decrypt(seal.pubkey, seal.content); } catch (error) { throw new Error(`Failed to decrypt seal: ${error}`); }