finish bond submission and implemented ActiveOrder structure

This commit is contained in:
Felix
2024-06-13 11:55:17 +00:00
parent c7e5341e5c
commit 9459ee6dae
5 changed files with 83 additions and 20 deletions

View File

@ -1,11 +1,18 @@
pub mod api;
use crate::{
cli::{OfferType, TraderSettings},
wallet::{bond::Bond, musig2::MuSigData},
};
use anyhow::Result;
use api::{BondSubmissionRequest, OfferCreationResponse, OrderActivatedResponse, OrderRequest};
use bdk::bitcoin::consensus::encode::serialize_hex;
use bdk::{
bitcoin::{consensus::Encodable, psbt::PartiallySignedTransaction},
wallet::AddressInfo,
};
use serde::{Deserialize, Serialize};
use crate::cli::{OfferType, TraderSettings};
use api::{OfferCreationResponse, OrderRequest};
impl OfferCreationResponse {
fn _format_request(trader_setup: &TraderSettings) -> OrderRequest {
let amount: u64;
@ -42,3 +49,35 @@ impl OfferCreationResponse {
Ok(res)
}
}
impl BondSubmissionRequest {
pub fn send(
robohash_hex: &String,
bond: &PartiallySignedTransaction,
musig_data: &mut MuSigData,
payout_address: &AddressInfo,
trader_setup: &TraderSettings,
) -> Result<OrderActivatedResponse> {
let signed_bond_hex = serialize_hex(&bond.to_owned().extract_tx());
let musig_pub_nonce_hex = hex::encode(musig_data.nonce.get_pub_for_sharing()?.serialize());
let musig_pubkey_hex = hex::encode(musig_data.public_key.0.serialize());
let request = BondSubmissionRequest {
robohash_hex: robohash_hex.clone(),
signed_bond_hex,
payout_address: payout_address.address.to_string(),
musig_pub_nonce_hex,
musig_pubkey_hex,
};
let client = reqwest::blocking::Client::new();
let res = client
.post(format!(
"{}{}",
trader_setup.coordinator_endpoint, "/submit-maker-bond"
))
.json(&request)
.send()?
.json::<OrderActivatedResponse>()?;
Ok(res)
}
}

View File

@ -1,14 +1,23 @@
use crate::cli::TraderSettings;
use crate::communication::api::OfferCreationResponse;
use crate::communication::api::{BondSubmissionRequest, OfferCreationResponse};
use crate::wallet::{
bond::Bond,
musig2::{MuSigData, MusigNonce},
TradingWallet,
};
use anyhow::Result;
use bdk::database::MemoryDatabase;
use bdk::{
bitcoin::psbt::PartiallySignedTransaction, database::MemoryDatabase, wallet::AddressInfo,
};
pub struct ActiveOffer {}
#[derive(Debug)]
pub struct ActiveOffer {
pub order_id_hex: String,
pub bond_locked_until_timestamp: u128,
pub used_musig_config: MuSigData,
pub used_bond: PartiallySignedTransaction,
pub expected_payout_address: AddressInfo,
}
impl ActiveOffer {
pub fn create(
@ -17,17 +26,30 @@ impl ActiveOffer {
) -> Result<ActiveOffer> {
let trading_wallet = &trading_wallet.wallet;
// let offer_conditions = OfferCreationResponse::fetch(maker_config)?;
let offer_conditions = OfferCreationResponse {
// hardcoded for testing, locking_address is owned by .env xprv
locking_amount_sat: 90000,
bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm"
.to_string(),
};
let offer_conditions = OfferCreationResponse::fetch(maker_config)?;
// let offer_conditions = OfferCreationResponse {
// // hardcoded for testing, locking_address is owned by .env xprv
// locking_amount_sat: 90000,
// bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm"
// .to_string(),
// };
let bond = Bond::assemble(trading_wallet, &offer_conditions, maker_config)?;
let payout_pubkey = trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
let musig_data = MuSigData::create(&maker_config.wallet_xprv, trading_wallet.secp_ctx())?;
Ok(ActiveOffer {})
let payout_address = trading_wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
let mut musig_data =
MuSigData::create(&maker_config.wallet_xprv, trading_wallet.secp_ctx())?;
let submission_result = BondSubmissionRequest::send(
&maker_config.robosats_robohash_hex,
&bond,
&mut musig_data,
&payout_address,
maker_config,
)?;
Ok(ActiveOffer {
order_id_hex: submission_result.order_id_hex,
bond_locked_until_timestamp: submission_result.bond_locked_until_timestamp,
used_musig_config: musig_data,
used_bond: bond,
expected_payout_address: payout_address,
})
}
}

View File

@ -9,7 +9,7 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
let wallet = TradingWallet::load_wallet(maker_config)?; // initialize the wallet with xprv
let offer = ActiveOffer::create(&wallet, maker_config)?;
// dbg!(&bond.extract_tx().txid());
dbg!(&offer);
Ok(())
}

View File

@ -19,8 +19,8 @@ pub struct Outpoint {
}
pub struct Bond {
pub signed_bond_tx_hex: String,
pub used_outpoint: Outpoint,
// pub signed_bond_tx_hex: String, not needed
// pub used_outpoint: Outpoint,
}
impl Bond {

View File

@ -14,6 +14,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
// https://docs.rs/musig2/latest/musig2/
#[derive(Debug)]
pub struct MuSigData {
pub nonce: MusigNonce,
pub public_key: (XOnlyPublicKey, Parity),
@ -21,6 +22,7 @@ pub struct MuSigData {
}
// secret nonce has to be used only one time!
#[derive(Debug)]
pub struct MusigNonce {
secret_nonce: SecNonce,
accessed_for_signing: bool,