ave_common/
request.rs

1use ave_identity::{DigestIdentifier, PublicKey};
2use borsh::{BorshDeserialize, BorshSerialize};
3use serde::{Deserialize, Serialize};
4
5use crate::{Namespace, SchemaType, ValueWrapper};
6
7
8/// An enum representing a Ave Ledger event request.
9#[derive(
10    Debug,
11    Clone,
12    Serialize,
13    Deserialize,
14    Eq,
15    PartialEq,
16    BorshSerialize,
17    BorshDeserialize,
18)]
19pub enum EventRequest {
20    /// A request to create a new subject.
21    Create(CreateRequest),
22    /// A request to add a fact to a subject.
23    Fact(FactRequest),
24    /// A request to transfer ownership of a subject.
25    Transfer(TransferRequest),
26
27    Confirm(ConfirmRequest),
28
29    Reject(RejectRequest),
30    /// A request to mark a subject as end-of-life.
31    EOL(EOLRequest),
32}
33
34impl EventRequest {
35    pub fn check_request_signature(
36        &self,
37        signer: &PublicKey,
38        owner: &PublicKey,
39        new_owner: &Option<PublicKey>,
40    ) -> bool {
41        match self {
42            EventRequest::Create(..)
43            | EventRequest::Transfer(..)
44            | EventRequest::EOL(..) => signer == owner,
45            EventRequest::Confirm(..) | EventRequest::Reject(..) => {
46                if let Some(new_owner) = new_owner {
47                    new_owner == signer
48                } else {
49                    false
50                }
51            }
52            EventRequest::Fact(..) => true,
53        }
54    }
55
56    pub fn is_create_event(&self) -> bool {
57        matches!(self, EventRequest::Create(_create_request))
58    }
59    pub fn is_fact_event(&self) -> bool {
60        matches!(self, EventRequest::Fact(_fact_request))
61    }
62
63    pub fn get_subject_id(&self) -> DigestIdentifier {
64        match self {
65            EventRequest::Create(_create_request) => {
66                DigestIdentifier::default()
67            }
68            EventRequest::Fact(fact_request) => fact_request.subject_id.clone(),
69            EventRequest::Transfer(transfer_request) => {
70                transfer_request.subject_id.clone()
71            }
72            EventRequest::Confirm(confirm_request) => {
73                confirm_request.subject_id.clone()
74            }
75            EventRequest::Reject(reject_request) => {
76                reject_request.subject_id.clone()
77            }
78            EventRequest::EOL(eolrequest) => eolrequest.subject_id.clone(),
79        }
80    }
81}
82
83/// A struct representing a request to create a new subject.
84#[derive(
85    Debug,
86    Clone,
87    Serialize,
88    Deserialize,
89    Eq,
90    PartialEq,
91    BorshSerialize,
92    BorshDeserialize,
93)]
94pub struct CreateRequest {
95    /// The name of subject.
96    pub name: Option<String>,
97    /// The description of subject.
98    pub description: Option<String>,
99    /// The identifier of the governance contract.
100    pub governance_id: DigestIdentifier,
101    /// The identifier of the schema used to validate the event.
102    pub schema_id: SchemaType,
103    /// The namespace of the subject.
104    pub namespace: Namespace,
105}
106
107/// A struct representing a request to add a fact to a subject.
108#[derive(
109    Debug,
110    Clone,
111    Serialize,
112    Deserialize,
113    Eq,
114    PartialEq,
115    BorshSerialize,
116    BorshDeserialize,
117)]
118pub struct FactRequest {
119    /// The identifier of the subject to which the fact will be added.
120    pub subject_id: DigestIdentifier,
121    /// The payload of the fact to be added.
122    pub payload: ValueWrapper,
123}
124
125/// A struct representing a request to transfer ownership of a subject.
126#[derive(
127    Debug,
128    Clone,
129    Serialize,
130    Deserialize,
131    Eq,
132    PartialEq,
133    BorshSerialize,
134    BorshDeserialize,
135)]
136pub struct TransferRequest {
137    /// The identifier of the subject to transfer ownership of.
138    pub subject_id: DigestIdentifier,
139    /// The identifier of the public key of the new owner.
140    pub new_owner: PublicKey,
141}
142
143/// A struct representing a request to transfer ownership of a subject.
144#[derive(
145    Debug,
146    Clone,
147    Serialize,
148    Deserialize,
149    Eq,
150    PartialEq,
151    BorshSerialize,
152    BorshDeserialize,
153)]
154pub struct ConfirmRequest {
155    pub subject_id: DigestIdentifier,
156    /// The new name of old owner, only for governance confirm, if is None in governance confirm, old owner will not add to members
157    pub name_old_owner: Option<String>,
158}
159
160/// A struct representing a request to mark a subject as end-of-life.
161#[derive(
162    Debug,
163    Clone,
164    Serialize,
165    Deserialize,
166    Eq,
167    PartialEq,
168    BorshSerialize,
169    BorshDeserialize,
170)]
171pub struct EOLRequest {
172    /// The identifier of the subject to mark as end-of-life.
173    pub subject_id: DigestIdentifier,
174}
175
176#[derive(
177    Debug,
178    Clone,
179    Serialize,
180    Deserialize,
181    Eq,
182    PartialEq,
183    BorshSerialize,
184    BorshDeserialize,
185)]
186pub struct RejectRequest {
187    pub subject_id: DigestIdentifier,
188}