Skip to content

Commit

Permalink
Switch back to simple interface for nip44
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Staab committed Feb 20, 2024
1 parent 9318232 commit 51538c3
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 273 deletions.
10 changes: 0 additions & 10 deletions src/app/router/Prompt/Prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import WebbtcEnable from "~/app/screens/Enable/WebbtcEnable";
import WeblnEnable from "~/app/screens/Enable/WeblnEnable";
import NostrConfirmDecrypt from "~/app/screens/Nostr/ConfirmDecrypt";
import NostrConfirmEncrypt from "~/app/screens/Nostr/ConfirmEncrypt";
import NostrConfirmNip44Decrypt from "~/app/screens/Nostr/ConfirmNip44Decrypt";
import NostrConfirmNip44Encrypt from "~/app/screens/Nostr/ConfirmNip44Encrypt";
import type { NavigationState, OriginData } from "~/types";

// Parse out the parameters from the querystring.
Expand Down Expand Up @@ -134,14 +132,6 @@ function Prompt() {
path="public/nostr/confirmDecrypt"
element={<NostrConfirmDecrypt />}
/>
<Route
path="public/nostr/confirmNip44Encrypt"
element={<NostrConfirmNip44Encrypt />}
/>
<Route
path="public/nostr/confirmNip44Decrypt"
element={<NostrConfirmNip44Decrypt />}
/>
<Route
path="public/nostr/confirmGetPublicKey"
element={<NostrConfirmGetPublicKey />}
Expand Down
112 changes: 0 additions & 112 deletions src/app/screens/Nostr/ConfirmNip44Decrypt.tsx

This file was deleted.

121 changes: 0 additions & 121 deletions src/app/screens/Nostr/ConfirmNip44Encrypt.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const nip44DecryptOrPrompt = async (

if (hasPermission) {
const nostr = await state.getState().getNostr();
const response = await nostr.nip44Decrypt(message.args.pairs);
const response = await nostr.nip44Decrypt(
message.args.peer,
message.args.ciphertext
);

return { data: response };
} else {
Expand All @@ -32,7 +35,7 @@ const nip44DecryptOrPrompt = async (
rememberPermission: boolean;
}>({
...message,
action: "public/nostr/confirmNip44Decrypt",
action: "public/nostr/confirmDecrypt",
});

// add permission to db only if user decided to always allow this request
Expand All @@ -44,7 +47,10 @@ const nip44DecryptOrPrompt = async (
}
if (promptResponse.data.confirm) {
const nostr = await state.getState().getNostr();
const response = await nostr.nip44Decrypt(message.args.pairs);
const response = await nostr.nip44Decrypt(
message.args.peer,
message.args.ciphertext
);

return { data: response };
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { USER_REJECTED_ERROR } from "~/common/constants";
import nostr from "~/common/lib/nostr";
import utils from "~/common/lib/utils";
import { getHostFromSender } from "~/common/utils/helpers";
import {
Expand All @@ -23,7 +24,8 @@ const nip44EncryptOrPrompt = async (

if (hasPermission) {
const response = (await state.getState().getNostr()).nip44Encrypt(
message.args.pairs
message.args.peer,
message.args.plaintext
);
return { data: response };
} else {
Expand All @@ -32,10 +34,11 @@ const nip44EncryptOrPrompt = async (
rememberPermission: boolean;
}>({
...message,
action: "public/nostr/confirmNip44Encrypt",
action: "public/nostr/confirmEncrypt",
args: {
nip44Encrypt: {
pairs: message.args.pairs,
recipientNpub: nostr.hexToNip19(message.args.peer, "npub"),
message: message.args.plaintext,
},
},
});
Expand All @@ -49,7 +52,8 @@ const nip44EncryptOrPrompt = async (
}
if (promptResponse.data.confirm) {
const response = (await state.getState().getNostr()).nip44Encrypt(
message.args.pairs
message.args.peer,
message.args.plaintext
);

return { data: response };
Expand Down
12 changes: 4 additions & 8 deletions src/extension/background-script/nostr/__test__/nostr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ describe("nostr.nip44", () => {
const aliceNostr = new Nostr(alice.privateKey);

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

const bobNostr = new Nostr(bob.privateKey);

const [decrypted] = await bobNostr.nip44Decrypt([
[alice.publicKey, encrypted],
]);
const decrypted = await bobNostr.nip44Decrypt(alice.publicKey, encrypted);

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

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

const carolNostr = new Nostr(carol.privateKey);

let decrypted;
try {
[decrypted] = await carolNostr.nip44Decrypt([
[alice.publicKey, encrypted],
]);
decrypted = await carolNostr.nip44Decrypt(alice.publicKey, encrypted);
} catch (e) {
decrypted = "error decrypting message";
}
Expand Down
12 changes: 4 additions & 8 deletions src/extension/background-script/nostr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,12 @@ class Nostr {
return Utf8.stringify(decrypted);
}

nip44Encrypt(pairs: [string, string][]) {
return pairs.map(([pubkey, text]: [string, string]) =>
nip44.v2.encrypt(text, this.getNip44SharedSecret(pubkey))
);
nip44Encrypt(peer: string, plaintext: string) {
return nip44.v2.encrypt(plaintext, this.getNip44SharedSecret(peer));
}

nip44Decrypt(pairs: [string, string][]) {
return pairs.map(([pubkey, payload]) =>
nip44.v2.decrypt(payload, this.getNip44SharedSecret(pubkey))
);
nip44Decrypt(peer: string, ciphertext: string) {
return nip44.v2.decrypt(ciphertext, this.getNip44SharedSecret(peer))
}

getEventHash(event: Event) {
Expand Down
Loading

0 comments on commit 51538c3

Please sign in to comment.