RLP

RLP

It's a instance for ethereum recursive length encoding.

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]);

Summary

Methods
Properties
Constants
encode()
decode()
No public properties found
No constants found
decodeData()
encodeLength()
intToHex()
padToEven()
encodeInput()
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

encode()

encode(array  $inputs) : string

Return RLP encoded of the given inputs.

Parameters

array $inputs

array of data you want to RLP encode

Returns

string —

RLP encoded hex string of inputs

decode()

decode(string  $input) : array

Return RLP decoded of the given hex encoded data.

Parameters

string $input

hex encoded data

Returns

array —

decoded data

decodeData()

decodeData(string  $input) : array

Main function of RLP decode.

Parameters

string $input

hex encoded data

Returns

array —

decoded data

encodeLength()

encodeLength(integer  $length, integer  $offset) : string

Return RLP encoded the length of data.

Parameters

integer $length

length of data

integer $offset

offset of data

Returns

string —

hex encoded of the length

intToHex()

intToHex(integer  $input) : string

Return hex of the given integer.

Parameters

integer $input

integer

Returns

string —

hex encoded of the input

padToEven()

padToEven(string  $input) : string

Pad hex encoded data to even length (add 0).

Parameters

string $input

hex encoded string

Returns

string —

hex encoded string

encodeInput()

encodeInput(mixed  $input) : string

Main encode function to transform data to hex encoded string.

Parameters

mixed $input

data

Returns

string —

hex encoded string