Skip to content

Instantly share code, notes, and snippets.

@0xAshish
Created March 5, 2019 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xAshish/c6b41987e458f71aad0f8e6cb00cbeb3 to your computer and use it in GitHub Desktop.
Save 0xAshish/c6b41987e458f71aad0f8e6cb00cbeb3 to your computer and use it in GitHub Desktop.
import 'isomorphic-fetch'
import Web3 from 'web3'
import utils from 'ethereumjs-util'
import queryString from 'query-string'
import Trie from 'merkle-patricia-tree'
import EthereumTx from 'ethereumjs-tx'
import EthereumBlock from 'ethereumjs-block/from-rpc'
import {
getTxBytes,
getReceiptBytes,
getTxProof,
getReceiptProof,
verifyTxProof,
verifyReceiptProof,
verifyHeaderProof,
} from './helpers/proofs'
import { getHeaders, getBlockHeader } from './helpers/blocks'
import MerkleTree from './helpers/merkle-tree'
import RootChainArtifacts from '../artifacts/RootChain'
import ChildERC20Artifacts from '../artifacts/ChildERC20'
import ChildERC721Artifacts from '../artifacts/ChildERC721'
import StandardTokenArtifacts from '../artifacts/StandardToken'
import WithdrawManagerArtifacts from '../artifacts/WithdrawManager'
import DepositManagerArtifacts from '../artifacts/DepositManager'
const rlp = utils.rlp
let yo = async function() {
let web3 = new Web3('https://ropsten.infura.io/matic')
let web3Child = new Web3('http://68.183.166.5:8545')
let withdrawManager = new web3.eth.Contract(
WithdrawManagerArtifacts.abi,
'0xaac5836efaf18c2b0a8cdd35034876daf657535b'
)
const block = 53770
const withdrawBlock1 = await web3Child.eth.getBlock(block, true)
console.log(withdrawBlock1)
const transactionHash =
'0xf948fd7b921e8dbe63433843970239e0c8c057ffa9a5a41489343026691f7aeb'
const blockHash =
'0x300f71d061f6630c3457c01e3eb17447b5ee8ba00b0872f714fbfef08877454e'
const withdraw = await web3Child.eth.getTransaction(transactionHash)
const withdrawBlock = await web3Child.eth.getBlock(blockHash, true)
const withdrawReceipt = await web3Child.eth.getTransactionReceipt(
transactionHash
)
console.log(withdrawReceipt)
const headers = await getHeaders(53760, 54015, web3Child)
const tree = new MerkleTree(headers)
const headerRoot = utils.bufferToHex(tree.getRoot())
console.log(headerRoot)
const headerNumber = 850000
// set header block (mocking header block)
// await rootChain.setHeaderBlock(
// headerNumber,
// headerRoot,
// start,
// end,
// withdrawBlock.timestamp
// )
const blockHeader = getBlockHeader(withdrawBlock)
const headerProof = await tree.getProof(blockHeader)
// tree.verify(
// blockHeader,
// withdrawBlock.number - 52480,
// tree.getRoot(),
// headerProof0x91aad8a4d3691e7cde2491530e3a545be3b94ca76ee9a9cedddc54aa2ff25711
// )
// console.log(ff)
// validate tx proof
const txProof = await getTxProof(withdraw, withdrawBlock)
console.log('blah')
// console.log(txProof)
verifyTxProof(txProof)
// validate receipt proof
const receiptProof = await getReceiptProof(
withdrawReceipt,
withdrawBlock,
web3Child
)
verifyReceiptProof(receiptProof)
console.log([
headerNumber, // header block
utils.bufferToHex(Buffer.concat(headerProof)), // header proof
withdrawBlock.number, // block number
withdrawBlock.timestamp, // block timestamp
utils.bufferToHex(withdrawBlock.transactionsRoot), // tx root
utils.bufferToHex(withdrawBlock.receiptsRoot), // tx root
utils.bufferToHex(rlp.encode(receiptProof.path)), // key for trie (both tx and receipt)
utils.bufferToHex(getTxBytes(withdraw)), // tx bytes
utils.bufferToHex(rlp.encode(txProof.parentNodes)), // tx proof nodes
utils.bufferToHex(getReceiptBytes(withdrawReceipt)), // receipt bytes
utils.bufferToHex(rlp.encode(receiptProof.parentNodes)), // reciept proof nodes
])
// const burnWithdrawReceipt = await withdrawManager.withdrawBurntTokens(
// headerNumber, // header block
// utils.bufferToHex(Buffer.concat(headerProof)), // header proof
// withdrawBlock.number, // block number
// withdrawBlock.timestamp, // block timestamp
// utils.bufferToHex(withdrawBlock.transactionsRoot), // tx root
// utils.bufferToHex(withdrawBlock.receiptsRoot), // tx root
// utils.bufferToHex(rlp.encode(receiptProof.path)), // key for trie (both tx and receipt)
// utils.bufferToHex(getTxBytes(withdraw)), // tx bytes
// utils.bufferToHex(rlp.encode(txProof.parentNodes)), // tx proof nodes
// utils.bufferToHex(getReceiptBytes(withdrawReceipt)), // receipt bytes
// utils.bufferToHex(rlp.encode(receiptProof.parentNodes)), // reciept proof nodes
// {
// from: user,
// }
// )
// console.log()
}
yo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment