From 2dbeb552380e66faad36a7d90888ce0f42b07e78 Mon Sep 17 00:00:00 2001 From: f321x Date: Wed, 17 Jul 2024 17:39:34 +0200 Subject: [PATCH] improve rpc wallet connection (user/pass), remove cookie, derive wallet name from descriptor --- taptrade-cli-demo/coordinator/.cookie | 1 - taptrade-cli-demo/coordinator/.env | 4 +-- .../src/coordinator/mempool_monitoring.rs | 2 +- taptrade-cli-demo/coordinator/src/main.rs | 2 +- .../coordinator/src/wallet/mod.rs | 29 +++++++++++++------ .../src/communication/taker_requests.rs | 1 + taptrade-cli-demo/trader/src/wallet/mod.rs | 7 +++-- 7 files changed, 30 insertions(+), 16 deletions(-) delete mode 100644 taptrade-cli-demo/coordinator/.cookie diff --git a/taptrade-cli-demo/coordinator/.cookie b/taptrade-cli-demo/coordinator/.cookie deleted file mode 100644 index 7ae6cd7..0000000 --- a/taptrade-cli-demo/coordinator/.cookie +++ /dev/null @@ -1 +0,0 @@ -__cookie__:7b9dd407a2bf94a226776d1d35d5aeb64148b9425ddd6cf286381d6ab1d5f585 \ No newline at end of file diff --git a/taptrade-cli-demo/coordinator/.env b/taptrade-cli-demo/coordinator/.env index f2deecf..8b8f2e7 100644 --- a/taptrade-cli-demo/coordinator/.env +++ b/taptrade-cli-demo/coordinator/.env @@ -1,6 +1,6 @@ BITCOIN_RPC_ADDRESS_PORT="127.0.0.1:8332" -BITCOIN_RPC_COOKIE_FILE_PATH="./.cookie" # path to the cookie file for the bitcoind RPC -BITCOIN_RPC_WALLET_NAME="coordinator_wallet" # not used yet +BITCOIN_RPC_USER="coordinator" +BITCOIN_RPC_PASSWORD="test1234" DATABASE_PATH="./dbs/trades.db" # path to the coordinator sqlite database storing the trades BDK_DB_PATH="./dbs/bdk-wallet" # Path to the BDK Sled database (no .db postfix) WALLET_XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32" diff --git a/taptrade-cli-demo/coordinator/src/coordinator/mempool_monitoring.rs b/taptrade-cli-demo/coordinator/src/coordinator/mempool_monitoring.rs index 459f2db..67429dd 100644 --- a/taptrade-cli-demo/coordinator/src/coordinator/mempool_monitoring.rs +++ b/taptrade-cli-demo/coordinator/src/coordinator/mempool_monitoring.rs @@ -28,7 +28,7 @@ fn run_mempool(mempool: Arc) { loop { // sleep for a while std::thread::sleep(std::time::Duration::from_secs(15)); - debug!("Fetching mempool"); + trace!("Fetching mempool"); let mempool_txs = match mempool.json_rpc_client.deref().get_raw_mempool() { std::result::Result::Ok(mempool_txs) => mempool_txs, Err(e) => { diff --git a/taptrade-cli-demo/coordinator/src/main.rs b/taptrade-cli-demo/coordinator/src/main.rs index b6bed8e..62b8db9 100755 --- a/taptrade-cli-demo/coordinator/src/main.rs +++ b/taptrade-cli-demo/coordinator/src/main.rs @@ -10,7 +10,7 @@ use coordinator::monitoring::monitor_bonds; use coordinator::monitoring::*; use database::CoordinatorDB; use dotenv::dotenv; -use log::{debug, error, info, warn}; +use log::{debug, error, info, trace, warn}; use std::time::{SystemTime, UNIX_EPOCH}; use std::{env, sync::Arc}; use tokio::sync::Mutex; diff --git a/taptrade-cli-demo/coordinator/src/wallet/mod.rs b/taptrade-cli-demo/coordinator/src/wallet/mod.rs index e72e2f0..361792d 100644 --- a/taptrade-cli-demo/coordinator/src/wallet/mod.rs +++ b/taptrade-cli-demo/coordinator/src/wallet/mod.rs @@ -4,13 +4,16 @@ mod utils; use super::*; use anyhow::Context; use bdk::{ - bitcoin::{self, bip32::ExtendedPrivKey, consensus::encode::deserialize, Transaction}, + bitcoin::{ + self, bip32::ExtendedPrivKey, consensus::encode::deserialize, key::secp256k1, + Network::Regtest, Transaction, + }, bitcoincore_rpc::{Client, RawTx, RpcApi}, blockchain::{rpc::Auth, Blockchain, ConfigurableBlockchain, RpcBlockchain, RpcConfig}, sled::{self, Tree}, template::Bip86, wallet::verify::*, - KeychainKind, Wallet, + KeychainKind, SyncOptions, Wallet, }; use coordinator::mempool_monitoring::MempoolHandler; use std::{collections::HashMap, str::FromStr}; @@ -37,13 +40,21 @@ pub async fn init_coordinator_wallet() -> Result> let wallet_xprv = ExtendedPrivKey::from_str( &env::var("WALLET_XPRV").context("loading WALLET_XPRV from .env failed")?, )?; + let secp_context = secp256k1::Secp256k1::new(); let rpc_config = RpcConfig { url: env::var("BITCOIN_RPC_ADDRESS_PORT")?.to_string(), - auth: Auth::Cookie { - file: env::var("BITCOIN_RPC_COOKIE_FILE_PATH")?.into(), + auth: Auth::UserPass { + username: env::var("BITCOIN_RPC_USER")?, + password: env::var("BITCOIN_RPC_PASSWORD")?, }, - network: bdk::bitcoin::Network::Regtest, - wallet_name: env::var("BITCOIN_RPC_WALLET_NAME")?, + network: Regtest, + // wallet_name: env::var("BITCOIN_RPC_WALLET_NAME")?, + wallet_name: bdk::wallet::wallet_name_from_descriptor( + Bip86(wallet_xprv, KeychainKind::External), + Some(Bip86(wallet_xprv, KeychainKind::Internal)), + Regtest, + &secp_context, + )?, sync_params: None, }; let json_rpc_client = Arc::new(Client::new( @@ -63,9 +74,9 @@ pub async fn init_coordinator_wallet() -> Result> let json_rpc_client_clone = Arc::clone(&json_rpc_client); let mempool = MempoolHandler::new(json_rpc_client_clone).await; - // wallet - // .sync(&backend, SyncOptions::default()) - // .context("Connection to blockchain server failed.")?; // we could also use Esplora to make this async + wallet + .sync(&backend, SyncOptions::default()) + .context("Connection to blockchain server failed.")?; // we could also use Esplora to make this async dbg!(wallet.get_balance()?); Ok(CoordinatorWallet { wallet: Arc::new(Mutex::new(wallet)), diff --git a/taptrade-cli-demo/trader/src/communication/taker_requests.rs b/taptrade-cli-demo/trader/src/communication/taker_requests.rs index f169673..fa6b264 100644 --- a/taptrade-cli-demo/trader/src/communication/taker_requests.rs +++ b/taptrade-cli-demo/trader/src/communication/taker_requests.rs @@ -81,6 +81,7 @@ impl OfferPsbtRequest { .send()? .json::()?; + debug!("Trader received escrow psbt"); let psbt_bytes = hex::decode(res.trade_psbt_hex_to_sign)?; let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?; Ok(psbt) diff --git a/taptrade-cli-demo/trader/src/wallet/mod.rs b/taptrade-cli-demo/trader/src/wallet/mod.rs index 6bbfe1d..a00c0a7 100644 --- a/taptrade-cli-demo/trader/src/wallet/mod.rs +++ b/taptrade-cli-demo/trader/src/wallet/mod.rs @@ -2,6 +2,7 @@ pub mod bond; pub mod musig2; pub mod wallet_utils; +use super::*; use crate::{cli::TraderSettings, communication::api::BondRequirementResponse}; use anyhow::{anyhow, Result}; use bdk::{ @@ -73,7 +74,7 @@ impl TradingWallet { // taker input should be the same as in the previous bond transaction. // input amount should be the bond amount when buying, pub fn validate_taker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> { - dbg!("IMPLEMENT TAKER PSBT VALIDATION!"); + error!("IMPLEMENT TAKER PSBT VALIDATION!"); // tbd once the trade psbt is implemented on coordinator side Ok(self) } @@ -86,9 +87,11 @@ impl TradingWallet { Ok(self) } + // validate input amount, escrow output pub fn validate_maker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> { - dbg!("IMPLEMENT MAKER PSBT VALIDATION!"); + error!("IMPLEMENT MAKER PSBT VALIDATION!"); // tbd once the trade psbt is implemented on coordinator side + Ok(self) } }