From db513ab456bf26f73ec1a966117a39e87b1760ad Mon Sep 17 00:00:00 2001 From: sc0vu Date: Fri, 8 Nov 2019 00:10:18 +0800 Subject: [PATCH] Fix decode single byte and data is array --- src/RLP.php | 8 ++++---- test/unit/RLPTest.php | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/RLP.php b/src/RLP.php index 193e173..5eb6b14 100644 --- a/src/RLP.php +++ b/src/RLP.php @@ -76,12 +76,12 @@ protected function decodeData(string $input) if ($firstByte <= 0x7f) { return [ - 'data' => $firstByte, + 'data' => dechex($firstByte), 'remainder' => mb_substr($input, 2) ]; } elseif ($firstByte <= 0xb7) { $length = $firstByte - 0x7f; - $data = []; + $data = ''; if ($firstByte !== 0x80) { $data = mb_substr($input, 2, ($length - 1) * 2); @@ -113,7 +113,7 @@ protected function decodeData(string $input) ]; } elseif ($firstByte <= 0xf7) { $length = $firstByte - 0xbf; - $innerRemainder = mb_substr($input, 2, $length * 2); + $innerRemainder = mb_substr($input, 2, ($length - 1) * 2); $decoded = []; while (mb_strlen($innerRemainder)) { @@ -127,7 +127,7 @@ protected function decodeData(string $input) ]; } else { $llength = $firstByte - 0xf6; - $hexLength = mb_substr($input, 2, $llength * 2); + $hexLength = mb_substr($input, 2, ($llength - 1) * 2); $decoded = []; if ($hexLength === '00') { diff --git a/test/unit/RLPTest.php b/test/unit/RLPTest.php index bbf957c..d6dbad2 100644 --- a/test/unit/RLPTest.php +++ b/test/unit/RLPTest.php @@ -53,11 +53,14 @@ public function testDecode() $this->assertEquals(199999, hexdec($decoded[0])); $this->assertEquals(1, hexdec($decoded[1])); + $encoded = '0x' . $rlp->encode('0x25'); + $decoded = $rlp->decode($encoded); + $this->assertEquals('25', $decoded); } /** * testValidRlp - * + * * @return void */ public function testValidRlp()