Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Added klay_getProof and eth_getProof APIs #2068

Merged
merged 2 commits into from
Jan 11, 2024

Conversation

hyunsooda
Copy link
Contributor

Proposed changes

Added klay_getProof and eth_getProof APIs.

Types of changes

Please put an x in the boxes related to your change.

  • Bugfix
  • New feature or enhancement
  • Others

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING GUIDELINES doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes ($ make test)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Related issues

  • Please leave the issue numbers or links related to this PR here.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

@2dvorak
Copy link
Collaborator

2dvorak commented Jan 3, 2024

Tried to test using eth-proof, successfully generated accountProof and storageProof, however verification won't work because eth-object fails to decode proof. @blukat29 noticed that it is due to Klaytn having different account struct (prefixed AccountType).

Ended up comparing the stateRoot and storageRoot calcuated from the proof with the known value using below test script:

const Caver = require('caver-js');
const fs = require('fs');

const { GetAndVerify, GetProof, VerifyProof } = require('eth-proof');

(async function main() {
    let getAndVerify = new GetAndVerify("http://localhost:8551")
    let getProof = new GetProof("http://localhost:8551")
    let verifyProof = new VerifyProof("http://localhost:8551")

    const key = "c26e4a47a7443384def9d7d2b405dfae0826c5142385f1a3fb142836f68407d9"
    const abi = JSON.parse(fs.readFileSync('./build/MyContract.abi', 'utf8'));
    const code = fs.readFileSync('./build/MyContract.bin', 'utf8');
    const caver = new Caver("http://localhost:8551");
    const senderAddr = caver.klay.accounts.wallet.add(key).address;

    const instance = new caver.klay.Contract(abi);
    let contract = await instance.deploy({data: code, arguments:[1]})
        .send({ from: senderAddr, gas: 100000000, value: 0 });

    const block = await caver.rpc.klay.getBlockByNumber("latest");

    let accountProof = await getProof.accountProof(contract._address, block.hash);

    let stateRoot = VerifyProof.getStateRootFromHeader(accountProof.header)
    let stateRootFromProof = VerifyProof.getRootFromProof(accountProof.accountProof)
    // These should match, see eth-proof/getAndVerifyProof.js:34
    console.log(stateRoot);
    console.log(stateRootFromProof);

    // Get storageRoot using RPC, because we cannot decode proof into eth-object Account
    let account = await caver.rpc.klay.getAccount(contract._address, block.hash);

    let storageProof = await getProof.storageProof(contract._address, "0x0", block.hash);
    let storageRootFromProof = VerifyProof.getRootFromProof(storageProof.storageProof)
    // These should match, see eth-proof/getAndVerifyProof.js:47
    console.log(account.account.storageRoot);
    console.log(storageRootFromProof);
})();

Output would be like:

<Buffer d9 9a 99 0c f1 5a 21 1a ef 49 aa 0d ff d9 2b 23 dc 57 60 3e de 79 c1 09 6b ce 41 9b 38 b2 fd 4d>
<Buffer d9 9a 99 0c f1 5a 21 1a ef 49 aa 0d ff d9 2b 23 dc 57 60 3e de 79 c1 09 6b ce 41 9b 38 b2 fd 4d>
0x821e2556a290c86405f8160a2d662042a431ba456b9db265c79bb837c04be5f0
<Buffer 82 1e 25 56 a2 90 c8 64 05 f8 16 0a 2d 66 20 42 a4 31 ba 45 6b 9d b2 65 c7 9b b8 37 c0 4b e5 f0>

Seems our getProof API is working fine.

@hyunsooda hyunsooda merged commit 75c149a into klaytn:dev Jan 11, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants