UNPKG

@okxweb3/coin-base

Version:
216 lines 6.62 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.stripHexPrefix = exports.isHexPrefixed = exports.getLength = exports.decode = exports.encode = void 0; const bn_js_1 = __importDefault(require("bn.js")); function encode(input) { if (Array.isArray(input)) { const output = []; for (let i = 0; i < input.length; i++) { output.push(encode(input[i])); } const buf = Buffer.concat(output); return Buffer.concat([encodeLength(buf.length, 192), buf]); } else { const inputBuf = toBuffer(input); return inputBuf.length === 1 && inputBuf[0] < 128 ? inputBuf : Buffer.concat([encodeLength(inputBuf.length, 128), inputBuf]); } } exports.encode = encode; function safeParseInt(v, base) { if (v[0] === '0' && v[1] === '0') { throw new Error('invalid RLP: extra zeros'); } return parseInt(v, base); } function encodeLength(len, offset) { if (len < 56) { return Buffer.from([len + offset]); } else { const hexLength = intToHex(len); const lLength = hexLength.length / 2; const firstByte = intToHex(offset + 55 + lLength); return Buffer.from(firstByte + hexLength, 'hex'); } } function decode(input, stream = false) { if (!input || input.length === 0) { return Buffer.from([]); } const inputBuffer = toBuffer(input); const decoded = _decode(inputBuffer); if (stream) { return decoded; } if (decoded.remainder.length !== 0) { throw new Error('invalid remainder'); } return decoded.data; } exports.decode = decode; function getLength(input) { if (!input || input.length === 0) { return Buffer.from([]); } const inputBuffer = toBuffer(input); const 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) { return firstByte - 0xbf; } else { const llength = firstByte - 0xf6; const length = safeParseInt(inputBuffer.slice(1, llength).toString('hex'), 16); return llength + length; } } exports.getLength = getLength; function _decode(input) { let length, llength, data, innerRemainder, d; const decoded = []; const firstByte = input[0]; if (firstByte <= 0x7f) { return { data: input.slice(0, 1), remainder: input.slice(1), }; } else if (firstByte <= 0xb7) { length = firstByte - 0x7f; 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; 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) { 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 { llength = firstByte - 0xf6; length = safeParseInt(input.slice(1, llength).toString('hex'), 16); const 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'; } exports.isHexPrefixed = isHexPrefixed; function stripHexPrefix(str) { return isHexPrefixed(str) ? str.slice(2) : str; } exports.stripHexPrefix = stripHexPrefix; function intToHex(integer) { if (integer < 0) { throw new Error('Invalid integer as argument, must be unsigned!'); } const hex = integer.toString(16); return hex.length % 2 ? `0${hex}` : hex; } function padToEven(a) { return a.length % 2 ? `0${a}` : a; } function intToBuffer(integer) { const hex = intToHex(integer); return Buffer.from(hex, 'hex'); } 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_js_1.default.isBN(v)) { return Buffer.from(v.toArray()); } else { throw new Error('invalid type'); } } return v; } //# sourceMappingURL=rlp.js.map