Use TxBuilder in PSBT constructor
This commit is contained in:
parent
a39fc787d5
commit
e5aa51c3f8
28
src/lib.rs
28
src/lib.rs
@ -112,6 +112,7 @@ impl Progress for BdkProgressHolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct PartiallySignedBitcoinTransaction {
|
struct PartiallySignedBitcoinTransaction {
|
||||||
internal: Mutex<PartiallySignedTransaction>,
|
internal: Mutex<PartiallySignedTransaction>,
|
||||||
}
|
}
|
||||||
@ -123,25 +124,16 @@ impl PartiallySignedBitcoinTransaction {
|
|||||||
amount: u64,
|
amount: u64,
|
||||||
fee_rate: Option<f32>, // satoshis per vbyte
|
fee_rate: Option<f32>, // satoshis per vbyte
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
let wallet = wallet.get_wallet();
|
let mut tx_builder = TxBuilder::new().add_recipient(recipient, amount);
|
||||||
match Address::from_str(&recipient) {
|
if let Some(sat_per_vb) = fee_rate {
|
||||||
Ok(address) => {
|
tx_builder = tx_builder.fee_rate(sat_per_vb);
|
||||||
let (psbt, _details) = {
|
|
||||||
let mut builder = wallet.build_tx();
|
|
||||||
builder.add_recipient(address.script_pubkey(), amount);
|
|
||||||
if let Some(sat_per_vb) = fee_rate {
|
|
||||||
builder.fee_rate(FeeRate::from_sat_per_vb(sat_per_vb));
|
|
||||||
}
|
|
||||||
builder.finish()?
|
|
||||||
};
|
|
||||||
Ok(PartiallySignedBitcoinTransaction {
|
|
||||||
internal: Mutex::new(psbt),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Err(..) => Err(BdkError::Generic(
|
|
||||||
"failed to read wallet address".to_string(),
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
|
tx_builder
|
||||||
|
.build(wallet)
|
||||||
|
.map(|a| {
|
||||||
|
Arc::<PartiallySignedBitcoinTransaction>::try_unwrap(a).expect("unwrap Arc failed")
|
||||||
|
})
|
||||||
|
.map_err(|_| BdkError::Generic("failed to create PSBT".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deserialize(psbt_base64: String) -> Result<Self, Error> {
|
pub fn deserialize(psbt_base64: String) -> Result<Self, Error> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user