PHP WebShell

Текущая директория: /var/www/bitcardoApp/models/crypto

Просмотр файла: tron_send_trx.php

<?php

namespace Models\Crypto;

class TronSendTrx
{
    private $config;
    private $network;

    public function __construct()
    {
        $this->config = include __DIR__ . '/../../config/tron_config.php';
        $this->network = $this->config['network'];
    }

    /**
     * Send TRX from private key → receiver
     * @param string $privateKey Raw private key of sender
     * @param string $fromAddress Sender address
     * @param string $toAddress Receiver address
     * @param int $amount Raw Sun amount (1 TRX = 1,000,000 Sun)
     */
    public function send($privateKey, $fromAddress, $toAddress, $amount)
    {
        // 1. Build transaction
        $payload = [
            "to_address"   => $this->toHex($toAddress),
            "owner_address" => $this->toHex($fromAddress),
            "amount"       => $amount
        ];

        $url = $this->network . "/wallet/createtransaction";

        $transaction = $this->post($url, $payload);

        if (!isset($transaction['raw_data'])) {
            return ['error' => 'Failed to create TRX transaction'];
        }

        // 2. Sign transaction
        $signed = $this->signTransaction($transaction, $privateKey);

        // 3. Broadcast
        $broadcast = $this->post($this->network . "/wallet/broadcasttransaction", $signed);

        return $broadcast;
    }

    private function toHex($address)
    {
        $decoded = base58check_decode($address);
        return bin2hex($decoded);
    }

    private function signTransaction($transaction, $privateKey)
    {
        $signature = sign_with_private_key(json_encode($transaction['raw_data']), $privateKey);
        $transaction['signature'] = [$signature];
        return $transaction;
    }

    private function post($url, $payload)
    {
        $ch = curl_init();

        curl_setopt_array($ch, [
            CURLOPT_URL            => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => json_encode($payload),
            CURLOPT_HTTPHEADER     => [
                "Content-Type: application/json",
                "TRON-PRO-API-KEY: " . $this->config['api_key']
            ],
        ]);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }
}

Выполнить команду


Для локальной разработки. Не используйте в интернете!