Skip to content

Instantly share code, notes, and snippets.

@afilini
Last active May 21, 2022 18:44
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save afilini/7c2c33af095ea975f52f5d68302c91d6 to your computer and use it in GitHub Desktop.
taproot.rs
// Bitcoin Dev Kit
// Written in 2021 by Alekos Filini <alekos.filini@gmail.com>
//
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.
// bdk from https://github.com/afilini/bdk.git branch taproot-testing
use std::str::FromStr;
use std::sync::Arc;
use bdk::bitcoin::consensus::encode::{serialize_hex, deserialize};
use bdk::bitcoin::{self, Network};
use bdk::database::MemoryDatabase;
use bdk::wallet::*;
use bdk::wallet::tx_builder::TxOrdering;
use bdk::bitcoin::hashes::hex::FromHex;
use bdk::*;
use bdk::blockchain::electrum::*;
use bdk::blockchain::esplora::*;
use bdk::blockchain::*;
use bdk::electrum_client;
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let fee_rate = FeeRate::from_sat_per_vb(20.0);
let config = ElectrumBlockchainConfig {
url: "my-electrum-onion".to_string(),
socks5: Some("socks5://127.0.0.1:9050".to_string()),
retry: 10,
timeout: None,
stop_gap: 100,
};
let blockchain = Arc::new(ElectrumBlockchain::from_config(&config)?);
// ------ TAPROOT -------
let unspendable_key = bitcoin::PublicKey::from_str("020000000000000000000000000000000000000000000000000000000000000001").unwrap();
let taproot_key = bitcoin::PrivateKey::from_str("<redacted>").unwrap();
let taproot_key_2 = bitcoin::PrivateKey::from_str("<redacted>").unwrap();
let taproot_wallet = Wallet::new(
bdk::descriptor!(tr(unspendable_key,multi_a(1,taproot_key,taproot_key_2)))?,
None,
Network::Bitcoin,
MemoryDatabase::new(),
Arc::clone(&blockchain),
)?;
println!(
"desc: {}",
taproot_wallet.public_descriptor(KeychainKind::External)?.unwrap()
);
let taproot_addr = taproot_wallet.get_address(AddressIndex::New).unwrap();
println!("taproot_addr: {}", taproot_addr);
taproot_wallet.sync(noop_progress(), None)?;
println!("taproot_wallet balance: {}", taproot_wallet.get_balance()?);
let (mut send_back_psbt, details) = {
let mut builder = taproot_wallet.build_tx();
builder
.drain_to(p2pkh_addr.script_pubkey())
.add_data("gm taproot \u{1F955} https://bitcoindevkit.org".as_bytes())
.ordering(TxOrdering::Untouched)
.drain_wallet()
.enable_rbf()
.fee_rate(fee_rate);
builder.finish()?
};
let finalized = taproot_wallet.sign(&mut send_back_psbt, SignOptions::default())?;
dbg!(finalized);
let send_back_tx = send_back_psbt.extract_tx();
println!("send_back_tx: {}", serialize_hex(&send_back_tx));
taproot_wallet.broadcast(&send_back_tx)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment