audit_trails/core/locking/
mod.rs1use iota_interaction::types::base_types::ObjectID;
7use iota_interaction::{IotaKeySignature, OptionalSync};
8use product_common::core_client::CoreClient;
9use product_common::transaction::transaction_builder::TransactionBuilder;
10use secret_storage::Signer;
11
12use crate::core::trail::{AuditTrailFull, AuditTrailReadOnly};
13use crate::core::types::{LockingConfig, LockingWindow, TimeLock};
14use crate::error::Error;
15
16mod operations;
17mod transactions;
18
19pub use transactions::{UpdateDeleteRecordWindow, UpdateDeleteTrailLock, UpdateLockingConfig, UpdateWriteLock};
20
21use self::operations::LockingOps;
22
23#[derive(Debug, Clone)]
28pub struct TrailLocking<'a, C> {
29 pub(crate) client: &'a C,
30 pub(crate) trail_id: ObjectID,
31 pub(crate) selected_capability_id: Option<ObjectID>,
32}
33
34impl<'a, C> TrailLocking<'a, C> {
35 pub(crate) fn new(client: &'a C, trail_id: ObjectID, selected_capability_id: Option<ObjectID>) -> Self {
36 Self {
37 client,
38 trail_id,
39 selected_capability_id,
40 }
41 }
42
43 pub fn using_capability(mut self, capability_id: ObjectID) -> Self {
45 self.selected_capability_id = Some(capability_id);
46 self
47 }
48
49 pub fn update<S>(&self, config: LockingConfig) -> Result<TransactionBuilder<UpdateLockingConfig>, Error>
62 where
63 C: AuditTrailFull + CoreClient<S>,
64 S: Signer<IotaKeySignature> + OptionalSync,
65 {
66 config.validate()?;
67 let owner = self.client.sender_address();
68 Ok(TransactionBuilder::new(UpdateLockingConfig::new(
69 self.trail_id,
70 owner,
71 config,
72 self.selected_capability_id,
73 )))
74 }
75
76 pub fn update_delete_record_window<S>(
88 &self,
89 window: LockingWindow,
90 ) -> Result<TransactionBuilder<UpdateDeleteRecordWindow>, Error>
91 where
92 C: AuditTrailFull + CoreClient<S>,
93 S: Signer<IotaKeySignature> + OptionalSync,
94 {
95 window.validate()?;
96 let owner = self.client.sender_address();
97 Ok(TransactionBuilder::new(UpdateDeleteRecordWindow::new(
98 self.trail_id,
99 owner,
100 window,
101 self.selected_capability_id,
102 )))
103 }
104
105 pub fn update_delete_trail_lock<S>(
112 &self,
113 lock: TimeLock,
114 ) -> Result<TransactionBuilder<UpdateDeleteTrailLock>, Error>
115 where
116 C: AuditTrailFull + CoreClient<S>,
117 S: Signer<IotaKeySignature> + OptionalSync,
118 {
119 lock.validate_as_delete_trail_lock()?;
120 let owner = self.client.sender_address();
121 Ok(TransactionBuilder::new(UpdateDeleteTrailLock::new(
122 self.trail_id,
123 owner,
124 lock,
125 self.selected_capability_id,
126 )))
127 }
128
129 pub fn update_write_lock<S>(&self, lock: TimeLock) -> TransactionBuilder<UpdateWriteLock>
131 where
132 C: AuditTrailFull + CoreClient<S>,
133 S: Signer<IotaKeySignature> + OptionalSync,
134 {
135 let owner = self.client.sender_address();
136 TransactionBuilder::new(UpdateWriteLock::new(
137 self.trail_id,
138 owner,
139 lock,
140 self.selected_capability_id,
141 ))
142 }
143
144 pub async fn is_record_locked(&self, sequence_number: u64) -> Result<bool, Error>
154 where
155 C: AuditTrailReadOnly,
156 {
157 let tx = LockingOps::is_record_locked(self.client, self.trail_id, sequence_number).await?;
158 self.client.execute_read_only_transaction(tx).await
159 }
160}