import test from 'node:test'; import assert from 'node:assert/strict'; import vm from 'node:vm'; import { readFileSync } from 'node:fs'; import { setSolanaCluster } from '../js/services/solana-rpc-gateway.js'; globalThis.window = { location: { pathname: '/wallet' } }; const { parseRouteFromPath, toPrettyPath } = await import('../js/router.js'); import { USDT_MAINNET_MINT, ASSOCIATED_TOKEN_PROGRAM_ID, associatedTokenAddress } from '../js/services/solana-usdt-service.js'; import { WSOL_MINT, JUPITER_ROUTER_PROGRAM, swapPair, validateSwapInput, validateSwapBuildResponse, validateSwapInstructions, SWAP_BETA_MAX_SOL_LAMPORTS, SWAP_BETA_MAX_USDT_ATOMIC, requestSwapBuild, } from '../js/services/jupiter-swap-service.js'; const code = (rel) => readFileSync(new URL(rel, import.meta.url), 'utf8'); const ctx = { Buffer, Uint8Array, TextEncoder, TextDecoder, console, setTimeout, crypto: globalThis.crypto }; vm.createContext(ctx); vm.runInContext(code('../js/vendor/solana-web3.iife.min.js'), ctx); const solana = ctx.solanaWeb3; const wallet = solana.Keypair.generate().publicKey.toBase58(); const usdtAta = associatedTokenAddress(solana, wallet, USDT_MAINNET_MINT).toBase58(); const wsolAta = associatedTokenAddress(solana, wallet, WSOL_MINT).toBase58(); const fake = (inputCurrency = 'SOL', amount = 1000000n) => { const pair = swapPair(inputCurrency); return { pair, amount, build: { inputMint: pair.inputMint, outputMint: pair.outputMint, inAmount: amount.toString(), outAmount: '5000000', otherAmountThreshold: '4975000', swapMode: 'ExactIn', slippageBps: 50, transactionVersion: '0', routePlan: [{ swapInfo: { label: 'test' }, percent: 100 }], setupInstructions: [], cleanupInstruction: null, otherInstructions: [], computeBudgetInstructions: [], tipInstruction: null, addressesByLookupTableAddress: {}, swapInstruction: { programId: JUPITER_ROUTER_PROGRAM, accounts: [{ pubkey: wallet, isSigner: true, isWritable: true }], data: 'AA==' }, }, }; }; test('swap page wired from wallet, no external Jupiter wallet', () => { assert.deepEqual(parseRouteFromPath('/wallet/swap'), { pageId: 'wallet-swap-view', params: {} }); assert.equal(toPrettyPath('wallet-swap-view'), 'wallet/swap'); assert.match(code('../js/pages/wallet-view.js'), /navigate\('wallet\/swap'\)/); assert.match(code('../js/app.js'), /'wallet-swap-view': walletSwapView/); const page = code('../js/pages/wallet-swap-view.js'); assert.match(page, /executeJupiterSwap/); assert.match(page, /data-consent/); assert.match(page, /window\.confirm/); assert.doesNotMatch(page, /jup\.ag\/swap\/SOL/); }); test('only Mainnet swaps allowed, with conservative small beta limits', async () => { setSolanaCluster('devnet'); await assert.rejects(requestSwapBuild({ inputCurrency: 'SOL', amountAtomic: 1000000n, walletAddress: wallet }), /Mainnet/); setSolanaCluster('mainnet-beta'); assert.equal(swapPair('SOL').outputMint, USDT_MAINNET_MINT); assert.equal(swapPair('USDT').outputMint, WSOL_MINT); assert.throws(() => swapPair('FAKE'), /SOL и USDT/); assert.equal(SWAP_BETA_MAX_SOL_LAMPORTS, 20_000_000n); assert.equal(SWAP_BETA_MAX_USDT_ATOMIC, 5_000_000n); assert.throws(() => validateSwapInput('SOL', '0.020000001', { solLamports: 200_000_000n }), /Бета-лимит/); assert.throws(() => validateSwapInput('USDT', '5.000001', { solLamports: 200_000_000n }), /Бета-лимит/); assert.throws(() => validateSwapInput('USDT', '0.1', { solLamports: 0n }), /0,01 SOL/); assert.equal(validateSwapInput('SOL', '0.01', { solLamports: 100_000_000n }).amount, 10_000_000n); }); test('Jupiter response must match exact currency, input, slippage and expected minimum', () => { const { pair, amount, build } = fake(); assert.equal(validateSwapBuildResponse(build, pair, amount).minimum, 4975000n); assert.throws(() => validateSwapBuildResponse({ ...build, inAmount: '9999999' }, pair, amount), /неожиданные/); assert.throws(() => validateSwapBuildResponse({ ...build, inputMint: USDT_MAINNET_MINT }, pair, amount), /неожиданные/); assert.throws(() => validateSwapBuildResponse({ ...build, slippageBps: 150 }, pair, amount), /неожиданные/); assert.throws(() => validateSwapBuildResponse({ ...build, otherAmountThreshold: '100000' }, pair, amount), /минимум/); assert.throws(() => validateSwapBuildResponse({ ...build, tipInstruction: {} }, pair, amount), /Неподдерживаемый/); }); test('only Jupiter router can swap; unsigned extra signer and malicious setup rejected', () => { const { pair, amount, build } = fake(); assert.equal(validateSwapInstructions(build, pair, amount, wallet, solana).setupIxs.length, 0); const attacker = solana.Keypair.generate().publicKey.toBase58(); assert.throws(() => validateSwapInstructions({ ...build, swapInstruction: { ...build.swapInstruction, programId: attacker } }, pair, amount, wallet, solana), /router/); assert.throws(() => validateSwapInstructions({ ...build, swapInstruction: { ...build.swapInstruction, accounts: [{ pubkey: attacker, isSigner: true, isWritable: true }] } }, pair, amount, wallet, solana), /неизвестного подписанта/); const badSystem = { programId: '11111111111111111111111111111111', accounts: [ { pubkey: wallet, isSigner: true, isWritable: true }, { pubkey: attacker, isSigner: false, isWritable: true }, ], data: Buffer.from([2,0,0,0,1,0,0,0,0,0,0,0]).toString('base64') }; assert.throws(() => validateSwapInstructions({ ...build, setupInstructions: [badSystem] }, pair, amount, wallet, solana), /Недопустимый системный перевод/); }); test('canonical WSOL wrapping allowed up to input, not to arbitrary accounts', () => { const { pair, amount, build } = fake('SOL', 1_000_000n); const data = Buffer.alloc(12); data.writeUInt32LE(2, 0); data.writeBigUInt64LE(1_000_000n, 4); const instruction = { programId: '11111111111111111111111111111111', accounts: [{ pubkey: wallet, isSigner: true, isWritable: true }, { pubkey: wsolAta, isSigner: false, isWritable: true }], data: data.toString('base64') }; assert.equal(validateSwapInstructions({ ...build, setupInstructions: [instruction] }, pair, amount, wallet, solana).setupIxs.length, 1); const tooMuch = Buffer.alloc(12); tooMuch.writeUInt32LE(2, 0); tooMuch.writeBigUInt64LE(1_000_001n, 4); assert.throws(() => validateSwapInstructions({ ...build, setupInstructions: [{ ...instruction, data: tooMuch.toString('base64') }] }, pair, amount, wallet, solana), /лишние SOL/); const initAta = { programId: ASSOCIATED_TOKEN_PROGRAM_ID, accounts: [ { pubkey: wallet, isSigner: true, isWritable: true }, { pubkey: usdtAta, isSigner: false, isWritable: true }, { pubkey: wallet, isSigner: false, isWritable: false }, { pubkey: USDT_MAINNET_MINT, isSigner: false, isWritable: false }, { pubkey: '11111111111111111111111111111111', isSigner: false, isWritable: false }, { pubkey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', isSigner: false, isWritable: false }, ], data: Buffer.from([1]).toString('base64') }; assert.equal(validateSwapInstructions({ ...build, setupInstructions: [initAta] }, pair, amount, wallet, solana).setupIxs.length, 1); }); test('compiled Jupiter v0 route can be signed locally without external wallet', () => { const keypair = solana.Keypair.generate(); const sender = keypair.publicKey.toBase58(); const sample = fake(); sample.build.swapInstruction.accounts = [{ pubkey: sender, isSigner: true, isWritable: true }]; const checked = validateSwapInstructions(sample.build, sample.pair, sample.amount, sender, solana); const tx = new solana.VersionedTransaction(new solana.TransactionMessage({ payerKey: keypair.publicKey, recentBlockhash: solana.Keypair.generate().publicKey.toBase58(), instructions: [solana.ComputeBudgetProgram.setComputeUnitLimit({units: 200000}), checked.swapIx], }).compileToV0Message()); tx.sign([keypair]); assert.ok(tx.serialize().length > 100); assert.equal(tx.signatures[0].length, 64); }); test('i18n warnings clarify beta, risk and non-custodial key handling', () => { for (const lang of ['ru', 'en']) { const json = JSON.parse(code(`../js/i18n/locales/${lang}.json`)); assert.ok(json.wallet_swap.warning.length > 100); assert.ok(json.wallet_swap.accept_risk.length > 30); assert.ok(json.wallet_swap.wallet_button.includes('Jupiter')); } });