add support for NIP44 (chrome & firefox)

This commit is contained in:
DEV Sam Hayes
2025-02-15 15:08:48 +01:00
parent a8f3fad87b
commit c51e4d951f
16 changed files with 341 additions and 28 deletions

View File

@@ -12,7 +12,7 @@ import {
Permission_DECRYPTED,
Permission_ENCRYPTED,
} from '@common';
import { Event, EventTemplate, finalizeEvent, nip04 } from 'nostr-tools';
import { Event, EventTemplate, finalizeEvent, nip04, nip44 } from 'nostr-tools';
import { FirefoxMetaHandler } from './app/common/data/firefox-meta-handler';
import browser from 'webextension-polyfill';
@@ -146,11 +146,21 @@ export const checkPermissions = function (
return permissions.every((x) => x.methodPolicy === 'allow');
}
if (method === 'nip44.encrypt') {
// No evaluation of params required.
return permissions.every((x) => x.methodPolicy === 'allow');
}
if (method === 'nip04.decrypt') {
// No evaluation of params required.
return permissions.every((x) => x.methodPolicy === 'allow');
}
if (method === 'nip44.decrypt') {
// No evaluation of params required.
return permissions.every((x) => x.methodPolicy === 'allow');
}
return undefined;
};
@@ -243,6 +253,18 @@ export const nip04Encrypt = async function (
);
};
export const nip44Encrypt = async function (
privkey: string,
peerPubkey: string,
plaintext: string
): Promise<string> {
const key = nip44.v2.utils.getConversationKey(
NostrHelper.hex2bytes(privkey),
peerPubkey
);
return nip44.v2.encrypt(plaintext, key);
};
export const nip04Decrypt = async function (
privkey: string,
peerPubkey: string,
@@ -255,6 +277,19 @@ export const nip04Decrypt = async function (
);
};
export const nip44Decrypt = async function (
privkey: string,
peerPubkey: string,
ciphertext: string
): Promise<string> {
const key = nip44.v2.utils.getConversationKey(
NostrHelper.hex2bytes(privkey),
peerPubkey
);
return nip44.v2.decrypt(ciphertext, key);
};
const encryptPermission = async function (
permission: Permission_DECRYPTED,
iv: string,

View File

@@ -8,6 +8,8 @@ import {
getPosition,
nip04Decrypt,
nip04Encrypt,
nip44Decrypt,
nip44Encrypt,
PromptResponse,
PromptResponseMessage,
signEvent,
@@ -135,6 +137,13 @@ browser.runtime.onMessage.addListener(async (message /*, sender*/) => {
req.params.plaintext
);
case 'nip44.encrypt':
return await nip44Encrypt(
currentIdentity.privkey,
req.params.peerPubkey,
req.params.plaintext
);
case 'nip04.decrypt':
return await nip04Decrypt(
currentIdentity.privkey,
@@ -142,6 +151,13 @@ browser.runtime.onMessage.addListener(async (message /*, sender*/) => {
req.params.ciphertext
);
case 'nip44.decrypt':
return await nip44Decrypt(
currentIdentity.privkey,
req.params.peerPubkey,
req.params.ciphertext
);
default:
throw new Error(`Not supported request method '${req.method}'.`);
}

View File

@@ -110,15 +110,29 @@ const nostr = {
},
},
// nip44: {
// async encrypt(peer, plaintext) {
// return window.nostr._call('nip44.encrypt', { peer, plaintext });
// },
nip44: {
async encrypt(peerPubkey: string, plaintext: string): Promise<string> {
debug('nip44.encrypt received');
const ciphertext = (await nostr.messenger.request('nip44.encrypt', {
peerPubkey,
plaintext,
})) as string;
debug('nip44.encrypt response:');
debug(ciphertext);
return ciphertext;
},
// async decrypt(peer, ciphertext) {
// return window.nostr._call('nip44.decrypt', { peer, ciphertext });
// },
// },
async decrypt(peerPubkey: string, ciphertext: string): Promise<string> {
debug('nip44.decrypt received');
const plaintext = (await nostr.messenger.request('nip44.decrypt', {
peerPubkey,
ciphertext,
})) as string;
debug('nip44.decrypt response:');
debug(plaintext);
return plaintext;
},
},
};
window.nostr = nostr as any;

View File

@@ -24,10 +24,18 @@ switch (method) {
title = 'Encrypt';
break;
case 'nip44.encrypt':
title = 'Encrypt';
break;
case 'nip04.decrypt':
title = 'Decrypt';
break;
case 'nip44.decrypt':
title = 'Decrypt';
break;
case 'getRelays':
title = 'Get Relays';
break;
@@ -110,6 +118,24 @@ if (cardNip04EncryptElement && card2Nip04EncryptElement) {
}
}
const cardNip44EncryptElement = document.getElementById('cardNip44Encrypt');
const card2Nip44EncryptElement = document.getElementById('card2Nip44Encrypt');
if (cardNip44EncryptElement && card2Nip44EncryptElement) {
if (method === 'nip44.encrypt') {
const card2Nip44Encrypt_textElement = document.getElementById(
'card2Nip44Encrypt_text'
);
if (card2Nip44Encrypt_textElement) {
const eventObject: { peerPubkey: string; plaintext: string } =
JSON.parse(event);
card2Nip44Encrypt_textElement.innerText = eventObject.plaintext;
}
} else {
cardNip44EncryptElement.style.display = 'none';
card2Nip44EncryptElement.style.display = 'none';
}
}
const cardNip04DecryptElement = document.getElementById('cardNip04Decrypt');
const card2Nip04DecryptElement = document.getElementById('card2Nip04Decrypt');
if (cardNip04DecryptElement && card2Nip04DecryptElement) {
@@ -128,6 +154,24 @@ if (cardNip04DecryptElement && card2Nip04DecryptElement) {
}
}
const cardNip44DecryptElement = document.getElementById('cardNip44Decrypt');
const card2Nip44DecryptElement = document.getElementById('card2Nip44Decrypt');
if (cardNip44DecryptElement && card2Nip44DecryptElement) {
if (method === 'nip44.decrypt') {
const card2Nip44Decrypt_textElement = document.getElementById(
'card2Nip44Decrypt_text'
);
if (card2Nip44Decrypt_textElement) {
const eventObject: { peerPubkey: string; ciphertext: string } =
JSON.parse(event);
card2Nip44Decrypt_textElement.innerText = eventObject.ciphertext;
}
} else {
cardNip44DecryptElement.style.display = 'none';
card2Nip44DecryptElement.style.display = 'none';
}
}
//
// Functions
//