Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #53 from comit-network/provide-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Sep 16, 2021
2 parents 99705d7 + 5ba4402 commit 9cde561
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ jobs:
- run: cargo test --workspace
- name: Smoke test ${{ matrix.target }} binary
run: |
target/debug/maker &
target/debug/maker --data-dir=/tmp/maker --generate-seed &
sleep 5s # Wait for maker to start
target/debug/taker &
target/debug/taker --data-dir=/tmp/taker --generate-seed &
sleep 5s # Wait for taker to start
curl --fail http://localhost:8000/alive
139 changes: 139 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ edition = "2018"
anyhow = "1"
bdk = { git = "https://github.com/bitcoindevkit/bdk/" }
cfd_protocol = { path = "../cfd_protocol" }
clap = "3.0.0-beta.4"
futures = { version = "0.3", default-features = false }
hkdf = "0.11"
rand = "0.6"
rocket = { git = "https://github.com/SergioBenitez/Rocket", features = ["json"] }
rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket", features = ["sqlx_sqlite"] }
Expand All @@ -16,6 +18,7 @@ rust_decimal_macros = "1.15"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_with = { version = "1", features = ["macros"] }
sha2 = "0.9"
sqlx = { version = "0.5", features = ["offline"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "net"] }
tokio-util = { version = "0.6", features = ["codec"] }
Expand Down
67 changes: 51 additions & 16 deletions daemon/src/maker.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::seed::Seed;
use anyhow::Result;
use bdk::bitcoin::secp256k1::{schnorrsig, SECP256K1};
use bdk::bitcoin::{self, Amount};
use bdk::bitcoin::{Amount, Network};
use bdk::blockchain::{ElectrumBlockchain, NoopProgress};
use bdk::KeychainKind;
use clap::Clap;
use model::cfd::{Cfd, Order};
use rocket::fairing::AdHoc;
use rocket::figment::util::map;
use rocket::figment::value::{Map, Value};
use rocket_db_pools::Database;
use std::path::PathBuf;
use tokio::sync::{mpsc, watch};

mod db;
Expand All @@ -15,6 +17,7 @@ mod maker_cfd_actor;
mod maker_inc_connections_actor;
mod model;
mod routes_maker;
mod seed;
mod send_wire_message_actor;
mod to_sse_event;
mod wire;
Expand All @@ -23,19 +26,55 @@ mod wire;
#[database("maker")]
pub struct Db(sqlx::SqlitePool);

#[derive(Clap)]
struct Opts {
/// The port to listen on for p2p connections.
#[clap(long, default_value = "9999")]
p2p_port: u16,

/// The port to listen on for the HTTP API.
#[clap(long, default_value = "8001")]
http_port: u16,

/// URL to the electrum backend to use for the wallet.
#[clap(long, default_value = "ssl://electrum.blockstream.info:60002")]
electrum: String,

/// Where to permanently store data, defaults to the current working directory.
#[clap(long)]
data_dir: Option<PathBuf>,

/// Generate a seed file within the data directory.
#[clap(long)]
generate_seed: bool,
}

#[rocket::main]
async fn main() -> Result<()> {
let client =
bdk::electrum_client::Client::new("ssl://electrum.blockstream.info:60002").unwrap();
let opts = Opts::parse();

let data_dir = opts
.data_dir
.unwrap_or_else(|| std::env::current_dir().expect("unable to get cwd"));

if !data_dir.exists() {
tokio::fs::create_dir_all(&data_dir).await?;
}

let seed = Seed::initialize(&data_dir.join("maker_seed"), opts.generate_seed).await?;

let client = bdk::electrum_client::Client::new(&opts.electrum).unwrap();

// TODO: Replace with sqlite once https://github.com/bitcoindevkit/bdk/pull/376 is merged.
let db = bdk::sled::open("/tmp/maker.db")?;
let db = bdk::sled::open(data_dir.join("maker_wallet_db"))?;
let wallet_db = db.open_tree("wallet")?;

let ext_priv_key = seed.derive_extended_priv_key(Network::Testnet)?;

let wallet = bdk::Wallet::new(
"wpkh(tprv8ZgxMBicQKsPd95j7aKDzWZw9Z2SiLxpz5J5iFUdqFf1unqtoonSTteF1ZSrrB831BY1eufyHehediNH76DvcDSS2JDDyDXCQKJbyd7ozVf/*)#3vkm30lf",
None,
bitcoin::Network::Testnet,
bdk::template::Bip84(ext_priv_key, KeychainKind::External),
Some(bdk::template::Bip84(ext_priv_key, KeychainKind::Internal)),
ext_priv_key.network,
wallet_db,
ElectrumBlockchain::from(client),
)
Expand All @@ -48,15 +87,11 @@ async fn main() -> Result<()> {
let (order_feed_sender, order_feed_receiver) = watch::channel::<Option<Order>>(None);
let (_balance_feed_sender, balance_feed_receiver) = watch::channel::<Amount>(Amount::ZERO);

let db: Map<_, Value> = map! {
"url" => "./maker.sqlite".into(),
};

let figment = rocket::Config::figment()
.merge(("databases", map!["maker" => db]))
.merge(("port", 8001));
.merge(("databases.maker.url", data_dir.join("maker.sqlite")))
.merge(("port", opts.http_port));

let listener = tokio::net::TcpListener::bind("0.0.0.0:9999").await?;
let listener = tokio::net::TcpListener::bind(&format!("0.0.0.0:{}", opts.p2p_port)).await?;
let local_addr = listener.local_addr().unwrap();

println!("Listening on {}", local_addr);
Expand Down

0 comments on commit 9cde561

Please sign in to comment.