Giter Club home page Giter Club logo

extractoor-sdk's Introduction

extractoor

The extractoor package is designed to help the interaction with the Extractoor smart contracts library in order to verify information about the state of certain rollup inside its anchored L1. It is part of the ongoing effort of LimeLabs and LimeChain to give back and contribute to the blockchain community in the form of open source tooling and public goods infrastructure.

Instalation

npm install extractoor

Using the SDK

See the example directory for examples of various use cases.

EthereumExtractoorClient

Extractor Client for ethereum. Uses the fetch API in order to trigger RPC requests for Ethereum networks. Mainly used for getting blocks by number and getting proofs for given storage slot.

OptimismExtractoorClient

Extractoor Client for Optimism Bedrock. Exposes method for generating the output root data for a given L1 block by number - generateLatestOutputData. Instantiating the client can be as simple as:

import { OptimismExtractoorClient, BASE_GOERLI_CONFIG } from 'extractoor'

const fetcher = new OptimismExtractoorClient({YourL2RPC}, {YourL1RPC}, {YourConfig});
// const fetcher = new OptimismExtractoorClient(process.env.BASE_GOERLI_RPC_URL, process.env.GOERLI_RPC_URL, BASE_GOERLI_CONFIG);

Two default configs are available at te moment OPTIMISM_GOERLI_CONFIG and BASE_GOERLI_CONFIG. You can connect to a another OptimismBedrock rollup by providing config conforming to the OptimismNetworkConfig interface. Example:

import { OptimismExtractoorClient, OptimismNetworkConfig } from 'extractoor'

const customConfig: OptimismNetworkConfig =  = {
    L2WithdrawalContractAddress: "0x4200000000000000000000000000000000000016",
    OutputOracleAddress: "0xE6Dfba0953616Bacab0c9A8ecb3a9BBa77FC15c0",
    OutputOracleL2OutputPosition: 3
}

const fetcher = new OptimismExtractoorClient(process.env.CUSTOM_BEDROCK_RPC, process.env.L1_RPC, customConfig);

Contributing

Pull requests welcome. The project is built via tsdx. In order to run the compilation in watch mode use

yarn start

To build a distribution version use

yarn build

The tests can be run via

yarn test

Examples

Various runnable examples can be found in the example directory. Refer to its README for their usage and how to run them.

Getting the necessary data for OptimismInbox message receive

import { BN, bufferToHex, keccak, setLengthLeft, toBuffer, unpadBuffer } from 'ethereumjs-util'
import { MPTProofsEncoder, OptimismExtractoorClient, OPTIMISM_GOERLI_CONFIG } from 'extractoor'
const dotenv = require('dotenv');
dotenv.config()

// Inputs
const blockNum = 8529353; // The L1 block number we will be proving contains the Optimism state
const targetAccount = "0xcA7B05255F52C700AE25C278DdB03C02459F7AE8"; // The account inside Optimism we are proving for
const arrayDefinitionPosition = 0; // Definition position of the array inside the solidity contract
const indexInTheArray = 1; // The index of the element you are looking for

// Step 1 - Derive the storage slot from the array definition and index of the array
const arrayDefinitionHash = keccak(setLengthLeft(toBuffer(arrayDefinitionPosition), 32));
const arrayDefinitionBN = new BN(arrayDefinitionHash);
const indexBN = new BN(indexInTheArray);
const slotBN = arrayDefinitionBN.add(indexBN);
const slot = `0x${slotBN.toString("hex")}`

const fetcher = new OptimismExtractoorClient(process.env.OPTIMISM_GOERLI_RPC_URL, process.env.GOERLI_RPC_URL, OPTIMISM_GOERLI_CONFIG);

// Step 2 - Get all the information needed for the Optimism Output Root inclusion inside L1 proof
const output = await fetcher.generateLatestOutputData(`0x${blockNum.toString(16)}`);

// Step 3 - Get all the information needed for the Merkle inclusion proof inside Optimism
const getProofRes = await fetcher.optimism.getProof(targetAccount, slot, bufferToHex(unpadBuffer(toBuffer(output.blockNum))));

// Step 4 - RLP encode the Proof from Step 3
const inclusionProof = MPTProofsEncoder.rlpEncodeProofs([getProofRes.accountProof, getProofRes.storageProof[0].proof]);

// Use the below as parameters to L2OptimismBedrockStateProver
console.log(blockNum, output.outputIndex, output.optimismStateRoot, output.optimismStateRoot, output.withdrawalStorageRoot, output.blockHash, output.outputRootRLPProof, slot, inclusionProof);

Local verification of eth_getProof return data

import { bufferToHex } from 'ethereumjs-util'
import { MPTProofVerifier } from 'extractoor'


async function run() {
    const stateRoot = '0xf70f7bf933416dd187fc95de784e4535fa5547db1b0b9446191945bfc457bca7';
    const target = '0x058A39bEFBBA6a41e1CcBE97C3457dcc894B0fF2';
    const accountProof = [...];
    const storageSlot = '0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563';
    const storageProof = [...];

    const account = await MPTProofVerifier.verifyAccountProof(stateRoot, target, accountProof);
    console.log("Account:\n", bufferToHex(account.nonce), bufferToHex(account.balance), bufferToHex(account.stateRoot), bufferToHex(account.codeHash));

    const storageValue = await MPTProofVerifier.verifyStorageProof(bufferToHex(account.stateRoot), storageSlot, storageProof);
    console.log("Storage Value:\n", bufferToHex(Buffer.from(storageValue)))
}

run()

extractoor-sdk's People

Contributors

perseverance avatar

Stargazers

miguel avatar Luozhu avatar Viktor Todorov avatar

Watchers

 avatar Christian Veselinov avatar  avatar  avatar

Forkers

iwanwang-0

extractoor-sdk's Issues

Extractoor feedback

  • Computing message hashes in the inbox and outbox contracts should reuse the same library

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.