1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2020 UMI |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
declare(strict_types=1); |
26
|
|
|
|
27
|
|
|
namespace UmiTop\UmiCore\Address; |
28
|
|
|
|
29
|
|
|
use Exception; |
30
|
|
|
use UmiTop\UmiCore\Key\KeyInterface; |
31
|
|
|
use UmiTop\UmiCore\Key\PublicKey; |
32
|
|
|
use UmiTop\UmiCore\Key\PublicKeyInterface; |
33
|
|
|
use UmiTop\UmiCore\Util\Bech32; |
34
|
|
|
use UmiTop\UmiCore\Util\ConverterTrait; |
35
|
|
|
use UmiTop\UmiCore\Util\ValidatorTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Класс для работы с адресами. |
39
|
|
|
* @package UmiTop\UmiCore\Address |
40
|
|
|
*/ |
41
|
|
|
class Address implements AddressInterface |
42
|
|
|
{ |
43
|
|
|
use ConverterTrait; |
44
|
|
|
use ValidatorTrait; |
45
|
|
|
|
46
|
|
|
/** @var int Длина адреса в байтах. */ |
47
|
|
|
public const LENGTH = 34; |
48
|
|
|
|
49
|
|
|
/** @var string Адрес в бинарном виде. */ |
50
|
|
|
private $bytes; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Address constructor. |
54
|
|
|
*/ |
55
|
24 |
|
public function __construct() |
56
|
|
|
{ |
57
|
24 |
|
$this->bytes = str_repeat("\x0", self::LENGTH); |
58
|
24 |
|
$this->setPrefix('umi'); |
59
|
24 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Статический метод, создает объект из адреса в формате Bech32. |
63
|
|
|
* @param string $address Адрес в формате Bech32. |
64
|
|
|
* @return AddressInterface |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
1 |
|
public static function fromBech32(string $address): AddressInterface |
68
|
|
|
{ |
69
|
1 |
|
$adr = new Address(); |
70
|
|
|
|
71
|
1 |
|
return $adr->setBech32($address); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Статический метод, создает объект из бинарного представления. |
76
|
|
|
* @param string $bytes Адрес в бинарном виде. |
77
|
|
|
* @return AddressInterface |
78
|
|
|
* @throws Exception |
79
|
|
|
*/ |
80
|
1 |
|
public static function fromBytes(string $bytes): AddressInterface |
81
|
|
|
{ |
82
|
1 |
|
$adr = new Address(); |
83
|
|
|
|
84
|
1 |
|
return $adr->setBytes($bytes); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Статический метод, создает объект из публичного или приватного ключа. |
89
|
|
|
* @param KeyInterface $key Приватный или публичный ключ. |
90
|
|
|
* @return AddressInterface |
91
|
|
|
*/ |
92
|
1 |
|
public static function fromKey(KeyInterface $key): AddressInterface |
93
|
|
|
{ |
94
|
1 |
|
$adr = new Address(); |
95
|
|
|
|
96
|
1 |
|
return $adr->setPublicKey($key->getPublicKey()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Адрес в формате Bech32, длина 62 или 65 символов. |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
7 |
|
public function getBech32(): string |
104
|
|
|
{ |
105
|
7 |
|
$bech32 = new Bech32(); |
106
|
|
|
|
107
|
7 |
|
return $bech32->encode($this->bytes); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Устанавливает адрес из строки в формате Bech32 и возвращает $this. |
112
|
|
|
* @param string $address Адрес в формате Bech32. |
113
|
|
|
* @return AddressInterface |
114
|
|
|
* @throws Exception |
115
|
|
|
*/ |
116
|
15 |
|
public function setBech32(string $address): AddressInterface |
117
|
|
|
{ |
118
|
15 |
|
$bech32 = new Bech32(); |
119
|
|
|
|
120
|
15 |
|
return $this->setBytes($bech32->decode($address)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Адрес в бинарном виде, длина 34 байта. |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
4 |
|
public function getBytes(): string |
128
|
|
|
{ |
129
|
4 |
|
return $this->bytes; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Устанавливает адрес из бинарной строки и возвращает $this. |
134
|
|
|
* @param string $bytes Адрес в бинарном виде, длина 34 байта. |
135
|
|
|
* @return AddressInterface |
136
|
|
|
* @throws Exception |
137
|
|
|
*/ |
138
|
12 |
|
public function setBytes(string $bytes): AddressInterface |
139
|
|
|
{ |
140
|
12 |
|
$this->validateStr($bytes, self::LENGTH); |
141
|
11 |
|
$this->bytes = $bytes; |
142
|
|
|
|
143
|
11 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Префикс адреса, три символа латиницы в нижнем регистре. |
148
|
|
|
* @return string |
149
|
|
|
* @throws Exception |
150
|
|
|
*/ |
151
|
1 |
|
public function getPrefix(): string |
152
|
|
|
{ |
153
|
1 |
|
return $this->bytesToPrefix(substr($this->bytes, 0, 2)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Устанавливает префикс адреса и возвращает $this. |
158
|
|
|
* @param string $prefix Префикс. Три символа латиницы в нижнем регистре. |
159
|
|
|
* @return AddressInterface |
160
|
|
|
* @throws Exception |
161
|
|
|
*/ |
162
|
24 |
|
public function setPrefix(string $prefix): AddressInterface |
163
|
|
|
{ |
164
|
24 |
|
$this->bytes = substr_replace($this->bytes, $this->prefixToBytes($prefix), 0, 2); |
165
|
|
|
|
166
|
24 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Публичный ключ. |
171
|
|
|
* @return PublicKeyInterface |
172
|
|
|
*/ |
173
|
3 |
|
public function getPublicKey(): PublicKeyInterface |
174
|
|
|
{ |
175
|
3 |
|
return new PublicKey(substr($this->bytes, 2, 32)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Устанавливает публичный ключи и возвращает $this. |
180
|
|
|
* @param PublicKeyInterface $publicKey Публичный ключ. |
181
|
|
|
* @return AddressInterface |
182
|
|
|
*/ |
183
|
3 |
|
public function setPublicKey(PublicKeyInterface $publicKey): AddressInterface |
184
|
|
|
{ |
185
|
3 |
|
$this->bytes = substr_replace($this->bytes, $publicKey->getBytes(), 2, 32); |
186
|
|
|
|
187
|
3 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|