diff --git a/nip04.test.ts b/nip04.test.ts index 67cada3..bc94746 100644 --- a/nip04.test.ts +++ b/nip04.test.ts @@ -1,18 +1,9 @@ import { test, expect } from 'bun:test' -import crypto from 'node:crypto' import { encrypt, decrypt } from './nip04.ts' import { getPublicKey, generateSecretKey } from './pure.ts' import { bytesToHex, hexToBytes } from '@noble/hashes/utils' -try { - // @ts-ignore - // eslint-disable-next-line no-undef - globalThis.crypto = crypto -} catch (err) { - /***/ -} - test('encrypt and decrypt message', async () => { let sk1 = generateSecretKey() let sk2 = generateSecretKey() diff --git a/nip04.ts b/nip04.ts index bbb9447..230856d 100644 --- a/nip04.ts +++ b/nip04.ts @@ -1,15 +1,10 @@ import { bytesToHex, randomBytes } from '@noble/hashes/utils' import { secp256k1 } from '@noble/curves/secp256k1' +import { cbc } from '@noble/ciphers/aes' import { base64 } from '@scure/base' import { utf8Decoder, utf8Encoder } from './utils.ts' -// @ts-ignore -if (typeof crypto !== 'undefined' && !crypto.subtle && crypto.webcrypto) { - // @ts-ignore - crypto.subtle = crypto.webcrypto.subtle -} - export async function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): Promise { const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey const key = secp256k1.getSharedSecret(privkey, '02' + pubkey) @@ -17,8 +12,9 @@ export async function encrypt(secretKey: string | Uint8Array, pubkey: string, te let iv = Uint8Array.from(randomBytes(16)) let plaintext = utf8Encoder.encode(text) - let cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['encrypt']) - let ciphertext = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, cryptoKey, plaintext) + + let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext) + let ctb64 = base64.encode(new Uint8Array(ciphertext)) let ivb64 = base64.encode(new Uint8Array(iv.buffer)) @@ -31,14 +27,12 @@ export async function decrypt(secretKey: string | Uint8Array, pubkey: string, da let key = secp256k1.getSharedSecret(privkey, '02' + pubkey) let normalizedKey = getNormalizedX(key) - let cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['decrypt']) - let ciphertext = base64.decode(ctb64) let iv = base64.decode(ivb64) + let ciphertext = base64.decode(ctb64) - let plaintext = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, cryptoKey, ciphertext) + let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext) - let text = utf8Decoder.decode(plaintext) - return text + return utf8Decoder.decode(plaintext) } function getNormalizedX(key: Uint8Array): Uint8Array { diff --git a/package.json b/package.json index 8292ccb..39e3100 100644 --- a/package.json +++ b/package.json @@ -208,7 +208,7 @@ }, "license": "Unlicense", "dependencies": { - "@noble/ciphers": "0.2.0", + "@noble/ciphers": "^0.5.1", "@noble/curves": "1.2.0", "@noble/hashes": "1.3.1", "@scure/base": "1.1.1",