Skip to content

Commit 1b31920

Browse files
committed
refactor: nip04 encrypt decrypt functions
1 parent 9b431ab commit 1b31920

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

src/extension/background-script/actions/nostr/decryptOrPrompt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const decryptOrPrompt = async (message: MessageDecryptGet, sender: Sender) => {
2020

2121
if (hasPermission) {
2222
const nostr = await state.getState().getNostr();
23-
const response = await nostr.decrypt(
23+
const response = await nostr.nip04Decrypt(
2424
message.args.peer,
2525
message.args.ciphertext
2626
);
@@ -44,7 +44,7 @@ const decryptOrPrompt = async (message: MessageDecryptGet, sender: Sender) => {
4444
}
4545
if (promptResponse.data.confirm) {
4646
const nostr = await state.getState().getNostr();
47-
const response = await nostr.decrypt(
47+
const response = await nostr.nip04Decrypt(
4848
message.args.peer,
4949
message.args.ciphertext
5050
);

src/extension/background-script/actions/nostr/encryptOrPrompt.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const encryptOrPrompt = async (message: MessageEncryptGet, sender: Sender) => {
2020
);
2121

2222
if (hasPermission) {
23-
const response = (await state.getState().getNostr()).encrypt(
23+
const nostr = await state.getState().getNostr();
24+
const response = await nostr.nip04Encrypt(
2425
message.args.peer,
2526
message.args.plaintext
2627
);
@@ -48,7 +49,8 @@ const encryptOrPrompt = async (message: MessageEncryptGet, sender: Sender) => {
4849
);
4950
}
5051
if (promptResponse.data.confirm) {
51-
const response = (await state.getState().getNostr()).encrypt(
52+
const nostr = await state.getState().getNostr();
53+
const response = await nostr.nip04Encrypt(
5254
message.args.peer,
5355
message.args.plaintext
5456
);

src/extension/background-script/nostr/__test__/nostr.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ describe("nostr.nip04", () => {
2323
const aliceNostr = new Nostr(alice.privateKey);
2424

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

2828
const bobNostr = new Nostr(bob.privateKey);
2929

30-
const decrypted = await bobNostr.decrypt(alice.publicKey, encrypted);
30+
const decrypted = await bobNostr.nip04Decrypt(alice.publicKey, encrypted);
3131

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

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

4141
const carolNostr = new Nostr(carol.privateKey);
4242

4343
let decrypted;
4444
try {
45-
decrypted = await carolNostr.decrypt(alice.publicKey, encrypted);
45+
decrypted = await carolNostr.nip04Decrypt(alice.publicKey, encrypted);
4646
} catch (e) {
4747
decrypted = "error decrypting message";
4848
}

src/extension/background-script/nostr/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Nostr {
5353
return signedHex;
5454
}
5555

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

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

0 commit comments

Comments
 (0)