143 lines
3.9 KiB
JavaScript
143 lines
3.9 KiB
JavaScript
//See: https://github.com/ethereum/wiki/wiki/RLP
|
|
|
|
var convert = require('./convert.js');
|
|
|
|
function arrayifyInteger(value) {
|
|
var result = [];
|
|
while (value) {
|
|
result.unshift(value & 0xff);
|
|
value >>= 8;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function unarrayifyInteger(data, offset, length) {
|
|
var result = 0;
|
|
for (var i = 0; i < length; i++) {
|
|
result = (result * 256) + data[offset + i];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function _encode(object) {
|
|
if (Array.isArray(object)) {
|
|
var payload = [];
|
|
object.forEach(function(child) {
|
|
payload = payload.concat(_encode(child));
|
|
});
|
|
|
|
if (payload.length <= 55) {
|
|
payload.unshift(0xc0 + payload.length)
|
|
return payload;
|
|
}
|
|
|
|
var length = arrayifyInteger(payload.length);
|
|
length.unshift(0xf7 + length.length);
|
|
|
|
return length.concat(payload);
|
|
|
|
} else {
|
|
object = [].slice.call(convert.arrayify(object));
|
|
|
|
if (object.length === 1 && object[0] <= 0x7f) {
|
|
return object;
|
|
|
|
} else if (object.length <= 55) {
|
|
object.unshift(0x80 + object.length);
|
|
return object
|
|
}
|
|
|
|
var length = arrayifyInteger(object.length);
|
|
length.unshift(0xb7 + length.length);
|
|
|
|
return length.concat(object);
|
|
}
|
|
}
|
|
|
|
function encode(object) {
|
|
return convert.hexlify(_encode(object));
|
|
}
|
|
|
|
function _decodeChildren(data, offset, childOffset, length) {
|
|
var result = [];
|
|
|
|
while (childOffset < offset + 1 + length) {
|
|
var decoded = _decode(data, childOffset);
|
|
|
|
result.push(decoded.result);
|
|
|
|
childOffset += decoded.consumed;
|
|
if (childOffset > offset + 1 + length) {
|
|
throw new Error('invalid rlp');
|
|
}
|
|
}
|
|
|
|
return {consumed: (1 + length), result: result};
|
|
}
|
|
|
|
// returns { consumed: number, result: Object }
|
|
function _decode(data, offset) {
|
|
if (data.length === 0) { throw new Error('invalid rlp data'); }
|
|
|
|
// Array with extra length prefix
|
|
if (data[offset] >= 0xf8) {
|
|
var lengthLength = data[offset] - 0xf7;
|
|
if (offset + 1 + lengthLength > data.length) {
|
|
throw new Error('too short');
|
|
}
|
|
|
|
var length = unarrayifyInteger(data, offset + 1, lengthLength);
|
|
if (offset + 1 + lengthLength + length > data.length) {
|
|
throw new Error('to short');
|
|
}
|
|
|
|
return _decodeChildren(data, offset, offset + 1 + lengthLength, lengthLength + length);
|
|
|
|
} else if (data[offset] >= 0xc0) {
|
|
var length = data[offset] - 0xc0;
|
|
if (offset + 1 + length > data.length) {
|
|
throw new Error('invalid rlp data');
|
|
}
|
|
|
|
return _decodeChildren(data, offset, offset + 1, length);
|
|
|
|
} else if (data[offset] >= 0xb8) {
|
|
var lengthLength = data[offset] - 0xb7;
|
|
if (offset + 1 + lengthLength > data.length) {
|
|
throw new Error('invalid rlp data');
|
|
}
|
|
|
|
var length = unarrayifyInteger(data, offset + 1, lengthLength);
|
|
if (offset + 1 + lengthLength + length > data.length) {
|
|
throw new Error('invalid rlp data');
|
|
}
|
|
|
|
var result = convert.hexlify(data.slice(offset + 1 + lengthLength, offset + 1 + lengthLength + length));
|
|
return { consumed: (1 + lengthLength + length), result: result }
|
|
|
|
} else if (data[offset] >= 0x80) {
|
|
var length = data[offset] - 0x80;
|
|
if (offset + 1 + length > data.offset) {
|
|
throw new Error('invlaid rlp data');
|
|
}
|
|
|
|
var result = convert.hexlify(data.slice(offset + 1, offset + 1 + length));
|
|
return { consumed: (1 + length), result: result }
|
|
}
|
|
return { consumed: 1, result: convert.hexlify(data[offset]) };
|
|
}
|
|
|
|
function decode(data) {
|
|
data = convert.arrayify(data);
|
|
var decoded = _decode(data, 0);
|
|
if (decoded.consumed !== data.length) {
|
|
throw new Error('invalid rlp data');
|
|
}
|
|
return decoded.result;
|
|
}
|
|
|
|
module.exports = {
|
|
encode: encode,
|
|
decode: decode,
|
|
}
|