SHA256
Промежуточный коммит: состояние до нормальной Solana-first регистрации
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
} from '../solana-programs.js';
|
||||
|
||||
const CREATE_USER_PDA_DISCRIMINATOR = new Uint8Array([139, 157, 13, 41, 142, 174, 226, 214]);
|
||||
const CLASSIFY_LOGIN_DISCRIMINATOR = new Uint8Array([118, 253, 204, 124, 22, 232, 235, 32]);
|
||||
const ED25519_PROGRAM_ID = 'Ed25519SigVerify111111111111111111111111111';
|
||||
const SYSVAR_INSTRUCTIONS_ID = 'Sysvar1nstructions1111111111111111111111111';
|
||||
|
||||
@@ -183,6 +184,49 @@ function serializeCreateUserPdaArgs(
|
||||
return b.result();
|
||||
}
|
||||
|
||||
function serializeClassifyLoginArgs(login) {
|
||||
const b = new BorshBuf();
|
||||
b.raw(CLASSIFY_LOGIN_DISCRIMINATOR);
|
||||
b.str(String(login || ''));
|
||||
return b.result();
|
||||
}
|
||||
|
||||
function decodeU32FromB64(rawB64) {
|
||||
const bytes = Uint8Array.from(atob(rawB64), (ch) => ch.charCodeAt(0));
|
||||
if (bytes.length < 4) throw new Error('LOGIN_GUARD_BAD_RETURN_DATA');
|
||||
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0, true);
|
||||
}
|
||||
|
||||
export async function precheckLoginClassOnSolana({ login, solanaEndpoint }) {
|
||||
const solana = await loadSolanaLib();
|
||||
const connection = new solana.Connection(String(solanaEndpoint || ''), 'confirmed');
|
||||
const loginGuardProgram = new solana.PublicKey(SHINE_LOGIN_GUARD_PROGRAM_ID);
|
||||
const signer = solana.Keypair.generate();
|
||||
const ix = new solana.TransactionInstruction({
|
||||
programId: loginGuardProgram,
|
||||
keys: [{ pubkey: signer.publicKey, isSigner: true, isWritable: false }],
|
||||
data: serializeClassifyLoginArgs(String(login || '').toLowerCase()),
|
||||
});
|
||||
const tx = new solana.Transaction().add(ix);
|
||||
tx.feePayer = signer.publicKey;
|
||||
tx.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
|
||||
tx.sign(signer);
|
||||
|
||||
const sim = await connection.simulateTransaction(tx, { commitment: 'confirmed', sigVerify: true });
|
||||
if (sim?.value?.err) {
|
||||
throw new Error(`LOGIN_GUARD_SIMULATION_FAILED: ${JSON.stringify(sim.value.err)}`);
|
||||
}
|
||||
const returnData = sim?.value?.returnData;
|
||||
if (!returnData || returnData.programId !== SHINE_LOGIN_GUARD_PROGRAM_ID) {
|
||||
throw new Error('LOGIN_GUARD_BAD_RETURN_DATA');
|
||||
}
|
||||
const classValue = decodeU32FromB64(returnData.data?.[0] || '');
|
||||
if (classValue === 0) return { classCode: 0, className: 'free' };
|
||||
if (classValue === 1) return { classCode: 1, className: 'premium' };
|
||||
if (classValue === 2) return { classCode: 2, className: 'company' };
|
||||
return { classCode: classValue, className: 'unknown' };
|
||||
}
|
||||
|
||||
export async function registerUserOnSolana({ login, keyBundle, solanaEndpoint }) {
|
||||
const solana = await loadSolanaLib();
|
||||
const connection = new solana.Connection(String(solanaEndpoint || ''), 'confirmed');
|
||||
@@ -249,12 +293,12 @@ export async function registerUserOnSolana({ login, keyBundle, solanaEndpoint })
|
||||
const ed25519RootIx = new solana.TransactionInstruction({
|
||||
programId: ed25519Program,
|
||||
keys: [],
|
||||
data: Buffer.from(buildEd25519IxData(rootSig64, rootKey32, unsignedHash)),
|
||||
data: buildEd25519IxData(rootSig64, rootKey32, unsignedHash),
|
||||
});
|
||||
const ed25519BchIx = new solana.TransactionInstruction({
|
||||
programId: ed25519Program,
|
||||
keys: [],
|
||||
data: Buffer.from(buildEd25519IxData(lastBlockSig64, blockchainKey32, lbsHash)),
|
||||
data: buildEd25519IxData(lastBlockSig64, blockchainKey32, lbsHash),
|
||||
});
|
||||
const createUserIx = new solana.TransactionInstruction({
|
||||
programId: usersProgram,
|
||||
@@ -267,7 +311,7 @@ export async function registerUserOnSolana({ login, keyBundle, solanaEndpoint })
|
||||
{ pubkey: economyConfigPda, isSigner: false, isWritable: false },
|
||||
{ pubkey: loginGuardProgram, isSigner: false, isWritable: false },
|
||||
],
|
||||
data: Buffer.from(ixData),
|
||||
data: ixData,
|
||||
});
|
||||
|
||||
const sig = await solana.sendAndConfirmTransaction(
|
||||
|
||||
Reference in New Issue
Block a user