64 lines
2.9 KiB
JavaScript
64 lines
2.9 KiB
JavaScript
// Generated, do not edit. sha256:6be31fc22a74a523d2b0d59999ce972ba090fd8c02f866e6f13929e3137c6c1b
|
|||
|
|
import { describe, it } from 'node:test';
|
||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { readFileSync } from 'node:fs';
|
||
|
|
import { createHash } from 'node:crypto';
|
||
|
|
import { schnorr } from '@noble/curves/secp256k1.js';
|
||
|
|
import { conversationKey, messageKeys, paddedLength, encrypt, decrypt } from '../../client/nostr/nip44.js';
|
||
|
|
|
||
|
|
const V = JSON.parse(readFileSync(new URL('../fixtures/nip44.vectors.json', import.meta.url), 'utf8')).v2;
|
||
|
|
const hex = (b) => Buffer.from(b).toString('hex');
|
||
|
|
const sha = (s) => createHash('sha256').update(s, 'utf8').digest('hex');
|
||
|
|
const pubOf = (secHex) => hex(schnorr.getPublicKey(Buffer.from(secHex, 'hex')));
|
||
|
|
|
||
|
|
describe('NIP-44: de geldige vectoren', () => {
|
||
|
|
it('leidt elke conversatiesleutel af zoals de spec', () => {
|
||
|
|
for (const v of V.valid.get_conversation_key) assert.equal(hex(conversationKey(v.sec1, v.pub2)), v.conversation_key, v.note ?? v.sec1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('leidt de drie sleutels per bericht af zoals de spec', () => {
|
||
|
|
const { conversation_key: ck, keys } = V.valid.get_message_keys;
|
||
|
|
for (const k of keys) {
|
||
|
|
const m = messageKeys(ck, k.nonce);
|
||
|
|
assert.deepEqual([hex(m.chachaKey), hex(m.chachaNonce), hex(m.hmacKey)], [k.chacha_key, k.chacha_nonce, k.hmac_key]);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('vult elke lengte op zoals de spec', () => {
|
||
|
|
for (const [len, padded] of V.valid.calc_padded_len) assert.equal(paddedLength(len), padded, String(len));
|
||
|
|
});
|
||
|
|
|
||
|
|
it('versleutelt en ontsleutelt elk voorbeeld byte voor byte gelijk', () => {
|
||
|
|
for (const v of V.valid.encrypt_decrypt) {
|
||
|
|
const ck = conversationKey(v.sec1, pubOf(v.sec2));
|
||
|
|
assert.equal(hex(ck), v.conversation_key);
|
||
|
|
assert.equal(encrypt(v.plaintext, ck, v.nonce), v.payload);
|
||
|
|
assert.equal(decrypt(v.payload, ck), v.plaintext);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('kan ook de lange berichten, tot 65535 bytes', () => {
|
||
|
|
for (const v of V.valid.encrypt_decrypt_long_msg) {
|
||
|
|
const tekst = v.pattern.repeat(v.repeat);
|
||
|
|
assert.equal(sha(tekst), v.plaintext_sha256);
|
||
|
|
const payload = encrypt(tekst, v.conversation_key, v.nonce);
|
||
|
|
assert.equal(sha(payload), v.payload_sha256);
|
||
|
|
assert.equal(decrypt(payload, v.conversation_key), tekst);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('NIP-44: de ongeldige vectoren', () => {
|
||
|
|
it('weigert een bericht van een ongeldige lengte', () => {
|
||
|
|
const ck = V.valid.encrypt_decrypt[0].conversation_key;
|
||
|
|
for (const len of V.invalid.encrypt_msg_lengths) assert.throws(() => encrypt('a'.repeat(len), ck), Error, String(len));
|
||
|
|
});
|
||
|
|
|
||
|
|
it('weigert een ongeldige sleutel', () => {
|
||
|
|
for (const v of V.invalid.get_conversation_key) assert.throws(() => conversationKey(v.sec1, v.pub2), Error, v.note);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('weigert elke vervalste of verminkte payload', () => {
|
||
|
|
for (const v of V.invalid.decrypt) assert.throws(() => decrypt(v.payload, v.conversation_key), Error, v.note);
|
||
|
|
});
|
||
|
|
});
|