UNPKG

@lodestar/prover

Version:

A Typescript implementation of the Ethereum Consensus light client

92 lines 3.36 kB
import { isTruthy } from "./assertion.js"; export function numberToHex(num) { return "0x" + num.toString(16); } export function hexToNumber(num) { return num.startsWith("0x") ? parseInt(num.slice(2), 16) : parseInt(num, 16); } export function hexToBigInt(num) { return num.startsWith("0x") ? BigInt(num) : BigInt(`0x${num}`); } export function bigIntToHex(num) { return `0x${num.toString(16)}`; } export function bufferToHex(buffer) { return "0x" + Buffer.from(buffer).toString("hex"); } export function hexToBuffer(val) { const hexWithEvenLength = val.length % 2 ? `0${val}` : val; return Buffer.from(hexWithEvenLength.replace("0x", ""), "hex"); } export function padLeft(v, length) { const buf = Buffer.alloc(length); Buffer.from(v).copy(buf, length - v.length); if (Buffer.isBuffer(v)) return buf; return Uint8Array.from(buf); } export function headerDataFromELBlock(blockInfo) { return { parentHash: blockInfo.parentHash, uncleHash: blockInfo.sha3Uncles, coinbase: blockInfo.miner, stateRoot: blockInfo.stateRoot, transactionsTrie: blockInfo.transactionsRoot, receiptTrie: blockInfo.receiptsRoot, logsBloom: blockInfo.logsBloom, difficulty: BigInt(blockInfo.difficulty), number: BigInt(blockInfo.number), gasLimit: BigInt(blockInfo.gasLimit), gasUsed: BigInt(blockInfo.gasUsed), timestamp: BigInt(blockInfo.timestamp), extraData: blockInfo.extraData, mixHash: blockInfo.mixHash, // some reason the types are not up to date :( nonce: blockInfo.nonce, baseFeePerGas: blockInfo.baseFeePerGas ? BigInt(blockInfo.baseFeePerGas) : undefined, withdrawalsRoot: blockInfo.withdrawalsRoot ?? undefined, }; } export function txDataFromELBlock(txInfo) { return { ...txInfo, data: txInfo.input, gasPrice: isTruthy(txInfo.gasPrice) ? BigInt(txInfo.gasPrice) : null, gasLimit: txInfo.gas, to: isTruthy(txInfo.to) ? padLeft(hexToBuffer(txInfo.to), 20) : undefined, value: txInfo.value ? BigInt(txInfo.value) : undefined, maxFeePerGas: isTruthy(txInfo.maxFeePerGas) ? BigInt(txInfo.maxFeePerGas) : undefined, maxPriorityFeePerGas: isTruthy(txInfo.maxPriorityFeePerGas) ? BigInt(txInfo.maxPriorityFeePerGas) : undefined, }; } export function blockDataFromELBlock(blockInfo) { return { header: headerDataFromELBlock(blockInfo), transactions: blockInfo.transactions.map(txDataFromELBlock), }; } export function cleanObject(obj) { const isNullify = (v) => v === undefined || v === null; if (Array.isArray(obj)) return obj.filter((v) => isNullify(v)); if (typeof obj === "object") { for (const key of Object.keys(obj)) { if (isNullify(obj[key])) { delete obj[key]; } else if (typeof obj[key] === "object") { cleanObject(obj[key]); } } } return obj; } /** * Convert an array to array of chunks of length N * @example * chunkIntoN([1,2,3,4,5,6], 2) * => [[1,2], [3,4], [5,6]] */ export function chunkIntoN(arr, n) { return Array.from({ length: Math.ceil(arr.length / n) }, (_, i) => arr.slice(i * n, i * n + n)); } //# sourceMappingURL=conversion.js.map