-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencode.go
executable file
·212 lines (171 loc) · 5.73 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ethrlp
import "math/big"
var (
EmptyBytes = []byte{0x80}
EmptyArray = []byte{0xC0}
)
// EncodeBool encodes a single bool to RLP
func EncodeBool(input bool) []byte {
if input {
return []byte{0x01}
}
return EmptyBytes
}
// EncodeInt encodes an int64 to RLP
func EncodeInt(input int64) []byte {
return EncodeBigInt(big.NewInt(input))
}
// EncodeUint encodes a uint64 to RLP
func EncodeUint(input uint64) []byte {
return EncodeBigInt(big.NewInt(0).SetUint64(input))
}
// EncodeBigInt encodes a big.Int to RLP
func EncodeBigInt(input *big.Int) []byte {
return EncodeBytes(input.Bytes())
}
// EncodeString encodes a string to RLP
func EncodeString(input string) []byte {
return EncodeBytes([]byte(input))
}
// EncodeByte encodes a single byte to RLP
func EncodeByte(input byte) []byte {
// If input is a single byte in the [0x00, 0x7f] range,
// it itself is the RLP encoding
if input <= 0x7f {
return []byte{input}
}
// If input is a byte in the [0x80, 0xff] range,
// RLP encoding will concatenate 0x81 with the byte
return []byte{
0x81,
input,
}
}
// EncodeBytes encodes a byte array to RLP
func EncodeBytes(input []byte) []byte {
// If the input is a non-value
// (uint(0), []byte{}, string(""), empty pointer...),
// RLP encoding is 0x80
if len(input) == 0 {
return EmptyBytes
}
// If the input is a single value,
// encode it according to single byte rules
if len(input) == 1 {
return EncodeByte(input[0])
}
// If input is more than 55 bytes long,
// the RLP encoding consists of 3 parts:
// - The first part is a single byte with value 0xb7
// plus the length in bytes of the second part
// - The second part is hex value of the length of the string
// - The third part are the actual input bytes
if len(input) > 55 {
return encodeLongBytes(input)
}
// If input is between with 2–55 bytes long,
// the RLP encoding consists of 2 parts:
// - A single byte with value 0x80 plus the length of the second part,
// - The second part are the actual input bytes
return encodeShortBytes(input)
}
// encodeShortBytes encodes an input byte array that is <=55B long
func encodeShortBytes(input []byte) []byte {
result := make([]byte, 1+len(input))
result[0] = 0x80 + byte(len(input))
copy(result[1:], input)
return result
}
// encodeLongBytes encodes an input byte array that is >55B long
func encodeLongBytes(input []byte) []byte {
// The resulting RLP encoding is the entire input bytes array
// along with the single byte denoting the length,
// and the actual length bytes
lengthBytes := convertIntToHexArray(len(input))
result := make([]byte, 1+len(lengthBytes)+len(input))
// The first byte is the sum of 0xb7
// and the length of the incoming next bytes
result[0] = 0xb7 + byte(len(lengthBytes))
ln := len(lengthBytes)
copy(result[1:ln+1], lengthBytes)
copy(result[ln+1:], input)
// The rest of the RLP encoding
// are the actual concatenated bytes
return result
}
// convertIntToHexArray converts the integer value
// to an array of hex values (changes representation)
func convertIntToHexArray(length int) []byte {
// Allocate a byte slice with capacity for 8 bytes (64 bits)
lengthHex := make([]byte, 0, 8)
for length > 0 {
// Extract the least significant byte
lengthHex = append(lengthHex, byte(length&0xFF))
// Shift the number 8 bits to the right (next byte)
length >>= 8
}
// Reverse the byte array
for i, j := 0, len(lengthHex)-1; i < j; i, j = i+1, j-1 {
lengthHex[i], lengthHex[j] = lengthHex[j], lengthHex[i]
}
return lengthHex
}
// EncodeArray encodes an entire input array to RLP.
//
// NOTE: Assumes that the input array elements are RLP-encodings
// (the elements of the array are RLP-encoded prior to this call)
func EncodeArray(input [][]byte) []byte {
// If the input is an empty array,
// the RLP encoding is a single byte 0xc0
if len(input) == 0 {
return EmptyArray
}
// Encoded parts of the input array
encodingResults := make([][]byte, len(input))
// Keep track of the combined length (in bytes)
combinedLength := 0
// For each data point, encode it to RLP
for index, data := range input {
encodingResults[index] = data
combinedLength += len(encodingResults[index])
}
if combinedLength > 55 {
return encodeLongArray(encodingResults, combinedLength)
}
return encodeShortArray(encodingResults, combinedLength)
}
// encodeShortArray encodes an input byte array whose combined length is <=55B long
func encodeShortArray(input [][]byte, combinedLength int) []byte {
// The resulting RLP encoding is the entire encoded input array
// along with the single byte denoting the length
result := make([]byte, 0, 1+combinedLength)
// The first byte is the sum of 0xc0,
// and the combined length of the input bytes
result = append(result, 0xc0+byte(combinedLength)) // length is <= 0xff
// The rest of the RLP encoding
// are the actual concatenated bytes (RLP encoded)
for _, data := range input {
result = append(result, data...)
}
return result
}
// encodeLongArray encodes an input byte array whose combined length is >55B long
func encodeLongArray(input [][]byte, combinedLength int) []byte {
// The resulting RLP encoding is the entire input bytes array
// along with the single byte denoting the length,
// and the actual length bytes
lengthBytes := convertIntToHexArray(combinedLength)
result := make([]byte, 0, 1+len(lengthBytes)+len(input))
// The first byte is the sum of 0xf7
// and the length of the incoming next bytes
result = append(result, 0xf7+byte(len(lengthBytes)))
// The second part of the RLP encoding are the length bytes
// of the input value
result = append(result, lengthBytes...)
// The rest of the RLP encoding
// are the actual concatenated bytes (RLP encoded)
for _, data := range input {
result = append(result, data...)
}
return result
}