Skip to content

Commit db513ab

Browse files
committed
Fix decode single byte and data is array
1 parent b614b19 commit db513ab

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/RLP.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ protected function decodeData(string $input)
7676

7777
if ($firstByte <= 0x7f) {
7878
return [
79-
'data' => $firstByte,
79+
'data' => dechex($firstByte),
8080
'remainder' => mb_substr($input, 2)
8181
];
8282
} elseif ($firstByte <= 0xb7) {
8383
$length = $firstByte - 0x7f;
84-
$data = [];
84+
$data = '';
8585

8686
if ($firstByte !== 0x80) {
8787
$data = mb_substr($input, 2, ($length - 1) * 2);
@@ -113,7 +113,7 @@ protected function decodeData(string $input)
113113
];
114114
} elseif ($firstByte <= 0xf7) {
115115
$length = $firstByte - 0xbf;
116-
$innerRemainder = mb_substr($input, 2, $length * 2);
116+
$innerRemainder = mb_substr($input, 2, ($length - 1) * 2);
117117
$decoded = [];
118118

119119
while (mb_strlen($innerRemainder)) {
@@ -127,7 +127,7 @@ protected function decodeData(string $input)
127127
];
128128
} else {
129129
$llength = $firstByte - 0xf6;
130-
$hexLength = mb_substr($input, 2, $llength * 2);
130+
$hexLength = mb_substr($input, 2, ($llength - 1) * 2);
131131
$decoded = [];
132132

133133
if ($hexLength === '00') {

test/unit/RLPTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ public function testDecode()
5353
$this->assertEquals(199999, hexdec($decoded[0]));
5454
$this->assertEquals(1, hexdec($decoded[1]));
5555

56+
$encoded = '0x' . $rlp->encode('0x25');
57+
$decoded = $rlp->decode($encoded);
58+
$this->assertEquals('25', $decoded);
5659
}
5760

5861
/**
5962
* testValidRlp
60-
*
63+
*
6164
* @return void
6265
*/
6366
public function testValidRlp()

0 commit comments

Comments
 (0)