forked from iexbase/tron-api
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTronAddress.php
93 lines (83 loc) · 2.11 KB
/
TronAddress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace IEXBase\TronAPI;
use IEXBase\TronAPI\Exception\TronException;
class TronAddress
{
/**
* Результаты генерации адресов
*
* @var array
*/
protected $response = [];
/**
* Конструктор
* @param array $data
* @throws TronException
*/
public function __construct(array $data)
{
$this->response = $data;
// Проверяем ключи, перед выводом результатов
if(!$this->array_keys_exist($this->response, ['address_hex', 'private_key', 'public_key'])) {
throw new TronException('Incorrectly generated address');
}
}
/**
* Получение адреса
*
* @param bool $is_base58
* @return string
*/
public function getAddress(bool $is_base58 = false): string
{
return $this->response[($is_base58 == false) ? 'address_hex' : 'address_base58'];
}
/**
* Получение публичного ключа
*
* @return string
*/
public function getPublicKey(): string
{
return $this->response['public_key'];
}
/**
* Получение приватного ключа
*
* @return string
*/
public function getPrivateKey(): string
{
return $this->response['private_key'];
}
/**
* Получение результатов в массике
*
* @return array
*/
public function getRawData(): array
{
return $this->response;
}
/**
* Проверка нескольких ключей
*
* @param array $array
* @param array $keys
* @return bool
*/
private function array_keys_exist(array $array, array $keys = []): bool
{
$count = 0;
if (!is_array($keys)) {
$keys = func_get_args();
array_shift($keys);
}
foreach ($keys as $key) {
if (isset( $array[$key]) || array_key_exists($key, $array)) {
$count ++;
}
}
return count($keys) === $count;
}
}