Add pubkey to BudgetExpr::new_cancelable_future_payment for wallet

This commit is contained in:
Greg Fitzgerald 2019-03-18 06:12:21 -06:00 committed by Grimes
parent 3369019943
commit ea01ff2aab
2 changed files with 18 additions and 28 deletions

View File

@ -98,35 +98,37 @@ impl BudgetExpr {
) )
} }
/// Create a budget that pays `lamports` to `to` after the given DateTime. /// Create a budget that pays `lamports` to `to` after the given DateTime signed
/// by `dt_pubkey`.
pub fn new_future_payment( pub fn new_future_payment(
dt: DateTime<Utc>, dt: DateTime<Utc>,
from: &Pubkey, dt_pubkey: &Pubkey,
lamports: u64, lamports: u64,
to: &Pubkey, to: &Pubkey,
) -> Self { ) -> Self {
BudgetExpr::After( BudgetExpr::After(
Condition::Timestamp(dt, *from), Condition::Timestamp(dt, *dt_pubkey),
Box::new(Self::new_payment(lamports, to)), Box::new(Self::new_payment(lamports, to)),
) )
} }
/// Create a budget that pays `lamports` to `to` after the given DateTime /// Create a budget that pays `lamports` to `to` after the given DateTime
/// unless cancelled by `from`. /// signed by `dt_pubkey` unless canceled by `from`.
pub fn new_cancelable_future_payment( pub fn new_cancelable_future_payment(
dt: DateTime<Utc>, dt: DateTime<Utc>,
from: &Pubkey, dt_pubkey: &Pubkey,
lamports: u64, lamports: u64,
to: &Pubkey, to: &Pubkey,
from: &Pubkey,
) -> Self { ) -> Self {
BudgetExpr::Or( BudgetExpr::Or(
( (
Condition::Timestamp(dt, *from), Condition::Timestamp(dt, *dt_pubkey),
Box::new(Self::new_payment(lamports, to)), Box::new(Self::new_payment(lamports, to)),
), ),
( (
Condition::Signature(*from), Condition::Signature(*from),
Box::new(Self::new_payment(lamports, to)), Box::new(Self::new_payment(lamports, from)),
), ),
) )
} }
@ -211,7 +213,7 @@ mod tests {
assert!(BudgetExpr::new_payment(42, &to).verify(42)); assert!(BudgetExpr::new_payment(42, &to).verify(42));
assert!(BudgetExpr::new_authorized_payment(&from, 42, &to).verify(42)); assert!(BudgetExpr::new_authorized_payment(&from, 42, &to).verify(42));
assert!(BudgetExpr::new_future_payment(dt, &from, 42, &to).verify(42)); assert!(BudgetExpr::new_future_payment(dt, &from, 42, &to).verify(42));
assert!(BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to).verify(42)); assert!(BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from).verify(42));
} }
#[test] #[test]
@ -255,11 +257,11 @@ mod tests {
let from = Pubkey::default(); let from = Pubkey::default();
let to = Pubkey::default(); let to = Pubkey::default();
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to); let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from);
expr.apply_witness(&Witness::Timestamp(dt), &from); expr.apply_witness(&Witness::Timestamp(dt), &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &to)); assert_eq!(expr, BudgetExpr::new_payment(42, &to));
let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to); let mut expr = BudgetExpr::new_cancelable_future_payment(dt, &from, 42, &to, &from);
expr.apply_witness(&Witness::Signature, &from); expr.apply_witness(&Witness::Signature, &from);
assert_eq!(expr, BudgetExpr::new_payment(42, &from)); assert_eq!(expr, BudgetExpr::new_payment(42, &from));
} }

View File

@ -1,4 +1,4 @@
use crate::budget_expr::{BudgetExpr, Condition}; use crate::budget_expr::BudgetExpr;
use crate::budget_instruction::BudgetInstruction; use crate::budget_instruction::BudgetInstruction;
use crate::budget_state::BudgetState; use crate::budget_state::BudgetState;
use crate::id; use crate::id;
@ -43,22 +43,10 @@ impl BudgetScript {
cancelable: Option<Pubkey>, cancelable: Option<Pubkey>,
lamports: u64, lamports: u64,
) -> Script { ) -> Script {
let expr = if let Some(from) = cancelable { let expr = if let Some(from) = &cancelable {
BudgetExpr::Or( BudgetExpr::new_cancelable_future_payment(dt, dt_pubkey, lamports, to, from)
(
Condition::Timestamp(dt, *dt_pubkey),
Box::new(BudgetExpr::new_payment(lamports, to)),
),
(
Condition::Signature(from),
Box::new(BudgetExpr::new_payment(lamports, &from)),
),
)
} else { } else {
BudgetExpr::After( BudgetExpr::new_future_payment(dt, dt_pubkey, lamports, to)
Condition::Timestamp(dt, *dt_pubkey),
Box::new(BudgetExpr::new_payment(lamports, to)),
)
}; };
Self::new_account(from, contract, lamports, expr) Self::new_account(from, contract, lamports, expr)
@ -73,8 +61,8 @@ impl BudgetScript {
cancelable: Option<Pubkey>, cancelable: Option<Pubkey>,
lamports: u64, lamports: u64,
) -> Script { ) -> Script {
let expr = if let Some(from) = cancelable { let expr = if let Some(from) = &cancelable {
BudgetExpr::new_cancelable_authorized_payment(witness, lamports, to, &from) BudgetExpr::new_cancelable_authorized_payment(witness, lamports, to, from)
} else { } else {
BudgetExpr::new_authorized_payment(witness, lamports, to) BudgetExpr::new_authorized_payment(witness, lamports, to)
}; };