Skip to content

Commit

Permalink
Add function that creates receipt proof and submits it to the lightcl…
Browse files Browse the repository at this point in the history
…ient
  • Loading branch information
themicp committed Oct 27, 2023
1 parent a313738 commit 2e3a4df
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import { getEnv } from './utils.js'
import { bytesToHex, formatReceipt, getEnv } from './utils.js';
import { EthAPI } from './eth.js';
import { LightClientAPI } from './lightclient.js';
import { TrieWrapper} from './triewrapper.js';
import { Receipt, UserInput } from './types.js';
import assert from 'assert';

const CONTRACT_ADDRESS = getEnv("CONTRACT_ADDRESS")

async function isTopicInTransaction(api: EthAPI, lightClient: LightClientAPI, input: UserInput) {
const block = await api.getBlock(input.blockNumber);

const receipts: Receipt[] = (await api.getBlockTransactionReceipts(input.blockNumber)).map(formatReceipt);
const requestedTxReceipt = receipts.find((r) => r.transactionHash === input.transactionHash);
if (!requestedTxReceipt) throw new Error(`Could not find receipt for transaction ${input.transactionHash} in block ${input.blockNumber}`);

const transactionIndex = parseInt(requestedTxReceipt.transactionIndex, 16);
const receiptsTrie = await TrieWrapper.trieFromReceipts(receipts);
assert(
block.receiptsRoot.slice(2) === bytesToHex(receiptsTrie.root()),
`Expected receipts root (${block.receiptsRoot}) doesn't match the actual (${bytesToHex(receiptsTrie.root())})`);
const proof = await TrieWrapper.createProof(receiptsTrie, transactionIndex);

const isValidReceipt = true;
// const isValidReceipt = await lightClient.verify_receipt(
// block.receiptsRoot.slice(2),
// proof.map((p) => bytesToHex(p)),
// TrieWrapper.encodeKey(transactionIndex)
// );

if (isValidReceipt) {
return requestedTxReceipt.logs.find(log => log[1].includes(input.topic)) !== undefined;
}
}

const main = async () => {
const api = new EthAPI()
const lightClient = new LightClientAPI(CONTRACT_ADDRESS);
Expand Down

0 comments on commit 2e3a4df

Please sign in to comment.