Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable NIP-17 Encrypted Direct Messages #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/classes/accounts/account.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
decrypt: (pubkey: string, ciphertext: string) => Promise<string>;
};
}

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;
}

Expand All @@ -32,4 +39,4 @@ export class Account {
}
return this;
}
}
}
6 changes: 4 additions & 2 deletions src/services/decryption-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/services/signing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down