Skip to content

Commit

Permalink
refactor: nip04 encrypt decrypt functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanjoshi914 committed Mar 13, 2024
1 parent 9b431ab commit 1b31920
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const decryptOrPrompt = async (message: MessageDecryptGet, sender: Sender) => {

if (hasPermission) {
const nostr = await state.getState().getNostr();
const response = await nostr.decrypt(
const response = await nostr.nip04Decrypt(
message.args.peer,
message.args.ciphertext
);
Expand All @@ -44,7 +44,7 @@ const decryptOrPrompt = async (message: MessageDecryptGet, sender: Sender) => {
}
if (promptResponse.data.confirm) {
const nostr = await state.getState().getNostr();
const response = await nostr.decrypt(
const response = await nostr.nip04Decrypt(
message.args.peer,
message.args.ciphertext
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const encryptOrPrompt = async (message: MessageEncryptGet, sender: Sender) => {
);

if (hasPermission) {
const response = (await state.getState().getNostr()).encrypt(
const nostr = await state.getState().getNostr();
const response = await nostr.nip04Encrypt(
message.args.peer,
message.args.plaintext
);
Expand Down Expand Up @@ -48,7 +49,8 @@ const encryptOrPrompt = async (message: MessageEncryptGet, sender: Sender) => {
);
}
if (promptResponse.data.confirm) {
const response = (await state.getState().getNostr()).encrypt(
const nostr = await state.getState().getNostr();
const response = await nostr.nip04Encrypt(
message.args.peer,
message.args.plaintext
);
Expand Down
8 changes: 4 additions & 4 deletions src/extension/background-script/nostr/__test__/nostr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe("nostr.nip04", () => {
const aliceNostr = new Nostr(alice.privateKey);

const message = "Secret message that is sent from Alice to Bob";
const encrypted = aliceNostr.encrypt(bob.publicKey, message);
const encrypted = aliceNostr.nip04Encrypt(bob.publicKey, message);

const bobNostr = new Nostr(bob.privateKey);

const decrypted = await bobNostr.decrypt(alice.publicKey, encrypted);
const decrypted = await bobNostr.nip04Decrypt(alice.publicKey, encrypted);

expect(decrypted).toMatch(message);
});
Expand All @@ -36,13 +36,13 @@ describe("nostr.nip04", () => {
const aliceNostr = new Nostr(alice.privateKey);

const message = "Secret message that is sent from Alice to Bob";
const encrypted = aliceNostr.encrypt(bob.publicKey, message);
const encrypted = aliceNostr.nip04Encrypt(bob.publicKey, message);

const carolNostr = new Nostr(carol.privateKey);

let decrypted;
try {
decrypted = await carolNostr.decrypt(alice.publicKey, encrypted);
decrypted = await carolNostr.nip04Decrypt(alice.publicKey, encrypted);
} catch (e) {
decrypted = "error decrypting message";
}
Expand Down
4 changes: 2 additions & 2 deletions src/extension/background-script/nostr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Nostr {
return signedHex;
}

encrypt(pubkey: string, text: string) {
nip04Encrypt(pubkey: string, text: string) {
const key = secp256k1.getSharedSecret(this.privateKey, "02" + pubkey);
const normalizedKey = Buffer.from(key.slice(1, 33));
const hexNormalizedKey = secp256k1.etc.bytesToHex(normalizedKey);
Expand All @@ -68,7 +68,7 @@ class Nostr {
)}`;
}

async decrypt(pubkey: string, ciphertext: string) {
async nip04Decrypt(pubkey: string, ciphertext: string) {
const [cip, iv] = ciphertext.split("?iv=");
const key = secp256k1.getSharedSecret(this.privateKey, "02" + pubkey);
const normalizedKey = Buffer.from(key.slice(1, 33));
Expand Down

0 comments on commit 1b31920

Please sign in to comment.