|
| 1 | +// Bitcoin Dev Kit |
| 2 | +// Written in 2020 by Alekos Filini <[email protected]> |
| 3 | +// |
| 4 | +// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers |
| 5 | +// |
| 6 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 7 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 9 | +// You may not use this file except in accordance with one or both of these |
| 10 | +// licenses. |
| 11 | + |
| 12 | +extern crate bdk; |
| 13 | +extern crate env_logger; |
| 14 | +extern crate log; |
| 15 | +use std::error::Error; |
| 16 | + |
| 17 | +use bdk::bitcoin::Network; |
| 18 | +use bdk::descriptor::{policy::BuildSatisfaction, ExtractPolicy, IntoWalletDescriptor}; |
| 19 | +use bdk::wallet::signer::SignersContainer; |
| 20 | + |
| 21 | +/// This example describes the use of the BDK's [`bdk::descriptor::policy`] module. |
| 22 | +/// |
| 23 | +/// Policy is higher abstraction representation of the wallet descriptor spending condition. |
| 24 | +/// This is useful to express complex miniscript spending conditions into more human readable form. |
| 25 | +/// The resulting `Policy` structure can be used to derive spending conditions the wallet is capable |
| 26 | +/// to spend from. |
| 27 | +/// |
| 28 | +/// This example demos a Policy output for a 2of2 multisig between between 2 parties, where the wallet holds |
| 29 | +/// one of the Extend Private key. |
| 30 | +
|
| 31 | +fn main() -> Result<(), Box<dyn Error>> { |
| 32 | + env_logger::init_from_env( |
| 33 | + env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"), |
| 34 | + ); |
| 35 | + |
| 36 | + let secp = bitcoin::secp256k1::Secp256k1::new(); |
| 37 | + |
| 38 | + // The descriptor used in the example |
| 39 | + // The form is "wsh(multi(2, <privkey>, <pubkey>))" |
| 40 | + let desc = "wsh(multi(2,tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/*,tpubD6NzVbkrYhZ4XHndKkuB8FifXm8r5FQHwrN6oZuWCz13qb93rtgKvD4PQsqC4HP4yhV3tA2fqr2RbY5mNXfM7RxXUoeABoDtsFUq2zJq6YK/1/*))"; |
| 41 | + |
| 42 | + // Use the descriptor string to derive the full descriptor and a keymap. |
| 43 | + // The wallet descriptor can be used to create a new bdk::wallet. |
| 44 | + // While the `keymap` can be used to create a `SignerContainer`. |
| 45 | + // |
| 46 | + // The `SignerContainer` can sign for `PSBT`s. |
| 47 | + // a bdk::wallet internally uses these to handle transaction signing. |
| 48 | + // But they can be used as independent tools also. |
| 49 | + let (wallet_desc, keymap) = desc.into_wallet_descriptor(&secp, Network::Testnet)?; |
| 50 | + |
| 51 | + log::info!("Example Descriptor for policy analysis : {}", wallet_desc); |
| 52 | + |
| 53 | + // Create the signer with the keymap and descriptor. |
| 54 | + let signers_container = SignersContainer::build(keymap, &wallet_desc, &secp); |
| 55 | + |
| 56 | + // Extract the Policy from the given descriptor and signer. |
| 57 | + // Note that Policy is a wallet specific structure. It depends on the the descriptor, and |
| 58 | + // what the concerned wallet with a given signer can sign for. |
| 59 | + let policy = wallet_desc |
| 60 | + .extract_policy(&signers_container, BuildSatisfaction::None, &secp)? |
| 61 | + .expect("We expect a policy"); |
| 62 | + |
| 63 | + log::info!("Derived Policy for the descriptor {:#?}", policy); |
| 64 | + |
| 65 | + Ok(()) |
| 66 | +} |
0 commit comments