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

Merkle proof verification fail #399

Open
SetarehGhorshi opened this issue Nov 28, 2024 · 0 comments
Open

Merkle proof verification fail #399

SetarehGhorshi opened this issue Nov 28, 2024 · 0 comments

Comments

@SetarehGhorshi
Copy link

SetarehGhorshi commented Nov 28, 2024

Hey.

We are making an abci query to our cosmos chain and try to verify the proof. For this, we are attempting to use the ics23::verify_membership function but it always returns false (the calculated root does not match the root hash from the block). we have ascertained that the abci query response is correct. Could you please help with this issue?
Thanks.

Below is the code we are using for the verification:

fn verify_merkle_proof(
    proof_ops: &ProofOps,
    key: &[u8],
    value: &[u8],
    root_hash: &Vec<u8>,
) -> Result<bool> {
    // Convert ProofOps into an ICS23 CommitmentProof
    let proof_op = proof_ops
        .ops
        .iter()
        .find(|op| op.r#field_type == "ics23:iavl")
        .context("ProofOp for IAVL leaf not found")?;

    let commitment_proof;

    match convert_proof_op(&proof_op) {
        Ok(c_proof) => {
            println!("Successfully converted proof: {:?}", c_proof);
            commitment_proof = c_proof;
        }
        Err(err) if err.to_string().contains("decode") => {
            println!("Decoding error: {:?}", err);
            anyhow::bail!("Proof verification failed");
        }
        Err(err) => {
            println!("Unhandled error: {:?}", err);
            anyhow::bail!("Proof verification failed");
        }
    }

    let spec = ics23::iavl_spec();

    // Verify membership using ICS23 utilities.
    let is_valid =
        verify_membership::<HostFunctionsManager>(&commitment_proof, &spec, root_hash, key, value);

    if let Some(Proof::Exist(existence_proof)) = &commitment_proof.proof {
        println!("existence proof found: {:?}", existence_proof);
        match calculate_existence_root::<ics23::HostFunctionsManager>(existence_proof) {
            Ok(root) => {
                println!("Successfully calculated root: {:?}", root);
                println!("root hash from block: {:?}", root_hash);
            }
            Err(e) => {
                println!("Error calculating existence root: {:?}", e);
            }
        }
    } else {
        println!("existence proof not found");
    }

    if is_valid {
        return Ok(is_valid);
    } else {
        anyhow::bail!("Proof verification failed");
    }
}

Update:
I tried defining the spec dynamically based on the proof but the issue is still there.

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

No branches or pull requests

1 participant