import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import { resolve, dirname } from 'node:path'; import { encodeNamespacePda, decodeNamespacePda, normalizeFullLogin, userPdaSeed32, namespacePdaSeed32, buildFreeCertificateInnerMessage } from '../js/services/shine-namespace-service.js'; import { serializeCreateUserPdaArgs } from '../js/services/shine-user-pda-service.js'; const dirnameHere = dirname(fileURLToPath(import.meta.url)); const props = Object.fromEntries(fs.readFileSync(resolve(dirnameHere, '../../shine-solana/shine/tests/fixtures/registration-v2.properties'), 'utf8') .split(/\r?\n/).filter(x => x.includes('=')).map(line => [line.slice(0,line.indexOf('=')), line.slice(line.indexOf('=')+1)])); const hex = x => Buffer.from(x).toString('hex'); const bh = x => Uint8Array.from(Buffer.from(x, 'hex')); test('строгая нормализация полных логинов', () => { assert.equal(normalizeFullLogin('alice@shine'), 'alice@shine'); assert.equal(normalizeFullLogin('ALICE@shine'), 'ALICE@shine'); assert.equal(normalizeFullLogin('AlIcE@shine'), 'AlIcE@shine'); for (const bad of ['alice','alice@Shine',' alice@shine','alice@@shine','alice.s@shine','a'.repeat(30)+'@a']) assert.throws(() => normalizeFullLogin(bad)); assert.notEqual(normalizeFullLogin('a_b@shine'), normalizeFullLogin('ab@shine')); }); test('namespace v1 бинарно соответствует Rust/Java вектору', () => { const raw = bh(props.namespace_free_bytes_hex); const ns = decodeNamespacePda(raw); assert.equal(ns.namespace, 'free'); assert.equal(ns.type, 2); assert.equal(ns.serverLogin, 'registrar@shine'); assert.deepEqual(encodeNamespacePda(ns), raw); assert.throws(() => decodeNamespacePda(new Uint8Array([...raw, 0]))); assert.throws(() => decodeNamespacePda(raw.slice(0,-1))); }); test('SHA256 PDA seeds stable and domain separated', async () => { assert.equal(hex(await userPdaSeed32('bob@free')), props.user_bob_free_seed); assert.equal(hex(await namespacePdaSeed32('free')), props.namespace_free_seed); assert.notEqual(hex(await userPdaSeed32('bob@free')), hex(await namespacePdaSeed32('free'))); assert.equal(hex(await userPdaSeed32('BoB@free')), hex(await userPdaSeed32('bob@free'))); assert.equal(hex(await userPdaSeed32('BOB@free')), hex(await userPdaSeed32('bob@free'))); }); test('inner message is byte-for-byte fixture', () => { const b = buildFreeCertificateInnerMessage('bob@free', bh('03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8'), bh('29acbae141bccaf0b22e1a94d34d0bc7361e526d0bfe12c89794bc9322966dd7'), bh('2543b92ff1095511476adc8369db6ddc933665a11978dda1404ee1066ca9559d')); assert.equal(hex(b), props.inner_message_hex); const mixed = buildFreeCertificateInnerMessage('BoB@free', bh('03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8'), bh('29acbae141bccaf0b22e1a94d34d0bc7361e526d0bfe12c89794bc9322966dd7'), bh('2543b92ff1095511476adc8369db6ddc933665a11978dda1404ee1066ca9559d')); assert.notEqual(hex(mixed), hex(b)); // Подпись сертификата включает оригинальный регистр. }); test('create instruction suffix encodes registrar signature, never promo', () => { const common = {login:'bob@free', rootKey32: new Uint8Array(32), createdAtMs:1n, clientKey32:new Uint8Array(32), blockchainKey32:new Uint8Array(32), recordSignature64:new Uint8Array(64), accessServers:[], serverAddresses:[]}; const paid = serializeCreateUserPdaArgs(common); const free = serializeCreateUserPdaArgs({...common, registrarSignature64: new Uint8Array(64).fill(7)}); assert.equal(paid[paid.length-1], 0); assert.equal(free[paid.length-1], 1); assert.deepEqual(free.slice(paid.length), new Uint8Array(64).fill(7)); assert.equal(free[0],3); });