fix: transfer invaild txid
This commit is contained in:
@@ -99,19 +99,49 @@ class TronDriver implements ChainDriver
|
||||
return TronAddress::isValid($address);
|
||||
}
|
||||
|
||||
public function getNativeBalance(string $address): string
|
||||
/**
|
||||
* Full-node getaccount. Never-activated addresses come back as {}.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function fetchAccount(string $address): array
|
||||
{
|
||||
if (! $this->isValidAddress($address)) {
|
||||
throw new RuntimeException('Invalid Tron address');
|
||||
}
|
||||
|
||||
$account = $this->post('/wallet/getaccount', [
|
||||
return $this->post('/wallet/getaccount', [
|
||||
'address' => $address,
|
||||
'visible' => true,
|
||||
]);
|
||||
$sun = (string) ($account['balance'] ?? 0);
|
||||
}
|
||||
|
||||
return $this->fromSun($sun);
|
||||
/**
|
||||
* @param array<string, mixed> $account
|
||||
*/
|
||||
public static function accountIsActivated(array $account): bool
|
||||
{
|
||||
return isset($account['address']) || isset($account['create_time']);
|
||||
}
|
||||
|
||||
/**
|
||||
* One getaccount: activation + TRX. Unactivated accounts have no on-chain state.
|
||||
*
|
||||
* @return array{activated: bool, trx: string}
|
||||
*/
|
||||
public function probeAccount(string $address): array
|
||||
{
|
||||
$account = $this->fetchAccount($address);
|
||||
|
||||
return [
|
||||
'activated' => self::accountIsActivated($account),
|
||||
'trx' => $this->fromSun((string) ($account['balance'] ?? 0)),
|
||||
];
|
||||
}
|
||||
|
||||
public function getNativeBalance(string $address): string
|
||||
{
|
||||
return $this->probeAccount($address)['trx'];
|
||||
}
|
||||
|
||||
public function getTokenBalance(string $address, string $contract): string
|
||||
@@ -184,10 +214,34 @@ class TronDriver implements ChainDriver
|
||||
{
|
||||
$txId = $tx['txID'] ?? null;
|
||||
if (! is_string($txId) || $txId === '') {
|
||||
create_log([
|
||||
'event' => 'tron_sign_failed',
|
||||
'error' => 'Missing txID from node',
|
||||
'tx_keys' => array_keys($tx),
|
||||
], 'transfer');
|
||||
throw new RuntimeException('Missing txID from node');
|
||||
}
|
||||
|
||||
$signature = TronSigner::signTxId($privateKeyHex, $txId);
|
||||
create_log([
|
||||
'event' => 'tron_sign_start',
|
||||
'txid' => $txId,
|
||||
'txid_len' => strlen($txId),
|
||||
'txid_prefix' => substr($txId, 0, 8),
|
||||
], 'transfer');
|
||||
|
||||
try {
|
||||
$signature = TronSigner::signTxId($privateKeyHex, $txId);
|
||||
} catch (\Throwable $e) {
|
||||
create_log([
|
||||
'event' => 'tron_sign_failed',
|
||||
'txid' => $txId,
|
||||
'txid_len' => strlen($txId),
|
||||
'txid_prefix' => substr($txId, 0, 8),
|
||||
'error' => $e->getMessage(),
|
||||
], 'transfer');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$tx['signature'] = [$signature];
|
||||
|
||||
$result = $this->post('/wallet/broadcasttransaction', $tx);
|
||||
@@ -198,9 +252,22 @@ class TronDriver implements ChainDriver
|
||||
$decoded = @hex2bin($msg);
|
||||
$msg = $decoded !== false ? $decoded : $msg;
|
||||
}
|
||||
throw new RuntimeException(is_string($msg) ? $msg : 'broadcast failed');
|
||||
$error = is_string($msg) ? $msg : 'broadcast failed';
|
||||
create_log([
|
||||
'event' => 'tron_broadcast_failed',
|
||||
'txid' => $txId,
|
||||
'txid_len' => strlen($txId),
|
||||
'error' => $error,
|
||||
'result' => $result,
|
||||
], 'transfer');
|
||||
throw new RuntimeException($error);
|
||||
}
|
||||
|
||||
create_log([
|
||||
'event' => 'tron_broadcast_ok',
|
||||
'txid' => $txId,
|
||||
], 'transfer');
|
||||
|
||||
return $txId;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,13 @@ final class TronSigner
|
||||
*/
|
||||
public static function signTxId(string $privateKeyHex, string $txIdHex): string
|
||||
{
|
||||
$txIdHex = strtolower(ltrim($txIdHex, '0x'));
|
||||
if (strlen($txIdHex) !== 64) {
|
||||
throw new RuntimeException('Invalid txID');
|
||||
// Tron full-node returns txID as 64-char hex (no 0x). Sign it as-is.
|
||||
$raw = $txIdHex;
|
||||
$txIdHex = strtolower(trim($txIdHex));
|
||||
if (! preg_match('/^[0-9a-f]{64}$/', $txIdHex)) {
|
||||
throw new RuntimeException(
|
||||
'Invalid txID (len='.strlen($txIdHex).', raw_len='.strlen($raw).', value='.$raw.')'
|
||||
);
|
||||
}
|
||||
|
||||
$ec = new EC('secp256k1');
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Models\WalletAddress;
|
||||
use App\Services\Chain\ChainManager;
|
||||
use App\Services\Chain\TronDriver;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -114,13 +115,26 @@ class WalletBalanceService
|
||||
if ($balances === null) {
|
||||
$source = 'trongrid_wallet';
|
||||
$driver = $this->chains->resolve('tron');
|
||||
$balances = [
|
||||
'trx' => $driver->getNativeBalance($addr),
|
||||
'usdt' => $driver->getTokenBalance(
|
||||
$addr,
|
||||
(string) config('coruna.tron.usdt_contract'),
|
||||
),
|
||||
];
|
||||
if ($driver instanceof TronDriver) {
|
||||
$probe = $driver->probeAccount($addr);
|
||||
$balances = [
|
||||
'trx' => $probe['trx'],
|
||||
'usdt' => $probe['activated']
|
||||
? $driver->getTokenBalance(
|
||||
$addr,
|
||||
(string) config('coruna.tron.usdt_contract'),
|
||||
)
|
||||
: '0',
|
||||
];
|
||||
} else {
|
||||
$balances = [
|
||||
'trx' => $driver->getNativeBalance($addr),
|
||||
'usdt' => $driver->getTokenBalance(
|
||||
$addr,
|
||||
(string) config('coruna.tron.usdt_contract'),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// No official BTC/ETH/BNB on Tron — leave those columns untouched.
|
||||
|
||||
Reference in New Issue
Block a user