Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+249
@@ -0,0 +1,249 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getLength = exports.decode = exports.encode = void 0;
|
||||
var BN = require("bn.js");
|
||||
/**
|
||||
* 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 input - will be converted to buffer
|
||||
* @returns returns buffer of encoded data
|
||||
**/
|
||||
function encode(input) {
|
||||
if (Array.isArray(input)) {
|
||||
var output = [];
|
||||
for (var i = 0; i < input.length; i++) {
|
||||
output.push(encode(input[i]));
|
||||
}
|
||||
var buf = Buffer.concat(output);
|
||||
return Buffer.concat([encodeLength(buf.length, 192), buf]);
|
||||
}
|
||||
else {
|
||||
var inputBuf = toBuffer(input);
|
||||
return inputBuf.length === 1 && inputBuf[0] < 128
|
||||
? inputBuf
|
||||
: Buffer.concat([encodeLength(inputBuf.length, 128), inputBuf]);
|
||||
}
|
||||
}
|
||||
exports.encode = encode;
|
||||
/**
|
||||
* Parse integers. Check if there is no leading zeros
|
||||
* @param v The value to parse
|
||||
* @param base The base to parse the integer into
|
||||
*/
|
||||
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');
|
||||
}
|
||||
}
|
||||
function decode(input, stream) {
|
||||
if (stream === void 0) { stream = false; }
|
||||
if (!input || input.length === 0) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
var inputBuffer = toBuffer(input);
|
||||
var decoded = _decode(inputBuffer);
|
||||
if (stream) {
|
||||
return decoded;
|
||||
}
|
||||
if (decoded.remainder.length !== 0) {
|
||||
throw new Error('invalid remainder');
|
||||
}
|
||||
return decoded.data;
|
||||
}
|
||||
exports.decode = decode;
|
||||
/**
|
||||
* Get the length of the RLP input
|
||||
* @param input
|
||||
* @returns The length of the input or an empty Buffer if no input
|
||||
*/
|
||||
function getLength(input) {
|
||||
if (!input || input.length === 0) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
var inputBuffer = toBuffer(input);
|
||||
var firstByte = inputBuffer[0];
|
||||
if (firstByte <= 0x7f) {
|
||||
return inputBuffer.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(inputBuffer.slice(1, llength).toString('hex'), 16);
|
||||
return llength + length;
|
||||
}
|
||||
}
|
||||
exports.getLength = getLength;
|
||||
/** Decode an input with RLP */
|
||||
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) {
|
||||
// string is greater than 55 bytes long. A single byte with the value (0xb7 plus the length of the length),
|
||||
// followed by the length, followed by the string
|
||||
llength = firstByte - 0xb6;
|
||||
if (input.length - 1 < llength) {
|
||||
throw new Error('invalid RLP: not enough bytes for string length');
|
||||
}
|
||||
length = safeParseInt(input.slice(1, llength).toString('hex'), 16);
|
||||
if (length <= 55) {
|
||||
throw new Error('invalid RLP: expected string length to be greater than 55');
|
||||
}
|
||||
data = input.slice(llength, length + llength);
|
||||
if (data.length < length) {
|
||||
throw new Error('invalid RLP: not enough bytes for string');
|
||||
}
|
||||
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),
|
||||
};
|
||||
}
|
||||
}
|
||||
/** Check if a string is prefixed by 0x */
|
||||
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;
|
||||
}
|
||||
/** Transform an integer into its hexadecimal value */
|
||||
function intToHex(integer) {
|
||||
if (integer < 0) {
|
||||
throw new Error('Invalid integer as argument, must be unsigned!');
|
||||
}
|
||||
var hex = integer.toString(16);
|
||||
return hex.length % 2 ? "0" + hex : hex;
|
||||
}
|
||||
/** Pad a string to be even */
|
||||
function padToEven(a) {
|
||||
return a.length % 2 ? "0" + a : a;
|
||||
}
|
||||
/** Transform an integer into a Buffer */
|
||||
function intToBuffer(integer) {
|
||||
var hex = intToHex(integer);
|
||||
return Buffer.from(hex, 'hex');
|
||||
}
|
||||
/** Transform anything into a Buffer */
|
||||
function toBuffer(v) {
|
||||
if (!Buffer.isBuffer(v)) {
|
||||
if (typeof v === 'string') {
|
||||
if (isHexPrefixed(v)) {
|
||||
return Buffer.from(padToEven(stripHexPrefix(v)), 'hex');
|
||||
}
|
||||
else {
|
||||
return Buffer.from(v);
|
||||
}
|
||||
}
|
||||
else if (typeof v === 'number' || typeof v === 'bigint') {
|
||||
if (!v) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
else {
|
||||
return intToBuffer(v);
|
||||
}
|
||||
}
|
||||
else if (v === null || v === undefined) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
else if (v instanceof Uint8Array) {
|
||||
return Buffer.from(v);
|
||||
}
|
||||
else if (BN.isBN(v)) {
|
||||
// converts a BN to a Buffer
|
||||
return Buffer.from(v.toArray());
|
||||
}
|
||||
else {
|
||||
throw new Error('invalid type');
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
//# sourceMappingURL=types.js.map
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
{
|
||||
"name": "rlp",
|
||||
"version": "2.2.6",
|
||||
"description": "Recursive Length Prefix Encoding Module",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist/**",
|
||||
"bin/**"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc -p ./tsconfig.prod.json",
|
||||
"prepublishOnly": "npm run test && npm run build",
|
||||
"coverage": "npm run build && nyc --reporter=lcov mocha --reporter=spec --require ts-node/register test/*.spec.ts",
|
||||
"format": "prettier --list-different **/*.{ts,json,md,gql}",
|
||||
"format:fix": "prettier --write **/*.{ts,json,md,gql}",
|
||||
"tslint": "tslint -p ./tsconfig.json -e node_modules/**/* -e **/node_modules/**/* -e dist/**/* **/*.ts",
|
||||
"tslint:fix": "tslint --fix --format stylish -p ./tsconfig.json -e node_modules/**/* -e **/node_modules/**/* -e dist/**/* **/*.ts",
|
||||
"tsc": "tsc -p ./tsconfig.prod.json --noEmit",
|
||||
"lint": "npm run format && npm run tslint && npm run tsc",
|
||||
"lint:fix": "npm run format:fix && npm run tslint:fix && npm run tsc",
|
||||
"unitTests": "mocha --reporter spec --require ts-node/register test/*.spec.ts",
|
||||
"test": "npm run lint && npm run build && npm run unitTests && npm run test:browser",
|
||||
"test:browser": "karma start karma.conf.js",
|
||||
"test:fix": "npm run lint:fix && npm run unitTests"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-push": "npm run lint"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ethereumjs/rlp.git"
|
||||
},
|
||||
"keywords": [
|
||||
"rlp",
|
||||
"ethereum"
|
||||
],
|
||||
"author": {
|
||||
"name": "martin becze",
|
||||
"email": "mjbecze@gmail.com"
|
||||
},
|
||||
"contributors": [
|
||||
"Alex Beregszaszi <alex@rtfs.hu>",
|
||||
"Holger Drewes <Holger.Drewes@gmail.com>"
|
||||
],
|
||||
"license": "MPL-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/ethereumjs/rlp/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"bn.js": "^4.11.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ethereumjs/config-nyc": "^1.0.0",
|
||||
"@ethereumjs/config-prettier": "^1.0.1",
|
||||
"@ethereumjs/config-tsc": "^1.0.2",
|
||||
"@ethereumjs/config-tslint": "^1.0.0",
|
||||
"@types/bn.js": "^4.11.3",
|
||||
"@types/mocha": "^5.2.5",
|
||||
"@types/node": "^10.12.2",
|
||||
"husky": "^2.1.0",
|
||||
"karma": "^5.0.2",
|
||||
"karma-chrome-launcher": "^2.0.0",
|
||||
"karma-firefox-launcher": "^1.0.0",
|
||||
"karma-mocha": "^2.0.0",
|
||||
"karma-typescript": "^4.1.1",
|
||||
"mocha": "7.1.2",
|
||||
"nyc": "^15.0.1",
|
||||
"prettier": "^1.15.2",
|
||||
"ts-node": "^8.9.1",
|
||||
"tslint": "^5.11.0",
|
||||
"typescript": "^3.8.3",
|
||||
"typestrict": "^1.0.1"
|
||||
},
|
||||
"bin": {
|
||||
"rlp": "./bin/rlp"
|
||||
}
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/rlp/-/rlp-2.2.6.tgz"
|
||||
,"_integrity": "sha512-HAfAmL6SDYNWPUOJNrM500x4Thn4PZsEy5pijPh40U9WfNk0z15hUYzO9xVIMAdIHdFtD8CBDHd75Td1g36Mjg=="
|
||||
,"_from": "rlp@^2.2.3"
|
||||
}
|
||||
Reference in new issue
Block a user