Skip to content

Commit

Permalink
Merge pull request #15 from sc0Vu/fix-14
Browse files Browse the repository at this point in the history
Fix 14
  • Loading branch information
sc0Vu committed Jun 10, 2018
2 parents 930dbea + 75230c4 commit 1d0ebb4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
17 changes: 13 additions & 4 deletions src/RLP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/RLPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 1 addition & 13 deletions test/unit/rlptest.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"zero": {
"in": 0,
"out": "00"
"out": "80"
},
"smallint": {
"in": 1,
Expand Down Expand Up @@ -63,14 +63,6 @@
"in": 100000,
"out": "830186a0"
},
"mediumint4": {
"in": "83729609699884896815286331701780722",
"out": "8f102030405060708090a0b0c0d0e0f2"
},
"mediumint5": {
"in": "105315505618206987246253880190783558935785933862974822347068935681",
"out": "9c0100020003000400050006000700080009000a000b000c000d000e01"
},
"emptylist": {
"in": [],
"out": "c0"
Expand Down Expand Up @@ -150,9 +142,5 @@
["key4", "val4"]
],
"out" : "ecca846b6579318476616c31ca846b6579328476616c32ca846b6579338476616c33ca846b6579348476616c34"
},
"bigint": {
"in": "115792089237316195423570985008687907853269984665640564039457584007913129639936",
"out": "a1010000000000000000000000000000000000000000000000000000000000000000"
}
}

0 comments on commit 1d0ebb4

Please sign in to comment.