]> Untitled Git - bdk/commitdiff
[examples] Use `MemoryDatabase` in the compiler example
authorAlekos Filini <alekos.filini@gmail.com>
Sat, 8 Aug 2020 07:37:25 +0000 (09:37 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Sat, 8 Aug 2020 07:37:25 +0000 (09:37 +0200)
examples/compiler.rs

index 04558fba478b84b25c5509036d47dff50466e900..bc7dc63aa2d237867be1eff28482931c62daa532 100644 (file)
@@ -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 = 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();
+    let database = MemoryDatabase::new();
 
-        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,
     };
-
-    std::fs::remove_dir_all(temp_db).unwrap();
+    let wallet: OfflineWallet<_> =
+        Wallet::new_offline(&format!("{}", descriptor), None, network, database).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()
+        );
+    }
 }