Новый формат блоков CORE/CONTENT test5595

This commit is contained in:
AidarKC
2026-11-27 10:04:22 +03:00
parent d3a84dd451
commit f9e7c4ef94
180 changed files with 15365 additions and 1301 deletions
@@ -1,4 +1,5 @@
import { verifyAns104DataItem } from './ans104-data-item.js';
import { parseAns104DataItem } from './ans104-data-item.js';
import { unpackCoreContent, verifyCoreContentPair, CORE_MAX_CONTAINER_BYTES } from './core-content.js';
import { base64ToBytes, bytesToBase64Url } from './crypto-utils.js';
import { readShineUserPda } from './shine-user-pda-service.js';
@@ -17,7 +18,7 @@ export function arweaveBlockUrl(id) {
}
export function signedDataItemFilename(chain, number) {
const safe = String(chain || 'block').replace(/[^a-z0-9@._-]/giu, '_').slice(0, 100);
return `SHiNE-${safe}-${Number(number)}.ans104`;
return `SHiNE-${safe}-${Number(number)}.shine-pair`;
}
/** Получаем полный оригинал по существующему публичному API (не «от Arweave»). */
@@ -29,15 +30,18 @@ export async function readSignedBlock({ authService, blockchainName, blockNumber
throw new Error('Сервер вернул другой хеш блока');
}
const rawText = String(body?.blockBytesB64 || '');
if (rawText.length > Math.ceil(MAX_SIGNED_BYTES * 4 / 3) + 8) throw new Error('Размер подписанного блока превышает лимит');
if (rawText.length > Math.ceil(CORE_MAX_CONTAINER_BYTES * 4 / 3) + 8) throw new Error('Размер подписанного блока превышает лимит');
const bytes = base64ToBytes(rawText);
if (!bytes.length || bytes.length > MAX_SIGNED_BYTES) throw new Error('Некорректный размер блока');
const proof = await verifyAns104DataItem(bytes);
if (!proof.signatureValid) throw new Error('Недействительная подпись ANS-104');
if (hex(proof.frameHashBytes) !== String(blockHash).toLowerCase()) throw new Error('Хеш содержимого блока не совпал');
const actualId = bytesToBase64Url(proof.dataItemIdBytes);
if (!bytes.length || bytes.length > CORE_MAX_CONTAINER_BYTES) throw new Error('Некорректный размер блока');
const { coreDataItem, contentDataItem } = unpackCoreContent(bytes);
const item = parseAns104DataItem(coreDataItem);
const proof = await verifyCoreContentPair(coreDataItem, contentDataItem, item.owner32);
if (proof.core.blockNumber !== Number(blockNumber)) throw new Error('Сервер вернул другой номер блока');
if (hex(proof.coreHash32) !== String(blockHash).toLowerCase()) throw new Error('Хеш CORE не совпал');
const actualId = bytesToBase64Url(proof.coreDataItemId32);
if (dataItemId && actualId !== dataItemId) throw new Error('Идентификатор Arweave отличается от ожидаемого');
return { bytes, ...proof, actualId };
return { bytes, ...proof, owner32: item.owner32, data: item.data, actualId,
contentId: bytesToBase64Url(proof.core.contentDataItemId32), contentData: parseAns104DataItem(contentDataItem).data };
}
/** Вспомогательная независимая сверка: шлюз возвращает payload, а не ANS-104 оболочку. */
@@ -77,3 +81,13 @@ export async function checkSolanaAuthor({ authorLogin, blockchainName, owner32,
return { status: 'unavailable', reason: String(error?.message || error) };
}
}
/** Both payloads must be available and match the original signed pair. */
export async function checkArweavePair(proof) {
const [core, content] = await Promise.all([
checkArweavePayload(proof.actualId, proof.data), checkArweavePayload(proof.contentId, proof.contentData),
]);
const status = [core.status, content.status].includes('invalid') ? 'invalid' :
[core.status, content.status].every(s => s === 'valid') ? 'valid' : 'unavailable';
return { status, reason: [core.reason && `CORE: ${core.reason}`, content.reason && `CONTENT: ${content.reason}`].filter(Boolean).join('; ') };
}