Skip to content

Commit

Permalink
feature: rlp encode for VerifyProofPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Dec 14, 2023
1 parent d38ae58 commit 994c4ff
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions devtools/axon-tools/src/precompile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ckb_types::utilities::{merkle_root, MerkleProof};
use ckb_types::{packed, prelude::*};
use ethers_contract::{EthAbiCodec, EthAbiType};
use rlp::{Encodable, RlpStream};

#[derive(EthAbiCodec, EthAbiType, Clone, Debug, PartialEq, Eq)]
pub struct VerifyProofPayload {
Expand All @@ -13,13 +14,42 @@ pub struct VerifyProofPayload {
pub proof: Proof,
}

impl Encodable for VerifyProofPayload {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(5);
s.append(&self.verify_type);
s.append(&self.transactions_root.as_slice());
s.append(&self.witnesses_root.as_slice());
s.append(&self.raw_transactions_root.as_slice());
s.append(&self.proof);
}
}

#[derive(EthAbiCodec, EthAbiType, Clone, Debug, PartialEq, Eq)]
pub struct Proof {
pub indices: Vec<u32>,
pub lemmas: Vec<[u8; 32]>,
pub leaves: Vec<[u8; 32]>,
}

impl Encodable for Proof {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(3);
s.append_list(&self.indices);

// Append each lemma as a separate item
s.begin_list(self.lemmas.len());
for lemma in &self.lemmas {
s.append(&lemma.as_slice());
}

// Append each leaf as a separate item
s.begin_list(self.leaves.len());
for leaf in &self.leaves {
s.append(&leaf.as_slice());
}
}
}
/// A standalone function to verify the ckb-merkle-binary-tree proof.
pub fn verify_proof(payload: VerifyProofPayload) -> Result<(), String> {
// Firstly, verify the transactions_root is consist of the raw_transactions_root
Expand Down

0 comments on commit 994c4ff

Please sign in to comment.