import BalanceTree from './balance-tree' import { BigNumber } from 'ethers' const Buffer = require('buffer/').Buffer let balance : BalanceTree | null balance = null function parseProof(str : string) { return Buffer.from(str.slice('0x'.length), 'hex') } document.getElementById('js-distributions')?.addEventListener('submit', (event) => { event.preventDefault() const input = document.getElementById('js-json_distributions') as HTMLInputElement | null const json_distributions = input?.value if (!json_distributions || json_distributions.length < 1) return const data = JSON.parse(json_distributions) balance = new BalanceTree(data) const root = document.getElementById('js-merkle_root') as HTMLInputElement | null if (root) root.value = balance.getHexRoot() const verify = document.getElementById('js-root-verify') as HTMLInputElement | null if (verify) verify.value = balance.getHexRoot() const proofBlock = document.getElementById('js-proofs') as HTMLElement | null if (proofBlock) proofBlock.style.display = 'block' return false }) document.getElementById('js-proofs')?.addEventListener('submit', (event) => { event.preventDefault() if (!balance) return if (!event.target) return const formData = new FormData(event.target as HTMLFormElement) const index = BigNumber.from(formData.get("index")) const account = formData.get("account")?.toString() const amount = BigNumber.from(formData.get("amount")) if (!account) return const proof = JSON.stringify(balance.getProof(index, account, amount)) const proofInput = document.getElementById('js-proof') as HTMLInputElement | null if (proofInput) proofInput.value = proof const verify = document.getElementById('js-proof-verify') as HTMLInputElement | null if (verify) verify.value = proof return false }) document.getElementById('js-proof-verification')?.addEventListener('submit', (event) => { event.preventDefault() if (!balance) return if (!event.target) return const verificationResult = document.getElementById('js-verification') as HTMLElement | null if (!verificationResult) return verificationResult.innerHTML = 'verifing...' const formData = new FormData(event.target as HTMLFormElement) const index = BigNumber.from(formData.get("index")) const account = formData.get("account")?.toString() const amount = BigNumber.from(formData.get("amount")) const root = formData.get("root")?.toString() const claimProof = formData.get("proof")?.toString() if (!account || !root || !claimProof) return const claimProofs = JSON.parse(claimProof).map((v : string) => parseProof(v)) const result = BalanceTree.verifyProof(index, account, amount, claimProofs, parseProof(root)) if (result) { verificationResult.innerHTML = 'VALID PROOF' } else { verificationResult.innerHTML = 'INVALID PROOF' } return false })