Files
SHiNE-server/shine-UI/tests/blockchain-proof.test.mjs
T

47 lines
3.0 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 проверяется полная пара и отклоняется неверный номер, хеш или ID', async () => {
const { packCoreContent } = await import('../js/services/core-content.js');
const row = (await readFile(new URL('../../docs/Blockchain/vectors/core-content-v1.tsv', import.meta.url), 'utf8')).trim().split('\n')[1].split('\t');
const bytes = packCoreContent(Buffer.from(row[6], 'hex'), Buffer.from(row[8], 'hex'));
const id = Buffer.from(row[7], 'hex').toString('base64url');
const args = { blockchainName: 'wwwww@shine-001', blockNumber: 0, blockHash: row[3], dataItemId: id };
const authService = { ws: { request: async () => ({ status: 200, payload: {
blockHash: row[3], blockBytesB64: Buffer.from(bytes).toString('base64'),
} }) } };
assert.deepEqual((await readSignedBlock({authService, ...args})).bytes, bytes);
await assert.rejects(readSignedBlock({authService, ...args, dataItemId:'A'.repeat(43)}), /Arweave/);
await assert.rejects(readSignedBlock({authService, ...args, blockNumber:1}), /номер/);
await assert.rejects(readSignedBlock({authService, ...args, blockHash:'a'.repeat(64)}), /хеш/);
bytes[bytes.length - 1] ^= 1;
await assert.rejects(readSignedBlock({authService, ...args}));
assert.equal(arweaveBlockUrl('../evil'), '');
});