Files
SHiNE-server/shine-solana/shine/scripts/registration-v2/decode-namespace.mjs
T

40 lines
2.3 KiB
JavaScript

#!/usr/bin/env node
import { readFileSync } from 'node:fs';
// Автономный декодер namespace 1.0/1.1 (без RPC и внешних пакетов).
export function decodeNamespace(data) {
const b=Buffer.from(data);
if(b.length<13) throw Error('namespace: payload слишком короткий');
if(b[0]!==1 || ![0,1].includes(b[1])) throw Error('namespace: неизвестная версия');
const minor=b[1];
const type=b[2],minLocalLen=b[3],len=b[4];
if(type!==1 && type!==2)throw Error('namespace: неверный тип');
if(!len || !minLocalLen || len+1+minLocalLen>30) throw Error('namespace: некорректная длина');
let p=5;
if(p+len+8>b.length)throw Error('namespace: обрезанные поля');
const name=b.subarray(p,p+len).toString('utf8');p+=len;
if(!/^[a-z0-9_]+$/.test(name) || Buffer.byteLength(name)!==len) throw Error('namespace: некорректное название');
const fee=b.readBigUInt64LE(p).toString();p+=8;
const currencyCode=minor===1?b[p++]:1;
if(currencyCode!==1) throw Error('namespace: неподдерживаемый код валюты (пока только 1=SOL)');
let registrar=null,serverLogin=null;
if(type===2){
if(p+33>b.length)throw Error('namespace: отсутствуют ключ и логин сервера');
registrar=b.subarray(p,p+32).toString('hex');p+=32;
const serverLen=b[p++];
if(p+serverLen>b.length)throw Error('namespace: обрезан логин сервера');
serverLogin=b.subarray(p,p+serverLen).toString('utf8');p+=serverLen;
if(!/^[a-z0-9_]+@[a-z0-9_]+$/.test(serverLogin) || Buffer.byteLength(serverLogin)>30)throw Error('namespace: неверный логин сервера');
}
if(p!==b.length) throw Error('namespace: лишние байты запрещены');
return {major:1,minor,type,minLocalLen,name,registrationFeeLamports:fee,currencyCode,registrarPubkeyHex:registrar,serverLogin};
}
if(process.argv[1]?.endsWith('decode-namespace.mjs')){
const data=process.argv[2];
if(!data || !/^(?:[\da-fA-F]{2})+$/.test(data)){
console.error('Использование: node decode-namespace.mjs <hex-данные-аккаунта>');process.exit(2);
}
try{console.log(JSON.stringify(decodeNamespace(Buffer.from(data,'hex')),null,2));}
catch(e){console.error(e.message);process.exit(1);}
}