Унифицировать 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
+38 -69
View File
@@ -16,85 +16,54 @@ const encode = vm.runInNewContext(`function makeHeaderBodyBytes(${source}\nmakeH
Uint8Array,
});
test('client writes HEADER v2 as SHiNE → login → length + license → key', () => {
function inspectorParser() {
const stripped = inspector.replace(/^import .+;\s*$/gm, '').replace(/^export /gm, '');
return vm.runInNewContext(`${stripped}\nparseShineFrame`, {
TextDecoder, DataView, Uint8Array, BigInt,
bytesToBase64Url: bytes => Buffer.from(bytes).toString('base64url'), Buffer,
});
}
function headerFrame(body, version = 1) {
const frame = new Uint8Array(88 + body.length);
const view = new DataView(frame.buffer);
view.setUint16(0, 1, false);
view.setInt32(66, frame.length, false);
view.setInt32(70, 0, false);
view.setBigInt64(74, 0n, false);
view.setUint16(82, 0, false);
view.setUint16(84, 0, false);
view.setUint16(86, version, false);
frame.set(body, 88);
return frame;
}
test('client writes only HEADER 1.0: identity, arbitrary license, then initial key', () => {
const key = Uint8Array.from({ length: 32 }, (_, i) => i + 1);
const login = 'Alice@shine';
const license = 'Лицензия 🌞';
const body = encode({ login, initialBlockchainKey32: key, license });
const loginBytes = utf8(login);
const previous = 'Bob@free,Older';
const body = encode({ login, initialBlockchainKey32: key, accountType: 1, previousAccounts: ['Bob@free', 'Older'], license });
const expected = Uint8Array.from([
...utf8('SHiNE'), loginBytes.length, ...loginBytes,
utf8(license).length, ...utf8(license), ...key,
...utf8('SHiNE'), utf8(login).length, ...utf8(login),
1, 0, utf8(previous).length, ...utf8(previous), utf8(license).length, ...utf8(license), ...key,
]);
assert.deepEqual(Array.from(body), Array.from(expected));
assert.match(auth, /msgSubType: MSG_SUBTYPE_TECH_HEADER,\s*msgVersion: HEADER_VERSION/);
assert.match(auth, /export const HEADER_VERSION = 3/);
assert.match(auth, /export const HEADER_VERSION = 1/);
});
test('inspector decodes both v1 and v2 and supports arbitrary license and rejects malformed v2', () => {
const stripped = inspector.replace(/^import .+;\s*$/gm, '').replace(/^export /gm, '');
const inspect = vm.runInNewContext(`${stripped}\nparseShineFrame`, {
TextDecoder, DataView, Uint8Array, BigInt,
bytesToBase64Url: bytes => Buffer.from(bytes).toString('base64url'),
Buffer,
});
test('inspector accepts only HEADER 1.0 and validates its identity and license', () => {
const inspect = inspectorParser();
const key = Uint8Array.from({ length: 32 }, (_, i) => i + 1);
const login = utf8('Alice@shine');
const prefix = Uint8Array.from([...utf8('SHiNE'), login.length, ...login]);
const bodyV1 = Uint8Array.from([...prefix, ...key]);
const bodyV2 = Uint8Array.from([...prefix, ...utf8('license:CC0-1.0'), ...key]);
const frameFor = (version, body) => {
const frame = new Uint8Array(88 + body.length);
const view = new DataView(frame.buffer);
view.setUint16(0, 1, false); // frameCode
view.setInt32(66, frame.length, false);
view.setInt32(70, 0, false); // blockNumber
view.setBigInt64(74, 0n, false);
view.setUint16(82, 0, false); // type TECH
view.setUint16(84, 0, false); // subtype HEADER
view.setUint16(86, version, false);
frame.set(body, 88);
return frame;
};
const old = inspect(frameFor(1, bodyV1));
assert.equal(old.body.kind, 'header');
assert.equal(old.body.license, null);
const current = inspect(frameFor(2, bodyV2));
assert.equal(current.body.kind, 'header');
assert.equal(current.body.license, 'CC0-1.0');
assert.equal(current.body.initialBlockchainKey, Buffer.from(key).toString('base64url'));
assert.throws(() => inspect(frameFor(2, Uint8Array.from([...prefix, ...key, ...utf8('license:CC0-1.0')]))));
const invalid = bodyV2.slice();
invalid[prefix.length + 8] = 'X'.charCodeAt(0);
assert.throws(() => inspect(frameFor(2, invalid)));
for (const license of ['', 'CC-BY-NC-4.0', 'Лицензия 🌞', 'x'.repeat(255)]) {
const body = encode({ login: 'Alice@shine', initialBlockchainKey32: key, license });
assert.equal(inspect(frameFor(2, body)).body.license, license);
assert.throws(() => inspect(frameFor(2, body.slice(0, -1))));
const body = encode({ login: 'Alice@shine', initialBlockchainKey32: key, accountType: 1, previousAccounts: ['Bob@free'], license });
const parsed = inspect(headerFrame(body)).body;
assert.equal(parsed.accountType, 1);
assert.deepEqual(Array.from(parsed.previousAccounts), ['Bob@free']);
assert.equal(parsed.license, license);
assert.throws(() => inspect(headerFrame(body.slice(0, -1))));
assert.equal(inspect(headerFrame(body, 2)).body.kind, 'unsupported_version');
}
assert.throws(() => encode({ login: 'Alice@shine', initialBlockchainKey32: key, license: 'x'.repeat(256) }));
assert.throws(() => inspect(frameFor(2, Uint8Array.from([...prefix, 1, 255, ...key]))));
assert.throws(() => inspect(frameFor(2, new Uint8Array(0))));
});
test('HEADER v3: actual serializer and inspector agree on identity and reject malformed declarations', () => {
const stripped = inspector.replace(/^import .+;\s*$/gm, '').replace(/^export /gm, '');
const inspect = vm.runInNewContext(`${stripped}\nparseShineFrame`, {TextDecoder, DataView, Uint8Array, BigInt, bytesToBase64Url: b => Buffer.from(b).toString('base64url'), Buffer});
const login = 'Alice@shine', key = Uint8Array.from({length:32}, (_,i)=>i+1);
const prefix = [...utf8('SHiNE'), utf8(login).length, ...utf8(login)];
const frame = body => {const a=new Uint8Array(88+body.length), v=new DataView(a.buffer);v.setUint16(0,1);v.setInt32(66,a.length);v.setUint16(86,3);a.set(body,88);return a;};
const make = (accountType,previousAccounts=[],license='')=>encode({login,initialBlockchainKey32:key,accountType,previousAccounts,license});
assert.deepEqual([...make(0)], [...prefix,0,0,...key]);
assert.deepEqual([...make(1)], [...prefix,1,0,0,0,...key]);
for(const previousAccounts of [[],['Bob@free','Older'],Array.from({length:10},(_,i)=>'old'+i+'@free')]) {
const body=make(1,previousAccounts,'Лицензия');
const parsed=inspect(frame(body)).body;
assert.equal(parsed.accountType,1);assert.deepEqual(Array.from(parsed.previousAccounts),previousAccounts);assert.equal(parsed.license,'Лицензия');
assert.throws(()=>inspect(frame(body.slice(0,-1))));
}
assert.equal(inspect(frame(make(0))).body.accountType,0);
for(const list of [['ALICE@shine'],['Bob@free','bob@free'],['a,b'],Array.from({length:11},(_,i)=>'old'+i)]) assert.throws(()=>make(1,list));
assert.throws(()=>make(0,['Bob@free']));assert.throws(()=>make(2));
const bad=make(1);bad[prefix.length]=2;assert.throws(()=>inspect(frame(bad)));
const csv=utf8('Bob@free,bob@free');assert.throws(()=>inspect(frame(Uint8Array.from([...prefix,1,0,csv.length,...csv,0,...key]))));
assert.throws(() => encode({ login: 'Alice@shine', initialBlockchainKey32: key, accountType: 0, previousAccounts: ['Bob@free'] }));
});