Git Product home page Git Product logo

rlp's Introduction

rlp

PHP codecov Licensed under the MIT License

Recursive Length Prefix Encoding in PHP.

Install

Set minimum stability to dev

composer require web3p/rlp

Usage

RLP encode:

use Web3p\RLP\RLP;

$rlp = new RLP;
// c483646f67
$encoded = $rlp->encode(['dog']);

// 83646f67
$encoded = $rlp->encode('dog');

RLP decode:

use Web3p\RLP\RLP;
use Web3p\RLP\Types\Str;

$rlp = new RLP;
$encoded = $rlp->encode(['dog']);

// only accept 0x prefixed hex string
$decoded = $rlp->decode('0x' . $encoded);

// show 646f67
echo $decoded[0];

// show dog
echo hex2bin($decoded[0]);

// or you can
echo Str::decodeHex($decoded[0]);

API

Web3p\RLP\RLP

encode

Returns recursive length prefix encoding of given inputs.

encode(mixed $inputs)

Mixed inputs - array of string, integer or numeric string.

Note: output is not zero prefixed.

Example
  • Encode array of string.
use Web3p\RLP\RLP;

$rlp = new RLP;
$encoded = $rlp->encode(['web3p', 'ethereum', 'solidity']);

decode

Returns array recursive length prefix decoding of given data.

decode(string $input)

String input - recursive length prefix encoded string.

Note: output is not zero prefixed.

Example
  • Decode recursive length prefix encoded string.
use Web3p\RLP\RLP;
use Web3p\RLP\Types\Str;

$rlp = new RLP;
$encoded = $rlp->encode(['web3p', 'ethereum', 'solidity']);
$decoded = $rlp->decode('0x' . $encoded);

// echo web3p
echo hex2bin($decoded[0]);

// echo ethereum
echo hex2bin($decoded[1]);

// echo solidity
echo hex2bin($decoded[2]);

// or you can
echo Str::decodeHex($decoded[0]);
echo Str::decodeHex($decoded[1]);
echo Str::decodeHex($decoded[2]);

License

MIT

rlp's People

Contributors

bradez avatar sc0vu avatar vincentlg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rlp's Issues

Issues with 0x00... prefixed addresses

Whe using to 0x00 prefixed addresses it'll return following error from GETH.

rlp: input string too short for common.Address, decoding into (types.DynamicFeeTx).To

I think that's the reason of recently introduced patch to remove leading 0's from strings. Seems that geth client can't decode addresses with 0's removed. So this seems to be in accordance with specification, but geth still can't read the data encoded according to specs when address is shorter than "normal".

Consider string buffer encoding.

I think string in PHP is utf8 bytes. Consider the following code:

$buffer = new Buffer('abcàê߀abc', 'ascii');
echo $buffer->length(); // 15

if you dump you'll see:

  array(15) {
    [0]=>
    int(97)
    [1]=>
    int(98)
    [2]=>
    int(99)
    [3]=>
    int(195)
    [4]=>
    int(160)
    [5]=>
    int(195)
    [6]=>
    int(170)
    [7]=>
    int(195)
    [8]=>
    int(159)
    [9]=>
    int(226)
    [10]=>
    int(130)
    [11]=>
    int(172)
    [12]=>
    int(97)
    [13]=>
    int(98)
    [14]=>
    int(99)
  }

It should be 10 and looks like:

  array(10) {
    [0]=>
    int(97)
    [1]=>
    int(98)
    [2]=>
    int(99)
    [3]=>
    int(224)
    [4]=>
    int(234)
    [5]=>
    int(223)
    [6]=>
    int(172)
    [7]=>
    int(97)
    [8]=>
    int(98)
    [9]=>
    int(99)
  }

In nodejs:

  • Ascii:
new Buffer('abcàê߀abc', 'ascii');
<Buffer 61 62 63 e0 ea df ac 61 62 63> // [97, 98, 99, 224, 234, 223, 172, 97, 98, 99]
  • Utf8 (like the array above)
new Buffer('abcàê߀abc', 'utf8');
<Buffer 61 62 63 c3 a0 c3 aa c3 9f e2 82 ac 61 62 63> // [97, 98, 99, 195, 160, 195, 170, 195, 159, 226, 130, 172, 97, 98, 99]

How to fix it?

Integer 0 should be encoded to ox80?

When I try to serialize transaction object, I found that if nonce is zero, the serialized transaction was wrong.

Because web3p/rlp encode 0 to 0x00, and ethreumjs/rlp encode 0 to 0x80.

Still find more detail in rlp wiki.

rlp: input string too long

rlp: input string too long for uint64, decoding into (types.LegacyTx).Nonce

Failed to sendRawTransaction: transactionData=Array
(
    [from] => 0xCeE4892B23d6944CF5804Ab1038B2963Cd67d73E
    [nonce] => 0xa0
    [to] => 0x754eb48013f5ff2a3bb746b33a878bdd12595ef4
    [gas] => 0x8fde
    [gasPrice] => 0x156ba09800
    [value] => 0x0
    [chainId] => 3
    [data] => 0xa9059cbb00000000000000000000000032e62b619209b3398fdb2931371ba090059d57000000000000000000000000000000000000000000000000056bc75e2d63100000
)

The same code worked OK for the same wallet address, but stopped at the 160th nonce value.
Tested on Ropsten network.

Invalid RLP

transaction: Array
(
    [from] => 0xC14DeDeC7c21Fdd07e319Ef633606bFaaEB51F64
    [nonce] => 0xfa
    [to] => 0x1cb6138c21ac288e3e79e258d70112192c1bbdcc
    [gas] => 0x5208
    [gasPrice] => 0x0713fb3000
    [value] => 0x0de0b6b3a7640000
    [chainId] => 100
)

signedTransaction: 0xf86cfa850713fb3000825208941cb6138c21ac288e3e79e258d70112192c1bbdcc880de0b6b3a764000080eca093268b50f343ded33740d6a479a62a0e19d0ac624ba12d5fdd1056a0f91757a4a0460b277a83ee7c6a1ff81dd34d04da104d8eb6668cd84ef39880cd383ceafe22

Related to #21. Worked OK till some threshold nonce value and stopped then. xdai chain was tested in this case.

Invalid integer buffer

I try to encode number 1000, it should return 8203e8 but return 8103e8.

After traced step by step, I found that the buffer length of 1000 was wrong, it always return 1:

object(Web3p\RLP\Buffer)#323 (2) {
  ["data":protected]=>
  array(1) {
    [0]=>
    int(1000)
  }
  ["encoding":protected]=>
  string(4) "utf8"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.