+use std::collections::BTreeMap;
use std::str::FromStr;
use clap::{App, Arg, ArgMatches, SubCommand};
Arg::with_name("policy")
.long("policy")
.value_name("POLICY")
- .help("Selects which policy will be used to satisfy the descriptor")
+ .help("Selects which policy should be used to satisfy the descriptor")
.takes_value(true)
.number_of_values(1),
),
let unspendable = sub_matches
.values_of("unspendable")
.map(|s| s.map(|i| parse_outpoint(i).unwrap()).collect());
- let policy: Option<Vec<_>> = sub_matches
+ let policy: Option<_> = sub_matches
.value_of("policy")
- .map(|s| serde_json::from_str::<Vec<Vec<usize>>>(&s).unwrap());
+ .map(|s| serde_json::from_str::<BTreeMap<String, Vec<usize>>>(&s).unwrap());
let result = wallet.create_tx(
addressees,
#[allow(unused_imports)]
use log::{debug, error, info, trace};
+use super::checksum::get_checksum;
use super::error::Error;
use crate::descriptor::{Key, MiniscriptExtractPolicy};
use crate::psbt::PSBTSatisfier;
_ => true,
}
}
+
+ pub fn id(&self) -> String {
+ get_checksum(&serde_json::to_string(self).expect("Failed to serialize a SatisfiableItem"))
+ .expect("Failed to compute a SatisfiableItem id")
+ }
}
fn combinations(vec: &Vec<usize>, size: usize) -> Vec<Vec<usize>> {
#[derive(Debug, Clone, Serialize)]
pub struct Policy {
+ id: String,
+
#[serde(flatten)]
item: SatisfiableItem,
satisfaction: Satisfaction,
#[derive(Debug)]
pub enum PolicyError {
- NotEnoughItemsSelected(usize),
- TooManyItemsSelected(usize),
+ NotEnoughItemsSelected(String),
+ TooManyItemsSelected(String),
IndexOutOfRange(usize),
AddOnLeaf,
AddOnPartialComplete,
impl Policy {
pub fn new(item: SatisfiableItem) -> Self {
Policy {
+ id: item.id(),
item,
satisfaction: Satisfaction::None,
contribution: Satisfaction::None,
}
pub fn requires_path(&self) -> bool {
- self.get_requirements(&vec![]).is_err()
- }
-
- pub fn get_requirements(&self, path: &Vec<Vec<usize>>) -> Result<Condition, PolicyError> {
- self.recursive_get_requirements(path, 0)
+ self.get_requirements(&BTreeMap::new()).is_err()
}
- fn recursive_get_requirements(
+ pub fn get_requirements(
&self,
- path: &Vec<Vec<usize>>,
- index: usize,
+ path: &BTreeMap<String, Vec<usize>>,
) -> Result<Condition, PolicyError> {
// if items.len() == threshold, selected can be omitted and we take all of them by default
let default = match &self.item {
}
_ => vec![],
};
- let selected = match path.get(index) {
+ let selected = match path.get(&self.id) {
_ if !default.is_empty() => &default,
Some(arr) => arr,
_ => &default,
SatisfiableItem::Thresh { items, threshold } => {
let mapped_req = items
.iter()
- .map(|i| i.recursive_get_requirements(path, index + 1))
+ .map(|i| i.get_requirements(path))
.collect::<Result<Vec<_>, _>>()?;
// if all the requirements are null we don't care about `selected` because there
// an empty value for this step in case of n-of-n, because `selected` is set to all
// the elements above
if selected.len() < *threshold {
- return Err(PolicyError::NotEnoughItemsSelected(index));
+ return Err(PolicyError::NotEnoughItemsSelected(self.id.clone()));
+ } else if selected.len() > *threshold {
+ return Err(PolicyError::TooManyItemsSelected(self.id.clone()));
}
// check the selected items, see if there are conflicting requirements
Ok(requirements)
}
- _ if !selected.is_empty() => Err(PolicyError::TooManyItemsSelected(index)),
+ _ if !selected.is_empty() => Err(PolicyError::TooManyItemsSelected(self.id.clone())),
SatisfiableItem::AbsoluteTimelock { value } => Ok(Condition {
csv: None,
timelock: Some(*value),
addressees: Vec<(Address, u64)>,
send_all: bool,
fee_perkb: f32,
- policy_path: Option<Vec<Vec<usize>>>,
+ policy_path: Option<BTreeMap<String, Vec<usize>>>,
utxos: Option<Vec<OutPoint>>,
unspendable: Option<Vec<OutPoint>>,
) -> Result<(PSBT, TransactionDetails), Error> {
if policy.requires_path() && policy_path.is_none() {
return Err(Error::SpendingPolicyRequired);
}
- let requirements = policy.get_requirements(&policy_path.unwrap_or(vec![]))?;
+ let requirements = policy.get_requirements(&policy_path.unwrap_or(BTreeMap::new()))?;
debug!("requirements: {:?}", requirements);
let mut tx = Transaction {