From fc9b3d1a5d6904d8d30fb36d427edb98be156470 Mon Sep 17 00:00:00 2001 From: f321x Date: Mon, 29 Jul 2024 14:21:38 +0200 Subject: [PATCH] working on escrow psbt creation --- .../coordinator/src/communication/api.rs | 3 ++ .../trader/src/communication/api.rs | 3 ++ taptrade-cli-demo/trader/src/wallet/mod.rs | 39 ++++++++++++++----- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/taptrade-cli-demo/coordinator/src/communication/api.rs b/taptrade-cli-demo/coordinator/src/communication/api.rs index b654def..a6f6885 100644 --- a/taptrade-cli-demo/coordinator/src/communication/api.rs +++ b/taptrade-cli-demo/coordinator/src/communication/api.rs @@ -64,6 +64,9 @@ pub struct PublicOffers { pub struct OfferTakenResponse { pub escrow_output_descriptor: String, pub escrow_tx_fee_address: String, + pub escrow_amount_maker_sat: u64, + pub escrow_amount_taker_sat: u64, + pub escrow_fee_sat_per_participant: u64, } // request to receive the escrow psbt to sign for the specified offer to take it diff --git a/taptrade-cli-demo/trader/src/communication/api.rs b/taptrade-cli-demo/trader/src/communication/api.rs index 9c7899e..95db5e3 100644 --- a/taptrade-cli-demo/trader/src/communication/api.rs +++ b/taptrade-cli-demo/trader/src/communication/api.rs @@ -48,6 +48,9 @@ pub struct OfferTakenRequest { pub struct OfferTakenResponse { pub escrow_output_descriptor: String, pub escrow_tx_fee_address: String, + pub escrow_amount_maker_sat: u64, + pub escrow_amount_taker_sat: u64, + pub escrow_fee_sat_per_participant: u64, } // Taker structures // diff --git a/taptrade-cli-demo/trader/src/wallet/mod.rs b/taptrade-cli-demo/trader/src/wallet/mod.rs index 87d7e0c..2f29bf7 100644 --- a/taptrade-cli-demo/trader/src/wallet/mod.rs +++ b/taptrade-cli-demo/trader/src/wallet/mod.rs @@ -14,16 +14,16 @@ use bdk::{ bip32::ExtendedPrivKey, key::{KeyPair, Secp256k1, XOnlyPublicKey}, psbt::PartiallySignedTransaction, - Network, + Address, Network, }, blockchain::ElectrumBlockchain, - database::MemoryDatabase, + database::{Database, MemoryDatabase}, electrum_client::Client, keys::DescriptorPublicKey, - miniscript::Descriptor, + miniscript::{descriptor::Tr, Descriptor}, template::{Bip86, DescriptorTemplate}, - wallet::AddressInfo, - KeychainKind, SignOptions, SyncOptions, Wallet, + wallet::{AddressIndex, AddressInfo}, + FeeRate, KeychainKind, SignOptions, SyncOptions, Wallet, }; use bond::Bond; use musig2::MuSigData; @@ -92,12 +92,33 @@ impl TradingWallet { &self, escrow_psbt_requirements: OfferTakenResponse, trader_config: &TraderSettings, + escrow_amount: u64, + coordinator_fee_amount: u64, ) -> Result { - let fee_address = escrow_psbt_requirements.escrow_tx_fee_address; - let output_descriptor = escrow_psbt_requirements.escrow_output_descriptor; - + let fee_output = Address::from_str(&escrow_psbt_requirements.escrow_tx_fee_address)? + .assume_checked() + .script_pubkey(); + let escrow_output = { + let temp_wallet = Wallet::new( + &escrow_psbt_requirements.escrow_output_descriptor, + None, + Network::Regtest, + MemoryDatabase::new(), + )?; + temp_wallet.get_address(AddressIndex::New)?.script_pubkey() + }; self.wallet.sync(&self.backend, SyncOptions::default())?; - Ok(()) + let (mut psbt, details) = { + let mut builder = self.wallet.build_tx(); + builder + .add_recipient(escrow_output, escrow_amount) + .add_recipient(fee_output, coordinator_fee_amount) + .fee_rate(FeeRate::from_sat_per_vb(10.0)); + builder.finish()? + }; + debug!("Signing escrow psbt."); + self.wallet.sign(&mut psbt, SignOptions::default())?; + Ok(psbt) } // validate that the taker psbt references the correct inputs and amounts