Add NIP-44 encryption and decryption
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
nip04,
|
nip04,
|
||||||
nip19,
|
nip19,
|
||||||
|
nip44,
|
||||||
generateSecretKey,
|
generateSecretKey,
|
||||||
getPublicKey,
|
getPublicKey,
|
||||||
finalizeEvent,
|
finalizeEvent,
|
||||||
@@ -57,6 +58,8 @@ browser.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|||||||
case 'signEvent':
|
case 'signEvent':
|
||||||
case 'nip04.encrypt':
|
case 'nip04.encrypt':
|
||||||
case 'nip04.decrypt':
|
case 'nip04.decrypt':
|
||||||
|
case 'nip44.encrypt':
|
||||||
|
case 'nip44.decrypt':
|
||||||
case 'getRelays':
|
case 'getRelays':
|
||||||
validations[uuid] = sendResponse;
|
validations[uuid] = sendResponse;
|
||||||
ask(uuid, message);
|
ask(uuid, message);
|
||||||
@@ -153,6 +156,12 @@ function complete({ payload, origKind, event, remember, host }) {
|
|||||||
case 'nip04.decrypt':
|
case 'nip04.decrypt':
|
||||||
nip04Decrypt(event).then(e => sendResponse(e));
|
nip04Decrypt(event).then(e => sendResponse(e));
|
||||||
break;
|
break;
|
||||||
|
case 'nip44.encrypt':
|
||||||
|
nip44Encrypt(event).then(e => sendResponse(e));
|
||||||
|
break;
|
||||||
|
case 'nip44.decrypt':
|
||||||
|
nip44Decrypt(event).then(e => sendResponse(e));
|
||||||
|
break;
|
||||||
case 'getRelays':
|
case 'getRelays':
|
||||||
getRelays().then(e => sendResponse(e));
|
getRelays().then(e => sendResponse(e));
|
||||||
break;
|
break;
|
||||||
@@ -237,6 +246,18 @@ async function nip04Decrypt({ pubKey, cipherText }) {
|
|||||||
return nip04.decrypt(privKey, pubKey, cipherText);
|
return nip04.decrypt(privKey, pubKey, cipherText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function nip44Encrypt({ pubKey, plainText }) {
|
||||||
|
let privKey = await getPrivKey();
|
||||||
|
let conversationKey = nip44.getConversationKey(privKey, pubKey)
|
||||||
|
return nip44.encrypt(plainText, conversationKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function nip44Decrypt({ pubKey, cipherText }) {
|
||||||
|
let privKey = await getPrivKey();
|
||||||
|
let conversationKey = nip44.getConversationKey(privKey, pubKey)
|
||||||
|
return nip44.decrypt(cipherText, conversationKey);
|
||||||
|
}
|
||||||
|
|
||||||
async function getRelays() {
|
async function getRelays() {
|
||||||
let profile = await currentProfile();
|
let profile = await currentProfile();
|
||||||
let relays = profile.relays;
|
let relays = profile.relays;
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ window.addEventListener('message', async message => {
|
|||||||
'getRelays',
|
'getRelays',
|
||||||
'nip04.encrypt',
|
'nip04.encrypt',
|
||||||
'nip04.decrypt',
|
'nip04.decrypt',
|
||||||
|
'nip44.encrypt',
|
||||||
|
'nip44.decrypt',
|
||||||
];
|
];
|
||||||
let { kind, reqId, payload } = message.data;
|
let { kind, reqId, payload } = message.data;
|
||||||
if (!validEvents.includes(kind)) return;
|
if (!validEvents.includes(kind)) return;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"default_locale": "en",
|
"default_locale": "en",
|
||||||
"name": "__MSG_extension_name__",
|
"name": "__MSG_extension_name__",
|
||||||
"description": "__MSG_extension_description__",
|
"description": "__MSG_extension_description__",
|
||||||
"version": "2.0.0",
|
"version": "2.0.1",
|
||||||
"icons": {
|
"icons": {
|
||||||
"48": "images/icon-48.png",
|
"48": "images/icon-48.png",
|
||||||
"96": "images/icon-96.png",
|
"96": "images/icon-96.png",
|
||||||
|
|||||||
@@ -42,6 +42,22 @@ window.nostr = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
nip44: {
|
||||||
|
async encrypt(pubKey, plainText) {
|
||||||
|
return await window.nostr.broadcast('nip44.encrypt', {
|
||||||
|
pubKey,
|
||||||
|
plainText,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async decrypt(pubKey, cipherText) {
|
||||||
|
return await window.nostr.broadcast('nip44.decrypt', {
|
||||||
|
pubKey,
|
||||||
|
cipherText,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener('message', message => {
|
window.addEventListener('message', message => {
|
||||||
@@ -51,6 +67,8 @@ window.addEventListener('message', message => {
|
|||||||
'getRelays',
|
'getRelays',
|
||||||
'nip04.encrypt',
|
'nip04.encrypt',
|
||||||
'nip04.decrypt',
|
'nip04.decrypt',
|
||||||
|
'nip44.encrypt',
|
||||||
|
'nip44.decrypt',
|
||||||
].map(e => `return_${e}`);
|
].map(e => `return_${e}`);
|
||||||
let { kind, reqId, payload } = message.data;
|
let { kind, reqId, payload } = message.data;
|
||||||
|
|
||||||
|
|||||||
@@ -71,9 +71,13 @@ Alpine.data('permission', () => ({
|
|||||||
case 'getRelays':
|
case 'getRelays':
|
||||||
return 'Read relay list';
|
return 'Read relay list';
|
||||||
case 'nip04.encrypt':
|
case 'nip04.encrypt':
|
||||||
return 'Encrypt private message';
|
return 'Encrypt private message (NIP-04)';
|
||||||
case 'nip04.decrypt':
|
case 'nip04.decrypt':
|
||||||
return 'Decrypt private message';
|
return 'Decrypt private message (NIP-04)';
|
||||||
|
case 'nip44.encrypt':
|
||||||
|
return 'Encrypt private message (NIP-44)';
|
||||||
|
case 'nip44.decrypt':
|
||||||
|
return 'Decrypt private message (NIP-44)';
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -350,9 +350,13 @@ export function humanPermission(p) {
|
|||||||
case 'getRelays':
|
case 'getRelays':
|
||||||
return 'Read relay list';
|
return 'Read relay list';
|
||||||
case 'nip04.encrypt':
|
case 'nip04.encrypt':
|
||||||
return 'Encrypt private message';
|
return 'Encrypt private message (NIP-04)';
|
||||||
case 'nip04.decrypt':
|
case 'nip04.decrypt':
|
||||||
return 'Decrypt private message';
|
return 'Decrypt private message (NIP-04)';
|
||||||
|
case 'nip44.encrypt':
|
||||||
|
return 'Encrypt private message (NIP-44)';
|
||||||
|
case 'nip44.decrypt':
|
||||||
|
return 'Decrypt private message (NIP-44)';
|
||||||
default:
|
default:
|
||||||
return 'Unknown';
|
return 'Unknown';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user