From 83d8593a3a5519f3480ed4648975dbc74b037a3c Mon Sep 17 00:00:00 2001 From: sc0vu Date: Thu, 7 Nov 2019 22:19:36 +0800 Subject: [PATCH] Remove Buffer usage and clean code --- src/Buffer.php | 312 --------------------------------------- src/RLP.php | 162 +++++++------------- src/types/Numeric.php | 38 +++++ src/types/Str.php | 89 +++++++++++ test/unit/BufferTest.php | 179 ---------------------- test/unit/RLPTest.php | 58 ++++---- 6 files changed, 217 insertions(+), 621 deletions(-) delete mode 100644 src/Buffer.php create mode 100644 src/types/Numeric.php create mode 100644 src/types/Str.php delete mode 100644 test/unit/BufferTest.php diff --git a/src/Buffer.php b/src/Buffer.php deleted file mode 100644 index 680fba6..0000000 --- a/src/Buffer.php +++ /dev/null @@ -1,312 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3p\RLP; - -use InvalidArgumentException; -use ArrayAccess; - -class Buffer implements ArrayAccess -{ - /** - * data - * - * @var array - */ - protected $data = []; - - /** - * encoding - * - * @var string - */ - protected $encoding = ''; - - /** - * construct - * - * @param mixed $data - * @param string $encoding the data encoding - * @return void - */ - public function __construct($data=[], string $encoding='utf8') - { - $this->encoding = strtolower($encoding); - - if ($data) { - $this->data = $this->decodeToData($data); - } - } - - /** - * offsetSet - * - * @param mixed $offset - * @param mixed $value - * @return void - */ - public function offsetSet($offset, $value) - { - if (is_null($offset)) { - $this->data[] = $value; - } else { - $this->data[$offset] = $value; - } - } - - /** - * offsetExists - * - * @param mixed $offset - * @return bool - */ - public function offsetExists($offset) - { - return isset($this->data[$offset]); - } - - /** - * offsetUnset - * - * @param mixed $offset - * @return void - */ - public function offsetUnset($offset) - { - unset($this->data[$offset]); - } - - /** - * offsetGet - * - * @param mixed $offset - * @return mixed - */ - public function offsetGet($offset) - { - return isset($this->data[$offset]) ? $this->data[$offset] : null; - } - - /** - * toString - * - * @param string $encoding - * @return string - */ - public function toString(string $encoding='utf8') - { - $output = ''; - $input = $this->data; - - switch ($encoding) { - case 'hex': - foreach ($input as $data) { - $hex = dechex($data); - - // pad zero - if ((strlen($hex) % 2) !== 0) { - $hex = '0' . $hex; - } - $output .= $hex; - } - break; - case 'ascii': - foreach ($input as $data) { - $output .= chr($data); - } - break; - case 'utf8': - // $length = count($input); - - // for ($i = array_keys($input)[0]; $i < $length; $i += 3) { - // $output .= chr($input[$i]) . chr($input[$i + 1]) . chr($input[$i + 2]); - // } - // change to bytes string - foreach ($input as $data) { - $output .= chr($data); - } - break; - default: - throw new InvalidArgumentException('ToString encoding must be valid.'); - break; - } - return $output; - } - - /** - * length - * - * @return int - */ - public function length() - { - return count($this->data); - } - - /** - * concat - * - * @param mixed $inputs - * @return \RLP\Buffer - */ - public function concat() - { - $inputs = func_get_args(); - - foreach ($inputs as $input) { - if (is_array($input)) { - $input = new Buffer($input); - } - if ($input instanceof Buffer) { - $length = $input->length(); - - for ($i = 0; $i < $length; $i++) { - $this->data[] = $input[$i]; - } - } else { - throw new InvalidArgumentException('Input must be array or Buffer when call concat.'); - } - } - return $this; - } - - /** - * slice - * - * @param int $start - * @param mixed $end - * @return \RLP\Buffer - */ - public function slice(int $start=0, $end=null) - { - if ($end === null) { - $end = $this->length(); - } - if ($end > 0) { - $end -= $start; - } elseif ($end === 0) { - return new Buffer([]); - } - $sliced = array_slice($this->data, $start, $end); - return new Buffer($sliced); - } - - /** - * decodeToData - * - * @param mixed $input - * @return array - */ - protected function decodeToData($input) - { - $output = []; - - if (is_array($input)) { - $output = $this->arrayToData($input); - } elseif (is_int($input)) { - $output = $this->intToData($input); - } elseif (is_numeric($input)) { - $output = $this->numericToData($input); - } elseif (is_string($input)) { - $output = $this->stringToData($input, $this->encoding); - } - return $output; - } - - /** - * arrayToData - * - * @param array $inputs - * @return array - */ - protected function arrayToData(array $inputs) - { - $output = []; - - foreach ($inputs as $input) { - if (is_array($input)) { - // throw exception, maybe support future - // $output[] = $this->arrayToData($input); - throw new InvalidArgumentException('Do not use multidimensional array.'); - } elseif (is_string($input)) { - $output = array_merge($output, $this->stringToData($input, $this->encoding)); - } elseif (is_numeric($input)) { - $output = array_merge($output, $this->numericToData($input)); - } - } - return $output; - } - - /** - * stringToData - * - * @param string $input - * @param string $encoding - * @return array - */ - protected function stringToData(string $input, string $encoding) - { - $output = []; - - switch ($encoding) { - case 'hex': - if (strpos($input, '0x') === 0) { - // hex string - $input = str_replace('0x', '', $input); - } - if (strlen($input) % 2 !== 0) { - $input = '0' . $input; - } - // $splited = str_split($input, 2); - - // foreach ($splited as $data) { - // $output[] = hexdec($data); - // } - $output = array_map('hexdec', str_split($input, 2)); - - break; - case 'ascii': - $output = array_map('ord', str_split($input, 1)); - break; - case 'utf8': - $output = unpack('C*', $input); - break; - default: - throw new InvalidArgumentException('StringToData encoding must be valid.'); - break; - } - return $output; - } - - /** - * numericToData - * - * @param mixed $intput - * @return array - */ - protected function numericToData($intput) - { - $output = (int) $intput; - - return [$output]; - } - - /** - * intToData - * - * @param mixed $intput - * @return array - */ - protected function intToData($input) - { - return array_fill(0, $input, 0); - } -} \ No newline at end of file diff --git a/src/RLP.php b/src/RLP.php index 317c743..193e173 100644 --- a/src/RLP.php +++ b/src/RLP.php @@ -14,123 +14,120 @@ use InvalidArgumentException; use RuntimeException; use Web3p\RLP\Buffer; +use Web3p\RLP\Types\Str; +use Web3p\RLP\Types\Numeric; class RLP { /** * encode - * - * @param mixed $inputs array of string - * @return \Web3p\RLP\Buffer + * + * @param mixed $inputs array of data + * @return string */ public function encode($inputs) { + $output = ''; if (is_array($inputs)) { - $output = new Buffer; - $result = new Buffer; - foreach ($inputs as $input) { - $output->concat($this->encode($input)); + $output .= $this->encode($input); } - return $result->concat($this->encodeLength($output->length(), 192), $output); + $length = mb_strlen($output) / 2; + return $this->encodeLength($length, 192) . $output; } - $output = new Buffer; - $input = $this->toBuffer($inputs); - $length = $input->length(); + $input = $this->encodeInput($inputs); + $length = mb_strlen($input) / 2; - if ($length === 1 && $input[0] < 128) { + if ($length === 1 && hexdec(mb_substr($input, 0, 2)) < 128) { return $input; - } else { - return $output->concat($this->encodeLength($length, 128), $input); } + return $this->encodeLength($length, 128) . $input; } /** * decode * Maybe use bignumber future. - * + * * @param string $input * @return array */ public function decode(string $input) { - // if (!is_string($input)) { - // throw new InvalidArgumentException('Input must be string when call decode.'); - // } - $input = $this->toBuffer($input); + if (strpos($input, '0x') === 0) { + $input = str_replace('0x', '', $input); + } + if (!preg_match('/[a-f0-9]/i', $input)) { + throw new InvalidArgumentException('The input type didn\'t support.'); + } + $input = $this->padToEven($input); $decoded = $this->decodeData($input); - return $decoded['data']; } /** * decodeData - * Maybe use bignumber future. - * - * @param \Web3p\RLP\Buffer $input + * + * @param string $input * @return array */ - protected function decodeData(Buffer $input) + protected function decodeData(string $input) { - $firstByte = $input[0]; - $output = new Buffer; + $firstByte = hexdec(mb_substr($input, 0, 2)); if ($firstByte <= 0x7f) { return [ - 'data' => $input->slice(0, 1), - 'remainder' => $input->slice(1) + 'data' => $firstByte, + 'remainder' => mb_substr($input, 2) ]; } elseif ($firstByte <= 0xb7) { $length = $firstByte - 0x7f; - $data = new Buffer([]); + $data = []; if ($firstByte !== 0x80) { - // for ($i = 1; $i < $length; $i++) { - // $data[] = $input[$i]; - // } - $data = $input->slice(1, $length); + $data = mb_substr($input, 2, ($length - 1) * 2); } - if ($length === 2 && $data[0] < 0x80) { + $firstByteData = hexdec(mb_substr($data, 0, 2)); + if ($length === 2 && $firstByteData < 0x80) { throw new RuntimeException('Byte must be less than 0x80.'); } return [ 'data' => $data, - 'remainder' => $input->slice($length) + 'remainder' => mb_substr($input, $length * 2) ]; } elseif ($firstByte <= 0xbf) { $llength = $firstByte - 0xb6; - $hexLength = $input->slice(1, $llength)->toString('hex'); + $hexLength = mb_substr($input, 2, ($llength - 1) * 2); if ($hexLength === '00') { throw new RuntimeException('Invalid RLP.'); } $length = hexdec($hexLength); - $data = $input->slice($llength, $length + $llength); + $data = mb_substr($input, $llength * 2, ($length + $llength - 1) * 2); - if ($data->length() < $length) { + if (mb_strlen($data) < $length * 2) { throw new RuntimeException('Invalid RLP.'); } return [ 'data' => $data, - 'remainder' => $input->slice($length + $llength) + 'remainder' => mb_substr($input, ($length + $llength) * 2) ]; } elseif ($firstByte <= 0xf7) { $length = $firstByte - 0xbf; - $innerRemainder = $input->slice(1, $length); + $innerRemainder = mb_substr($input, 2, $length * 2); $decoded = []; - while ($innerRemainder->length()) { + while (mb_strlen($innerRemainder)) { $data = $this->decodeData($innerRemainder); $decoded[] = $data['data']; $innerRemainder = $data['remainder']; } return [ 'data' => $decoded, - 'remainder' => $input->slice($length) + 'remainder' => mb_substr($input, $length * 2) ]; } else { $llength = $firstByte - 0xf6; - $hexLength = $input->slice(1, $llength)->toString('hex'); + $hexLength = mb_substr($input, 2, $llength * 2); $decoded = []; if ($hexLength === '00') { @@ -139,23 +136,23 @@ protected function decodeData(Buffer $input) $length = hexdec($hexLength); $totalLength = $llength + $length; - if ($totalLength > $input->length()) { + if ($totalLength * 2 > mb_strlen($input)) { throw new RuntimeException('Invalid RLP: total length is bigger than data length.'); } - $innerRemainder = $input->slice($llength, $totalLength); + $innerRemainder = $hexLength = mb_substr($input, $llength * 2, $totalLength * 2); - if ($innerRemainder->length() === 0) { + if (mb_strlen($innerRemainder) === 0) { throw new RuntimeException('Invalid RLP: list has invalid length.'); } - while ($innerRemainder->length()) { + while (mb_strlen($innerRemainder)) { $data = $this->decodeData($innerRemainder); $decoded[] = $data['data']; $innerRemainder = $data['remainder']; } return [ 'data' => $decoded, - 'remainder' => $input->slice($length) + 'remainder' => mb_substr($input, $length * 2) ]; } } @@ -165,20 +162,16 @@ protected function decodeData(Buffer $input) * * @param int $length * @param int $offset - * @return \Web3p\RLP\Buffer + * @return string */ protected function encodeLength(int $length, int $offset) { - // if (!is_int($length) || !is_int($offset)) { - // throw new InvalidArgumentException('Length and offset must be int when call encodeLength.'); - // } if ($length < 56) { - // var_dump($length, $offset); - return new Buffer(strval($length + $offset)); + return dechex(strval($length + $offset)); } $hexLength = $this->intToHex($length); $firstByte = $this->intToHex($offset + 55 + (strlen($hexLength) / 2)); - return new Buffer(strval($firstByte . $hexLength), 'hex'); + return $firstByte . $hexLength; } /** @@ -189,9 +182,6 @@ protected function encodeLength(int $length, int $offset) */ protected function intToHex(int $value) { - // if (!is_int($value)) { - // throw new InvalidArgumentException('Value must be int when call intToHex.'); - // } $hex = dechex($value); return $this->padToEven($hex); @@ -205,9 +195,6 @@ protected function intToHex(int $value) */ protected function padToEven(string $value) { - // if (!is_string($value)) { - // throw new InvalidArgumentException('Value must be string when call padToEven.'); - // } if ((strlen($value) % 2) !== 0 ) { $value = '0' . $value; } @@ -215,58 +202,23 @@ protected function padToEven(string $value) } /** - * toArray - * Format input to value, deprecated when we have toBuffer. - * + * encodeInput + * Encode input to hex string. + * * @param mixed $input - * @return array - */ - // protected function toArray($input) - // { - // if (is_string($input)) { - // if (strpos($input, '0x') === 0) { - // // hex string - // $value = str_replace('0x', '', $input); - // return $input; - // } else { - // return str_split($input, 1); - // } - // } - // throw new InvalidArgumentException('The input type didn\'t support.'); - // } - - /** - * toBuffer - * Format input to buffer. - * - * @param mixed $input - * @return \Web3p\RLP\Buffer + * @return string */ - protected function toBuffer($input) + protected function encodeInput($input) { if (is_string($input)) { if (strpos($input, '0x') === 0) { - // hex string - // $input = str_replace('0x', '', $input); - return new Buffer($input, 'hex'); + return Str::encode($input, 'hex'); } - return new Buffer(str_split($input, 1)); + return Str::encode($input); } elseif (is_numeric($input)) { - if (!$input || $input < 0) { - return new Buffer([]); - } - if (is_float($input)) { - $input = number_format($input, 0, '', ''); - var_dump($input); - } - $gmpInput = gmp_init($input, 10); - return new Buffer('0x' . gmp_strval($gmpInput, 16), 'hex'); + return Numeric::encode($input); } elseif ($input === null) { - return new Buffer([]); - } elseif (is_array($input)) { - return new Buffer($input); - } elseif ($input instanceof Buffer) { - return $input; + return ''; } throw new InvalidArgumentException('The input type didn\'t support.'); } diff --git a/src/types/Numeric.php b/src/types/Numeric.php new file mode 100644 index 0000000..7b54378 --- /dev/null +++ b/src/types/Numeric.php @@ -0,0 +1,38 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3p\RLP\Types; + +class Numeric +{ + /** + * encode + * + * @param string $input + * @return string encoded hex of input + */ + static function encode(string $input) + { + if (!$input || $input < 0) { + return ''; + } + if (is_float($input)) { + $input = number_format($input, 0, '', ''); + } + $intInput = strval($input); + $output = dechex($intInput); + $outputLen = mb_strlen($output); + if ($outputLen > 0 && $outputLen % 2 !== 0) { + return '0' . $output; + } + return $output; + } +} diff --git a/src/types/Str.php b/src/types/Str.php new file mode 100644 index 0000000..cc56553 --- /dev/null +++ b/src/types/Str.php @@ -0,0 +1,89 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3p\RLP\Types; + +use InvalidArgumentException; + +class Str +{ + /** + * encode + * + * @param string $input + * @param string $encoding + * @return string encoded hex of input + */ + static function encode(string $input, string $encoding='utf8') + { + $output = ''; + switch ($encoding) { + case 'hex': + if (strpos($input, '0x') === 0) { + $input = str_replace('0x', '', $input); + } + $output = $input; + + break; + case 'ascii': + $outputs = array_map('ord', str_split($input, 1)); + foreach ($outputs as $src) { + $output .= dechex($src); + } + break; + case 'utf8': + $outputs = unpack('C*', $input); + foreach ($outputs as $src) { + $output .= dechex($src); + } + break; + default: + throw new InvalidArgumentException('Didn\'t support the encoding.'); + break; + } + $outputLen = mb_strlen($output); + if ($outputLen > 0 && $outputLen % 2 !== 0) { + return '0' . $output; + } + return $output; + } + + /** + * decodeHex + * same with hex2bin + * + * @param string $input + * @return string decoded hex of input + */ + static function decodeHex(string $input) + { + if (strpos($input, '0x') === 0) { + $input = str_replace('0x', '', $input); + } + if (!preg_match('/[a-f0-9]+/i', $input)) { + throw new InvalidArgumentException('Invalid hex string.'); + } + $inputLen = mb_strlen($input); + if ($inputLen > 0 && $inputLen % 2 !== 0) { + $input = '0' . $input; + $inputLen += 1; + } + $output = ''; + $start = 0; + while ($start < $inputLen) { + $hex = mb_substr($input, $start, 2); + $chr = chr(hexdec($hex)); + $output .= $chr; + $start += 2; + } + return $output; + } +} \ No newline at end of file diff --git a/test/unit/BufferTest.php b/test/unit/BufferTest.php deleted file mode 100644 index af9ba35..0000000 --- a/test/unit/BufferTest.php +++ /dev/null @@ -1,179 +0,0 @@ -assertEquals('Hello World', $buffer->toString('ascii')); - $this->assertEquals(11, $buffer->length()); - - $buffer = new Buffer('abcdabcdabcdabcd', 'hex'); - $this->assertEquals('abcdabcdabcdabcd', $buffer->toString('hex')); - $this->assertEquals(8, $buffer->length()); - - $buffer = new Buffer('bcdabcdabcdabcd', 'hex'); - $this->assertEquals('0bcdabcdabcdabcd', $buffer->toString('hex')); - $this->assertEquals(8, $buffer->length()); - - $buffer = new Buffer('我是測試'); - $this->assertEquals('我是測試', $buffer->toString('utf8')); - $this->assertEquals('e68891e698afe6b8ace8a9a6', $buffer->toString('hex')); - $this->assertEquals(12, $buffer->length()); - - try { - $this->assertEquals('我是測試', $buffer->toString('wrongencoding')); - } catch (InvalidArgumentException $e) { - $this->assertEquals('ToString encoding must be valid.', $e->getMessage()); - } - try { - $buffer = new Buffer('Wrong encoding.', 'wrongencoding'); - } catch (InvalidArgumentException $e) { - $this->assertEquals('StringToData encoding must be valid.', $e->getMessage()); - } - } - - /** - * testCreateArrayBuffer - * - * @return void - */ - public function testCreateArrayBuffer() - { - $buffer = new Buffer(['Hello World', 'abcdabcdabcdabcd'], 'ascii'); - $this->assertEquals('Hello Worldabcdabcdabcdabcd', $buffer->toString('ascii')); - $this->assertEquals(27, $buffer->length()); - - $buffer = new Buffer(['Hello World', 'abcdabcdabcdabcd'], 'ascii'); - $this->assertEquals('48656c6c6f20576f726c6461626364616263646162636461626364', $buffer->toString('hex')); - - $buffer = new Buffer([''], 'ascii'); - $this->assertEquals('00', $buffer->toString('hex')); - $this->assertEquals(1, $buffer->length()); - } - - /** - * testCreateIntegerBuffer - * - * @return void - */ - public function testCreateIntegerBuffer() - { - $buffer = new Buffer(10); - $this->assertEquals('00000000000000000000', $buffer->toString('hex')); - $this->assertEquals(10, $buffer->length()); - - $buffer = new Buffer(15); - $this->assertEquals('000000000000000000000000000000', $buffer->toString('hex')); - $this->assertEquals(15, $buffer->length()); - - $buffer = new Buffer(20); - $this->assertEquals('0000000000000000000000000000000000000000', $buffer->toString('hex')); - $this->assertEquals(20, $buffer->length()); - } - - /** - * testCreateMultidimentionalArrayBuffer - * - * @return void - */ - public function testCreateMultidimentionalArrayBuffer() - { - $this->expectException(InvalidArgumentException::class); - - $buffer = new Buffer(['Hello World', 'abcdabcdabcdabcd', ['Hello World', 'abcdabcdabcdabcd']], 'ascii'); - } - - /** - * testCreateNumberBuffer - * - * @return void - */ - public function testCreateNumberBuffer() - { - $buffer = new Buffer('1'); - $this->assertEquals('1', $buffer->toString('hex')); - $this->assertEquals(1, $buffer->length()); - - $buffer = new Buffer(1.56); - $this->assertEquals('1', $buffer->toString('hex')); - $this->assertEquals(1, $buffer->length()); - } - - /** - * testConcate - * - * @return void - */ - public function testConcat() - { - $buffer = new Buffer(['Hello World', 'abcdabcdabcdabcd'], 'ascii'); - $this->assertEquals('Hello Worldabcdabcdabcdabcd', $buffer->toString('ascii')); - $this->assertEquals(27, $buffer->length()); - - $buffer->concat(['Test', 'Yo', 1]); - $this->assertEquals('48656c6c6f20576f726c646162636461626364616263646162636454657374596f01', $buffer->toString('hex')); - $this->assertEquals(34, $buffer->length()); - - $bufferB = new Buffer(['A lo ha'], 'ascii'); - $buffer->concat($bufferB); - $this->assertEquals('48656c6c6f20576f726c646162636461626364616263646162636454657374596f0141206c6f206861', $buffer->toString('hex')); - $this->assertEquals(41, $buffer->length()); - - $bufferC = new Buffer(['Goog'], 'ascii'); - $buffer->concat($bufferC, ['yo']); - $this->assertEquals('48656c6c6f20576f726c646162636461626364616263646162636454657374596f0141206c6f206861476f6f67796f', $buffer->toString('hex')); - $this->assertEquals(47, $buffer->length()); - } - - /** - * testSlice - * - * @return void - */ - public function testSlice() - { - $buffer = new Buffer(['Hello World', 'abcdabcdabcdabcd', 'Test', 'Yo', 1], 'ascii'); - $this->assertEquals('l', $buffer->slice(2, 3)->toString('ascii')); - $this->assertEquals(1, $buffer->slice(2, 3)->length()); - - $this->assertEquals('', $buffer->slice(2, 2)->toString('ascii')); - $this->assertEquals(0, $buffer->slice(2, 2)->length()); - - $this->assertEquals('llo WorldabcdabcdabcdabcdTestYo', $buffer->slice(2, -1)->toString('ascii')); - $this->assertEquals(31, $buffer->slice(2, -1)->length()); - - $this->assertEquals('', $buffer->slice(2, 0)->toString('ascii')); - $this->assertEquals(0, $buffer->slice(2, 0)->length()); - } - - /** - * testArrayAccess - * - * @return void - */ - public function testArrayAccess() - { - $buffer = new Buffer('Hello World', 'ascii'); - $this->assertTrue(isset($buffer[10])); - - $buffer[11] = 65; - $this->assertEquals('Hello WorldA', $buffer->toString('ascii')); - $this->assertEquals(12, $buffer->length()); - - unset($buffer[11]); - $this->assertEquals('Hello World', $buffer->toString('ascii')); - $this->assertEquals(11, $buffer->length()); - } -} \ No newline at end of file diff --git a/test/unit/RLPTest.php b/test/unit/RLPTest.php index 763bb56..bbf957c 100644 --- a/test/unit/RLPTest.php +++ b/test/unit/RLPTest.php @@ -3,6 +3,7 @@ namespace Test\Unit; use Test\TestCase; +use Web3p\RLP\Types\Str; class RLPTest extends TestCase { @@ -16,35 +17,42 @@ public function testEncode() $rlp = $this->rlp; $encoded = $rlp->encode(['dog', 'god', 'cat']); - $this->assertEquals('cc83646f6783676f6483636174', $encoded->toString('hex')); - $this->assertEquals(13, $encoded->length()); + $this->assertEquals('cc83646f6783676f6483636174', $encoded); $encoded = $rlp->encode(['0xabcd', '0xdeff', '0xaaaa']); - $this->assertEquals('c982abcd82deff82aaaa', $encoded->toString('hex')); - $this->assertEquals(10, $encoded->length()); + $this->assertEquals('c982abcd82deff82aaaa', $encoded); + $this->assertEquals('00', $rlp->encode(chr(0))); + $this->assertEquals('01', $rlp->encode(chr(1))); } /** * testDecode - * + * * @return void */ public function testDecode() { $rlp = $this->rlp; - $encoded = '0x' . $rlp->encode(['dog', 'god', 'cat'])->toString('hex'); + $encoded = '0x' . $rlp->encode(['dog', 'god', 'cat']); $decoded = $rlp->decode($encoded); $this->assertEquals(3, count($decoded)); - $this->assertEquals('dog', $decoded[0]->toString('utf8')); - $this->assertEquals('god', $decoded[1]->toString('utf8')); - $this->assertEquals('cat', $decoded[2]->toString('utf8')); + $this->assertEquals('dog', Str::decodeHex(strtoupper($decoded[0]))); + $this->assertEquals('god', Str::decodeHex($decoded[1])); + $this->assertEquals('cat', Str::decodeHex($decoded[2])); - $encoded = '0x' . $rlp->encode(['0xabcd', '0xdeff', '0xaaaa'])->toString('hex'); + $encoded = '0x' . $rlp->encode(['0xabcd', '0xdeff', '0xaaaa']); $decoded = $rlp->decode($encoded); $this->assertEquals(3, count($decoded)); - $this->assertEquals('abcd', $decoded[0]->toString('hex')); - $this->assertEquals('deff', $decoded[1]->toString('hex')); - $this->assertEquals('aaaa', $decoded[2]->toString('hex')); + $this->assertEquals('abcd', $decoded[0]); + $this->assertEquals('deff', $decoded[1]); + $this->assertEquals('aaaa', $decoded[2]); + + $encoded = '0x' . $rlp->encode([199999, 1]); + $decoded = $rlp->decode($encoded); + $this->assertEquals(2, count($decoded)); + $this->assertEquals(199999, hexdec($decoded[0])); + $this->assertEquals(1, hexdec($decoded[1])); + } /** @@ -59,11 +67,11 @@ public function testValidRlp() $this->assertTrue($rlptestJson !== false); $rlptest = json_decode($rlptestJson, true); - + foreach ($rlptest as $test) { $encoded = $rlp->encode($test['in']); - $this->assertEquals($test['out'], $encoded->toString('hex')); + $this->assertEquals($test['out'], $encoded); } } @@ -77,14 +85,14 @@ public function testValidRlp() public function testIssue14() { $rlp = $this->rlp; - $this->assertEquals('c0', $rlp->encode([])->toString('hex')); - $this->assertEquals('80', $rlp->encode(0)->toString('hex')); - $this->assertEquals('80', $rlp->encode(0x0)->toString('hex')); - $this->assertEquals('80', $rlp->encode(-1)->toString('hex')); - $this->assertEquals('80', $rlp->encode(-2)->toString('hex')); - $this->assertEquals('30', $rlp->encode('0')->toString('hex')); - $this->assertEquals('00', $rlp->encode('0x0')->toString('hex')); - $this->assertEquals('80', $rlp->encode(null)->toString('hex')); + $this->assertEquals('c0', $rlp->encode([])); + $this->assertEquals('80', $rlp->encode(0)); + $this->assertEquals('80', $rlp->encode(0x0)); + $this->assertEquals('80', $rlp->encode(-1)); + $this->assertEquals('80', $rlp->encode(-2)); + $this->assertEquals('30', $rlp->encode('0')); + $this->assertEquals('00', $rlp->encode('0x0')); + $this->assertEquals('80', $rlp->encode(null)); } /** @@ -100,11 +108,11 @@ public function testIssue14() // $this->assertTrue($invalidrlptestJson !== false); // $invalidrlptest = json_decode($invalidrlptestJson, true); - + // foreach ($invalidrlptest as $test) { // $encoded = $rlp->encode($test['in']); - // $this->assertEquals($test['out'], $encoded->toString('hex')); + // $this->assertEquals($test['out'], $encoded); // } // } } \ No newline at end of file