begin coordinator wallet implementation

This commit is contained in:
f321x
2024-06-25 13:54:40 +00:00
parent e0a2ad39ea
commit 23dc8bf782
4 changed files with 38 additions and 7 deletions

View File

@ -1,2 +1,3 @@
ELECTRUM_BACKEND="127.0.0.1:50001"
DATABASE_PATH="./db/trades.db"
WALLET_XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32"

View File

@ -4,11 +4,12 @@ use super::*;
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite};
#[derive(Clone, Debug)]
pub struct SqliteDB {
pub struct CoordinatorDB {
pub db_pool: Arc<Pool<Sqlite>>,
}
impl SqliteDB {
// is our implementation secure 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> {
let db_pool = SqlitePoolOptions::new()

View File

@ -1,13 +1,13 @@
mod communication;
mod coordinator;
mod sqlite_db;
mod database;
mod wallet;
use anyhow::{anyhow, Result};
use bdk::{database::MemoryDatabase, Wallet};
use communication::{api::*, api_server};
use database::CoordinatorDB;
use dotenv::dotenv;
use sqlite_db::SqliteDB;
use std::{env, sync::Arc};
use wallet::CoordinatorWallet;
@ -16,8 +16,8 @@ use wallet::CoordinatorWallet;
async fn main() -> Result<()> {
dotenv().ok();
// Initialize the database pool
let coordinator_db = SqliteDB::init().await?;
let wallet = CoordinatorWallet::init().await?;
let coordinator_db = CoordinatorDB::init().await?;
// let wallet = CoordinatorWallet::init().await?;
api_server(coordinator_db).await?;
Ok(())

View File

@ -1,7 +1,36 @@
use super::*;
use anyhow::Context;
use bdk::{
bitcoin::{self, bip32::ExtendedPrivKey},
blockchain::ElectrumBlockchain,
electrum_client::Client,
template::Bip86,
KeychainKind, SyncOptions, Wallet,
};
use std::str::FromStr;
pub struct CoordinatorWallet {
pub wallet: Wallet<MemoryDatabase>,
}
impl CoordinatorWallet {}
impl CoordinatorWallet {
pub async fn init() -> Result<Self> {
let wallet_xprv = ExtendedPrivKey::from_str(
&env::var("WALLET_XPRV").context("loading WALLET_XPRV from .env failed")?,
)?;
let backend = ElectrumBlockchain::from(Client::new(
&env::var("ELECTRUM_BACKEND")
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
)?);
let wallet = Wallet::new(
Bip86(wallet_xprv, KeychainKind::External),
Some(Bip86(wallet_xprv, KeychainKind::Internal)),
bitcoin::Network::Testnet,
MemoryDatabase::default(), // non-permanent storage
)?;
wallet.sync(&backend, SyncOptions::default())?;
dbg!("Balance: {} SAT", wallet.get_balance()?);
Ok(TradingWallet { wallet, backend })
}
}