feature
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Chain;
|
||||
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use RuntimeException;
|
||||
|
||||
class TronDriver implements ChainDriver
|
||||
{
|
||||
public function chainId(): string
|
||||
{
|
||||
return 'tron';
|
||||
}
|
||||
|
||||
public function deriveAddress(string $mnemonic, int $index = 0): string
|
||||
{
|
||||
$derived = Bip44::derive($mnemonic, $this->path($index));
|
||||
|
||||
return TronAddress::fromUncompressedPublicKey($derived['public_key_uncompressed']);
|
||||
}
|
||||
|
||||
public function sendNative(string $mnemonic, int $index, string $to, string $amount): string
|
||||
{
|
||||
if (! $this->isValidAddress($to)) {
|
||||
throw new RuntimeException('Invalid Tron address');
|
||||
}
|
||||
|
||||
$derived = Bip44::derive($mnemonic, $this->path($index));
|
||||
$from = TronAddress::fromUncompressedPublicKey($derived['public_key_uncompressed']);
|
||||
$sun = $this->toSun($amount);
|
||||
|
||||
$tx = $this->post('/wallet/createtransaction', [
|
||||
'owner_address' => $from,
|
||||
'to_address' => $to,
|
||||
'amount' => (int) $sun,
|
||||
'visible' => true,
|
||||
]);
|
||||
|
||||
return $this->signAndBroadcast($tx, $derived['private_key']);
|
||||
}
|
||||
|
||||
public function sendToken(string $mnemonic, int $index, string $to, string $amount, string $contract): string
|
||||
{
|
||||
if (! $this->isValidAddress($to) || ! $this->isValidAddress($contract)) {
|
||||
throw new RuntimeException('Invalid Tron address');
|
||||
}
|
||||
|
||||
$derived = Bip44::derive($mnemonic, $this->path($index));
|
||||
$from = TronAddress::fromUncompressedPublicKey($derived['public_key_uncompressed']);
|
||||
$sun = $this->toSun($amount);
|
||||
$parameter = $this->encodeTransferParameter($to, $sun);
|
||||
|
||||
$ext = $this->post('/wallet/triggersmartcontract', [
|
||||
'owner_address' => $from,
|
||||
'contract_address' => $contract,
|
||||
'function_selector' => 'transfer(address,uint256)',
|
||||
'parameter' => $parameter,
|
||||
'fee_limit' => (int) config('coruna.tron.fee_limit', 100_000_000),
|
||||
'call_value' => 0,
|
||||
'visible' => true,
|
||||
]);
|
||||
|
||||
$tx = $ext['transaction'] ?? null;
|
||||
if (! is_array($tx)) {
|
||||
$msg = $ext['result']['message'] ?? ($ext['message'] ?? 'triggersmartcontract failed');
|
||||
throw new RuntimeException(is_string($msg) ? $msg : 'triggersmartcontract failed');
|
||||
}
|
||||
|
||||
return $this->signAndBroadcast($tx, $derived['private_key']);
|
||||
}
|
||||
|
||||
public function isValidAddress(string $address): bool
|
||||
{
|
||||
return TronAddress::isValid($address);
|
||||
}
|
||||
|
||||
private function path(int $index): string
|
||||
{
|
||||
return "m/44'/195'/0'/0/{$index}";
|
||||
}
|
||||
|
||||
private function toSun(string $amount): string
|
||||
{
|
||||
if (! preg_match('/^\d+(\.\d{1,6})?$/', $amount)) {
|
||||
throw new RuntimeException('Invalid amount');
|
||||
}
|
||||
$sun = bcmul($amount, '1000000', 0);
|
||||
if (bccomp($sun, '0') <= 0) {
|
||||
throw new RuntimeException('Amount must be positive');
|
||||
}
|
||||
|
||||
return $sun;
|
||||
}
|
||||
|
||||
private function encodeTransferParameter(string $toBase58, string $amountSun): string
|
||||
{
|
||||
$toHex = TronAddress::toHex($toBase58); // 41 + 20 bytes
|
||||
$addressWord = str_pad($toHex, 64, '0', STR_PAD_LEFT);
|
||||
$amountWord = str_pad(gmp_strval(gmp_init($amountSun, 10), 16), 64, '0', STR_PAD_LEFT);
|
||||
|
||||
return $addressWord.$amountWord;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $tx
|
||||
*/
|
||||
private function signAndBroadcast(array $tx, string $privateKeyHex): string
|
||||
{
|
||||
$txId = $tx['txID'] ?? null;
|
||||
if (! is_string($txId) || $txId === '') {
|
||||
throw new RuntimeException('Missing txID from node');
|
||||
}
|
||||
|
||||
$signature = TronSigner::signTxId($privateKeyHex, $txId);
|
||||
$tx['signature'] = [$signature];
|
||||
|
||||
$result = $this->post('/wallet/broadcasttransaction', $tx);
|
||||
$ok = ($result['result'] ?? false) === true;
|
||||
if (! $ok) {
|
||||
$msg = $result['message'] ?? ($result['code'] ?? 'broadcast failed');
|
||||
if (is_string($msg) && ctype_xdigit($msg)) {
|
||||
$decoded = @hex2bin($msg);
|
||||
$msg = $decoded !== false ? $decoded : $msg;
|
||||
}
|
||||
throw new RuntimeException(is_string($msg) ? $msg : 'broadcast failed');
|
||||
}
|
||||
|
||||
return $txId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function post(string $path, array $payload): array
|
||||
{
|
||||
$resp = $this->http()->post(rtrim((string) config('coruna.tron.full_node'), '/').$path, $payload);
|
||||
if (! $resp->successful()) {
|
||||
throw new RuntimeException('Tron node HTTP '.$resp->status());
|
||||
}
|
||||
$json = $resp->json();
|
||||
if (! is_array($json)) {
|
||||
throw new RuntimeException('Invalid Tron node response');
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
private function http(): PendingRequest
|
||||
{
|
||||
$req = Http::timeout(30)->acceptJson()->asJson();
|
||||
$apiKey = (string) config('coruna.tron.api_key', '');
|
||||
if ($apiKey !== '') {
|
||||
$req = $req->withHeaders(['TRON-PRO-API-KEY' => $apiKey]);
|
||||
}
|
||||
|
||||
return $req;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user