Skip to content

Instantly share code, notes, and snippets.

@zengzengzenghuy
Created November 17, 2023 08:27
Show Gist options
  • Save zengzengzenghuy/c4a6ee209c454c5acafa65fc787116fd to your computer and use it in GitHub Desktop.
Save zengzengzenghuy/c4a6ee209c454c5acafa65fc787116fd to your computer and use it in GitHub Desktop.
const { Trie } = require("@ethereumjs/trie")
const { RLP } = require("@ethereumjs/rlp")
const { bufferToHex, toBuffer } = require("@ethereumjs/util")
// const { hexlify } = require("ethers")
import { isHexString } from "@ethereumjs/util"
import {
createWalletClient,
http,
Chain,
toHex,
publicActions,
WalletClient,
PublicClient,
createPublicClient,
keccak256,
isHex,
fromHex,
size,
} from "viem"
import { privateKeyToAccount } from "viem/accounts"
import { goerli } from "viem/chains"
const TOPIC = "0x48257dc961b6f792c2b78a080dacfed693b660960a702de21cee364e20270e2f"
const pingpongEmitter = "0x36411CdA1beC67D0162f1F06F4cf9BdF5EF8AfEE"
const getBytesEncodedReceipt = (_receipt: any) => {
const logs = _receipt.logs.map((_log: any) => [_log.address, _log.topics.map((_topic: any) => _topic), _log.data])
if (_receipt.type === 0) {
return Buffer.from(RLP.encode([_receipt.status, _receipt.cumulativeGasUsed, _receipt.logsBloom, logs]))
}
return Buffer.concat([
toBuffer(_receipt.type),
RLP.encode([_receipt.status, _receipt.cumulativeGasUsed, _receipt.logsBloom, logs]),
])
}
const Hexlify = (value: string) => {
return `0x` + Number(value).toString(16)
}
const blockRLP = (block: any) => {
const values = [
block.parentHash,
block.sha3Uncles,
block.miner,
block.stateRoot,
block.transactionsRoot,
block.receiptsRoot,
block.logsBloom,
block.difficulty, //bn
block.number, //bn
block.gasLimit, //bn
block.gasUsed, //bn
block.timestamp, //bn
block.extraData,
block.mixHash,
block.nonce,
block.baseFeePerGas, //bn
]
console.log(values)
console.log(values.map(Hexlify))
return RLP.encode(values.map(Hexlify))
}
const main = async () => {
const expectedTransactionIndex = 0
const client = createPublicClient({
chain: goerli,
transport: http("https://goerli.infura.io/v3/13ff4899c9bd4d5db8208880b54f1f54"),
}).extend(publicActions)
const tx = await client.getTransactionReceipt({
hash: "0xb8e473106c9fb97109da3ccffd1b6a048c6b9a2f7f2963d172578e837895cd68",
})
// console.log(tx)
const block = await client.getBlock({ blockNumber: tx.blockNumber })
// console.log(block)
// const blockHeaders = blockRLP(block)
// console.log(keccak256(blockHeaders))
// console.log(block.hash)
// console.log(block.receiptsRoot)
// console.log(block.transactions)
// const receipts = []
// for (const tx of block.transactions) {
// const ricit = await client.getTransactionReceipt({ hash: tx })
// receipts.push(ricit)
// }
// const receiptTrie = new Trie()
// const receipt = receipts.find(({ transactionIndex }) => transactionIndex === expectedTransactionIndex)
// const logIndex = receipt?.logs.findIndex(
// ({ address, topics }) => address.toLowerCase() === pingpongEmitter.toLowerCase() && topics[0] === TOPIC,
// )
// const encodedReceipts = receipts.map((_receipt) => getBytesEncodedReceipt(_receipt))
// const rlpEncodedKeys = receipts.map(({ transactionIndex }) => RLP.encode(transactionIndex))
// const receiptsRootTree = new Trie()
// await Promise.all(encodedReceipts.map((_receipt, _index) => receiptsRootTree.put(rlpEncodedKeys[_index], _receipt)))
// const expectedReceiptRoot = receiptsRootTree.root()
// console.log(keccak256(expectedReceiptRoot))
// console.log(block.receiptsRoot)
// const key = rlpEncodedKeys[expectedTransactionIndex]
// const receiptsRootPath = await receiptsRootTree.findPath(key, true)
// const receiptsRootProof = {
// parentNodes: receiptsRootPath.stack.map((_el) => _el.raw()),
// path: key,
// }
// fetch receipt
// fetch block
// consruct receipt tree
// return ()
// receiptsRootProofPath;
// receiptsRootProofParentNodes;
// receipt;
// logIndex;
// blockHeader;
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment