minter/php-rlp

Recursive Length Prefix Encoding in PHP.

v1.0.0 2020-03-11 09:01 UTC

This package is auto-updated.

Last update: 2024-12-11 20:22:51 UTC


README

Recursive Length Prefix Encoding in PHP.

Build Status codecov Licensed under the MIT License

Install

Set minimum stability to dev

composer require web3p/rlp

Usage

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

// show dog
echo $decodedArray[0]->toString('utf8');

API

Web3p\RLP\RLP

encode

Returns recursive length prefix encoding of given data.

encode(mixed $inputs)

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

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

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

decode

Returns array recursive length prefix decoding of given data.

decode(string $input)

String input - recursive length prefix encoded string.

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

$rlp = new RLP;
$encodedBuffer = $rlp->encode(['web3p', 'ethereum', 'solidity']);
$encodedString = $encodedBuffer;
$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');

License

MIT