import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import vm from 'node:vm'; import { webcrypto } from 'node:crypto'; const html=fs.readFileSync(new URL('../static-sites/arweave-viewer/index.html',import.meta.url),'utf8'); const fixtures=fs.readFileSync(new URL('../../docs/Blockchain/vectors/core-content-v1.tsv',import.meta.url),'utf8').trim().split('\n').slice(1).map(l=>Object.fromEntries(l.split('\t').map((v,i)=>[['name','owner','core','coreHash','content','contentHash','coreItem','coreItemId','contentItem','contentItemId'][i],v]))); const source=html.slice(html.indexOf(' const CORE_MAX_NUMBER'),html.indexOf(' const CHANNEL_TYPE_PERSONAL')); const funcs=html.slice(html.indexOf(' async function digestSha256'),html.indexOf(' function refKey(')); const b64=html.slice(html.indexOf(' function base64Url('),html.indexOf(' async function arweaveOwnerAddressFromBlockchainKey')); const loader=html.slice(html.indexOf(' async function loadArweavePairs('),html.indexOf(' async function checkSolanaEndpoint(')); const checks=html.slice(html.indexOf(' function v3ZeroHash('),html.indexOf(' function v3MetricText(')); const ctx=vm.createContext({crypto:webcrypto,Uint8Array,DataView,TextDecoder,TextEncoder,btoa, MAGIC:'SHiNE',bytesToHex:b=>Buffer.from(b).toString('hex'),baseUrl:s=>s,fetch:()=>{throw Error('unexpected fetch')}}); vm.runInContext(source+funcs+b64+loader+checks+';globalThis.viewer={parseCorePayload,parseBlockchainBlock,checkIndexedCoreTags,loadArweavePairs,v3Verification}',ctx); const v=ctx.viewer,bytes=s=>Uint8Array.from(Buffer.from(s,'hex')); const itemid=x=>Buffer.from(x,'hex').toString('base64url'); const find=n=>fixtures.find(f=>f.name===n); const parse=f=>v.parseBlockchainBlock(bytes(f.core),bytes(f.content)); test('viewer: parses all CORE/CONTENT vectors and computes exact hashes',async()=>{ for(const f of fixtures){const b=await parse(f);assert.equal(b.hashHex,f.coreHash);assert.equal(b.contentDataItemId,itemid(f.contentItemId));assert.equal(b.contentHashChecked,true);} assert.equal((await parse(find('header'))).body.kind,'header');assert.equal((await parse(find('post'))).body.message,'Hello'); }); test('viewer: rejects tampered CONTENT, invalid CORE and wrong indexed tags',async()=>{ const f=find('post');const bad=bytes(f.content);bad[bad.length-1]^=1; await assert.rejects(v.parseBlockchainBlock(bytes(f.core),bad),/CONTENT hash/); assert.throws(()=>v.parseCorePayload(bytes(f.core).slice(0,108)),/Invalid CORE/); assert.throws(()=>v.checkIndexedCoreTags({tags:[{name:'App',value:'test5592'}]},v.parseCorePayload(bytes(f.core))),/tags mismatch/); }); test('viewer: requests CORE then CONTENT by ID and never claims signature verification',async()=>{ const f=find('post'),id=itemid(f.coreItemId),cid=itemid(f.contentItemId),calls=[]; ctx.fetch=async url=>{calls.push(url);const raw=url.endsWith(cid)?f.content:f.core;return {ok:true,arrayBuffer:async()=>bytes(raw).buffer};}; const r=await v.loadArweavePairs('https://mock.invalid',[{id,owner:{address:'indexed'},tags:[{name:'App',value:'test5595'},{name:'n',value:'2'},{name:'l',value:'1'}]}]); assert.deepEqual(calls,[`https://mock.invalid/${id}`,`https://mock.invalid/${cid}`]);assert.equal(r.blocks[0].hashHex,f.coreHash); const checks=v.v3Verification(r.blocks);assert.equal(checks.signature.ok,0);assert.equal(checks.signature.na,1);assert.equal(checks.global.na,1); ctx.fetch=async url=>url.endsWith(cid)?{ok:false,status:404}:{ok:true,arrayBuffer:async()=>bytes(f.core).buffer}; await assert.rejects(v.loadArweavePairs('https://mock.invalid',[{id,tags:[{name:'App',value:'test5595'},{name:'n',value:'2'},{name:'l',value:'1'}]}]),/CONTENT.*404/); });