add taker escrow psbt submission

This commit is contained in:
Felix
2024-06-18 08:04:37 +00:00
parent e4cf054fd9
commit 3c24424a9f
7 changed files with 78 additions and 20 deletions

View File

@ -77,3 +77,11 @@ pub struct OfferPsbtRequest {
pub offer: PublicOffer, pub offer: PublicOffer,
pub trade_data: BondSubmissionRequest, pub trade_data: BondSubmissionRequest,
} }
// submit signed escrow psbt back to coordinator in a Json like this
#[derive(Debug, Serialize)]
pub struct PsbtSubmissionRequest {
pub signed_psbt_hex: String,
pub offer_id_hex: String,
pub robohash_hex: String,
}

View File

@ -9,7 +9,7 @@ use crate::{
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use api::{ use api::{
BondRequirementResponse, BondSubmissionRequest, OfferTakenRequest, OfferTakenResponse, BondRequirementResponse, BondSubmissionRequest, OfferTakenRequest, OfferTakenResponse,
OrderActivatedResponse, OrderRequest, OrderActivatedResponse, OrderRequest, PsbtSubmissionRequest,
}; };
use bdk::bitcoin::consensus::encode::serialize_hex; use bdk::bitcoin::consensus::encode::serialize_hex;
use bdk::{ use bdk::{

View File

@ -83,3 +83,32 @@ impl OfferPsbtRequest {
Ok(psbt) Ok(psbt)
} }
} }
impl PsbtSubmissionRequest {
pub fn submit_taker_psbt(
psbt: &PartiallySignedTransaction,
offer_id_hex: String,
taker_config: &TraderSettings,
) -> Result<()> {
let request = PsbtSubmissionRequest {
signed_psbt_hex: psbt.serialize_hex(),
offer_id_hex,
robohash_hex: taker_config.robosats_robohash_hex.clone(),
};
let client = reqwest::blocking::Client::new();
let res = client
.post(format!(
"{}{}",
taker_config.coordinator_endpoint, "/submit-taker-psbt"
))
.json(&request)
.send()?;
if res.status() != 200 {
return Err(anyhow!(
"Submitting taker psbt failed. Status: {}",
res.status()
));
}
Ok(())
}
}

View File

@ -43,9 +43,8 @@ pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
available_offers = PublicOffers::fetch(taker_config)?; available_offers = PublicOffers::fetch(taker_config)?;
} }
let selected_offer: &PublicOffer = available_offers.ask_user_to_select()?; let selected_offer: &PublicOffer = available_offers.ask_user_to_select()?;
let accepted_offer = ActiveOffer::take(&wallet, taker_config, selected_offer)?; let accepted_offer =
ActiveOffer::take(&wallet, taker_config, selected_offer)?.wait_on_maker()?;
// accepted_offer.wait_on_maker();
Ok(()) Ok(())
} }

View File

@ -1,6 +1,6 @@
use bdk::electrum_client::Request; use bdk::electrum_client::Request;
use crate::communication::api::OfferPsbtRequest; use crate::communication::api::{OfferPsbtRequest, PsbtSubmissionRequest};
use super::utils::*; use super::utils::*;
use super::*; use super::*;
@ -10,9 +10,7 @@ impl ActiveOffer {
trading_wallet: &TradingWallet, trading_wallet: &TradingWallet,
taker_config: &TraderSettings, taker_config: &TraderSettings,
offer: &PublicOffer, offer: &PublicOffer,
) -> Result<()> { ) -> Result<ActiveOffer> {
// return Ok(Active offer)
// fetching the bond requirements for the requested Offer (amount, locking address) // fetching the bond requirements for the requested Offer (amount, locking address)
let bond_conditions: BondRequirementResponse = offer.request_bond(taker_config)?; let bond_conditions: BondRequirementResponse = offer.request_bond(taker_config)?;
@ -28,18 +26,32 @@ impl ActiveOffer {
&mut musig_data, &mut musig_data,
taker_config, taker_config,
)?; )?;
let escrow_contract_psbt = let mut escrow_contract_psbt =
OfferPsbtRequest::taker_request(offer, bond_submission_request, taker_config)?; OfferPsbtRequest::taker_request(offer, bond_submission_request, taker_config)?;
// now we have to verify, sign and submit the escrow psbt again // now we have to verify, sign and submit the escrow psbt again
if !trading_wallet.validate_taker_psbt(&escrow_contract_psbt) { trading_wallet
panic!("taker psbt invalid!"); .validate_taker_psbt(&escrow_contract_psbt)?
} .sign_escrow_psbt(&mut escrow_contract_psbt)?;
Ok(())
// submit signed escrow psbt back to coordinator
PsbtSubmissionRequest::submit_taker_psbt(
&escrow_contract_psbt,
offer.offer_id_hex.clone(),
taker_config,
)?;
Ok(ActiveOffer {
order_id_hex: offer.offer_id_hex.clone(),
used_musig_config: musig_data,
used_bond: bond,
expected_payout_address: payout_address,
escrow_psbt: escrow_contract_psbt,
})
} }
pub fn wait_on_maker(&self) -> Result<()> { pub fn wait_on_maker(self) -> Result<Self> {
// tbd // tbd
Ok(()) Ok(self)
} }
} }

View File

@ -4,8 +4,8 @@ use super::*;
#[derive(Debug)] #[derive(Debug)]
pub struct ActiveOffer { pub struct ActiveOffer {
pub order_id_hex: String, pub order_id_hex: String,
pub bond_locked_until_timestamp: u128,
pub used_musig_config: MuSigData, pub used_musig_config: MuSigData,
pub used_bond: PartiallySignedTransaction, pub used_bond: PartiallySignedTransaction,
pub expected_payout_address: AddressInfo, pub expected_payout_address: AddressInfo,
pub escrow_psbt: PartiallySignedTransaction,
} }

View File

@ -3,7 +3,7 @@ pub mod musig2;
pub mod wallet_utils; pub mod wallet_utils;
use crate::{cli::TraderSettings, communication::api::BondRequirementResponse}; use crate::{cli::TraderSettings, communication::api::BondRequirementResponse};
use anyhow::Result; use anyhow::{anyhow, Result};
use bdk::{ use bdk::{
bitcoin::{self, bip32::ExtendedPrivKey, psbt::PartiallySignedTransaction, Network}, bitcoin::{self, bip32::ExtendedPrivKey, psbt::PartiallySignedTransaction, Network},
blockchain::ElectrumBlockchain, blockchain::ElectrumBlockchain,
@ -13,7 +13,7 @@ use bdk::{
miniscript::Descriptor, miniscript::Descriptor,
template::{Bip86, DescriptorTemplate}, template::{Bip86, DescriptorTemplate},
wallet::AddressInfo, wallet::AddressInfo,
KeychainKind, SyncOptions, Wallet, KeychainKind, SignOptions, SyncOptions, Wallet,
}; };
use bond::Bond; use bond::Bond;
use musig2::MuSigData; use musig2::MuSigData;
@ -75,7 +75,17 @@ impl TradingWallet {
// validate that the taker psbt references the correct inputs and amounts // validate that the taker psbt references the correct inputs and amounts
// 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) -> bool { pub fn validate_taker_psbt(&self, psbt: &PartiallySignedTransaction) -> Result<&Self> {
false dbg!("IMPLEMENT TAKER PSBT VALIDATION!");
// tbd once the trade psbt is implemented
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)
} }
} }