init
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Chain;
|
||||
|
||||
use Elliptic\EC;
|
||||
use RuntimeException;
|
||||
|
||||
final class TronSigner
|
||||
{
|
||||
/**
|
||||
* Sign a Tron txID (sha256 hex of raw_data) → 65-byte hex signature (r||s||v).
|
||||
*/
|
||||
public static function signTxId(string $privateKeyHex, string $txIdHex): string
|
||||
{
|
||||
$txIdHex = strtolower(ltrim($txIdHex, '0x'));
|
||||
if (strlen($txIdHex) !== 64) {
|
||||
throw new RuntimeException('Invalid txID');
|
||||
}
|
||||
|
||||
$ec = new EC('secp256k1');
|
||||
$key = $ec->keyFromPrivate($privateKeyHex);
|
||||
$sig = $key->sign($txIdHex, ['canonical' => true]);
|
||||
|
||||
$r = str_pad($sig->r->toString(16), 64, '0', STR_PAD_LEFT);
|
||||
$s = str_pad($sig->s->toString(16), 64, '0', STR_PAD_LEFT);
|
||||
$v = dechex($sig->recoveryParam & 0xff);
|
||||
if (strlen($v) === 1) {
|
||||
$v = '0'.$v;
|
||||
}
|
||||
|
||||
return $r.$s.$v;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user