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

112 lines
8.0 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', () => {
const server=fs.readFileSync(new URL('../../SHiNE-server/shine-server-blockchain/src/main/java/blockchain/ShineProtocolTags.java',import.meta.url),'utf8');
assert.equal(SHINE_CORE_NAMESPACE,'test5595');
assert.match(server,/CORE_NAMESPACE\s*=\s*"test5595"/);
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);
}
});
import { encodeCore, decodeCore, encodeContent, decodeContent, signCoreContentPair, verifyCoreContentPair,
packCoreContent, unpackCoreContent, CORE_MAX_CONTAINER_BYTES } from '../js/services/core-content.js';
test('Minimal pair codec and container: exact vectors, tampering, unsigned lengths', 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']);
for (const [name,ownerHex,coreHex,coreHash,contentHex,contentHash,coreItemHex,coreId,contentItemHex,contentId] of rows) {
const core=decodeCore(unhex(coreHex)), content=decodeContent(unhex(contentHex)), owner=unhex(ownerHex);
assert.equal(hex(encodeCore(core)),coreHex); assert.equal(hex(encodeContent(content)),contentHex);
const signed=await signCoreContentPair({owner32:owner,privateKey:key,content, ...core});
assert.equal(hex(signed.coreDataItem),coreItemHex,name); assert.equal(hex(signed.contentDataItem),contentItemHex,name);
const raw=packCoreContent(signed.coreDataItem,signed.contentDataItem);
assert.equal(Buffer.from(raw).readUInt32BE(),signed.coreDataItem.length);
const pair=unpackCoreContent(raw);
assert.equal(hex(pair.coreDataItem),coreItemHex); assert.equal(hex(pair.contentDataItem),contentItemHex);
const verified=await verifyCoreContentPair(pair.coreDataItem,pair.contentDataItem,owner);
assert.equal(hex(verified.coreHash32),coreHash); assert.equal(hex(verified.coreDataItemId32),coreId);
await assert.rejects(verifyCoreContentPair(pair.coreDataItem,pair.contentDataItem,new Uint8Array(32)));
const badCore=pair.coreDataItem.slice(); badCore[2]^=1;
await assert.rejects(verifyCoreContentPair(badCore,pair.contentDataItem,owner));
const badContent=pair.contentDataItem.slice(); badContent[2]^=1;
await assert.rejects(verifyCoreContentPair(pair.coreDataItem,badContent,owner));
const badLen=raw.slice(); new DataView(badLen.buffer).setUint32(0,0xffffffff);
assert.throws(()=>unpackCoreContent(badLen));
assert.throws(()=>unpackCoreContent(raw.slice(0,4+pair.coreDataItem.length)));
}
assert.throws(()=>unpackCoreContent(new Uint8Array(CORE_MAX_CONTAINER_BYTES+1)));
});
import { inspectBlockchainDataItem } from '../js/services/blockchain-entry-inspector.js';
test('HEADER v2 signed CORE/CONTENT and inspector accept arbitrary UTF-8 license', 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 owner=unhex(rows[0][1]);
const login=Buffer.from('Alice@shine');
for (const license of ['', 'CC-BY-NC-4.0', 'Лицензия 🌞', '\ufeffx', 'x'.repeat(255)]) {
const bytes=Buffer.from(license);
const body=Buffer.concat([Buffer.from('SHiNE'),Buffer.of(login.length),login,Buffer.of(bytes.length),bytes,owner]);
const content={type:0,subType:0,version:2,body};
const signed=await signCoreContentPair({owner32:owner,privateKey:key,content,prevCoreHash32:new Uint8Array(32),blockNumber:0,timestamp:1700001000n,line:null});
const raw=packCoreContent(signed.coreDataItem,signed.contentDataItem);
const result=await inspectBlockchainDataItem(Buffer.from(raw).toString('base64'));
assert.equal(result.frame.body.license,license);
assert.equal(result.frame.version,2);
await assert.rejects(signCoreContentPair({owner32:owner,privateKey:key,content:{...content,version:4},prevCoreHash32:new Uint8Array(32),blockNumber:0,timestamp:1700001000n,line:null}));
}
});