[examples] Use MemoryDatabase in the compiler example

This commit is contained in:
Alekos Filini 2020-08-08 09:37:25 +02:00
parent a5188209b2
commit f0a1e670df
No known key found for this signature in database
GPG Key ID: 5E8AFC3034FDFA4F

View File

@ -3,23 +3,19 @@ extern crate clap;
extern crate log; extern crate log;
extern crate magical_bitcoin_wallet; extern crate magical_bitcoin_wallet;
extern crate miniscript; extern crate miniscript;
extern crate rand;
extern crate serde_json; extern crate serde_json;
extern crate sled;
use std::str::FromStr; use std::str::FromStr;
use log::info; use log::info;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use clap::{App, Arg}; use clap::{App, Arg};
use bitcoin::Network; use bitcoin::Network;
use miniscript::policy::Concrete; use miniscript::policy::Concrete;
use miniscript::Descriptor; use miniscript::Descriptor;
use magical_bitcoin_wallet::database::memory::MemoryDatabase;
use magical_bitcoin_wallet::types::ScriptType; use magical_bitcoin_wallet::types::ScriptType;
use magical_bitcoin_wallet::{OfflineWallet, Wallet}; use magical_bitcoin_wallet::{OfflineWallet, Wallet};
@ -73,37 +69,22 @@ fn main() {
info!("... Descriptor: {}", descriptor); info!("... Descriptor: {}", descriptor);
let temp_db = { let database = MemoryDatabase::new();
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,
let network = match matches.value_of("network") { Some("testnet") | _ => Network::Testnet,
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 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()
);
}
} }