111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\Chain\TronDriver;
|
|
use Illuminate\Console\Command;
|
|
use RuntimeException;
|
|
|
|
class TronToolCommand extends Command
|
|
{
|
|
protected $signature = 'coruna:tron
|
|
{action : create | balance | address}
|
|
{value? : balance 时填地址;address 时填私钥}';
|
|
|
|
protected $description = 'TRON 工具:创建地址/私钥、由私钥推地址、查 TRX/USDT 余额';
|
|
|
|
public function handle(TronDriver $tron): int
|
|
{
|
|
$action = strtolower(trim((string) $this->argument('action')));
|
|
|
|
return match ($action) {
|
|
'create' => $this->createWallet(),
|
|
'balance' => $this->showBalance($tron),
|
|
'address' => $this->addressFromKey(),
|
|
default => $this->invalidAction($action),
|
|
};
|
|
}
|
|
|
|
private function createWallet(): int
|
|
{
|
|
$privateKey = bin2hex(random_bytes(32));
|
|
$address = TronDriver::addressFromPrivateKey($privateKey);
|
|
|
|
$this->line('address : '.$address);
|
|
$this->line('privateKey : '.$privateKey);
|
|
$this->warn('请妥善保存私钥,丢失无法找回。');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function addressFromKey(): int
|
|
{
|
|
$key = trim((string) ($this->argument('value') ?? ''));
|
|
if ($key === '') {
|
|
$this->error('用法: php artisan coruna:tron address <privateKeyHex>');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
try {
|
|
$address = TronDriver::addressFromPrivateKey($key);
|
|
} catch (\Throwable $e) {
|
|
$this->error('无效私钥: '.$e->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->line('address : '.$address);
|
|
$this->line('privateKey : '.strtolower(str_starts_with($key, '0x') ? substr($key, 2) : $key));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function showBalance(TronDriver $tron): int
|
|
{
|
|
$address = trim((string) ($this->argument('value') ?? ''));
|
|
if ($address === '') {
|
|
$this->error('用法: php artisan coruna:tron balance <TAddress>');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
if (! $tron->isValidAddress($address)) {
|
|
$this->error('无效 TRON 地址');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
try {
|
|
$trx = $tron->getNativeBalance($address);
|
|
$contract = trim((string) config('coruna.tron.usdt_contract', ''));
|
|
$usdt = $contract !== '' ? $tron->getTokenBalance($address, $contract) : '0';
|
|
} catch (RuntimeException $e) {
|
|
$this->error($e->getMessage());
|
|
|
|
return self::FAILURE;
|
|
} catch (\Throwable $e) {
|
|
$this->error('查询失败: '.$e->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->line('address : '.$address);
|
|
$this->line('TRX : '.$trx);
|
|
$this->line('USDT : '.$usdt);
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function invalidAction(string $action): int
|
|
{
|
|
$this->error("未知操作: {$action}");
|
|
$this->line('用法:');
|
|
$this->line(' php artisan coruna:tron create');
|
|
$this->line(' php artisan coruna:tron address <privateKeyHex>');
|
|
$this->line(' php artisan coruna:tron balance <TAddress>');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
}
|