• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitcoindevkit / bdk / 5834188079

pending completion
5834188079

Pull #1071

github

web-flow
Merge 68b42331c into 0ba6bbe11
Pull Request #1071: Update rust bitcoin (BDK 0.28)

563 of 563 new or added lines in 28 files covered. (100.0%)

14625 of 18342 relevant lines covered (79.74%)

9267.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/psbt_signer.rs
1
// Copyright (c) 2020-2022 Bitcoin Dev Kit Developers
2
//
3
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
4
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
6
// You may not use this file except in accordance with one or both of these
7
// licenses.
8

9
use bdk::blockchain::{Blockchain, ElectrumBlockchain};
10
use bdk::database::MemoryDatabase;
11
use bdk::wallet::AddressIndex;
12
use bdk::{descriptor, SyncOptions};
13
use bdk::{FeeRate, SignOptions, Wallet};
14
use bitcoin::secp256k1::Secp256k1;
15
use bitcoin::{Address, Network};
16
use electrum_client::Client;
17
use miniscript::descriptor::DescriptorSecretKey;
18
use std::error::Error;
19
use std::str::FromStr;
20

21
/// This example shows how to sign and broadcast the transaction for a PSBT (Partially Signed
22
/// Bitcoin Transaction) for a single key, witness public key hash (WPKH) based descriptor wallet.
23
/// The electrum protocol is used to sync blockchain data from the testnet bitcoin network and
24
/// wallet data is stored in an ephemeral in-memory database. The process steps are:
25
/// 1. Create a "signing" wallet and a "watch-only" wallet based on the same private keys.
26
/// 2. Deposit testnet funds into the watch only wallet.
27
/// 3. Sync the watch only wallet and create a spending transaction to return all funds to the testnet faucet.
28
/// 4. Sync the signing wallet and sign and finalize the PSBT created by the watch only wallet.
29
/// 5. Broadcast the transactions from the finalized PSBT.
30
fn main() -> Result<(), Box<dyn Error>> {
×
31
    // test key created with `bdk-cli key generate` and `bdk-cli key derive` commands
×
32
    let external_secret_xkey = DescriptorSecretKey::from_str("[e9824965/84'/1'/0']tprv8fvem7qWxY3SGCQczQpRpqTKg455wf1zgixn6MZ4ze8gRfHjov5gXBQTadNfDgqs9ERbZZ3Bi1PNYrCCusFLucT39K525MWLpeURjHwUsfX/0/*").unwrap();
×
33
    let internal_secret_xkey = DescriptorSecretKey::from_str("[e9824965/84'/1'/0']tprv8fvem7qWxY3SGCQczQpRpqTKg455wf1zgixn6MZ4ze8gRfHjov5gXBQTadNfDgqs9ERbZZ3Bi1PNYrCCusFLucT39K525MWLpeURjHwUsfX/1/*").unwrap();
×
34

×
35
    let secp = Secp256k1::new();
×
36
    let external_public_xkey = external_secret_xkey.to_public(&secp).unwrap();
×
37
    let internal_public_xkey = internal_secret_xkey.to_public(&secp).unwrap();
×
38

×
39
    let signing_external_descriptor = descriptor!(wpkh(external_secret_xkey)).unwrap();
×
40
    let signing_internal_descriptor = descriptor!(wpkh(internal_secret_xkey)).unwrap();
×
41

×
42
    let watch_only_external_descriptor = descriptor!(wpkh(external_public_xkey)).unwrap();
×
43
    let watch_only_internal_descriptor = descriptor!(wpkh(internal_public_xkey)).unwrap();
×
44

45
    // create client for Blockstream's testnet electrum server
46
    let blockchain =
×
47
        ElectrumBlockchain::from(Client::new("ssl://electrum.blockstream.info:60002")?);
×
48

49
    // create watch only wallet
50
    let watch_only_wallet: Wallet<MemoryDatabase> = Wallet::new(
×
51
        watch_only_external_descriptor,
×
52
        Some(watch_only_internal_descriptor),
×
53
        Network::Testnet,
×
54
        MemoryDatabase::default(),
×
55
    )?;
×
56

57
    // create signing wallet
58
    let signing_wallet: Wallet<MemoryDatabase> = Wallet::new(
×
59
        signing_external_descriptor,
×
60
        Some(signing_internal_descriptor),
×
61
        Network::Testnet,
×
62
        MemoryDatabase::default(),
×
63
    )?;
×
64

65
    println!("Syncing watch only wallet.");
×
66
    watch_only_wallet.sync(&blockchain, SyncOptions::default())?;
×
67

68
    // get deposit address
69
    let deposit_address = watch_only_wallet.get_address(AddressIndex::New)?;
×
70

71
    let balance = watch_only_wallet.get_balance()?;
×
72
    println!("Watch only wallet balances in SATs: {}", balance);
×
73

×
74
    if balance.get_total() < 10000 {
×
75
        println!(
×
76
            "Send at least 10000 SATs (0.0001 BTC) from the u01.net testnet faucet to address '{addr}'.\nFaucet URL: https://bitcoinfaucet.uo1.net/?to={addr}",
×
77
            addr = deposit_address.address
×
78
        );
×
79
    } else if balance.get_spendable() < 10000 {
×
80
        println!(
×
81
            "Wait for at least 10000 SATs of your wallet transactions to be confirmed...\nBe patient, this could take 10 mins or longer depending on how testnet is behaving."
×
82
        );
×
83
        for tx_details in watch_only_wallet
×
84
            .list_transactions(false)?
×
85
            .iter()
×
86
            .filter(|txd| txd.received > 0 && txd.confirmation_time.is_none())
×
87
        {
×
88
            println!(
×
89
                "See unconfirmed tx for {} SATs: https://mempool.space/testnet/tx/{}",
×
90
                tx_details.received, tx_details.txid
×
91
            );
×
92
        }
×
93
    } else {
94
        println!("Creating a PSBT sending 9800 SATs plus fee to the u01.net testnet faucet return address 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt'.");
×
95
        let return_address = Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt")?
×
96
            .require_network(Network::Testnet)?;
×
97
        let mut builder = watch_only_wallet.build_tx();
×
98
        builder
×
99
            .add_recipient(return_address.script_pubkey(), 9_800)
×
100
            .enable_rbf()
×
101
            .fee_rate(FeeRate::from_sat_per_vb(1.0));
×
102

103
        let (mut psbt, details) = builder.finish()?;
×
104
        println!("Transaction details: {:#?}", details);
×
105
        println!("Unsigned PSBT: {}", psbt);
×
106

107
        // Sign and finalize the PSBT with the signing wallet
108
        let finalized = signing_wallet.sign(&mut psbt, SignOptions::default())?;
×
109
        assert!(finalized, "The PSBT was not finalized!");
×
110
        println!("The PSBT has been signed and finalized.");
×
111

×
112
        // Broadcast the transaction
×
113
        let raw_transaction = psbt.extract_tx();
×
114
        let txid = raw_transaction.txid();
×
115

×
116
        blockchain.broadcast(&raw_transaction)?;
×
117
        println!("Transaction broadcast! TXID: {txid}.\nExplorer URL: https://mempool.space/testnet/tx/{txid}", txid = txid);
×
118
    }
119

120
    Ok(())
×
121
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc