testnet faucet comment

This commit is contained in:
f321x
2024-07-07 11:27:57 +02:00
parent 48b208f7ca
commit 08e21d6ce0
2 changed files with 147 additions and 122 deletions

View File

@ -2,29 +2,29 @@
/// It includes functions to combine and broadcast the partially signed transactions (PSBTs) /// It includes functions to combine and broadcast the partially signed transactions (PSBTs)
/// from multiple participants, create a Taproot script descriptor, create a PSBT from the /// from multiple participants, create a Taproot script descriptor, create a PSBT from the
/// descriptor, and handle the case when the taker is unresponsive. /// descriptor, and handle the case when the taker is unresponsive.
use bdk::bitcoin::address::NetworkUnchecked; use bdk::bitcoin::address::NetworkUnchecked;
use bitcoin::address::NetworkChecked;
use bitcoin::Address;
use bdk::descriptor::Descriptor;
use bdk::miniscript::psbt::PsbtExt;
use bdk::bitcoin::psbt::PartiallySignedTransaction; use bdk::bitcoin::psbt::PartiallySignedTransaction;
use bdk::blockchain::EsploraBlockchain;
use bdk::SignOptions;
use bdk::bitcoin::secp256k1::Secp256k1; use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::blockchain::EsploraBlockchain;
use bdk::database::MemoryDatabase;
use bdk::descriptor::Descriptor;
use bdk::miniscript::descriptor::TapTree; use bdk::miniscript::descriptor::TapTree;
use bdk::miniscript::policy::Concrete; use bdk::miniscript::policy::Concrete;
use std::sync::Arc; use bdk::miniscript::psbt::PsbtExt;
use bdk::database::MemoryDatabase;
use bdk::wallet::AddressIndex; use bdk::wallet::AddressIndex;
use bdk::{FeeRate, Wallet, KeychainKind, SyncOptions}; use bdk::SignOptions;
use bdk::{FeeRate, KeychainKind, SyncOptions, Wallet};
use bitcoin::address::NetworkChecked;
use bitcoin::Address;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc;
/// The main function in this module is `combine_and_broadcast`, which combines the PSBTs /// The main function in this module is `combine_and_broadcast`, which combines the PSBTs
/// from the maker and taker, finalizes the transaction, and broadcasts it on the blockchain. /// from the maker and taker, finalizes the transaction, and broadcasts it on the blockchain.
pub async fn combine_and_broadcast() -> Result<(), Box<dyn std::error::Error>> { pub async fn combine_and_broadcast() -> Result<(), Box<dyn std::error::Error>> {
let mut base_psbt = PartiallySignedTransaction::from_str("TODO: insert the psbt created in step 3 here")?; let mut base_psbt =
PartiallySignedTransaction::from_str("TODO: insert the psbt created in step 3 here")?;
let signed_psbts = vec![ let signed_psbts = vec![
// TODO: Paste each participant's PSBT here // TODO: Paste each participant's PSBT here
"makers_psbt", "makers_psbt",
@ -46,19 +46,31 @@ pub async fn combine_and_broadcast() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
/// Other functions include `create_script`, which creates a Taproot script descriptor from /// Other functions include `create_script`, which creates a Taproot script descriptor from
async fn create_script(coordinator_key: &str, maker_key:&str, taker_key:&str ) -> Result<(bdk::descriptor::Descriptor<std::string::String>), Box<dyn std::error::Error>> { async fn create_script(
coordinator_key: &str,
maker_key: &str,
taker_key: &str,
) -> Result<(bdk::descriptor::Descriptor<std::string::String>), Box<dyn std::error::Error>> {
// let maker_key = "020202020202020202020202020202020202020202020202020202020202020202"; // let maker_key = "020202020202020202020202020202020202020202020202020202020202020202";
// let taker_key = "03833be68fb7559c0e62ffdbb6d46cc44a58c19c6ba82e51144b583cff0519c791"; // let taker_key = "03833be68fb7559c0e62ffdbb6d46cc44a58c19c6ba82e51144b583cff0519c791";
// let coordinator_key = "03b2f6e8abf3624f8e9b93f7b2567b158c15b0f20ab368f9fcb2d9251d6a788d09"; // let coordinator_key = "03b2f6e8abf3624f8e9b93f7b2567b158c15b0f20ab368f9fcb2d9251d6a788d09";
// Define policies based on the scripts provided // Define policies based on the scripts provided
let script_a = format!("and(and(after(escrow_timer),pk({})),pk({}))", maker_key, coordinator_key); let script_a = format!(
let script_b = format!("and_v(v:pk({}),and_v(v:pk({}),pk({})))", maker_key, taker_key, coordinator_key); "and(and(after(escrow_timer),pk({})),pk({}))",
maker_key, coordinator_key
);
let script_b = format!(
"and_v(v:pk({}),and_v(v:pk({}),pk({})))",
maker_key, taker_key, coordinator_key
);
let script_c = format!("and(pk({}),pk({}))", maker_key, coordinator_key); let script_c = format!("and(pk({}),pk({}))", maker_key, coordinator_key);
let script_d = format!("and(pk({}),pk({}))", taker_key, coordinator_key); let script_d = format!("and(pk({}),pk({}))", taker_key, coordinator_key);
let script_e = format!("and(pk({}),after(very_long_timelock))", maker_key); let script_e = format!("and(pk({}),after(very_long_timelock))", maker_key);
let script_f = format!("and_v(and_v(v:pk({}),v:pk({})),after(2048))", maker_key, taker_key); let script_f = format!(
"and_v(and_v(v:pk({}),v:pk({})),after(2048))",
maker_key, taker_key
);
// Compile the policies // Compile the policies
let compiled_a = Concrete::<String>::from_str(&script_a)?.compile()?; let compiled_a = Concrete::<String>::from_str(&script_a)?.compile()?;
@ -80,33 +92,39 @@ async fn create_script(coordinator_key: &str, maker_key:&str, taker_key:&str ) -
let tap_tree = TapTree::Tree(Arc::new(tap_leaf_a), Arc::new(tap_leaf_b)); let tap_tree = TapTree::Tree(Arc::new(tap_leaf_a), Arc::new(tap_leaf_b));
// Define a dummy internal key (replace with an actual key) // Define a dummy internal key (replace with an actual key)
let dummy_internal_key = "020202020202020202020202020202020202020202020202020202020202020202".to_string(); let dummy_internal_key =
"020202020202020202020202020202020202020202020202020202020202020202".to_string();
// Create the descriptor // Create the descriptor
let descriptor = Descriptor::new_tr(dummy_internal_key, Some(tap_tree))?; let descriptor = Descriptor::new_tr(dummy_internal_key, Some(tap_tree))?;
println!("{}", descriptor); println!("{}", descriptor);
Ok(descriptor) Ok(descriptor)
} }
/// the provided keys, and `create_psbt`, which creates a PSBT from the descriptor /// the provided keys, and `create_psbt`, which creates a PSBT from the descriptor
/// Figure out how to put UTXO's /// Figure out how to put UTXO's
pub async fn create_psbt(descriptor: Descriptor<String>)-> Result<(PartiallySignedTransaction), Box<dyn std::error::Error>> { pub async fn create_psbt(
descriptor: Descriptor<String>,
) -> Result<(PartiallySignedTransaction), Box<dyn std::error::Error>> {
// Step 1: Create a BDK wallet // Step 1: Create a BDK wallet
let wallet = Wallet::new( let wallet = Wallet::new(
// TODO: insert your descriptor here // TODO: insert your descriptor here
"tr(youshouldputyourdescriptorhere)", "tr(youshouldputyourdescriptorhere)",
None, None,
bdk::bitcoin::Network::Testnet, bdk::bitcoin::Network::Testnet,
MemoryDatabase::new() MemoryDatabase::new(),
)?; )?;
// Step 2: Print the first address // Step 2: Print the first address
println!("Deposit funds here: {:?}", wallet.get_address(AddressIndex::New)?); println!(
"Deposit funds here: {:?}",
wallet.get_address(AddressIndex::New)?
);
// Step 3: Deposit funds // Step 3: Deposit funds
// Use some testnet faucet, such as https://bitcoinfaucet.uo1.net/send.php // Use some testnet faucet, such as https://bitcoinfaucet.uo1.net/send.php
// https://coinfaucet.eu/en/btc-testnet4/
// Step 4: Print balance // Step 4: Print balance
let blockchain = EsploraBlockchain::new("https://blockstream.info/testnet/api", 20); let blockchain = EsploraBlockchain::new("https://blockstream.info/testnet/api", 20);
@ -120,7 +138,6 @@ pub async fn create_psbt(descriptor: Descriptor<String>)-> Result<(PartiallySign
// Recipient address (where funds will be sent) // Recipient address (where funds will be sent)
let recipient_address = Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt")?; let recipient_address = Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt")?;
// Build the PSBT // Build the PSBT
let mut tx_builder = wallet.build_tx(); let mut tx_builder = wallet.build_tx();
tx_builder tx_builder
@ -136,11 +153,16 @@ pub async fn create_psbt(descriptor: Descriptor<String>)-> Result<(PartiallySign
Ok(psbt) Ok(psbt)
} }
/// The `taker_unresponsive` function handles the case when the taker is unresponsive and /// The `taker_unresponsive` function handles the case when the taker is unresponsive and
/// the coordinator needs to sign the PSBT using an alternative path. /// the coordinator needs to sign the PSBT using an alternative path.
// TODO: Figure out how to use UTXO's // TODO: Figure out how to use UTXO's
fn taker_unresponsive(psbt: PartiallySignedTransaction, wallet: Wallet<MemoryDatabase>, maker_utxos: Vec<UTXO>, taker_utxos: Vec<UTXO>, recipient_address: Address<NetworkChecked>) -> Result<(), Box<dyn std::error::Error>> { fn taker_unresponsive(
psbt: PartiallySignedTransaction,
wallet: Wallet<MemoryDatabase>,
maker_utxos: Vec<UTXO>,
taker_utxos: Vec<UTXO>,
recipient_address: Address<NetworkChecked>,
) -> Result<(), Box<dyn std::error::Error>> {
// Maker signs the PSBT // Maker signs the PSBT
let maker_signed_psbt = wallet.sign(&mut psbt.clone(), SignOptions::default())?; let maker_signed_psbt = wallet.sign(&mut psbt.clone(), SignOptions::default())?;
println!("Maker signed PSBT: {:?}", maker_signed_psbt); println!("Maker signed PSBT: {:?}", maker_signed_psbt);
@ -149,7 +171,10 @@ fn taker_unresponsive(psbt: PartiallySignedTransaction, wallet: Wallet<MemoryDat
let taker_responsive = false; // Assume taker is unresponsive let taker_responsive = false; // Assume taker is unresponsive
if !taker_responsive { if !taker_responsive {
let mut path = BTreeMap::new(); let mut path = BTreeMap::new();
path.insert(wallet.policies(KeychainKind::External)?.unwrap().id, vec![1]); // Path for coordinator and maker path.insert(
wallet.policies(KeychainKind::External)?.unwrap().id,
vec![1],
); // Path for coordinator and maker
let mut coordinator_tx_builder = wallet.build_tx(); let mut coordinator_tx_builder = wallet.build_tx();
coordinator_tx_builder coordinator_tx_builder