wallet coordinator

This commit is contained in:
f321x
2024-06-26 09:03:41 +00:00
parent 23dc8bf782
commit b4659ad912
6 changed files with 16 additions and 12 deletions

View File

@ -1,3 +1,4 @@
ELECTRUM_BACKEND="127.0.0.1:50001"
DATABASE_PATH="./db/trades.db"
DATABASE_PATH="./db/trades.db" # path to the coordinator sqlite database storing the trades
BDK_DB_PATH="./db/bdk-wallet" # Path to the BDK Sled database (no .db postfix)
WALLET_XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32"

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.86"
axum = { version = "0.7.5", features = ["tokio", "json"] }
bdk = "0.29.0"
bdk = { version = "0.29.0", features = ["key-value-db", "async-interface"] }
dotenv = "0.15.0"
hex = "0.4.3"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }

View File

@ -15,11 +15,11 @@ use tokio::net::TcpListener;
// Handler function to process the received data
async fn receive_order(
Extension(state): Extension<Arc<SqliteDB>>,
Extension(state): Extension<Arc<CoordinatorDB>>,
Json(order): Json<OrderRequest>,
) -> Result<Json<BondRequirementResponse>, AppError> {
// insert offer into sql database
// generate locking address for bond
// insert offer into sql database
println!("Coordinator received new offer: {:?}", order);
Ok(Json(BondRequirementResponse {
@ -42,7 +42,7 @@ async fn receive_order(
// Json(response)
// }
pub async fn api_server(database: SqliteDB) -> Result<()> {
pub async fn api_server(database: CoordinatorDB) -> Result<()> {
let app = Router::new()
.route("/create-offer", post(receive_order))
.layer(Extension(database));

View File

@ -8,7 +8,7 @@ pub struct CoordinatorDB {
pub db_pool: Arc<Pool<Sqlite>>,
}
// is our implementation secure against sql injections?
// is our implementation resistant against sql injections?
impl CoordinatorDB {
// will either create a new db or load existing one. Will create according tables in new db
pub async fn init() -> Result<Self> {

View File

@ -17,7 +17,7 @@ async fn main() -> Result<()> {
dotenv().ok();
// Initialize the database pool
let coordinator_db = CoordinatorDB::init().await?;
// let wallet = CoordinatorWallet::init().await?;
let wallet = CoordinatorWallet::init().await?;
api_server(coordinator_db).await?;
Ok(())

View File

@ -3,14 +3,16 @@ use anyhow::Context;
use bdk::{
bitcoin::{self, bip32::ExtendedPrivKey},
blockchain::ElectrumBlockchain,
database::any::SledDbConfiguration,
electrum_client::Client,
sled,
template::Bip86,
KeychainKind, SyncOptions, Wallet,
};
use std::str::FromStr;
pub struct CoordinatorWallet {
pub wallet: Wallet<MemoryDatabase>,
pub wallet: Wallet<SledDbConfiguration>,
}
impl CoordinatorWallet {
@ -22,15 +24,16 @@ impl CoordinatorWallet {
&env::var("ELECTRUM_BACKEND")
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
)?);
let sled_db = sled::open(env::var("BDK_DB_PATH")?)?.open_tree("default_wallet")?;
let wallet = Wallet::new(
Bip86(wallet_xprv, KeychainKind::External),
Some(Bip86(wallet_xprv, KeychainKind::Internal)),
bitcoin::Network::Testnet,
MemoryDatabase::default(), // non-permanent storage
sled_db,
)?;
wallet.sync(&backend, SyncOptions::default())?;
dbg!("Balance: {} SAT", wallet.get_balance()?);
Ok(TradingWallet { wallet, backend })
wallet.sync(&backend, SyncOptions::default()).await?;
dbg!("BDK Wallet loaded, Balance: {} SAT", wallet.get_balance()?);
Ok(CoordinatorWallet { wallet })
}
}