2021-03-03 13:22:05 -08:00
|
|
|
// Bitcoin Dev Kit
|
|
|
|
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
|
2020-08-31 11:26:36 +02:00
|
|
|
//
|
2021-03-03 13:22:05 -08:00
|
|
|
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
|
2020-08-31 11:26:36 +02:00
|
|
|
//
|
2021-03-03 13:22:05 -08:00
|
|
|
// 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.
|
2020-08-31 11:26:36 +02:00
|
|
|
|
2020-09-14 14:25:38 +02:00
|
|
|
extern crate bdk;
|
2020-04-29 11:52:45 +02:00
|
|
|
extern crate bitcoin;
|
|
|
|
extern crate clap;
|
|
|
|
extern crate log;
|
|
|
|
extern crate miniscript;
|
|
|
|
extern crate serde_json;
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
use std::error::Error;
|
2020-04-29 11:52:45 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use log::info;
|
|
|
|
|
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
use bitcoin::Network;
|
|
|
|
use miniscript::policy::Concrete;
|
|
|
|
use miniscript::Descriptor;
|
|
|
|
|
2020-09-14 14:25:38 +02:00
|
|
|
use bdk::database::memory::MemoryDatabase;
|
2021-03-08 16:17:10 -08:00
|
|
|
use bdk::wallet::AddressIndex::New;
|
2020-12-23 13:48:17 +11:00
|
|
|
use bdk::{KeychainKind, Wallet};
|
2020-04-29 11:52:45 +02:00
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2020-04-29 11:52:45 +02:00
|
|
|
env_logger::init_from_env(
|
|
|
|
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = App::new("Miniscript Compiler")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("POLICY")
|
|
|
|
.help("Sets the spending policy to compile")
|
|
|
|
.required(true)
|
|
|
|
.index(1),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("TYPE")
|
|
|
|
.help("Sets the script type used to embed the compiled policy")
|
|
|
|
.required(true)
|
|
|
|
.index(2)
|
|
|
|
.possible_values(&["sh", "wsh", "sh-wsh"]),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("parsed_policy")
|
|
|
|
.long("parsed_policy")
|
|
|
|
.short("p")
|
|
|
|
.help("Also return the parsed spending policy in JSON format"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("network")
|
|
|
|
.short("n")
|
|
|
|
.long("network")
|
|
|
|
.help("Sets the network")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("testnet")
|
2021-02-05 10:23:17 -05:00
|
|
|
.possible_values(&["testnet", "regtest", "bitcoin", "signet"]),
|
2020-04-29 11:52:45 +02:00
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let policy_str = matches.value_of("POLICY").unwrap();
|
|
|
|
info!("Compiling policy: {}", policy_str);
|
|
|
|
|
2021-02-02 20:06:40 -05:00
|
|
|
let policy = Concrete::<String>::from_str(&policy_str)?;
|
2020-04-29 11:52:45 +02:00
|
|
|
|
|
|
|
let descriptor = match matches.value_of("TYPE").unwrap() {
|
2021-02-02 20:06:40 -05:00
|
|
|
"sh" => Descriptor::new_sh(policy.compile()?)?,
|
|
|
|
"wsh" => Descriptor::new_wsh(policy.compile()?)?,
|
|
|
|
"sh-wsh" => Descriptor::new_sh_wsh(policy.compile()?)?,
|
2020-04-29 11:52:45 +02:00
|
|
|
_ => panic!("Invalid type"),
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("... Descriptor: {}", descriptor);
|
|
|
|
|
2020-08-08 09:37:25 +02:00
|
|
|
let database = MemoryDatabase::new();
|
2020-04-29 11:52:45 +02:00
|
|
|
|
2021-02-05 10:23:17 -05:00
|
|
|
let network = matches
|
|
|
|
.value_of("network")
|
2021-02-10 11:36:12 +11:00
|
|
|
.map(|n| Network::from_str(n))
|
2021-02-05 10:23:17 -05:00
|
|
|
.transpose()
|
|
|
|
.unwrap()
|
|
|
|
.unwrap_or(Network::Testnet);
|
2021-02-02 20:06:40 -05:00
|
|
|
let wallet = Wallet::new_offline(&format!("{}", descriptor), None, network, database)?;
|
2020-08-08 09:37:25 +02:00
|
|
|
|
2021-03-08 16:17:10 -08:00
|
|
|
info!("... First address: {}", wallet.get_address(New)?);
|
2020-08-08 09:37:25 +02:00
|
|
|
|
|
|
|
if matches.is_present("parsed_policy") {
|
2021-02-02 20:06:40 -05:00
|
|
|
let spending_policy = wallet.policies(KeychainKind::External)?;
|
2020-08-08 09:37:25 +02:00
|
|
|
info!(
|
|
|
|
"... Spending policy:\n{}",
|
2021-02-02 20:06:40 -05:00
|
|
|
serde_json::to_string_pretty(&spending_policy)?
|
2020-08-08 09:37:25 +02:00
|
|
|
);
|
|
|
|
}
|
2021-02-02 20:06:40 -05:00
|
|
|
|
|
|
|
Ok(())
|
2020-04-29 11:52:45 +02:00
|
|
|
}
|