begin client psbt assembly

This commit is contained in:
f321x
2024-07-26 13:21:30 +02:00
parent 2ee3668652
commit 36754a1ea2
3 changed files with 34 additions and 24 deletions

View File

@ -29,16 +29,11 @@ impl ActiveOffer {
// polling until offer is taken, in production a more efficient way would make sense
// returns the PSBT of the escrow trade transaction we have to validate, sign and return
pub fn wait_until_taken(
&self,
trader_config: &TraderSettings,
) -> Result<PartiallySignedTransaction> {
pub fn wait_until_taken(&self, trader_config: &TraderSettings) -> Result<OfferTakenResponse> {
loop {
thread::sleep(Duration::from_secs(10));
if let Some(offer_taken_response) = OfferTakenResponse::check(self, trader_config)? {
let psbt_bytes = hex::decode(offer_taken_response.trade_psbt_hex_to_sign)?;
let psbt = PartiallySignedTransaction::deserialize(&psbt_bytes)?;
return Ok(psbt);
return Ok(offer_taken_response);
}
}
}

View File

@ -30,10 +30,10 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
let offer = ActiveOffer::create(&wallet, maker_config)?;
info!("Maker offer created: {:#?}", &offer);
let mut escrow_contract_psbt = offer.wait_until_taken(maker_config)?;
wallet
.validate_maker_psbt(&escrow_contract_psbt)?
.sign_escrow_psbt(&mut escrow_contract_psbt)?;
let escrow_psbt_requirements = offer.wait_until_taken(maker_config)?;
let escrow_psbt = wallet.get_escrow_psbt(escrow_psbt_requirements, maker_config);
// .validate_maker_psbt(&escrow_contract_psbt)?
// .sign_escrow_psbt(&mut escrow_contract_psbt)?;
// submit signed escrow psbt back to coordinator
PsbtSubmissionRequest::submit_escrow_psbt(

View File

@ -3,7 +3,10 @@ pub mod musig2;
pub mod wallet_utils;
use super::*;
use crate::{cli::TraderSettings, communication::api::BondRequirementResponse};
use crate::{
cli::TraderSettings,
communication::api::{BondRequirementResponse, OfferTakenResponse},
};
use anyhow::{anyhow, Result};
use bdk::{
bitcoin::{
@ -85,6 +88,18 @@ impl TradingWallet {
Ok((bond, musig_data, payout_address))
}
pub async fn get_escrow_psbt(
&self,
escrow_psbt_requirements: OfferTakenResponse,
trader_config: &TraderSettings,
) -> Result<PartiallySignedTransaction> {
let fee_address = escrow_psbt_requirements.escrow_tx_fee_address;
let output_descriptor = escrow_psbt_requirements.escrow_output_descriptor;
self.wallet.sync(&self.backend, SyncOptions::default())?;
Ok(())
}
// validate that the taker psbt references the correct inputs and amounts
// taker input should be the same as in the previous bond transaction.
// input amount should be the bond amount when buying,
@ -94,19 +109,19 @@ impl TradingWallet {
Ok(self)
}
pub fn sign_escrow_psbt(&self, escrow_psbt: &mut PartiallySignedTransaction) -> Result<&Self> {
let finalized = self.wallet.sign(escrow_psbt, SignOptions::default())?;
if !finalized {
return Err(anyhow!("Signing of taker escrow psbt failed!"));
}
Ok(self)
}
// pub fn sign_escrow_psbt(&self, escrow_psbt: &mut PartiallySignedTransaction) -> Result<&Self> {
// let finalized = self.wallet.sign(escrow_psbt, SignOptions::default())?;
// if !finalized {
// return Err(anyhow!("Signing of taker escrow psbt failed!"));
// }
// Ok(self)
// }
// validate amounts, escrow output
pub fn validate_maker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> {
error!("IMPLEMENT MAKER PSBT VALIDATION!");
// tbd once the trade psbt is implemented on coordinator side
// pub fn validate_maker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> {
// error!("IMPLEMENT MAKER PSBT VALIDATION!");
// // tbd once the trade psbt is implemented on coordinator side
Ok(self)
}
// Ok(self)
// }
}