Recursive Length Prefix Encoding in PHP.
Set minimum stability to dev
composer require web3p/rlp
RLP encode:
use Web3p\RLP\RLP;
$rlp = new RLP;
$encodedBuffer = $rlp->encode(['dog']);
// to string, encoding: ascii utf8 hex
$encodedBuffer->toString($encoding);
RLP decode:
use Web3p\RLP\RLP;
$rlp = new RLP;
$encodedBuffer = $rlp->encode(['dog']);
// only accept 0x prefixed hex string
$decodedArray = $rlp->decode('0x' . $encodedBuffer->toString('hex'));
// show dog
echo $decodedArray[0]->toString('utf8');
Returns recursive length prefix encoding of given data.
encode(mixed $inputs)
Mixed inputs - array of string, integer or numeric string.
- Encode array of string.
use Web3p\RLP\RLP;
$rlp = new RLP;
$encodedBuffer = $rlp->encode(['web3p', 'ethereum', 'solidity']);
$encodedString = $encodedBuffer->toString('hex');
Returns array recursive length prefix decoding of given data.
decode(string $input)
String input - recursive length prefix encoded string.
- Decode recursive length prefix encoded string.
use Web3p\RLP\RLP;
$rlp = new RLP;
$encodedBuffer = $rlp->encode(['web3p', 'ethereum', 'solidity']);
$encodedString = $encodedBuffer->toString('hex');
$decodedArray = $rlp->decode('0x' . $encodedString);
// echo web3p
echo $decodedArray[0]->toString('utf8');
// echo ethereum
echo $decodedArray[1]->toString('utf8');
// echo solidity
echo $decodedArray[2]->toString('utf8');
Thank you for considering to help out with the source code!
Pull requests and issues are always welcome.
MIT