import { NetworkProvider, sleep, UIProvider } from '@ton/blueprint'; import { encryptContent, getRecipientPublicKey } from './crypto/crypto'; import { getKeypair, deployGift, getListOfGifts, readGift, myAddress, client } from './toolchain'; import { Address } from '@ton/core'; function checkEnv() { if (!process.env.WALLET_MNEMONIC) { throw new Error('WALLET_MNEMONIC is not set in environment variables'); } if (!process.env.WALLET_ADDRESS) { throw new Error('WALLET_ADDRESS is not set in environment variables'); } if (process.env.WALLET_VERSION !== 'V4') { throw new Error('WALLET_VERSION must be set to V4 in environment variables'); } } export async function run(provider: NetworkProvider) { const ui = provider.ui(); checkEnv(); while (true) { const choice = await ui.choose('Select an option:', ['Create gift', 'Receive gift', 'Exit'], (c) => c); switch (choice) { case 'Create gift': await handleCreateGift(ui); break; case 'Receive gift': await handleReceiveGift(ui); break; case 'Exit': ui.write('Goodbye!'); return; } } async function handleCreateGift(ui: UIProvider) { const text = await ui.input('Enter text to send:'); const recipientAddress = await ui.inputAddress( 'Enter recipient TON address:', Address.parse('0QDl9pZ0wzHiYCKxyzdbR5WN6kAVTS2Ggra-Fx-O182Ms47d'), ); ui.write(`Original text: ${text}`); try { const senderKp = await getKeypair(); const recipientPublicKey = await getRecipientPublicKey(recipientAddress); const encrypted = await encryptContent(senderKp.secretKey, recipientPublicKey, text); ui.write(`Encrypted text: ${Buffer.from(encrypted).toString('base64')}`); // Placeholder for sending ui.setActionPrompt('Sending to blockchain...'); // create gift here await deployGift(provider, recipientAddress, text); ui.clearActionPrompt(); ui.write('Gift sent successfully!'); } catch (error) { ui.write(`Error: ${(error as Error).message}`); } } async function handleReceiveGift(ui: any) { try { const gifts = await getListOfGifts(myAddress()); if (gifts.length === 0) { ui.write('No gifts found.'); await sleep(2000); return; } const choices = gifts; const selectedGift = await ui.choose('Select a gift:', choices, (g: any) => { const date = new Date(g.time * 1000); const timeStr = date.toTimeString().slice(0, 5); // HH:MM const dateStr = date.toLocaleDateString('en-GB'); // dd/mm/yyyy, but we need dd-mm-yy const dayMonthYear = `${date.getDate().toString().padStart(2, '0')}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getFullYear().toString().slice(-2)}`; const addressStr = g.gift?.address?.toString() || 'Unknown'; return `${timeStr} ${dayMonthYear} ${addressStr}`; }); ui.write(`Selected gift: ${selectedGift.address?.toString() || 'Unknown'}`); ui.setActionPrompt('Decrypting gift...'); const content = await readGift(selectedGift.gift); ui.clearActionPrompt(); ui.write(`Decrypted text: ${content}`); } catch (error) { ui.write(`Error: ${(error as Error).message}`); } } }