Skip to main content

ave_common/
request.rs

1//! Ledger event request types.
2//!
3//! These structures describe the input accepted by the core ledger flow before
4//! it is wrapped in transport-specific formats.
5
6use ave_identity::{DigestIdentifier, PublicKey};
7use borsh::{BorshDeserialize, BorshSerialize};
8use serde::{Deserialize, Serialize};
9
10use crate::{Namespace, SchemaType, ValueWrapper};
11
12/// Event request accepted by the ledger.
13#[derive(
14    Debug,
15    Clone,
16    Serialize,
17    Deserialize,
18    Eq,
19    PartialEq,
20    BorshSerialize,
21    BorshDeserialize,
22)]
23pub enum EventRequest {
24    /// Creates a new subject.
25    Create(CreateRequest),
26    /// Appends a fact to an existing subject.
27    Fact(FactRequest),
28    /// Transfers subject ownership.
29    Transfer(TransferRequest),
30    /// Confirms a transfer.
31    Confirm(ConfirmRequest),
32    /// Rejects a transfer.
33    Reject(RejectRequest),
34    /// Marks a subject as end-of-life.
35    EOL(EOLRequest),
36}
37
38impl EventRequest {
39    /// Returns `true` when `signer` is allowed to sign this request.
40    pub fn check_request_signature(
41        &self,
42        signer: &PublicKey,
43        owner: &PublicKey,
44        new_owner: &Option<PublicKey>,
45    ) -> bool {
46        match self {
47            Self::Create(..) | Self::Transfer(..) | Self::EOL(..) => {
48                signer == owner
49            }
50            Self::Confirm(..) | Self::Reject(..) => {
51                new_owner.as_ref() == Some(signer)
52            }
53            Self::Fact(..) => true,
54        }
55    }
56
57    /// Returns `true` when the request is a `Create`.
58    pub const fn is_create_event(&self) -> bool {
59        matches!(self, Self::Create(_create_request))
60    }
61
62    /// Returns `true` when the request is a `Fact`.
63    pub const fn is_fact_event(&self) -> bool {
64        matches!(self, Self::Fact(_fact_request))
65    }
66
67    /// Returns the subject identifier affected by the request.
68    ///
69    /// `Create` requests do not have a subject id yet, so they return the empty
70    /// digest placeholder used by the rest of the workspace.
71    pub fn get_subject_id(&self) -> DigestIdentifier {
72        match self {
73            Self::Create(_create_request) => DigestIdentifier::default(),
74            Self::Fact(fact_request) => fact_request.subject_id.clone(),
75            Self::Transfer(transfer_request) => {
76                transfer_request.subject_id.clone()
77            }
78            Self::Confirm(confirm_request) => {
79                confirm_request.subject_id.clone()
80            }
81            Self::Reject(reject_request) => reject_request.subject_id.clone(),
82            Self::EOL(eolrequest) => eolrequest.subject_id.clone(),
83        }
84    }
85}
86
87/// Payload for a `Create` event.
88#[derive(
89    Debug,
90    Clone,
91    Serialize,
92    Deserialize,
93    Eq,
94    PartialEq,
95    BorshSerialize,
96    BorshDeserialize,
97)]
98pub struct CreateRequest {
99    /// Optional subject name.
100    pub name: Option<String>,
101    /// Optional subject description.
102    pub description: Option<String>,
103    /// Governance identifier.
104    pub governance_id: DigestIdentifier,
105    /// Schema used to validate the initial state.
106    pub schema_id: SchemaType,
107    /// Subject namespace.
108    pub namespace: Namespace,
109}
110
111/// Payload for a `Fact` event.
112#[derive(
113    Debug,
114    Clone,
115    Serialize,
116    Deserialize,
117    Eq,
118    PartialEq,
119    BorshSerialize,
120    BorshDeserialize,
121)]
122pub struct FactRequest {
123    /// Subject identifier.
124    pub subject_id: DigestIdentifier,
125    /// JSON payload to append to the subject state.
126    pub payload: ValueWrapper,
127}
128
129/// Payload for a `Transfer` event.
130#[derive(
131    Debug,
132    Clone,
133    Serialize,
134    Deserialize,
135    Eq,
136    PartialEq,
137    BorshSerialize,
138    BorshDeserialize,
139)]
140pub struct TransferRequest {
141    /// Subject identifier.
142    pub subject_id: DigestIdentifier,
143    /// Public key of the new owner.
144    pub new_owner: PublicKey,
145}
146
147/// Payload for a `Confirm` event.
148#[derive(
149    Debug,
150    Clone,
151    Serialize,
152    Deserialize,
153    Eq,
154    PartialEq,
155    BorshSerialize,
156    BorshDeserialize,
157)]
158pub struct ConfirmRequest {
159    pub subject_id: DigestIdentifier,
160    /// Optional name for the previous owner in governance updates.
161    pub name_old_owner: Option<String>,
162}
163
164/// Payload for an `EOL` event.
165#[derive(
166    Debug,
167    Clone,
168    Serialize,
169    Deserialize,
170    Eq,
171    PartialEq,
172    BorshSerialize,
173    BorshDeserialize,
174)]
175pub struct EOLRequest {
176    /// Subject identifier.
177    pub subject_id: DigestIdentifier,
178}
179
180/// Payload for a `Reject` event.
181#[derive(
182    Debug,
183    Clone,
184    Serialize,
185    Deserialize,
186    Eq,
187    PartialEq,
188    BorshSerialize,
189    BorshDeserialize,
190)]
191pub struct RejectRequest {
192    pub subject_id: DigestIdentifier,
193}