nostr-tools
2.20.02.21.0
lib/esm/nip04.js~
lib/esm/nip04.jsModified+8−8
Index: package/lib/esm/nip04.js
===================================================================
--- package/lib/esm/nip04.js
+++ package/lib/esm/nip04.js
@@ -1,19 +1,19 @@
// nip04.ts
-import { bytesToHex as bytesToHex2, randomBytes } from "@noble/hashes/utils";
-import { secp256k1 } from "@noble/curves/secp256k1";
-import { cbc } from "@noble/ciphers/aes";
+import { hexToBytes as hexToBytes2, randomBytes } from "@noble/hashes/utils.js";
+import { secp256k1 } from "@noble/curves/secp256k1.js";
+import { cbc } from "@noble/ciphers/aes.js";
import { base64 } from "@scure/base";
// utils.ts
-import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
+import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
var utf8Decoder = new TextDecoder("utf-8");
var utf8Encoder = new TextEncoder();
// nip04.ts
function encrypt(secretKey, pubkey, text) {
- const privkey = secretKey instanceof Uint8Array ? bytesToHex2(secretKey) : secretKey;
- const key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
+ const privkey = secretKey instanceof Uint8Array ? secretKey : hexToBytes2(secretKey);
+ const key = secp256k1.getSharedSecret(privkey, hexToBytes2("02" + pubkey));
const normalizedKey = getNormalizedX(key);
let iv = Uint8Array.from(randomBytes(16));
let plaintext = utf8Encoder.encode(text);
let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext);
@@ -21,11 +21,11 @@
let ivb64 = base64.encode(new Uint8Array(iv.buffer));
return `${ctb64}?iv=${ivb64}`;
}
function decrypt(secretKey, pubkey, data) {
- const privkey = secretKey instanceof Uint8Array ? bytesToHex2(secretKey) : secretKey;
+ const privkey = secretKey instanceof Uint8Array ? secretKey : hexToBytes2(secretKey);
let [ctb64, ivb64] = data.split("?iv=");
- let key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
+ let key = secp256k1.getSharedSecret(privkey, hexToBytes2("02" + pubkey));
let normalizedKey = getNormalizedX(key);
let iv = base64.decode(ivb64);
let ciphertext = base64.decode(ctb64);
let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext);