Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for proper encoding of addresses (don't remove leading 0s from string) #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ $rlp = new RLP;
$encoded = $rlp->encode(['web3p', 'ethereum', 'solidity']);
```

* Encode ethereum address

There's an ability to force type of specific encoded item. For ethereum addresses, leading zeros can't be removed as these are specified as 20 byte array (not a string). To properly encode it you can pass it as hex_fixed.
```
use Web3p\RLP\RLP;

$rlp = new RLP;
$encoded = $rlp->encode(['web3p', '0x4f15a948f2e2a13731c9aa3cef52cdf8d3e6a120', 'test', '0x4f15a948f2e2a13731c9aa3cef52cdf8d3e6a120'], [1=>'hex_fixed', 3=>'hex_fixed']);
```

#### decode

Returns array recursive length prefix decoding of given data.
Expand Down
41 changes: 23 additions & 18 deletions src/RLP.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ class RLP
* Return RLP encoded of the given inputs.
*
* @param mixed $inputs mixed type of data you want to RLP encode
* @param mixed $types mixed type of data you want to encode, if there's no $types[X] for corresponding $inputs[X] - will be autodetected
* @return string RLP encoded hex string of inputs
*/
public function encode($inputs)
public function encode($inputs, $types = false)
{
$output = '';
if (is_array($inputs)) {
foreach ($inputs as $input) {
$output .= $this->encode($input);
foreach ($inputs as $k=>$input) {
$output .= $this->encode($input, $types[$k] ?? false);
}
$length = mb_strlen($output) / 2;
return $this->encodeLength($length, 192) . $output;
}
$input = $this->encodeInput($inputs);
$input = $this->encodeInput($inputs, $types);
$length = mb_strlen($input) / 2;

// first byte < 0x80
Expand Down Expand Up @@ -247,20 +248,24 @@ protected function padToEven(string $input)
* Main encode function to transform data to hex encoded string.
*
* @param mixed $input data
* @param string type data type to be encoded for not-numeric type
* @return string hex encoded string
*/
protected function encodeInput($input)
{
if (is_string($input)) {
if (strpos($input, '0x') === 0) {
return Str::encode($input, 'hex');
}
return Str::encode($input);
} elseif (is_numeric($input)) {
return Numeric::encode($input);
} elseif ($input === null) {
return '';
}
throw new InvalidArgumentException('The input type didn\'t support.');
}
protected function encodeInput($input, $type = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we submit another PR for this feature?

{
if (is_string($input)) {
if ($type !== false) {
return Str::encode($input, $type);
}
if (strpos($input, '0x') === 0) {
return Str::encode($input, 'hex');
}
return Str::encode($input);
} elseif (is_numeric($input)) {
return Numeric::encode($input);
} elseif ($input === null) {
return '';
}
throw new InvalidArgumentException('The input type didn\'t support.');
}
}
10 changes: 9 additions & 1 deletion src/Types/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ static function encode(string $input, string $encoding='utf8')
{
$output = '';
switch ($encoding) {
case 'hex':
// fixed length hex
case 'hex_fixed':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. How about use type instead of hex_fixed here?

if (strpos($input, '0x') === 0) {
$input = str_replace('0x', '', $input);
}
$output = $input;
break;

case 'hex':
if (strpos($input, '0x') === 0) {
$input = str_replace('0x', '', $input);
}
Expand Down