#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Vault {
pub clawback_seconds: u64,
}
impl Default for Vault {
fn default() -> Self {
Self {
clawback_seconds: 24 * 60 * 60,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct HotWallet {
pub auto_send_limit: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CustodyPolicy {
Vault(Vault),
Hot(HotWallet),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CustodyScope {
profile: crate::id::ProfileIx,
spending_wallet: Option<chia_protocol::Bytes32>,
}
impl CustodyScope {
pub(crate) fn new(
profile: crate::id::ProfileIx,
custody: &CustodyPolicy,
hot_wallet: chia_protocol::Bytes32,
) -> Self {
Self {
profile,
spending_wallet: match custody {
CustodyPolicy::Hot(_) => Some(hot_wallet),
CustodyPolicy::Vault(_) => None,
},
}
}
pub(crate) fn assert_signable_by(
&self,
profile: crate::id::ProfileIx,
puzzle_hash: chia_protocol::Bytes32,
) -> crate::error::Result<()> {
if self.profile != profile {
return Err(crate::error::AccountError::PolicyDenied(format!(
"this permission was minted by the custody gate for profile {}, but the signer is \
for profile {}; a gate's rules bound only the wallet it was configured for",
self.profile.0, profile.0
)));
}
match self.spending_wallet {
Some(configured) if configured != puzzle_hash => {
Err(crate::error::AccountError::PolicyDenied(format!(
"this permission was minted by a hot-wallet gate configured for wallet {}, but \
the signer's wallet is {}",
hex::encode(configured),
hex::encode(puzzle_hash)
)))
}
_ => Ok(()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::id::ProfileIx;
use chia_protocol::Bytes32;
const WALLET: Bytes32 = Bytes32::new([1u8; 32]);
const OTHER: Bytes32 = Bytes32::new([2u8; 32]);
fn hot() -> CustodyPolicy {
CustodyPolicy::Hot(HotWallet::default())
}
#[test]
fn a_hot_scope_admits_its_own_profile_and_wallet() {
let scope = CustodyScope::new(ProfileIx(3), &hot(), WALLET);
assert!(scope.assert_signable_by(ProfileIx(3), WALLET).is_ok());
}
#[test]
fn a_hot_scope_refuses_another_profile() {
let scope = CustodyScope::new(ProfileIx(3), &hot(), WALLET);
let err = scope
.assert_signable_by(ProfileIx(4), WALLET)
.expect_err("a different profile must be refused");
assert!(
matches!(&err, crate::error::AccountError::PolicyDenied(m) if m.contains("profile")),
"{err:?}"
);
}
#[test]
fn a_hot_scope_refuses_a_wallet_it_was_not_configured_for() {
let scope = CustodyScope::new(ProfileIx(3), &hot(), WALLET);
let err = scope
.assert_signable_by(ProfileIx(3), OTHER)
.expect_err("a different wallet must be refused");
assert!(
matches!(&err, crate::error::AccountError::PolicyDenied(m) if m.contains("wallet")),
"{err:?}"
);
}
#[test]
fn a_vault_scope_binds_the_profile_but_not_the_wallet() {
let scope = CustodyScope::new(
ProfileIx(3),
&CustodyPolicy::Vault(Vault::default()),
WALLET,
);
assert!(
scope.assert_signable_by(ProfileIx(3), OTHER).is_ok(),
"the vault key's own puzzle hash is unknown to the gate, so it cannot be compared"
);
assert!(scope.assert_signable_by(ProfileIx(4), OTHER).is_err());
}
#[test]
fn vault_defaults_to_a_24h_clawback_window() {
assert_eq!(Vault::default().clawback_seconds, 24 * 60 * 60);
}
#[test]
fn hot_wallet_defaults_to_a_zero_auto_send_limit() {
assert_eq!(HotWallet::default().auto_send_limit, 0);
}
#[test]
fn custody_policy_variants_are_distinct() {
let vault = CustodyPolicy::Vault(Vault::default());
let hot = CustodyPolicy::Hot(HotWallet {
auto_send_limit: 1_000,
});
assert_ne!(vault, hot);
assert_eq!(vault, CustodyPolicy::Vault(Vault::default()));
}
}