improve rpc wallet connection (user/pass), remove cookie, derive wallet name from descriptor

This commit is contained in:
f321x
2024-07-17 17:39:34 +02:00
parent ad07b393a3
commit 2dbeb55238
7 changed files with 30 additions and 16 deletions

View File

@ -1 +0,0 @@
__cookie__:7b9dd407a2bf94a226776d1d35d5aeb64148b9425ddd6cf286381d6ab1d5f585

View File

@ -1,6 +1,6 @@
BITCOIN_RPC_ADDRESS_PORT="127.0.0.1:8332" 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_USER="coordinator"
BITCOIN_RPC_WALLET_NAME="coordinator_wallet" # not used yet BITCOIN_RPC_PASSWORD="test1234"
DATABASE_PATH="./dbs/trades.db" # path to the coordinator sqlite database storing the trades 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) BDK_DB_PATH="./dbs/bdk-wallet" # Path to the BDK Sled database (no .db postfix)
WALLET_XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32" WALLET_XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32"

View File

@ -28,7 +28,7 @@ fn run_mempool(mempool: Arc<Mempool>) {
loop { loop {
// sleep for a while // sleep for a while
std::thread::sleep(std::time::Duration::from_secs(15)); 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() { let mempool_txs = match mempool.json_rpc_client.deref().get_raw_mempool() {
std::result::Result::Ok(mempool_txs) => mempool_txs, std::result::Result::Ok(mempool_txs) => mempool_txs,
Err(e) => { Err(e) => {

View File

@ -10,7 +10,7 @@ use coordinator::monitoring::monitor_bonds;
use coordinator::monitoring::*; use coordinator::monitoring::*;
use database::CoordinatorDB; use database::CoordinatorDB;
use dotenv::dotenv; use dotenv::dotenv;
use log::{debug, error, info, warn}; use log::{debug, error, info, trace, warn};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, sync::Arc}; use std::{env, sync::Arc};
use tokio::sync::Mutex; use tokio::sync::Mutex;

View File

@ -4,13 +4,16 @@ mod utils;
use super::*; use super::*;
use anyhow::Context; use anyhow::Context;
use bdk::{ 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}, bitcoincore_rpc::{Client, RawTx, RpcApi},
blockchain::{rpc::Auth, Blockchain, ConfigurableBlockchain, RpcBlockchain, RpcConfig}, blockchain::{rpc::Auth, Blockchain, ConfigurableBlockchain, RpcBlockchain, RpcConfig},
sled::{self, Tree}, sled::{self, Tree},
template::Bip86, template::Bip86,
wallet::verify::*, wallet::verify::*,
KeychainKind, Wallet, KeychainKind, SyncOptions, Wallet,
}; };
use coordinator::mempool_monitoring::MempoolHandler; use coordinator::mempool_monitoring::MempoolHandler;
use std::{collections::HashMap, str::FromStr}; use std::{collections::HashMap, str::FromStr};
@ -37,13 +40,21 @@ pub async fn init_coordinator_wallet() -> Result<CoordinatorWallet<sled::Tree>>
let wallet_xprv = ExtendedPrivKey::from_str( let wallet_xprv = ExtendedPrivKey::from_str(
&env::var("WALLET_XPRV").context("loading WALLET_XPRV from .env failed")?, &env::var("WALLET_XPRV").context("loading WALLET_XPRV from .env failed")?,
)?; )?;
let secp_context = secp256k1::Secp256k1::new();
let rpc_config = RpcConfig { let rpc_config = RpcConfig {
url: env::var("BITCOIN_RPC_ADDRESS_PORT")?.to_string(), url: env::var("BITCOIN_RPC_ADDRESS_PORT")?.to_string(),
auth: Auth::Cookie { auth: Auth::UserPass {
file: env::var("BITCOIN_RPC_COOKIE_FILE_PATH")?.into(), username: env::var("BITCOIN_RPC_USER")?,
password: env::var("BITCOIN_RPC_PASSWORD")?,
}, },
network: bdk::bitcoin::Network::Regtest, network: Regtest,
wallet_name: env::var("BITCOIN_RPC_WALLET_NAME")?, // 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, sync_params: None,
}; };
let json_rpc_client = Arc::new(Client::new( let json_rpc_client = Arc::new(Client::new(
@ -63,9 +74,9 @@ pub async fn init_coordinator_wallet() -> Result<CoordinatorWallet<sled::Tree>>
let json_rpc_client_clone = Arc::clone(&json_rpc_client); let json_rpc_client_clone = Arc::clone(&json_rpc_client);
let mempool = MempoolHandler::new(json_rpc_client_clone).await; let mempool = MempoolHandler::new(json_rpc_client_clone).await;
// wallet wallet
// .sync(&backend, SyncOptions::default()) .sync(&backend, SyncOptions::default())
// .context("Connection to blockchain server failed.")?; // we could also use Esplora to make this async .context("Connection to blockchain server failed.")?; // we could also use Esplora to make this async
dbg!(wallet.get_balance()?); dbg!(wallet.get_balance()?);
Ok(CoordinatorWallet { Ok(CoordinatorWallet {
wallet: Arc::new(Mutex::new(wallet)), wallet: Arc::new(Mutex::new(wallet)),

View File

@ -81,6 +81,7 @@ impl OfferPsbtRequest {
.send()? .send()?
.json::<OfferTakenResponse>()?; .json::<OfferTakenResponse>()?;
debug!("Trader received escrow psbt");
let psbt_bytes = hex::decode(res.trade_psbt_hex_to_sign)?; let psbt_bytes = hex::decode(res.trade_psbt_hex_to_sign)?;
let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?; let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?;
Ok(psbt) Ok(psbt)

View File

@ -2,6 +2,7 @@ pub mod bond;
pub mod musig2; pub mod musig2;
pub mod wallet_utils; pub mod wallet_utils;
use super::*;
use crate::{cli::TraderSettings, communication::api::BondRequirementResponse}; use crate::{cli::TraderSettings, communication::api::BondRequirementResponse};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use bdk::{ use bdk::{
@ -73,7 +74,7 @@ impl TradingWallet {
// taker input should be the same as in the previous bond transaction. // taker input should be the same as in the previous bond transaction.
// input amount should be the bond amount when buying, // input amount should be the bond amount when buying,
pub fn validate_taker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> { 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 // tbd once the trade psbt is implemented on coordinator side
Ok(self) Ok(self)
} }
@ -86,9 +87,11 @@ impl TradingWallet {
Ok(self) Ok(self)
} }
// validate input amount, escrow output
pub fn validate_maker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> { 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 // tbd once the trade psbt is implemented on coordinator side
Ok(self) Ok(self)
} }
} }