Solana RPC gateway для клиента и сервера

This commit is contained in:
AidarKC
2026-10-18 00:03:46 +03:00
parent b67d06ce43
commit aa5addb5d9
17 changed files with 228 additions and 85 deletions
@@ -1,3 +1,4 @@
import { sendAndConfirmSolanaHttp } from './solana-rpc-gateway.js';
import { base58ToBytes, base64ToBytes, bytesToBase58, importPkcs8Ed25519, sha256Bytes, signBytes } from './crypto-utils.js';
import { extractSeed32FromPkcs8B64 } from './client-key-utils.js';
import {
@@ -623,7 +624,7 @@ async function createShineUserPdaOnSolana({
const tx = new ctx.solana.Transaction();
if (promoEdIx) tx.add(promoEdIx);
tx.add(rootIx, recordIx, createIx);
const signature = await ctx.solana.sendAndConfirmTransaction(ctx.connection, tx, [ctx.payerKeypair], { commitment: 'confirmed' });
const signature = await sendAndConfirmSolanaHttp(ctx.connection, tx, [ctx.payerKeypair]);
return { signature, userPda: ctx.userPda.toBase58(), pdaAddress: ctx.userPda.toBase58(), blockchainName: `${cleanLogin}-001` };
} catch (error) { throw await attachSolanaLogs(error, ctx.connection); }
}
@@ -660,7 +661,7 @@ export async function closeLegacyShineUserPdaOnSolana({ login, solanaEndpoint, c
],
data: serializeCloseLegacyArgs(cleanLogin),
});
const signature = await solana.sendAndConfirmTransaction(connection, new solana.Transaction().add(ix), [caller], { commitment: 'confirmed' });
const signature = await sendAndConfirmSolanaHttp(connection, new solana.Transaction().add(ix), [caller]);
return { signature, pdaAddress: userPda.toBase58() };
}
@@ -779,7 +780,7 @@ export async function updateShineUserPdaOnSolana({
data: serializeUpdateUserPdaArgs({ login: current.login, rootKey32: rootKey, updatedAtMs, additionalLimitBytes: addLimit, clientKey32: clientKey, authMode, newBlockchainKey32: appendedKey, serverAddresses: addresses, accessServers: nextAccess, recordSignature64: recordSig }),
});
try {
const signature = await ctx.solana.sendAndConfirmTransaction(ctx.connection, new ctx.solana.Transaction().add(authIx, recordIx, updateIx), [ctx.payerKeypair], { commitment: 'confirmed' });
const signature = await sendAndConfirmSolanaHttp(ctx.connection, new ctx.solana.Transaction().add(authIx, recordIx, updateIx), [ctx.payerKeypair]);
return { signature, userPda: ctx.userPda.toBase58(), pdaAddress: ctx.userPda.toBase58(), paidLimitBytes: forks.at(-1).paidLimitBytes, forkCount: forks.length };
} catch (error) { throw await attachSolanaLogs(error, ctx.connection); }
}
@@ -0,0 +1,41 @@
/** Solana JSON-RPC transport. Network selection belongs to the connected SHiNE server. */
export const DEVNET_FAUCET_RPC = 'https://api.devnet.solana.com';
let cluster = '';
export function setSolanaCluster(value) {
const next = String(value || '').toLowerCase();
if (next !== 'devnet' && next !== 'mainnet-beta' && next !== 'testnet') throw new Error(`Unsupported server Solana cluster: ${value}`);
cluster = next;
}
export function getSolanaCluster() { return cluster; }
export function rpcEndpointFromShineHttp(httpBase) {
const base = String(httpBase || '').trim();
if (!/^https?:\/\//i.test(base)) throw new Error('SHiNE server HTTP address is unavailable');
return `${base.replace(/\/+$/, '')}/solana-rpc`;
}
export async function confirmSolanaTransactionHttp(connection, signature, {
lastValidBlockHeight = null, commitment = 'confirmed', timeoutMs = 90_000,
} = {}) {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
const response = await connection.getSignatureStatuses([signature], { searchTransactionHistory: true });
const status = response?.value?.[0];
if (status?.err) throw new Error(`Solana transaction failed: ${JSON.stringify(status.err)}`);
if (status && (status.confirmationStatus === 'finalized' ||
(commitment !== 'finalized' && status.confirmationStatus === 'confirmed'))) return signature;
if (lastValidBlockHeight != null) {
const height = await connection.getBlockHeight('confirmed');
if (height > lastValidBlockHeight) throw new Error(`Solana transaction expired: ${signature}`);
}
await new Promise((resolve) => setTimeout(resolve, 1800));
}
throw new Error(`Solana transaction confirmation timed out: ${signature}`);
}
export async function sendAndConfirmSolanaHttp(connection, transaction, signers) {
const latest = await connection.getLatestBlockhash('confirmed');
transaction.recentBlockhash = latest.blockhash;
if (!transaction.feePayer && signers?.[0]) transaction.feePayer = signers[0].publicKey;
transaction.sign(...signers);
const signature = await connection.sendRawTransaction(transaction.serialize(), { skipPreflight: false });
await confirmSolanaTransactionHttp(connection, signature, { lastValidBlockHeight: latest.lastValidBlockHeight });
return signature;
}
@@ -1,3 +1,4 @@
import { confirmSolanaTransactionHttp, sendAndConfirmSolanaHttp } from './solana-rpc-gateway.js';
import { extractEd25519Seed32FromStoredValue } from './client-key-utils.js';
import { base64ToBytes } from './crypto-utils.js';
import { loadEncryptedUserSecrets } from './key-vault.js';
@@ -259,7 +260,7 @@ export async function requestAirdropSol({ endpoint, address, amountSol = 1 }) {
const pubkey = new solana.PublicKey(String(address || '').trim());
const lamports = Math.max(1, Math.floor(Number(amountSol) * solana.LAMPORTS_PER_SOL));
const signature = await conn.requestAirdrop(pubkey, lamports);
await conn.confirmTransaction(signature, 'confirmed');
await confirmSolanaTransactionHttp(conn, signature);
return { endpoint: rpc, signature, lamports };
}
@@ -283,9 +284,7 @@ export async function transferSol({ endpoint, fromKeypair, toAddress, amountSol
}),
);
const signature = await solana.sendAndConfirmTransaction(conn, tx, [fromKeypair], {
commitment: 'confirmed',
});
const signature = await sendAndConfirmSolanaHttp(conn, tx, [fromKeypair]);
return { endpoint: rpc, signature, lamports };
}
@@ -338,11 +337,7 @@ export async function transferAllSol({ endpoint, fromKeypair, toAddress }) {
}));
tx.sign(fromKeypair);
const signature = await conn.sendRawTransaction(tx.serialize(), { skipPreflight: false });
await conn.confirmTransaction({
signature,
blockhash: latest.blockhash,
lastValidBlockHeight: latest.lastValidBlockHeight,
}, 'confirmed');
await confirmSolanaTransactionHttp(conn, signature, { lastValidBlockHeight: latest.lastValidBlockHeight });
return {
endpoint: rpc,
signature,