referrerpolicy=no-referrer-when-downgrade

snowbridge_pallet_inbound_queue/
envelope.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3use snowbridge_core::ChannelId;
4use snowbridge_inbound_queue_primitives::Log;
5
6use sp_core::{RuntimeDebug, H160, H256};
7use sp_std::prelude::*;
8
9use alloy_core::{primitives::B256, sol, sol_types::SolEvent};
10
11sol! {
12	event OutboundMessageAccepted(bytes32 indexed channel_id, uint64 nonce, bytes32 indexed message_id, bytes payload);
13}
14
15/// An inbound message that has had its outer envelope decoded.
16#[derive(Clone, RuntimeDebug)]
17pub struct Envelope {
18	/// The address of the outbound queue on Ethereum that emitted this message as an event log
19	pub gateway: H160,
20	/// The message Channel
21	pub channel_id: ChannelId,
22	/// A nonce for enforcing replay protection and ordering.
23	pub nonce: u64,
24	/// An id for tracing the message on its route (has no role in bridge consensus)
25	pub message_id: H256,
26	/// The inner payload generated from the source application.
27	pub payload: Vec<u8>,
28}
29
30#[derive(Copy, Clone, RuntimeDebug)]
31pub struct EnvelopeDecodeError;
32
33impl TryFrom<&Log> for Envelope {
34	type Error = EnvelopeDecodeError;
35
36	fn try_from(log: &Log) -> Result<Self, Self::Error> {
37		let topics: Vec<B256> = log.topics.iter().map(|x| B256::from_slice(x.as_ref())).collect();
38
39		let event = OutboundMessageAccepted::decode_raw_log_validate(topics, &log.data)
40			.map_err(|_| EnvelopeDecodeError)?;
41
42		Ok(Self {
43			gateway: log.address,
44			channel_id: ChannelId::from(event.channel_id.as_ref()),
45			nonce: event.nonce,
46			message_id: H256::from(event.message_id.as_ref()),
47			payload: event.payload.into(),
48		})
49	}
50}