mattvb91 /
tron-trx-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace mattvb91\TronTrx; |
||
| 4 | |||
| 5 | use mattvb91\TronTrx\Support\Base58Check; |
||
| 6 | use mattvb91\TronTrx\Support\Hash; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class Address |
||
| 10 | * @package mattvb91\TronTrx |
||
| 11 | */ |
||
| 12 | class Address |
||
| 13 | { |
||
| 14 | public $privateKey, |
||
| 15 | $address, |
||
| 16 | $hexAddress = ''; |
||
| 17 | |||
| 18 | const ADDRESS_SIZE = 34; |
||
| 19 | const ADDRESS_PREFIX = "41"; |
||
| 20 | const ADDRESS_PREFIX_BYTE = 0x41; |
||
| 21 | |||
| 22 | public function __construct(string $address = '', string $privateKey = '', string $hexAddress = '') |
||
| 23 | { |
||
| 24 | if (strlen($address) === 0) { |
||
| 25 | throw new \InvalidArgumentException('Address can not be empty'); |
||
| 26 | } |
||
| 27 | |||
| 28 | $this->privateKey = $privateKey; |
||
| 29 | $this->address = $address; |
||
| 30 | $this->hexAddress = $hexAddress; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Dont rely on this. Always use Wallet::validateAddress to double check |
||
| 35 | * against tronGrid. |
||
| 36 | * |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | public function isValid(): bool |
||
| 40 | { |
||
| 41 | if (strlen($this->address) !== Address::ADDRESS_SIZE) { |
||
| 42 | return false; |
||
| 43 | } |
||
| 44 | |||
| 45 | $address = Base58Check::decode($this->address, false, 0, false); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 46 | $utf8 = hex2bin($address); |
||
| 47 | |||
| 48 | if (strlen($utf8) !== 25) { |
||
| 49 | return false; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (strpos($utf8, self::ADDRESS_PREFIX_BYTE) !== 0) { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | $checkSum = substr($utf8, 21); |
||
| 57 | $address = substr($utf8, 0, 21); |
||
| 58 | |||
| 59 | $hash0 = Hash::SHA256($address); |
||
| 60 | $hash1 = Hash::SHA256($hash0); |
||
| 61 | $checkSum1 = substr($hash1, 0, 4); |
||
| 62 | |||
| 63 | if ($checkSum === $checkSum1) { |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | return false; |
||
| 68 | } |
||
| 69 | } |