|
| 1 | +/* |
| 2 | + Copyright 2021 Integritee AG and Supercomputing Systems AG |
| 3 | + Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved. |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +
|
| 17 | +*/ |
| 18 | + |
| 19 | +// Todo: I think we can upstream the codec change, then we can delete this crate. |
| 20 | + |
| 21 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 22 | + |
| 23 | +#[cfg(not(feature = "std"))] |
| 24 | +extern crate alloc; |
| 25 | +#[cfg(not(feature = "std"))] |
| 26 | +use alloc::vec::Vec; |
| 27 | + |
| 28 | +use parity_scale_codec::{Decode, Encode}; |
| 29 | +use serde::{Deserialize, Serialize}; |
| 30 | + |
| 31 | +// re-export the original one implementing all the merkle/logic. |
| 32 | +pub use binary_merkle_tree::{merkle_proof, merkle_root, verify_proof, MerkleProof}; |
| 33 | + |
| 34 | +/// Custom Merkle proof that implements codec |
| 35 | +/// The difference to the original one is that implements the scale-codec and that the fields contain u32 instead of usize. |
| 36 | +#[derive(Debug, PartialEq, Eq, Decode, Encode, Deserialize, Serialize)] |
| 37 | +pub struct MerkleProofWithCodec<H, L> { |
| 38 | + /// Root hash of generated merkle tree. |
| 39 | + pub root: H, |
| 40 | + /// Proof items (does not contain the leaf hash, nor the root obviously). |
| 41 | + /// |
| 42 | + /// This vec contains all inner node hashes necessary to reconstruct the root hash given the |
| 43 | + /// leaf hash. |
| 44 | + pub proof: Vec<H>, |
| 45 | + /// Number of leaves in the original tree. |
| 46 | + /// |
| 47 | + /// This is needed to detect a case where we have an odd number of leaves that "get promoted" |
| 48 | + /// to upper layers. |
| 49 | + pub number_of_leaves: u64, |
| 50 | + /// Index of the leaf the proof is for (0-based). |
| 51 | + pub leaf_index: u64, |
| 52 | + /// Leaf content. |
| 53 | + pub leaf: L, |
| 54 | +} |
| 55 | + |
| 56 | +impl<H, L> From<MerkleProof<H, L>> for MerkleProofWithCodec<H, L> { |
| 57 | + fn from(source: MerkleProof<H, L>) -> Self { |
| 58 | + Self { |
| 59 | + root: source.root, |
| 60 | + proof: source.proof, |
| 61 | + // usize as u64 can't panic |
| 62 | + number_of_leaves: source.number_of_leaves as u64, |
| 63 | + leaf_index: source.leaf_index as u64, |
| 64 | + leaf: source.leaf, |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments