43 public static function encode(
string $val)
46 $val = self::removeHexPrefix($val);
49 $length = strlen($val);
53 $offset =
'0x0000000000000000000000000000000000000000000000000000000000000020';
55 if ($length % 2 && ctype_xdigit($val)) {
59 if ($length < self::THRESHOLD_LONG) {
63 $lengthInByte = self::getByteLength($length / 2);
71 $lengthInByte = self::getByteLength($length / 2);
74 return $offset . self::removeHexPrefix($lengthInByte) . self::padRight($val);
85 private static function padRight($val)
87 $fillUp = 64 - (strlen($val) % 64);
89 return $val . str_repeat(
"0", $fillUp);
100 public static function decode(
string $hexVal): array
102 if (!is_string($hexVal) || !strlen($hexVal)) {
106 $hexVal = self::removeHexPrefix($hexVal);
109 $dataLength = strlen($hexVal);
110 self::decodeValues($hexVal, 0, 0, $dataLength, 1, $rlpCollection);
111 return $rlpCollection->get();
123 private static function decodeValues(
132 if (is_null($msgData) || strlen($msgData) === 0) {
141 $currentPos = $startPos;
146 while ($currentPos < $endPos) {
148 $prefix = self::getByteValueAtOffsetPos($msgData, $currentPos);
152 if ($prefix < $currentPos) {
156 $length = self::getLengthInByte($msgData, $currentPos);
159 if ($prefix <= 127) {
161 $currentPos = self::processSingleByteItem($msgData, $rlpCollection, $currentPos);
164 elseif ($prefix <= 183 && $length > $prefix - 128) {
169 $currentPos = self::processSingleByteItem($msgData, $rlpCollection, $currentPos);
172 elseif ($prefix <= 191 && $length > $prefix - 183 &&
true) {
178 throw new \Exception(
'Did not run into this yet @Rlp::decodeValues()');
181 elseif ($prefix <= 247 && $length > $prefix - 192) {
188 $currentPos = self::processSingleByteItem($msgData, $rlpCollection, $currentPos);
191 elseif ($prefix <= 255 && $length > $prefix - 247 &&
true) {
195 throw new \Exception(
'Did not run into this yet @Rlp::decodeValues()');
198 throw new \Exception(
'Did not run into this yet @Rlp::decodeValues()');
245 throw new \Exception(
'Invalid RLP ');
259 RLPCollection $rlpCollection,
262 $thisData = substr($msgData, $currentPos);
263 $item =
new RlpItem($thisData);
264 $rlpCollection->add($item);
265 return $item->getCharLength();
274 private static function byteLength(
string $hex)
279 private static function charLength(
string $hex)
281 $hex = self::ensureHexPrefix($hex);
282 return 2 * hexdec($hex);
287 return self::byteLength(substr($msgData, $pos, 64));
299 return self::charLength(substr($msgData, $pos, 64));
313 if (strlen($msgData) < $pos + 64) {
314 throw new \Exception(
'Not ennough data');
316 $lastByte = substr($msgData, $pos + 62, 2);
317 return self::byteLength($lastByte);
331 return (
new EthQ($l, [
'abi' =>
'uint256']))->hexVal();
343 return (strlen($str) - $currentPos + 64) / 2;
352 return $length + 64 - ($length % 64);
static getByteValueAtOffsetPos(string $msgData, int $pos)
const PREF_SELF_CONTAINED
static getByteLength(int $l)
static paddedLength(int $length)
static getByteValueAt(string $msgData, int $pos)
static decode(string $hexVal)
static getStringLengthAt(string $msgData, int $pos)
static encode(string $val)
static processSingleByteItem(string $msgData, RLPCollection $rlpCollection, int $currentPos)
static getLengthInByte(string $str, int $currentPos)