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