forked from ethereumjs/rlp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
298 lines (257 loc) · 7.24 KB
/
Copy pathindex.js
File metadata and controls
298 lines (257 loc) · 7.24 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const assert = require('assert');
const Buffer = require('safe-buffer').Buffer;
const BN = require('bn.js');
const JAVA_LONG_MAX = new BN("9223372036854775807");
const MASK = new BN("4294967295");
const INT_MASK = MASK;
const SHORT_MASK = new BN("0xFFFF", 16);
//*--- AION LONG ---*/
function AionLong (n) {
"use strict"
const _this = this;
if (!(_this instanceof AionLong)) {
// allow constructor call without new
return new AionLong(n);
}
if (n === null || typeof n === "undefined" || !('toArray' in n)) {
throw new Error("unsupported input type");
}
if (new BN(n.toArray()).cmp(JAVA_LONG_MAX) > 0) {
throw new Error("violated upper bound");
}
this.buf = n.toArray();
}
AionLong.prototype._aionLong = true;
AionLong.prototype.toArray = function() {
return this.buf;
}
AionLong.isAionLong = (a) => {
if (a instanceof AionLong) {
return true;
}
return a !== null &&
typeof a === 'object' &&
a._aionLong === true;
}
AionLong._aionEncodeLong = (bn) => {
const top = bn.shrn(32).and(MASK);
const bottom = bn.and(MASK);
const buf = Buffer.alloc(8);
buf.writeUInt32BE(top.toNumber(), 0);
buf.writeUInt32BE(bottom.toNumber(), 4);
return buf;
}
AionLong.aionEncodeLong = (aionLong) => {
const bn = new BN(aionLong.buf);
if (bn.and(INT_MASK).cmp(bn) == 0) {
return Buffer.from(bn.toArray());
}
// otherwise this must be a long
return AionLong._aionEncodeLong(bn);
}
exports.AionLong = AionLong;
/**
* RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP
* This function takes in a data, convert it to buffer if not, and a length for recursion
*
* @param {Buffer,String,Integer,Array} data - will be converted to buffer
* @returns {Buffer} - returns buffer of encoded data
**/
exports.encode = function (input) {
if (input instanceof Array) {
var output = []
for (var i = 0; i < input.length; i++) {
output.push(exports.encode(input[i]))
}
var buf = Buffer.concat(output)
return Buffer.concat([encodeLength(buf.length, 192), buf])
} else {
input = toBuffer(input)
if (input.length === 1 && input[0] < 128) {
return input
} else {
return Buffer.concat([encodeLength(input.length, 128), input])
}
}
}
function safeParseInt (v, base) {
if (v.slice(0, 2) === '00') {
throw (new Error('invalid RLP: extra zeros'))
}
return parseInt(v, base)
}
function encodeLength (len, offset) {
if (len < 56) {
return Buffer.from([len + offset])
} else {
var hexLength = intToHex(len)
var lLength = hexLength.length / 2
var firstByte = intToHex(offset + 55 + lLength)
return Buffer.from(firstByte + hexLength, 'hex')
}
}
/**
* RLP Decoding based on: {@link https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP|RLP}
* @param {Buffer,String,Integer,Array} data - will be converted to buffer
* @returns {Array} - returns decode Array of Buffers containg the original message
**/
exports.decode = function (input, stream) {
if (!input || input.length === 0) {
return Buffer.from([])
}
input = toBuffer(input)
var decoded = _decode(input)
if (stream) {
return decoded
}
assert.equal(decoded.remainder.length, 0, 'invalid remainder')
return decoded.data
}
exports.getLength = function (input) {
if (!input || input.length === 0) {
return Buffer.from([])
}
input = toBuffer(input)
var firstByte = input[0]
if (firstByte <= 0x7f) {
return input.length
} else if (firstByte <= 0xb7) {
return firstByte - 0x7f
} else if (firstByte <= 0xbf) {
return firstByte - 0xb6
} else if (firstByte <= 0xf7) {
// a list between 0-55 bytes long
return firstByte - 0xbf
} else {
// a list over 55 bytes long
var llength = firstByte - 0xf6
var length = safeParseInt(input.slice(1, llength).toString('hex'), 16)
return llength + length
}
}
function _decode (input) {
var length, llength, data, innerRemainder, d
var decoded = []
var firstByte = input[0]
if (firstByte <= 0x7f) {
// a single byte whose value is in the [0x00, 0x7f] range, that byte is its own RLP encoding.
return {
data: input.slice(0, 1),
remainder: input.slice(1)
}
} else if (firstByte <= 0xb7) {
// string is 0-55 bytes long. A single byte with value 0x80 plus the length of the string followed by the string
// The range of the first byte is [0x80, 0xb7]
length = firstByte - 0x7f
// set 0x80 null to 0
if (firstByte === 0x80) {
data = Buffer.from([])
} else {
data = input.slice(1, length)
}
if (length === 2 && data[0] < 0x80) {
throw new Error('invalid rlp encoding: byte must be less 0x80')
}
return {
data: data,
remainder: input.slice(length)
}
} else if (firstByte <= 0xbf) {
llength = firstByte - 0xb6
length = safeParseInt(input.slice(1, llength).toString('hex'), 16)
data = input.slice(llength, length + llength)
if (data.length < length) {
throw (new Error('invalid RLP'))
}
return {
data: data,
remainder: input.slice(length + llength)
}
} else if (firstByte <= 0xf7) {
// a list between 0-55 bytes long
length = firstByte - 0xbf
innerRemainder = input.slice(1, length)
while (innerRemainder.length) {
d = _decode(innerRemainder)
decoded.push(d.data)
innerRemainder = d.remainder
}
return {
data: decoded,
remainder: input.slice(length)
}
} else {
// a list over 55 bytes long
llength = firstByte - 0xf6
length = safeParseInt(input.slice(1, llength).toString('hex'), 16)
var totalLength = llength + length
if (totalLength > input.length) {
throw new Error('invalid rlp: total length is larger than the data')
}
innerRemainder = input.slice(llength, totalLength)
if (innerRemainder.length === 0) {
throw new Error('invalid rlp, List has a invalid length')
}
while (innerRemainder.length) {
d = _decode(innerRemainder)
decoded.push(d.data)
innerRemainder = d.remainder
}
return {
data: decoded,
remainder: input.slice(totalLength)
}
}
}
function isHexPrefixed (str) {
return str.slice(0, 2) === '0x'
}
// Removes 0x from a given String
function stripHexPrefix (str) {
if (typeof str !== 'string') {
return str
}
return isHexPrefixed(str) ? str.slice(2) : str
}
function intToHex (i) {
var hex = i.toString(16)
if (hex.length % 2) {
hex = '0' + hex
}
return hex
}
function padToEven (a) {
if (a.length % 2) a = '0' + a
return a
}
function intToBuffer (i) {
var hex = intToHex(i)
return Buffer.from(hex, 'hex')
}
function toBuffer (v) {
if (!Buffer.isBuffer(v)) {
if (typeof v === 'string') {
if (isHexPrefixed(v)) {
v = Buffer.from(padToEven(stripHexPrefix(v)), 'hex')
} else {
v = Buffer.from(v)
}
} else if (AionLong.isAionLong(v)) {
v = AionLong.aionEncodeLong(v);
} else if (typeof v === 'number') {
if (!v) {
v = Buffer.from([])
} else {
v = intToBuffer(v)
}
} else if (v === null || v === undefined) {
v = Buffer.from([])
} else if (v.toArray) {
// converts a BN to a Buffer
v = Buffer.from(v.toArray())
} else {
throw new Error('invalid type')
}
}
return v
}