Skip to content

Commit 39e581d

Browse files
committed
js: add nip44_encrypt and nip44_decrypt functions
1 parent a30c6df commit 39e581d

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { Keys, nip44_encrypt, nip44_decrypt, Nip44Version, loadWasmSync } = require("../");
2+
3+
function main() {
4+
loadWasmSync();
5+
6+
let alice_keys = Keys.fromSkStr("5c0c523f52a5b6fad39ed2403092df8cebc36318b39383bca6c00808626fab3a");
7+
let bob_keys = Keys.fromSkStr("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99");
8+
9+
let payload = nip44_encrypt(alice_keys.secretKey, bob_keys.publicKey, "hello", 1);
10+
console.log(payload);
11+
12+
plaintext = nip44_decrypt(bob_keys.secretKey, alice_keys.publicKey, payload)
13+
console.log(plaintext);
14+
}
15+
16+
main();

bindings/nostr-js/src/nips/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod nip04;
55
pub mod nip05;
66
pub mod nip11;
77
pub mod nip26;
8+
pub mod nip44;

bindings/nostr-js/src/nips/nip04.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ use crate::key::{JsPublicKey, JsSecretKey};
1111

1212
/// Encrypt (NIP04)
1313
#[wasm_bindgen]
14-
pub fn encrypt(sk: &JsSecretKey, pk: &JsPublicKey, text: String) -> Result<String> {
14+
pub fn nip04_encrypt(sk: &JsSecretKey, pk: &JsPublicKey, text: String) -> Result<String> {
1515
nip04::encrypt(sk.deref(), pk.deref(), text).map_err(into_err)
1616
}
1717

1818
/// Decrypt (NIP04)
1919
#[wasm_bindgen]
20-
pub fn decrypt(sk: &JsSecretKey, pk: &JsPublicKey, encrypted_content: String) -> Result<String> {
20+
pub fn nip04_decrypt(
21+
sk: &JsSecretKey,
22+
pk: &JsPublicKey,
23+
encrypted_content: String,
24+
) -> Result<String> {
2125
nip04::decrypt(sk.deref(), pk.deref(), encrypted_content).map_err(into_err)
2226
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Distributed under the MIT software license
3+
4+
use std::ops::Deref;
5+
6+
use nostr::nips::nip44::{self, Version};
7+
use wasm_bindgen::prelude::*;
8+
9+
use crate::error::{into_err, Result};
10+
use crate::key::{JsPublicKey, JsSecretKey};
11+
12+
#[wasm_bindgen(js_name = NIP44Version)]
13+
pub enum JsNIP44Version {
14+
/// XChaCha20
15+
XChaCha20 = 1,
16+
}
17+
18+
impl From<Version> for JsNIP44Version {
19+
fn from(version: Version) -> Self {
20+
match version {
21+
Version::XChaCha20 => Self::XChaCha20,
22+
}
23+
}
24+
}
25+
26+
impl From<JsNIP44Version> for Version {
27+
fn from(version: JsNIP44Version) -> Self {
28+
match version {
29+
JsNIP44Version::XChaCha20 => Self::XChaCha20,
30+
}
31+
}
32+
}
33+
34+
/// Encrypt (NIP44)
35+
#[wasm_bindgen]
36+
pub fn nip44_encrypt(
37+
sk: &JsSecretKey,
38+
pk: &JsPublicKey,
39+
content: String,
40+
version: JsNIP44Version,
41+
) -> Result<String> {
42+
nip44::encrypt(sk.deref(), pk.deref(), content, version.into()).map_err(into_err)
43+
}
44+
45+
/// Decrypt (NIP44)
46+
#[wasm_bindgen]
47+
pub fn nip44_decrypt(sk: &JsSecretKey, pk: &JsPublicKey, payload: String) -> Result<String> {
48+
nip44::decrypt(sk.deref(), pk.deref(), payload).map_err(into_err)
49+
}

0 commit comments

Comments
 (0)