barechain / rlp
Recursive Length Prefix Encoding in PHP.
v1.0.1
2021-04-17 12:24 UTC
Requires
- ext-gmp: *
Requires (Dev)
- phpunit/phpunit: ~6.0
README
Recursive Length Prefix Encoding in PHP.
Install
Set minimum stability to dev
composer require barechain/rlp
Usage
RLP encode:
use Barechain\RLP\RLP; $rlp = new RLP; $encodedBuffer = $rlp->encode(['dog']); // to string, encoding: ascii utf8 hex $encodedBuffer->toString($encoding);
RLP decode:
use Barechain\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
Barechain\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 Barechain\RLP\RLP; $rlp = new RLP; $encodedBuffer = $rlp->encode(['barechain', '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 Barechain\RLP\RLP; $rlp = new RLP; $encodedBuffer = $rlp->encode(['barechain', 'ethereum', 'solidity']); $encodedString = $encodedBuffer; $decodedArray = $rlp->decode('0x' . $encodedString); // echo barechain echo $decodedArray[0]->toString('utf8'); // echo ethereum echo $decodedArray[1]->toString('utf8'); // echo solidity echo $decodedArray[2]->toString('utf8');
License
MIT