Skip to main content

pallet_gear_eth_bridge_primitives/
lib.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Gear Ethereum Bridge Primitives.
5
6#![cfg_attr(not(feature = "std"), no_std)]
7#![warn(missing_docs)]
8#![doc(html_logo_url = "https://gear-tech.io/logo.png")]
9#![doc(html_favicon_url = "https://gear-tech.io/favicon.ico")]
10#![cfg_attr(docsrs, feature(doc_cfg))]
11
12extern crate alloc;
13
14use alloc::vec::Vec;
15use binary_merkle_tree::MerkleProof;
16use gprimitives::{ActorId, H160, H256, U256};
17use parity_scale_codec::{Decode, Encode};
18use scale_info::TypeInfo;
19
20/// Type representing merkle proof of message's inclusion into bridging queue.
21#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, TypeInfo)]
22#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
23pub struct Proof {
24    /// Merkle root of the tree this proof associated with.
25    pub root: H256,
26    /// Proof itself: collection of hashes required for verification.
27    pub proof: Vec<H256>,
28    /// Number of leaves in the tree.
29    pub number_of_leaves: u64,
30    /// Leaf index we're proving inclusion.
31    pub leaf_index: u64,
32    /// Leaf value for inclusion proving.
33    pub leaf: H256,
34}
35
36impl From<MerkleProof<H256, H256>> for Proof {
37    fn from(value: MerkleProof<H256, H256>) -> Self {
38        Self {
39            root: value.root,
40            proof: value.proof,
41            number_of_leaves: value.number_of_leaves as u64,
42            leaf_index: value.leaf_index as u64,
43            leaf: value.leaf,
44        }
45    }
46}
47
48/// Type representing message being bridged from gear to eth.
49#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq, PartialOrd, Ord, TypeInfo)]
50pub struct EthMessage {
51    nonce: U256,
52    source: H256,
53    destination: H160,
54    payload: Vec<u8>,
55}
56
57impl EthMessage {
58    /// Creates a new [`EthMessage`] with unchecked parameters.
59    ///
60    /// # Safety
61    ///
62    /// `nonce` must be unique for each message, `source` must be valid ActorId,
63    /// `destination` must be valid Ethereum address, and `payload` must not exceed
64    /// the maximum allowed size for message.
65    pub unsafe fn new_unchecked(
66        nonce: U256,
67        source: ActorId,
68        destination: H160,
69        payload: Vec<u8>,
70    ) -> Self {
71        Self {
72            nonce,
73            source: source.into_bytes().into(),
74            destination,
75            payload,
76        }
77    }
78
79    /// Message's nonce getter.
80    pub fn nonce(&self) -> U256 {
81        self.nonce
82    }
83
84    /// Message's source getter.
85    pub fn source(&self) -> H256 {
86        self.source
87    }
88
89    /// Message's destination getter.
90    pub fn destination(&self) -> H160 {
91        self.destination
92    }
93
94    /// Message's payload bytes getter.
95    pub fn payload(&self) -> &[u8] {
96        &self.payload
97    }
98}