Унифицировать HEADER в формате 1.0

This commit is contained in:
AidarKC
2026-12-02 17:57:28 +03:00
parent 58e4bac3c8
commit c61cbf239c
24 changed files with 230 additions and 394 deletions
@@ -124,33 +124,20 @@ function parseTechBody(subType, body, frame) {
const magic = reader.text(5, 'HEADER magic');
if (magic !== 'SHiNE') throw new Error('Неправильная сигнатура HEADER');
const login = reader.textU8('HEADER login');
let license = null;
let accountType = null;
const accountType = reader.u8('HEADER account type');
if (accountType !== 0 && accountType !== 1) throw new Error('Invalid HEADER account type');
let previousAccounts = [];
if (frame.version === 3) {
accountType = reader.u8('HEADER account type');
if (accountType !== 0 && accountType !== 1) throw new Error('Invalid HEADER account type');
if (accountType === 1) {
const csv = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(reader.raw(reader.u16('HEADER previous accounts length'), 'HEADER previous accounts'));
previousAccounts = csv ? csv.split(',') : [];
const seen = new Set([login.toLowerCase()]);
if (previousAccounts.length > 10) throw new Error('Too many previous accounts');
for (const previous of previousAccounts) {
if (!(/^[A-Za-z0-9_]{1,255}$/.test(previous) || (previous.length <= 30 && /^[A-Za-z0-9_]+@[a-z0-9_]+$/.test(previous))) || seen.has(previous.toLowerCase())) throw new Error('Invalid previous account');
seen.add(previous.toLowerCase());
}
}
license = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(reader.raw(reader.u8('HEADER license length'), 'HEADER license'));
} else if (frame.version === 2) {
const field = reader.raw(reader.remaining() - 32, 'HEADER license');
const marker = 'license:CC0-1.0';
if (field.length === marker.length && field.every((b, i) => b === marker.charCodeAt(i))) {
license = 'CC0-1.0'; // исторический HEADER v2
} else {
if (!field.length || field[0] !== field.length - 1) throw new Error('Некорректная длина лицензии HEADER');
license = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(field.slice(1));
if (accountType === 1) {
const csv = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(reader.raw(reader.u16('HEADER previous accounts length'), 'HEADER previous accounts'));
previousAccounts = csv ? csv.split(',') : [];
const seen = new Set([login.toLowerCase()]);
if (previousAccounts.length > 10) throw new Error('Too many previous accounts');
for (const previous of previousAccounts) {
if (!(/^[A-Za-z0-9_]{1,255}$/.test(previous) || (previous.length <= 30 && /^[A-Za-z0-9_]+@[a-z0-9_]+$/.test(previous))) || seen.has(previous.toLowerCase())) throw new Error('Invalid previous account');
seen.add(previous.toLowerCase());
}
}
const license = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(reader.raw(reader.u8('HEADER license length'), 'HEADER license'));
const initialBlockchainKey = bytesToBase64Url(reader.raw(32, 'initialBlockchainKey32'));
if (reader.remaining() !== 0) throw new Error('Неожиданные данные после HEADER');
return { kind: 'header', magic, login, license, accountType, previousAccounts, initialBlockchainKey };
@@ -260,8 +247,7 @@ function parseStatusBody(subType, body) {
}
function parseBody(type, subType, version, body, frame) {
// Only TECH HEADER subtype 0 has a second version; all other records stay v1.
if (version !== 1 && !(type === 0 && subType === 0 && (version === 2 || version === 3))) {
if (version !== 1) {
return { kind: 'unsupported_version', rawBodyLength: body.length };
}
if (type === 0) return parseTechBody(subType, body, frame);