SHA256
Улучшить UI регистрации и связанные сценарии
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Общий бинарный протокол namespace PDA v1; используется платным create и upgrade.
|
||||
// Namespace PDA v1.0 (legacy) / v1.1 (валюта) для прямой регистрации и upgrade.
|
||||
import { sha256Bytes } from './crypto-utils.js';
|
||||
import { loadSolanaWeb3 } from '../vendor/solana-web3-loader.js';
|
||||
import { SHINE_USERS_PROGRAM_ID } from '../solana-programs.js';
|
||||
@@ -35,7 +35,7 @@ export function buildFreeCertificateInnerMessage(fullLogin, root32, blockchain32
|
||||
}
|
||||
export function decodeNamespacePda(data) {
|
||||
const bytes = new Uint8Array(data);
|
||||
if (bytes.length < 13 || bytes[0] !== 1 || bytes[1] !== 0 || (bytes[2] !== 1 && bytes[2] !== 2)) throw new Error('Неподдерживаемый namespace PDA');
|
||||
if (bytes.length < 13 || bytes[0] !== 1 || ![0, 1].includes(bytes[1]) || (bytes[2] !== 1 && bytes[2] !== 2)) throw new Error('Неподдерживаемый namespace PDA');
|
||||
const type = bytes[2], minLocalpartLength = bytes[3], len = bytes[4];
|
||||
if (len === 0 || 5 + len + 8 > bytes.length) throw new Error('Обрезанный namespace PDA');
|
||||
const namespace = dec.decode(bytes.slice(5, 5 + len));
|
||||
@@ -43,6 +43,8 @@ export function decodeNamespacePda(data) {
|
||||
if (minLocalpartLength < 1 || minLocalpartLength + len + 1 > 30) throw new Error('Недопустимая минимальная длина');
|
||||
const fee = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getBigUint64(5 + len, true);
|
||||
let offset = 5 + len + 8, registrarPublicKey32 = null, serverLogin = null;
|
||||
const currencyCode = bytes[1] === 1 ? bytes[offset++] : 1;
|
||||
if (currencyCode !== 1) throw new Error('Валюта namespace пока не поддерживается (разрешена только SOL)');
|
||||
if (type === 2) {
|
||||
if (bytes.length < offset + 33) throw new Error('В namespace типа 2 отсутствует ключ регистратора');
|
||||
registrarPublicKey32 = bytes.slice(offset, offset + 32); offset += 32;
|
||||
@@ -52,21 +54,23 @@ export function decodeNamespacePda(data) {
|
||||
offset += serverLen;
|
||||
}
|
||||
if (bytes.length !== offset) throw new Error('Лишние данные namespace PDA');
|
||||
return { namespace, type, minLocalpartLength, registrationFeeLamports: fee, registrarPublicKey32, serverLogin };
|
||||
return { namespace, type, minLocalpartLength, registrationFeeLamports: fee, currencyCode, registrarPublicKey32, serverLogin };
|
||||
}
|
||||
export function encodeNamespacePda({ namespace, type, minLocalpartLength, registrationFeeLamports, registrarPublicKey32, serverLogin }) {
|
||||
export function encodeNamespacePda({ namespace, type, minLocalpartLength, registrationFeeLamports, currencyCode = 1, registrarPublicKey32, serverLogin }) {
|
||||
const name = enc.encode(normalizeNamespace(namespace));
|
||||
if (!Number.isInteger(minLocalpartLength) || minLocalpartLength < 1 || minLocalpartLength + name.length + 1 > 30) throw new Error('Недопустимая длина локального логина');
|
||||
if (type !== 1 && type !== 2) throw new Error('Недопустимый тип namespace');
|
||||
if (currencyCode !== 1) throw new Error('Валюта namespace пока не поддерживается (разрешена только SOL)');
|
||||
const fee = BigInt(registrationFeeLamports);
|
||||
if (fee < 0n || fee > (2n ** 64n - 1n)) throw new Error('Неверная цена регистрации');
|
||||
const server = type === 2 ? enc.encode(normalizeFullLogin(serverLogin)) : new Uint8Array();
|
||||
const pub = type === 2 ? new Uint8Array(registrarPublicKey32) : new Uint8Array();
|
||||
if (type === 2 && pub.length !== 32) throw new Error('Ключ регистратора должен быть 32 байта');
|
||||
const out = new Uint8Array(5 + name.length + 8 + (type === 2 ? 32 + 1 + server.length : 0));
|
||||
out.set([1, 0, type, minLocalpartLength, name.length]); out.set(name, 5);
|
||||
const out = new Uint8Array(5 + name.length + 8 + 1 + (type === 2 ? 32 + 1 + server.length : 0));
|
||||
out.set([1, 1, type, minLocalpartLength, name.length]); out.set(name, 5);
|
||||
let offset = 5 + name.length;
|
||||
new DataView(out.buffer).setBigUint64(offset, fee, true); offset += 8;
|
||||
out[offset++] = currencyCode;
|
||||
if (type === 2) { out.set(pub, offset); offset += 32; out[offset++] = server.length; out.set(server, offset); }
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user