@@ -15,28 +15,38 @@ composer require web3p/rlp
15
15
# Usage
16
16
17
17
RLP encode:
18
+
18
19
``` php
19
20
use Web3p\RLP\RLP;
20
21
21
22
$rlp = new RLP;
22
- $encodedBuffer = $rlp->encode(['dog']);
23
+ // c483646f67
24
+ $encoded = $rlp->encode(['dog']);
23
25
24
- // to string, encoding: ascii utf8 hex
25
- $encodedBuffer->toString($encoding );
26
+ // 83646f67
27
+ $encoded = $rlp->encode('dog' );
26
28
```
27
29
28
30
RLP decode:
31
+
29
32
``` php
30
33
use Web3p\RLP\RLP;
34
+ use Web3p\RLP\Types\Str;
31
35
32
36
$rlp = new RLP;
33
- $encodedBuffer = $rlp->encode(['dog']);
37
+ $encoded = $rlp->encode(['dog']);
34
38
35
39
// only accept 0x prefixed hex string
36
- $decodedArray = $rlp->decode('0x' . $encodedBuffer->toString('hex'));
40
+ $decoded = $rlp->decode('0x' . $encoded);
41
+
42
+ // show 646f67
43
+ echo $decoded[0];
37
44
38
45
// show dog
39
- echo $decodedArray[0]->toString('utf8');
46
+ echo hex2bin($decoded[0]);
47
+
48
+ // or you can
49
+ echo Str::decodeHex($decoded[0]);
40
50
```
41
51
42
52
# API
@@ -45,12 +55,14 @@ echo $decodedArray[0]->toString('utf8');
45
55
46
56
#### encode
47
57
48
- Returns recursive length prefix encoding of given data .
58
+ Returns recursive length prefix encoding of given inputs .
49
59
50
60
` encode(mixed $inputs) `
51
61
52
62
Mixed inputs - array of string, integer or numeric string.
53
63
64
+ > Note: output is not zero prefixed.
65
+
54
66
###### Example
55
67
56
68
* Encode array of string.
@@ -59,9 +71,7 @@ Mixed inputs - array of string, integer or numeric string.
59
71
use Web3p\RLP\RLP;
60
72
61
73
$rlp = new RLP;
62
- $encodedBuffer = $rlp->encode(['web3p', 'ethereum', 'solidity']);
63
- $encodedString = $encodedBuffer->toString('hex');
64
-
74
+ $encoded = $rlp->encode(['web3p', 'ethereum', 'solidity']);
65
75
```
66
76
67
77
#### decode
@@ -72,26 +82,33 @@ Returns array recursive length prefix decoding of given data.
72
82
73
83
String input - recursive length prefix encoded string.
74
84
85
+ > Note: output is not zero prefixed.
86
+
75
87
###### Example
76
88
77
89
* Decode recursive length prefix encoded string.
78
90
79
91
``` php
80
92
use Web3p\RLP\RLP;
93
+ use Web3p\RLP\Types\Str;
81
94
82
95
$rlp = new RLP;
83
- $encodedBuffer = $rlp->encode(['web3p', 'ethereum', 'solidity']);
84
- $encodedString = $encodedBuffer->toString('hex');
85
- $decodedArray = $rlp->decode('0x' . $encodedString);
96
+ $encoded = $rlp->encode(['web3p', 'ethereum', 'solidity']);
97
+ $decoded = $rlp->decode('0x' . $encoded);
86
98
87
99
// echo web3p
88
- echo $decodedArray [0]->toString('utf8' );
100
+ echo hex2bin($decoded [0]);
89
101
90
102
// echo ethereum
91
- echo $decodedArray [1]->toString('utf8' );
103
+ echo hex2bin($decoded [1]);
92
104
93
105
// echo solidity
94
- echo $decodedArray[2]->toString('utf8');
106
+ echo hex2bin($decoded[2]);
107
+
108
+ // or you can
109
+ echo Str::decodeHex($decoded[0]);
110
+ echo Str::decodeHex($decoded[1]);
111
+ echo Str::decodeHex($decoded[2]);
95
112
```
96
113
97
114
# License
0 commit comments