nostr-tools
2.20.02.21.0
lib/cjs/nip04.js~
lib/cjs/nip04.jsModified+8−8
Index: package/lib/cjs/nip04.js
===================================================================
--- package/lib/cjs/nip04.js
+++ package/lib/cjs/nip04.js
@@ -23,22 +23,22 @@
decrypt: () => decrypt,
encrypt: () => encrypt
});
module.exports = __toCommonJS(nip04_exports);
-var import_utils2 = require("@noble/hashes/utils");
-var import_secp256k1 = require("@noble/curves/secp256k1");
-var import_aes = require("@noble/ciphers/aes");
+var import_utils2 = require("@noble/hashes/utils.js");
+var import_secp256k1 = require("@noble/curves/secp256k1.js");
+var import_aes = require("@noble/ciphers/aes.js");
var import_base = require("@scure/base");
// utils.ts
-var import_utils = require("@noble/hashes/utils");
+var import_utils = require("@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 ? (0, import_utils2.bytesToHex)(secretKey) : secretKey;
- const key = import_secp256k1.secp256k1.getSharedSecret(privkey, "02" + pubkey);
+ const privkey = secretKey instanceof Uint8Array ? secretKey : (0, import_utils2.hexToBytes)(secretKey);
+ const key = import_secp256k1.secp256k1.getSharedSecret(privkey, (0, import_utils2.hexToBytes)("02" + pubkey));
const normalizedKey = getNormalizedX(key);
let iv = Uint8Array.from((0, import_utils2.randomBytes)(16));
let plaintext = utf8Encoder.encode(text);
let ciphertext = (0, import_aes.cbc)(normalizedKey, iv).encrypt(plaintext);
@@ -46,11 +46,11 @@
let ivb64 = import_base.base64.encode(new Uint8Array(iv.buffer));
return `${ctb64}?iv=${ivb64}`;
}
function decrypt(secretKey, pubkey, data) {
- const privkey = secretKey instanceof Uint8Array ? (0, import_utils2.bytesToHex)(secretKey) : secretKey;
+ const privkey = secretKey instanceof Uint8Array ? secretKey : (0, import_utils2.hexToBytes)(secretKey);
let [ctb64, ivb64] = data.split("?iv=");
- let key = import_secp256k1.secp256k1.getSharedSecret(privkey, "02" + pubkey);
+ let key = import_secp256k1.secp256k1.getSharedSecret(privkey, (0, import_utils2.hexToBytes)("02" + pubkey));
let normalizedKey = getNormalizedX(key);
let iv = import_base.base64.decode(ivb64);
let ciphertext = import_base.base64.decode(ctb64);
let plaintext = (0, import_aes.cbc)(normalizedKey, iv).decrypt(ciphertext);