entropy_programs_core/
programs.rs

1//! Contains traits that programs should implement, including Architecture-agnostic programs and generic programs.
2//!
3//! For runtime and binary size optimizations, program construction should be done at compile time by using `const` types, if possible. This can be done by using `const` generic parameters,
4//! or by using a `const` builder. Both methods are described nicely here: https://wapl.es/rust/2022/07/03/const-builder-pattern.html
5
6use crate::architecture::Architecture;
7use crate::bindgen::Error;
8
9/// Programs using binary (or other unserialized) data must implement this. This is the most barebones trait for programs.
10pub trait Satisfiable {
11    /// Indicates that the data satisfies the constraint.
12    fn is_satisfied_by(self, data: &[u8]) -> Result<(), Error>;
13}
14
15/// Any program using transaction-like/architecture-agnostic signature requests must implement this.
16///
17/// For example, a program that checks that the recipient is not a blacklisted address would implement this trait for EVM, and would be similar to this:
18/// ```
19/// use entropy_programs_acl::*;
20/// use entropy_programs_evm::*;
21///
22/// let non_blacklisted_addr: [u8; 20] = [1u8; 20];
23/// let blacklisted_addr_1: [u8; 20] = [2u8; 20];
24/// let blacklisted_addr_2: [u8; 20] = [3u8; 20];
25///
26/// let no_malicious_addresses = Acl {
27///    addresses: vec![blacklisted_addr_1, blacklisted_addr_2],
28///    kind: AclKind::Deny,
29///    allow_null_recipient: false,
30/// };
31///
32/// let non_blacklisted_recipient_tx = EvmTransactionRequest {
33///    to: Some(NameOrAddress::Address(H160::from(non_blacklisted_addr))),
34///   ..Default::default()
35/// };
36///
37/// let blacklisted_recipient_tx = EvmTransactionRequest {
38///    to: Some(NameOrAddress::Address(H160::from(blacklisted_addr_1))),
39///   ..Default::default()
40/// };
41///
42/// // This will be allowed, since the recipient is not in the blacklisted ACL.
43/// no_malicious_addresses.clone().is_satisfied_by(&non_blacklisted_recipient_tx)?;
44/// // This will return an error, because the recipient is not in the ACL.
45/// assert!(no_malicious_addresses.is_satisfied_by(&blacklisted_recipient_tx).is_err());
46/// Ok::<(), CoreError>(())
47/// ```
48///
49pub trait SatisfiableForArchitecture<A: Architecture> {
50    /// Indicates that the transaction request satisfies the constraint.
51    fn is_satisfied_by(self, tx: &<A as Architecture>::TransactionRequest) -> Result<(), Error>;
52}