Skip to main content

chia_sdk_driver/clear_signing/
vault_message.rs

1use chia_protocol::{Bytes, Bytes32};
2use chia_sdk_types::{MessageFlags, MessageSide, conditions::SendMessage};
3use clvm_traits::FromClvm;
4use clvmr::{Allocator, NodePtr};
5
6use crate::DriverError;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct VaultMessage {
10    pub spent_coin_id: Bytes32,
11    pub data: Bytes,
12}
13
14/// We intentionally expect a pretty rigid format here, out of an abundance of caution.
15/// If the message format is violated, we return an error. This can be expanded in the future if needed.
16pub fn parse_vault_message(
17    allocator: &Allocator,
18    condition: SendMessage<NodePtr>,
19) -> Result<VaultMessage, DriverError> {
20    let sender = MessageFlags::decode(condition.mode, MessageSide::Sender);
21    let receiver = MessageFlags::decode(condition.mode, MessageSide::Receiver);
22
23    if sender != MessageFlags::PUZZLE || receiver != MessageFlags::COIN || condition.data.len() != 1
24    {
25        return Err(DriverError::InvalidVaultMessageFormat);
26    }
27
28    let coin_id = Bytes32::from_clvm(allocator, condition.data[0])?;
29
30    Ok(VaultMessage {
31        spent_coin_id: coin_id,
32        data: condition.message,
33    })
34}