Skip to main content

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