From 961dbb8bc5ad2387d0a1497c2adbaaf4482b94ee Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 10 Jun 2018 13:46:31 +0800 Subject: [PATCH 1/2] Fix 14 1. Return Buffer([]) when input is 0, negative number, null. 2. Add tests for issue 14. --- src/RLP.php | 17 +++++++++++++---- test/unit/RLPTest.php | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/RLP.php b/src/RLP.php index 070b327..317c743 100644 --- a/src/RLP.php +++ b/src/RLP.php @@ -244,16 +244,25 @@ protected function padToEven(string $value) */ protected function toBuffer($input) { - if (is_numeric($input)) { - $gmpInput = gmp_init($input, 10); - return new Buffer('0x' . gmp_strval($gmpInput, 16), 'hex'); - } elseif (is_string($input)) { + if (is_string($input)) { if (strpos($input, '0x') === 0) { // hex string // $input = str_replace('0x', '', $input); return new Buffer($input, 'hex'); } return new Buffer(str_split($input, 1)); + } 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'); + } elseif ($input === null) { + return new Buffer([]); } elseif (is_array($input)) { return new Buffer($input); } elseif ($input instanceof Buffer) { diff --git a/test/unit/RLPTest.php b/test/unit/RLPTest.php index 3159a6a..763bb56 100644 --- a/test/unit/RLPTest.php +++ b/test/unit/RLPTest.php @@ -67,6 +67,26 @@ public function testValidRlp() } } + /** + * testIssue14 + * See: https://github.com/web3p/rlp/issues/14 + * You can find test in: https://github.com/ethereum/wiki/wiki/RLP#examples + * + * @return void + */ + 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')); + } + /** * testInvalidRlp * Try to figure out what invalidrlptest.json is. From 75230c494fbce18b24a1b9b48e6c0b866d535d50 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 10 Jun 2018 13:47:12 +0800 Subject: [PATCH 2/2] Change rlptest.json Remove mediumint and bigint test (loss of precision, the problem also in rlp js library). --- test/unit/rlptest.json | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/test/unit/rlptest.json b/test/unit/rlptest.json index c72bcee..a3a4530 100644 --- a/test/unit/rlptest.json +++ b/test/unit/rlptest.json @@ -33,7 +33,7 @@ }, "zero": { "in": 0, - "out": "00" + "out": "80" }, "smallint": { "in": 1, @@ -63,14 +63,6 @@ "in": 100000, "out": "830186a0" }, - "mediumint4": { - "in": "83729609699884896815286331701780722", - "out": "8f102030405060708090a0b0c0d0e0f2" - }, - "mediumint5": { - "in": "105315505618206987246253880190783558935785933862974822347068935681", - "out": "9c0100020003000400050006000700080009000a000b000c000d000e01" - }, "emptylist": { "in": [], "out": "c0" @@ -150,9 +142,5 @@ ["key4", "val4"] ], "out" : "ecca846b6579318476616c31ca846b6579328476616c32ca846b6579338476616c33ca846b6579348476616c34" - }, - "bigint": { - "in": "115792089237316195423570985008687907853269984665640564039457584007913129639936", - "out": "a1010000000000000000000000000000000000000000000000000000000000000000" } } \ No newline at end of file