Files
SHiNE-server/shine-UI/tests/core-content-vectors.test.mjs
T

68 lines
4.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { createHash } from 'node:crypto';
import { verifyAns104DataItem, createAns104DataItem } from '../js/services/ans104-data-item.js';
import { SHINE_CORE_NAMESPACE, SHINE_CORE_LINE_PROFILE, SHINE_CORE_LINE_CONNECTION,
SHINE_CORE_LINE_TECH, SHINE_CORE_MAX_BLOCK_NUMBER } from '../js/services/protocol-tags.js';
const file = new URL('../../docs/Blockchain/vectors/core-content-v1.tsv', import.meta.url);
const rows = fs.readFileSync(file,'utf8').trim().split('\n').slice(1).map(r=>r.split('\t'));
const hex = b=>Buffer.from(b).toString('hex');
const unhex = s=>Buffer.from(s,'hex');
const hash = b=>createHash('sha256').update(b).digest('hex');
const special = new Map([[SHINE_CORE_LINE_PROFILE,'P'],[SHINE_CORE_LINE_CONNECTION,'C'],[SHINE_CORE_LINE_TECH,'T']]);
test('CORE v2 namespace и числовые линии согласованы в Java, UI, Viewer, Python', () => {
const server=fs.readFileSync(new URL('../../SHiNE-server/shine-server-blockchain/src/main/java/blockchain/ShineProtocolTags.java',import.meta.url),'utf8');
const viewer=fs.readFileSync(new URL('../static-sites/arweave-viewer/index.html',import.meta.url),'utf8');
const publisher=fs.readFileSync(new URL('../../tools/test-publisher/protocol_tags.py',import.meta.url),'utf8');
assert.equal(SHINE_CORE_NAMESPACE,'test5593');
assert.match(server,/CORE_NAMESPACE\s*=\s*"test5593"/);
assert.match(viewer,/SHINE_CORE_NAMESPACE\s*=\s*'test5593'/);
assert.match(publisher,/CORE_NAMESPACE\s*=.*SHINE_CORE_NAMESPACE/);
assert.equal(SHINE_CORE_MAX_BLOCK_NUMBER,2147482623);
assert.deepEqual([...special.keys()],[2147483645,2147483646,2147483647]);
});
// Независимые Python fixtures, проверенные также Java. Воссоздание байтов CORE/CONTENT
// через Buffer не переключает runtime браузерного клиента (это часть C).
test('CORE/CONTENT fixtures: BigEndian, hash, ANS-104 Ed25519 и подписи браузерной библиотеки', async () => {
const pkcs8=Buffer.concat([unhex('302e020100300506032b657004220420'),Buffer.from(Array.from({length:32},(_,i)=>i))]);
const key=await crypto.subtle.importKey('pkcs8',pkcs8,{name:'Ed25519'},false,['sign']);
const history=[];
for(const [name,ownerHex,coreHex,coreHash,contentHex,contentHash,coreItemHex,coreId,contentItemHex,contentId] of rows) {
const core=unhex(coreHex), content=unhex(contentHex), owner=unhex(ownerHex);
assert.ok([109,149].includes(core.length),name);
assert.equal(core[0],1); assert.equal(content[0],1);
assert.equal(core.readInt32BE(33),history.length);
assert.equal(hash(core),coreHash); assert.equal(hash(content),contentHash);
assert.equal(hex(core.subarray(45,77)),contentHash); assert.equal(hex(core.subarray(77,109)),contentId);
assert.equal(hex(core.subarray(1,33)),history.at(-1)??'00'.repeat(32));
const newCore=Buffer.alloc(core.length);
newCore[0]=1;
unhex(history.at(-1)??'00'.repeat(32)).copy(newCore,1);
newCore.writeInt32BE(history.length,33);
newCore.writeBigInt64BE(core.readBigInt64BE(37),37);
unhex(contentHash).copy(newCore,45); unhex(contentId).copy(newCore,77);
const tags=[{name:'App',value:SHINE_CORE_NAMESPACE},{name:'n',value:String(history.length)}];
if(core.length===149) {
const line=core.readInt32BE(109), prev=core.readInt32BE(113);
newCore.writeInt32BE(line,109); newCore.writeInt32BE(prev,113); unhex(history[prev]).copy(newCore,117);
tags.push({name:'l',value:special.get(line)??String(line)});
}
const newContent=Buffer.alloc(content.length); newContent[0]=1;
for(const offset of [1,3,5]) newContent.writeUInt16BE(content.readUInt16BE(offset),offset);
content.subarray(7).copy(newContent,7);
assert.equal(hex(newCore),coreHex); assert.equal(hex(newContent),contentHex);
for(const [data,expectedItem,id,expectedTags] of [[newCore,coreItemHex,coreId,tags],[newContent,contentItemHex,contentId,[]]]) {
const verified=await verifyAns104DataItem(unhex(expectedItem));
assert.ok(verified.signatureValid,name); assert.equal(hex(verified.owner32),ownerHex);
assert.equal(hex(verified.dataItemIdBytes),id); assert.equal(hex(verified.data),hex(data));
const produced=await createAns104DataItem({owner32:owner,privateKey:key,data,tags:expectedTags});
assert.equal(hex(produced),expectedItem,name);
}
history.push(coreHash);
}
});