-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexConverter.php
271 lines (229 loc) · 6.83 KB
/
HexConverter.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
namespace Enjin\BlockchainTools;
use Closure;
use phpseclib3\Math\BigInteger;
class HexConverter
{
/**
* Add the '0x' prefix to a string if it does not have the prefix.
*
* @param string $value
*
* @return string
*/
public static function prefix(string $value): string
{
if (!self::hasPrefix($value)) {
$value = '0x' . $value;
}
return $value;
}
/**
* Remove the '0x' prefix from a string if it has the prefix.
*
* @param string $value
*
* @return string
*/
public static function unPrefix(string $value): string
{
if (self::hasPrefix($value)) {
$value = substr($value, 2);
}
return $value;
}
/**
* Check if a string has the prefix '0x'.
*
* @param string $value
*
* @return bool true if prefixed
*/
public static function hasPrefix(string $value): bool
{
return substr($value, 0, 2) == '0x';
}
public static function stringToHex(string $string, int $length = null): string
{
$hex = implode('', unpack('H*', $string));
$padLength = 0;
if ($length) {
$padLength = strlen($hex) + $length - strlen($hex) % $length;
}
return str_pad($hex, $padLength, '0');
}
public static function stringToHexPrefixed(string $string, int $length = null): string
{
return '0x' . static::stringToHex($string, $length);
}
public static function hexToString(string $hex): string
{
return (string) pack('H*', static::unPrefix($hex));
}
/**
* Convert base 10 signed int to a hex signed int.
*
* @param string $int base 10 signed int
* @param int|null $length hex string length to pad on left
*
* @return string hex signed int
*/
public static function intToHex(string $int, int $length = null): string
{
$isNegative = $int[0] === '-';
$value = new BigInteger($int, 10);
$hex = $value->toHex(true);
if ($length) {
$pad = $isNegative ? 'f' : '0';
$hex = str_pad($hex, $length, $pad, STR_PAD_LEFT);
}
return $hex;
}
/**
* Convert base 10 signed int to a hex signed int.
*
* @param string $int base 10 signed int
* @param int|null $length hex string length to pad on left
*
* @return string hex signed int
*/
public static function intToHexPrefixed(string $int, int $length = null): string
{
return '0x' . self::intToHex($int, $length);
}
/**
* Convert a hex signed int to a base 10 signed int.
*
* @param string $hex hex signed int
*
* @return string base 10 signed int
*/
public static function hexToInt(string $hex): string
{
$hex = static::unPrefix($hex);
// -16 negative indicates that hex is encoded with two's complement
$value = new BigInteger($hex, -16);
return $value->toString();
}
/**
* Convert base 10 unsigned int to a hex unsigned int.
*
* @param string $int base 10 unsigned int
* @param int|null $length hex string length to pad on left
*
* @return string hex unsigned int
*/
public static function uintToHex(string $int, int $length = null): string
{
$hex = (new BigInteger($int))->toHex();
if ($length) {
$hex = str_pad($hex, $length, '0', STR_PAD_LEFT);
}
return $hex;
}
public static function uintToHexPrefixed(string $int, int $length = null): string
{
return '0x' . self::uintToHex($int, $length);
}
/**
* Convert hex unsigned int to a base 10 unsigned int.
*
* @param string $hex hex unsigned int
*
* @return string base 10 unsigned int
*/
public static function hexToUInt(string $hex): string
{
$hex = self::unPrefix($hex);
return (string) (new BigInteger($hex, 16))->toString();
}
public static function hexToBytes(string $hex): array
{
$hex = self::unPrefix($hex);
if ($hex !== '0000000000000000000000000000000000000000000000000000000000000000' && ltrim($hex, '0') === '') {
return [];
}
$bin = hex2bin($hex);
$array = unpack('C*', $bin);
return array_values($array);
}
public static function bytesToHex(array $bytes): string
{
if (!$bytes) {
return '00';
}
$bytes = array_map('chr', $bytes);
$bytes = implode('', $bytes);
return bin2hex($bytes);
}
public static function bytesToHexPrefixed(array $bytes): string
{
return '0x' . static::bytesToHex($bytes);
}
/**
* pad a hex string on the left leaving the prefix intact if it has one.
*
* @param string $hex
* @param int $length
* @param string $string
*
* @return string
*/
public static function padLeft(string $hex, int $length, string $string = '0'): string
{
return static::withPrefixIntact($hex, function ($hex) use ($length, $string) {
return str_pad($hex, $length, $string, STR_PAD_LEFT);
});
}
/**
* pad a hex string on the right leaving the prefix intact if it has one.
*
* @param string $hex
* @param int $length
* @param string $string
*
* @return string
*/
public static function padRight(string $hex, int $length, string $string = '0'): string
{
return static::withPrefixIntact($hex, function ($hex) use ($length, $string) {
return str_pad($hex, $length, $string, STR_PAD_RIGHT);
});
}
/**
* mutate a hex string keeping the prefix intact if it has one.
*
* @param string $hex
* @param Closure $callback performs the mutation, passed a single argument: the unprefixed hex
*
* @return string mutated string with prefix intact if it has one
*/
public static function withPrefixIntact(string $hex, Closure $callback)
{
$hasPrefix = static::hasPrefix($hex);
if ($hasPrefix) {
$hex = substr($hex, 2);
}
$value = $callback($hex);
if ($hasPrefix) {
$value = '0x' . $value;
}
return $value;
}
public static function hexToAddress(string $hex): string
{
return substr(strtolower($hex), -40);
}
public static function hexToAddressPrefixed(string $hex): string
{
return self::prefix(self::hexToAddress($hex));
}
public static function addressToEventTopic(string $address): string
{
return str_pad(strtolower(self::unprefix($address)), 64, '0', STR_PAD_LEFT);
}
public static function addressToEventTopicPrefixed(string $address): string
{
return self::prefix(self::addressToEventTopic($address));
}
}