ethers.js/utils/rlp.js

117 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2018-06-13 15:39:39 -04:00
"use strict";
2017-02-24 14:57:46 -05:00
//See: https://github.com/ethereum/wiki/wiki/RLP
2018-06-13 15:39:39 -04:00
Object.defineProperty(exports, "__esModule", { value: true });
2018-06-17 16:47:28 -04:00
var bytes_1 = require("./bytes");
2017-02-24 14:57:46 -05:00
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 = [];
2018-06-13 15:39:39 -04:00
object.forEach(function (child) {
2017-02-24 14:57:46 -05:00
payload = payload.concat(_encode(child));
});
if (payload.length <= 55) {
2018-06-13 15:39:39 -04:00
payload.unshift(0xc0 + payload.length);
2017-02-24 14:57:46 -05:00
return payload;
}
var length = arrayifyInteger(payload.length);
length.unshift(0xf7 + length.length);
return length.concat(payload);
2018-06-13 15:39:39 -04:00
}
var data = Array.prototype.slice.call(bytes_1.arrayify(object));
if (data.length === 1 && data[0] <= 0x7f) {
return data;
}
else if (data.length <= 55) {
data.unshift(0x80 + data.length);
return data;
2017-02-24 14:57:46 -05:00
}
var length = arrayifyInteger(data.length);
length.unshift(0xb7 + length.length);
return length.concat(data);
2017-02-24 14:57:46 -05:00
}
function encode(object) {
2018-06-17 16:47:28 -04:00
return bytes_1.hexlify(_encode(object));
2017-02-24 14:57:46 -05:00
}
2018-06-13 15:39:39 -04:00
exports.encode = encode;
2017-02-28 20:50:53 -05:00
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');
}
}
2018-06-13 15:39:39 -04:00
return { consumed: (1 + length), result: result };
2017-02-28 20:50:53 -05:00
}
2017-02-24 14:57:46 -05:00
// returns { consumed: number, result: Object }
function _decode(data, offset) {
2018-06-13 15:39:39 -04:00
if (data.length === 0) {
throw new Error('invalid rlp data');
}
2017-02-24 14:57:46 -05:00
// 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');
}
2017-02-28 20:50:53 -05:00
return _decodeChildren(data, offset, offset + 1 + lengthLength, lengthLength + length);
2018-06-13 15:39:39 -04:00
}
else if (data[offset] >= 0xc0) {
2017-02-24 14:57:46 -05:00
var length = data[offset] - 0xc0;
if (offset + 1 + length > data.length) {
throw new Error('invalid rlp data');
}
2017-02-28 20:50:53 -05:00
return _decodeChildren(data, offset, offset + 1, length);
2018-06-13 15:39:39 -04:00
}
else if (data[offset] >= 0xb8) {
2017-02-24 14:57:46 -05:00
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');
}
2018-06-17 16:47:28 -04:00
var result = bytes_1.hexlify(data.slice(offset + 1 + lengthLength, offset + 1 + lengthLength + length));
2018-06-13 15:39:39 -04:00
return { consumed: (1 + lengthLength + length), result: result };
}
else if (data[offset] >= 0x80) {
2017-02-24 14:57:46 -05:00
var length = data[offset] - 0x80;
2018-06-13 15:39:39 -04:00
if (offset + 1 + length > data.length) {
2019-09-06 19:09:40 -04:00
throw new Error('invalid rlp data');
2017-02-24 14:57:46 -05:00
}
2018-06-17 16:47:28 -04:00
var result = bytes_1.hexlify(data.slice(offset + 1, offset + 1 + length));
2018-06-13 15:39:39 -04:00
return { consumed: (1 + length), result: result };
2017-02-24 14:57:46 -05:00
}
2018-06-17 16:47:28 -04:00
return { consumed: 1, result: bytes_1.hexlify(data[offset]) };
2017-02-24 14:57:46 -05:00
}
function decode(data) {
2018-06-17 16:47:28 -04:00
var bytes = bytes_1.arrayify(data);
2018-06-13 15:39:39 -04:00
var decoded = _decode(bytes, 0);
if (decoded.consumed !== bytes.length) {
2017-02-24 14:57:46 -05:00
throw new Error('invalid rlp data');
}
return decoded.result;
}
2018-06-13 15:39:39 -04:00
exports.decode = decode;