2022-08-03 13:11:46 -03:00
|
|
|
pub(crate) mod tx {
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2023-02-21 12:38:16 +11:00
|
|
|
use bdk::{SignOptions, Wallet, persist};
|
2022-08-03 13:11:46 -03:00
|
|
|
use bitcoin::{Address, Transaction};
|
|
|
|
|
2023-02-21 12:38:16 +11:00
|
|
|
pub fn build_signed_tx<()>(
|
|
|
|
wallet: &Wallet<()>,
|
2022-08-03 13:11:46 -03:00
|
|
|
recipient_address: &str,
|
|
|
|
amount: u64,
|
|
|
|
) -> Transaction {
|
|
|
|
// Create a transaction builder
|
|
|
|
let mut tx_builder = wallet.build_tx();
|
|
|
|
|
|
|
|
let to_address = Address::from_str(recipient_address).unwrap();
|
|
|
|
|
2023-02-21 12:38:16 +11:00
|
|
|
|
2022-08-03 13:11:46 -03:00
|
|
|
// Set recipient of the transaction
|
|
|
|
tx_builder.set_recipients(vec![(to_address.script_pubkey(), amount)]);
|
|
|
|
|
|
|
|
// Finalise the transaction and extract PSBT
|
|
|
|
let (mut psbt, _) = tx_builder.finish().unwrap();
|
|
|
|
|
|
|
|
// Sign the above psbt with signing option
|
|
|
|
wallet.sign(&mut psbt, SignOptions::default()).unwrap();
|
|
|
|
|
|
|
|
// Extract the final transaction
|
|
|
|
psbt.extract_tx()
|
|
|
|
}
|
|
|
|
}
|