Skip to content

Commit

Permalink
feat: expose policy::satisfaction
Browse files Browse the repository at this point in the history
  • Loading branch information
BitcoinZavior committed Nov 2, 2024
1 parent c79b378 commit d22510f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
15 changes: 15 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ interface Policy {
boolean requires_path();

SatisfiableItem item();

Satisfaction satisfaction();
};

[Enum]
Expand Down Expand Up @@ -481,6 +483,19 @@ interface LockTime {
Seconds(u32 consensus_time);
};

[Enum]
interface Satisfaction {
Partial(u64 n, u64 m, sequence<u64> items, boolean? sorted, record<u32, sequence<Condition>> conditions);
PartialComplete(u64 n, u64 m, sequence<u64> items, boolean? sorted, record<sequence<u32>, sequence<Condition>> conditions);
Complete(Condition condition);
None(string msg);
};

dictionary Condition {
u32? csv;
LockTime? timelock;
};

interface TxBuilder {
constructor();

Expand Down
2 changes: 2 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use crate::types::Balance;
use crate::types::BlockId;
use crate::types::CanonicalTx;
use crate::types::ChainPosition;
use crate::types::Condition;
use crate::types::ConfirmationBlockTime;
use crate::types::FullScanRequest;
use crate::types::FullScanRequestBuilder;
Expand All @@ -60,6 +61,7 @@ use crate::types::LocalOutput;
use crate::types::LockTime;
use crate::types::PkOrF;
use crate::types::Policy;
use crate::types::Satisfaction;
use crate::types::SatisfiableItem;
use crate::types::ScriptAmount;
use crate::types::SentAndReceivedValues;
Expand Down
101 changes: 100 additions & 1 deletion bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use bdk_wallet::chain::{
};

use bdk_wallet::descriptor::policy::{
PkOrF as BdkPkOrF, Policy as BdkPolicy, SatisfiableItem as BdkSatisfiableItem,
Condition as BdkCondition, PkOrF as BdkPkOrF, Policy as BdkPolicy,
Satisfaction as BdkSatisfaction, SatisfiableItem as BdkSatisfiableItem,
};
use bdk_wallet::AddressInfo as BdkAddressInfo;
use bdk_wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bdk_wallet::Update as BdkUpdate;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

#[derive(Debug)]
Expand Down Expand Up @@ -268,9 +270,14 @@ impl Policy {
pub fn requires_path(&self) -> bool {
self.0.requires_path()
}

pub fn item(&self) -> SatisfiableItem {
self.0.item.clone().into()
}

pub fn satisfaction(&self) -> Satisfaction {
self.0.satisfaction.clone().into()
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -393,3 +400,95 @@ impl From<BdkLockTime> for LockTime {
}
}
}
#[derive(Debug, Clone)]
pub enum Satisfaction {
Partial {
n: u64,
m: u64,
items: Vec<u64>,
sorted: Option<bool>,
conditions: HashMap<u32, Vec<Condition>>,
},
PartialComplete {
n: u64,
m: u64,
items: Vec<u64>,
sorted: Option<bool>,
conditions: HashMap<Vec<u32>, Vec<Condition>>,
},
Complete {
condition: Condition,
},

None {
msg: String,
},
}
impl From<BdkSatisfaction> for Satisfaction {
fn from(value: BdkSatisfaction) -> Self {
match value {
BdkSatisfaction::Partial {
n,
m,
items,
sorted,
conditions,
} => Satisfaction::Partial {
n: n as u64,
m: m as u64,
items: items.iter().map(|e| e.to_owned() as u64).collect(),
sorted,
conditions: conditions
.into_iter()
.map(|(index, conditions)| {
(
index as u32,
conditions.into_iter().map(|e| e.into()).collect(),
)
})
.collect(),
},
BdkSatisfaction::PartialComplete {
n,
m,
items,
sorted,
conditions,
} => Satisfaction::PartialComplete {
n: n as u64,
m: m as u64,
items: items.iter().map(|e| e.to_owned() as u64).collect(),
sorted,
conditions: conditions
.into_iter()
.map(|(index, conditions)| {
(
index.iter().map(|e| e.to_owned() as u32).collect(),
conditions.into_iter().map(|e| e.into()).collect(), // Convert each `Condition` to `YourType`
)
})
.collect(),
},
BdkSatisfaction::Complete { condition } => Satisfaction::Complete {
condition: condition.into(),
},
BdkSatisfaction::None => Satisfaction::None {
msg: "Cannot satisfy or contribute to the policy item".to_string(),
},
}
}
}

#[derive(Debug, Clone)]
pub struct Condition {
pub csv: Option<u32>,
pub timelock: Option<LockTime>,
}
impl From<BdkCondition> for Condition {
fn from(value: BdkCondition) -> Self {
Condition {
csv: value.csv.map(|e| e.to_consensus_u32()),
timelock: value.timelock.map(|e| e.into()),
}
}
}

0 comments on commit d22510f

Please sign in to comment.