essential_types/
contract.rs

1//! # Contract
2//!
3//! Types needed to represent an contract.
4
5use std::ops::{Deref, DerefMut};
6
7use serde::{Deserialize, Serialize};
8
9use crate::{predicate::Predicate, serde::hash, Hash, Signature};
10
11#[cfg(feature = "schema")]
12use schemars::JsonSchema;
13
14/// A contract of predicates whose content address has been signed.
15///
16/// For a shorthand constructor, see the downstream
17/// `essential_sign::contract::sign` function.
18#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
19pub struct SignedContract {
20    /// The contract of predicates whose content address has been signed.
21    pub contract: Contract,
22    /// A signature over the contract's content address.
23    ///
24    /// This signature must be produced by signing the contract's
25    /// [`ContentAddress`][crate::ContentAddress]. The contract's
26    /// content address can be produced using one of the downstream
27    /// `essential_hash::contract_addr` functions.
28    pub signature: Signature,
29}
30
31#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
32#[cfg_attr(feature = "schema", derive(JsonSchema))]
33/// A contract of predicates.
34pub struct Contract {
35    /// The contract of predicates.
36    pub predicates: Vec<Predicate>,
37    #[serde(
38        serialize_with = "hash::serialize",
39        deserialize_with = "hash::deserialize"
40    )]
41    /// The salt used to make the contract unique.
42    pub salt: Hash,
43}
44
45impl Contract {
46    /// Maximum number of predicates in a contract.
47    pub const MAX_PREDICATES: usize = 100;
48
49    /// Create a new contract with the given predicates but no salt.
50    pub fn without_salt(predicates: Vec<Predicate>) -> Self {
51        Self {
52            predicates,
53            ..Default::default()
54        }
55    }
56}
57
58impl From<Vec<Predicate>> for Contract {
59    fn from(predicates: Vec<Predicate>) -> Self {
60        Self {
61            predicates,
62            ..Default::default()
63        }
64    }
65}
66
67impl AsRef<[Predicate]> for Contract {
68    fn as_ref(&self) -> &[Predicate] {
69        &self.predicates
70    }
71}
72
73impl Deref for Contract {
74    type Target = Vec<Predicate>;
75
76    fn deref(&self) -> &Self::Target {
77        &self.predicates
78    }
79}
80
81impl DerefMut for Contract {
82    fn deref_mut(&mut self) -> &mut Self::Target {
83        &mut self.predicates
84    }
85}