From f0a1e670df197e4cc495ee447cb7921881e8030c Mon Sep 17 00:00:00 2001 From: Alekos Filini Date: Sat, 8 Aug 2020 09:37:25 +0200 Subject: [PATCH] [examples] Use `MemoryDatabase` in the compiler example --- examples/compiler.rs | 51 ++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/examples/compiler.rs b/examples/compiler.rs index 04558fba..bc7dc63a 100644 --- a/examples/compiler.rs +++ b/examples/compiler.rs @@ -3,23 +3,19 @@ extern crate clap; extern crate log; extern crate magical_bitcoin_wallet; extern crate miniscript; -extern crate rand; extern crate serde_json; -extern crate sled; use std::str::FromStr; use log::info; -use rand::distributions::Alphanumeric; -use rand::{thread_rng, Rng}; - use clap::{App, Arg}; use bitcoin::Network; use miniscript::policy::Concrete; use miniscript::Descriptor; +use magical_bitcoin_wallet::database::memory::MemoryDatabase; use magical_bitcoin_wallet::types::ScriptType; use magical_bitcoin_wallet::{OfflineWallet, Wallet}; @@ -73,37 +69,22 @@ fn main() { info!("... Descriptor: {}", descriptor); - let temp_db = { - let mut temp_db = std::env::temp_dir(); - let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(15).collect(); - temp_db.push(rand_string); + let database = MemoryDatabase::new(); - let database = sled::open(&temp_db).unwrap(); - - let network = match matches.value_of("network") { - Some("regtest") => Network::Regtest, - Some("testnet") | _ => Network::Testnet, - }; - let wallet: OfflineWallet<_> = Wallet::new_offline( - &format!("{}", descriptor), - None, - network, - database.open_tree("").unwrap(), - ) - .unwrap(); - - info!("... First address: {}", wallet.get_new_address().unwrap()); - - if matches.is_present("parsed_policy") { - let spending_policy = wallet.policies(ScriptType::External).unwrap(); - info!( - "... Spending policy:\n{}", - serde_json::to_string_pretty(&spending_policy).unwrap() - ); - } - - temp_db + let network = match matches.value_of("network") { + Some("regtest") => Network::Regtest, + Some("testnet") | _ => Network::Testnet, }; + let wallet: OfflineWallet<_> = + Wallet::new_offline(&format!("{}", descriptor), None, network, database).unwrap(); - std::fs::remove_dir_all(temp_db).unwrap(); + info!("... First address: {}", wallet.get_new_address().unwrap()); + + if matches.is_present("parsed_policy") { + let spending_policy = wallet.policies(ScriptType::External).unwrap(); + info!( + "... Spending policy:\n{}", + serde_json::to_string_pretty(&spending_policy).unwrap() + ); + } }