SHA256
39 lines
2.4 KiB
JavaScript
39 lines
2.4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { verifyAns104DataItem } from '../js/services/ans104-data-item.js';
|
|
import { arweaveBlockUrl, readSignedBlock } from '../js/services/blockchain-proof-service.js';
|
|
import { bytesToBase64Url } from '../js/services/crypto-utils.js';
|
|
import { webcrypto } from 'node:crypto';
|
|
|
|
if (!globalThis.crypto?.subtle) globalThis.crypto = webcrypto;
|
|
// Изолированный эталон ANS104 (файл взят из текущего тестового блока проекта).
|
|
const signedPath = new URL('./fixtures/block11.ans104', import.meta.url);
|
|
const expectedHash = '46db963eab3e9a9aeddba0b67bb2c44daf3be3472706c25a8035ad4ae914e99f';
|
|
const expectedId = 'T0dcFEjRymardqVuTEU5HdkPjkKm8ALU7nyf6LOzCXE';
|
|
test('подпись Ed25519 полного DataItem и SHA-256 полезного блока', async () => {
|
|
const bytes = new Uint8Array(await readFile(signedPath));
|
|
const proof = await verifyAns104DataItem(bytes);
|
|
assert.equal(bytes.length, 412);
|
|
assert.equal(proof.data.length, 274);
|
|
assert.equal(proof.signatureValid, true);
|
|
assert.equal(bytesToBase64Url(proof.dataItemIdBytes), expectedId);
|
|
assert.equal(Buffer.from(proof.frameHashBytes).toString('hex'), expectedHash);
|
|
});
|
|
test('подмена payload не проходит проверку подписи и SHA-256', async () => {
|
|
const bytes = new Uint8Array(await readFile(signedPath));
|
|
bytes[bytes.length - 1] ^= 0x01;
|
|
const proof = await verifyAns104DataItem(bytes);
|
|
assert.equal(proof.signatureValid, false);
|
|
assert.notEqual(Buffer.from(proof.frameHashBytes).toString('hex'), expectedHash);
|
|
});
|
|
test('по API нельзя скачать DataItem с подменённым хешем или ID', async () => {
|
|
const bytes = new Uint8Array(await readFile(signedPath));
|
|
const authService = { ws: { request: async () => ({ status: 200, payload: {
|
|
blockHash: expectedHash, blockBytesB64: Buffer.from(bytes).toString('base64'),
|
|
} }) } };
|
|
assert.equal((await readSignedBlock({authService, blockchainName:'wwwww@shine-001',blockNumber:11,blockHash:expectedHash,dataItemId:expectedId})).bytes.length, 412);
|
|
await assert.rejects(readSignedBlock({authService, blockchainName:'wwwww@shine-001',blockNumber:11,blockHash:expectedHash,dataItemId:'A'.repeat(43)}), /Arweave/);
|
|
assert.equal(arweaveBlockUrl('../evil'), '');
|
|
});
|