ethers.js/packages/rlp/lib.esm/index.js

120 lines
4.5 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
//See: https://github.com/ethereum/wiki/wiki/RLP
2020-03-21 19:48:22 +03:00
import { arrayify, hexlify, isBytesLike } from "@ethersproject/bytes";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-15 01:48:48 +03:00
function arrayifyInteger(value) {
2019-11-20 12:57:38 +03:00
const result = [];
2019-05-15 01:48:48 +03:00
while (value) {
result.unshift(value & 0xff);
value >>= 8;
}
return result;
}
function unarrayifyInteger(data, offset, length) {
let result = 0;
for (let i = 0; i < length; i++) {
2019-05-15 01:48:48 +03:00
result = (result * 256) + data[offset + i];
}
return result;
}
function _encode(object) {
if (Array.isArray(object)) {
let payload = [];
2019-05-15 01:48:48 +03:00
object.forEach(function (child) {
payload = payload.concat(_encode(child));
2019-05-15 01:48:48 +03:00
});
if (payload.length <= 55) {
payload.unshift(0xc0 + payload.length);
return payload;
2019-05-15 01:48:48 +03:00
}
2019-11-20 12:57:38 +03:00
const length = arrayifyInteger(payload.length);
length.unshift(0xf7 + length.length);
return length.concat(payload);
2019-05-15 01:48:48 +03:00
}
2020-03-21 19:48:22 +03:00
if (!isBytesLike(object)) {
logger.throwArgumentError("RLP object must be BytesLike", "object", object);
}
2019-11-20 12:57:38 +03:00
const data = Array.prototype.slice.call(arrayify(object));
2019-05-15 01:48:48 +03:00
if (data.length === 1 && data[0] <= 0x7f) {
return data;
}
else if (data.length <= 55) {
data.unshift(0x80 + data.length);
return data;
}
2019-11-20 12:57:38 +03:00
const length = arrayifyInteger(data.length);
2019-05-15 01:48:48 +03:00
length.unshift(0xb7 + length.length);
return length.concat(data);
}
export function encode(object) {
return hexlify(_encode(object));
2019-05-15 01:48:48 +03:00
}
function _decodeChildren(data, offset, childOffset, length) {
2019-11-20 12:57:38 +03:00
const result = [];
2019-05-15 01:48:48 +03:00
while (childOffset < offset + 1 + length) {
2019-11-20 12:57:38 +03:00
const decoded = _decode(data, childOffset);
2019-05-15 01:48:48 +03:00
result.push(decoded.result);
childOffset += decoded.consumed;
if (childOffset > offset + 1 + length) {
2020-03-21 19:48:22 +03:00
logger.throwError("child data too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
}
return { consumed: (1 + length), result: result };
}
// returns { consumed: number, result: Object }
function _decode(data, offset) {
if (data.length === 0) {
2020-03-21 19:48:22 +03:00
logger.throwError("data too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
// Array with extra length prefix
if (data[offset] >= 0xf8) {
2019-11-20 12:57:38 +03:00
const lengthLength = data[offset] - 0xf7;
2019-05-15 01:48:48 +03:00
if (offset + 1 + lengthLength > data.length) {
2020-03-21 19:48:22 +03:00
logger.throwError("data short segment too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
2019-11-20 12:57:38 +03:00
const length = unarrayifyInteger(data, offset + 1, lengthLength);
if (offset + 1 + lengthLength + length > data.length) {
2020-03-21 19:48:22 +03:00
logger.throwError("data long segment too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
return _decodeChildren(data, offset, offset + 1 + lengthLength, lengthLength + length);
2019-05-15 01:48:48 +03:00
}
else if (data[offset] >= 0xc0) {
2019-11-20 12:57:38 +03:00
const length = data[offset] - 0xc0;
if (offset + 1 + length > data.length) {
2020-03-21 19:48:22 +03:00
logger.throwError("data array too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
return _decodeChildren(data, offset, offset + 1, length);
2019-05-15 01:48:48 +03:00
}
else if (data[offset] >= 0xb8) {
2019-11-20 12:57:38 +03:00
const lengthLength = data[offset] - 0xb7;
2019-05-15 01:48:48 +03:00
if (offset + 1 + lengthLength > data.length) {
2020-03-21 19:48:22 +03:00
logger.throwError("data array too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
2019-11-20 12:57:38 +03:00
const length = unarrayifyInteger(data, offset + 1, lengthLength);
if (offset + 1 + lengthLength + length > data.length) {
2020-03-21 19:48:22 +03:00
logger.throwError("data array too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
2019-11-20 12:57:38 +03:00
const result = hexlify(data.slice(offset + 1 + lengthLength, offset + 1 + lengthLength + length));
return { consumed: (1 + lengthLength + length), result: result };
2019-05-15 01:48:48 +03:00
}
else if (data[offset] >= 0x80) {
2019-11-20 12:57:38 +03:00
const length = data[offset] - 0x80;
if (offset + 1 + length > data.length) {
2020-03-21 19:48:22 +03:00
logger.throwError("data too short", Logger.errors.BUFFER_OVERRUN, {});
2019-05-15 01:48:48 +03:00
}
2019-11-20 12:57:38 +03:00
const result = hexlify(data.slice(offset + 1, offset + 1 + length));
return { consumed: (1 + length), result: result };
2019-05-15 01:48:48 +03:00
}
return { consumed: 1, result: hexlify(data[offset]) };
2019-05-15 01:48:48 +03:00
}
export function decode(data) {
2019-11-20 12:57:38 +03:00
const bytes = arrayify(data);
const decoded = _decode(bytes, 0);
2019-05-15 01:48:48 +03:00
if (decoded.consumed !== bytes.length) {
2020-03-21 19:48:22 +03:00
logger.throwArgumentError("invalid rlp data", "data", data);
2019-05-15 01:48:48 +03:00
}
return decoded.result;
}