73 lines
1.8 KiB
PHP
73 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Sdk\Wallet;
|
|
|
|
use IEXBase\TronAPI\Support\Base58Check;
|
|
use IEXBase\TronAPI\Support\Hash;
|
|
use IEXBase\TronAPI\TronAwareTrait;
|
|
|
|
class TokenAddress
|
|
{
|
|
use TronAwareTrait;
|
|
|
|
public $privateKey = '';
|
|
public $address = '';
|
|
public $hexAddress = '';
|
|
|
|
const ADDRESS_SIZE = 34;
|
|
const ADDRESS_PREFIX = "41";
|
|
const ADDRESS_PREFIX_BYTE = 0x41;
|
|
|
|
public function __construct(string $address = '', string $privateKey = '', string $hexAddress = '')
|
|
{
|
|
if (strlen($address) === 0) {
|
|
throw new \InvalidArgumentException('Address can not be empty');
|
|
}
|
|
|
|
if (!empty($address) && empty($hexAddress)) $hexAddress = $this->address2HexString($address);
|
|
if (!empty($hexAddress) && empty($address)) $address = $this->hexString2Address($hexAddress);
|
|
|
|
$this->privateKey = $privateKey;
|
|
$this->address = $address;
|
|
$this->hexAddress = $hexAddress;
|
|
}
|
|
|
|
/**
|
|
* Dont rely on this. Always use Wallet::validateAddress to double check
|
|
* against tronGrid.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isValid(): bool
|
|
{
|
|
if (strlen($this->address) !== self::ADDRESS_SIZE) {
|
|
return false;
|
|
}
|
|
|
|
$address = Base58Check::decode($this->address, false, 0, false);
|
|
$utf8 = hex2bin($address);
|
|
|
|
if (strlen($utf8) !== 25) {
|
|
return false;
|
|
}
|
|
|
|
if (strpos($utf8, chr(self::ADDRESS_PREFIX_BYTE)) !== 0) {
|
|
return false;
|
|
}
|
|
|
|
$checkSum = substr($utf8, 21);
|
|
$address = substr($utf8, 0, 21);
|
|
|
|
$hash0 = Hash::SHA256($address);
|
|
$hash1 = Hash::SHA256($hash0);
|
|
$checkSum1 = substr($hash1, 0, 4);
|
|
|
|
if ($checkSum === $checkSum1) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|