Skip to content

Commit 49072ee

Browse files
committed
WIP: Removed bdk_chain dependency
This now makes it more involved to interface this with `bdk_chain` and `bdk_wallet`. Needs some more thought.
1 parent 030018a commit 49072ee

File tree

9 files changed

+534
-494
lines changed

9 files changed

+534
-494
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ readme = "README.md"
1111
[dependencies]
1212
miniscript = { version = "12", default-features = false }
1313
bdk_coin_select = "0.4.0"
14-
bdk_chain = { version = "0.21" }
1514

1615
[dev-dependencies]
1716
anyhow = "1"
1817
bdk_tx = { path = "." }
1918
bitcoin = { version = "0.32", features = ["rand-std"] }
2019
bdk_testenv = "0.11.1"
2120
bdk_bitcoind_rpc = "0.18.0"
21+
bdk_chain = { version = "0.21" }
2222

2323
[features]
2424
default = ["std"]

src/canonical_leaves.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use alloc::sync::Arc;
2+
3+
use bitcoin::{psbt, OutPoint, Sequence, Transaction, Txid};
4+
use miniscript::{bitcoin, plan::Plan};
5+
6+
use crate::{collections::HashMap, Input, InputStatus};
7+
8+
/// Tx with confirmation status.
9+
pub type TxWithStatus<T> = (T, Option<InputStatus>);
10+
11+
/// Our canonical view.
12+
#[derive(Debug, Clone)]
13+
pub struct CanonicalLeaves {
14+
txs: HashMap<Txid, Arc<Transaction>>,
15+
statuses: HashMap<Txid, InputStatus>,
16+
spends: HashMap<OutPoint, Txid>,
17+
}
18+
19+
impl CanonicalLeaves {
20+
/// Construct.
21+
pub fn new<T>(canonical_txs: impl IntoIterator<Item = TxWithStatus<T>>) -> Self
22+
where
23+
T: Into<Arc<Transaction>>,
24+
{
25+
let mut txs = HashMap::new();
26+
let mut statuses = HashMap::new();
27+
let mut spends = HashMap::new();
28+
for (tx, status) in canonical_txs {
29+
let tx: Arc<Transaction> = tx.into();
30+
let txid = tx.compute_txid();
31+
spends.extend(tx.input.iter().map(|txin| (txin.previous_output, txid)));
32+
txs.insert(txid, tx);
33+
if let Some(status) = status {
34+
statuses.insert(txid, status);
35+
}
36+
}
37+
Self {
38+
txs,
39+
statuses,
40+
spends,
41+
}
42+
}
43+
44+
/// Whether outpoint is a leaf (unspent).
45+
pub fn is_leaf(&self, outpoint: OutPoint) -> bool {
46+
if self.spends.contains_key(&outpoint) {
47+
return false;
48+
}
49+
match self.txs.get(&outpoint.txid) {
50+
Some(tx) => {
51+
let vout: usize = outpoint.vout.try_into().expect("vout must fit into usize");
52+
vout < tx.output.len()
53+
}
54+
None => false,
55+
}
56+
}
57+
58+
/// Try get leaf (unspent) of given `outpoint`.
59+
pub fn try_get_leaf(&self, outpoint: OutPoint, plan: Plan) -> Option<Input> {
60+
if self.spends.contains_key(&outpoint) {
61+
return None;
62+
}
63+
let prev_tx = Arc::clone(self.txs.get(&outpoint.txid)?);
64+
Input::from_prev_tx(
65+
plan,
66+
prev_tx,
67+
outpoint.vout.try_into().expect("vout must fit into usize"),
68+
self.statuses.get(&outpoint.txid).cloned(),
69+
)
70+
.ok()
71+
}
72+
73+
/// Try get leaves of given `outpoints`.
74+
pub fn try_get_leaves<'a, O>(&'a self, outpoints: O) -> impl Iterator<Item = Input> + 'a
75+
where
76+
O: IntoIterator<Item = (OutPoint, Plan)>,
77+
O::IntoIter: 'a,
78+
{
79+
outpoints
80+
.into_iter()
81+
.filter_map(|(op, plan)| self.try_get_leaf(op, plan))
82+
}
83+
84+
/// Try get foreign leaf.
85+
/// TODO: Check psbt_input data with our own prev tx data.
86+
/// TODO: Create `try_get_foreign_leaves` method.
87+
pub fn try_get_foreign_leaf(
88+
&self,
89+
outpoint: OutPoint,
90+
sequence: Sequence,
91+
psbt_input: psbt::Input,
92+
satisfaction_weight: usize,
93+
) -> Option<Input> {
94+
if self.spends.contains_key(&outpoint) {
95+
return None;
96+
}
97+
let prev_tx = Arc::clone(self.txs.get(&outpoint.txid)?);
98+
let output_index: usize = outpoint.vout.try_into().expect("vout must fit into usize");
99+
let _txout = prev_tx.output.get(output_index)?;
100+
let status = self.statuses.get(&outpoint.txid).cloned();
101+
Input::from_psbt_input(outpoint, sequence, psbt_input, satisfaction_weight, status)
102+
}
103+
}

src/coin_control.rs

Lines changed: 0 additions & 237 deletions
This file was deleted.

0 commit comments

Comments
 (0)