From aff225dc01c9e81f8a2ec05acbd3cfa33cbbd530 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Wed, 2 Oct 2024 11:23:04 +0500 Subject: [PATCH] Enable nip17 DMs --- src/classes/accounts/account.ts | 15 +++++++++++---- src/services/decryption-cache.ts | 6 ++++-- src/services/signing.tsx | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/classes/accounts/account.ts b/src/classes/accounts/account.ts index 6c24b8265..0e6e9b93b 100644 --- a/src/classes/accounts/account.ts +++ b/src/classes/accounts/account.ts @@ -1,16 +1,23 @@ import { AppSettings } from "../../services/settings/migrations"; import { Nip07Interface } from "applesauce-signer"; +interface ExtendedNip07Interface extends Nip07Interface { + nip17?: { + encrypt: (pubkey: string, plaintext: string) => Promise; + decrypt: (pubkey: string, ciphertext: string) => Promise; + }; +} + export class Account { readonly type: string = "unknown"; pubkey: string; localSettings?: AppSettings; - protected _signer?: Nip07Interface | undefined; - public get signer(): Nip07Interface | undefined { + protected _signer?: ExtendedNip07Interface | undefined; + public get signer(): ExtendedNip07Interface | undefined { return this._signer; } - public set signer(value: Nip07Interface | undefined) { + public set signer(value: ExtendedNip07Interface | undefined) { this._signer = value; } @@ -32,4 +39,4 @@ export class Account { } return this; } -} +} \ No newline at end of file diff --git a/src/services/decryption-cache.ts b/src/services/decryption-cache.ts index b1f3a1388..6f8ff286d 100644 --- a/src/services/decryption-cache.ts +++ b/src/services/decryption-cache.ts @@ -6,12 +6,12 @@ import signingService from "./signing"; import accountService from "./account"; import { logger } from "../helpers/debug"; -type EncryptionType = "nip04" | "nip44"; +type EncryptionType = "nip04" | "nip44" | "nip17"; class DecryptionContainer { /** event id */ id: string; - type: "nip04" | "nip44"; + type: EncryptionType; pubkey: string; cipherText: string; @@ -51,6 +51,8 @@ class DecryptionCache { return await signingService.nip04Decrypt(container.cipherText, container.pubkey, account); case "nip44": return await signingService.nip44Decrypt(container.cipherText, container.pubkey, account); + case "nip17": + return await signingService.nip17Decrypt(container.cipherText, container.pubkey, account); } } diff --git a/src/services/signing.tsx b/src/services/signing.tsx index 768e4aa3d..bcbe9b137 100644 --- a/src/services/signing.tsx +++ b/src/services/signing.tsx @@ -68,6 +68,24 @@ class SigningService { if (!account.signer.nip44) throw new Error("Signer does not support NIP-44"); return account.signer.nip44.decrypt(pubkey, ciphertext); } + + async nip17Encrypt(plaintext: string, pubkey: string, account: Account) { + if (account.readonly) throw new Error("Can not encrypt in readonly mode"); + await this.unlockAccount(account); + + if (!account.signer) throw new Error("Account missing signer"); + if (!account.signer.nip17) throw new Error("Signer does not support NIP-17"); + return account.signer.nip17.encrypt(pubkey, plaintext); + } + + async nip17Decrypt(ciphertext: string, pubkey: string, account: Account) { + if (account.readonly) throw new Error("Can not decrypt in readonly mode"); + await this.unlockAccount(account); + + if (!account.signer) throw new Error("Account missing signer"); + if (!account.signer.nip17) throw new Error("Signer does not support NIP-17"); + return account.signer.nip17.decrypt(pubkey, ciphertext); + } } const signingService = new SigningService();