From 6a5a10546a183ac8556230393a98010645ae9baa Mon Sep 17 00:00:00 2001 From: fbock Date: Wed, 14 Aug 2024 18:38:31 +0200 Subject: [PATCH] working on client musig partitial sig --- taptrade-cli-demo/trader/src/trading/mod.rs | 4 +-- taptrade-cli-demo/trader/src/wallet/mod.rs | 27 +++++++++++++++---- .../src/wallet/{musig2.rs => musig2_utils.rs} | 14 +++++----- 3 files changed, 31 insertions(+), 14 deletions(-) rename taptrade-cli-demo/trader/src/wallet/{musig2.rs => musig2_utils.rs} (83%) diff --git a/taptrade-cli-demo/trader/src/trading/mod.rs b/taptrade-cli-demo/trader/src/trading/mod.rs index f81f093..523b788 100644 --- a/taptrade-cli-demo/trader/src/trading/mod.rs +++ b/taptrade-cli-demo/trader/src/trading/mod.rs @@ -13,7 +13,7 @@ use crate::{ }, wallet::{ bond::Bond, - musig2::{MuSigData, MusigNonce}, + musig2_utils::{MuSigData, MusigNonce}, TradingWallet, }, }; @@ -55,7 +55,7 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> { let signed_payout_psbt = wallet .validate_payout_psbt(&payout_keyspend_psbt)? - .sign_payout_psbt(payout_keyspend_psbt, agg_pub_nonce, agg_pubk_ctx)?; + .sign_keyspend_payout_psbt(payout_keyspend_psbt, agg_pubk_ctx, agg_pub_nonce, local_musig_state: &offer.used_musig_config)?; // submit signed payout psbt back to coordinator panic!("Payout to be implemented!"); } else { diff --git a/taptrade-cli-demo/trader/src/wallet/mod.rs b/taptrade-cli-demo/trader/src/wallet/mod.rs index 1e9c99e..3a41c02 100644 --- a/taptrade-cli-demo/trader/src/wallet/mod.rs +++ b/taptrade-cli-demo/trader/src/wallet/mod.rs @@ -1,5 +1,5 @@ pub mod bond; -pub mod musig2; +pub mod musig2_utils; pub mod wallet_utils; use super::*; @@ -31,7 +31,9 @@ use bdk::{ }; use bond::Bond; use cli::OfferType; -use musig2::MuSigData; +use hex::ToHex; +use musig2::secp::MaybeScalar; +use musig2_utils::MuSigData; use serde::Serialize; use std::{ops::Add, str::FromStr}; use wallet_utils::get_seed; @@ -216,9 +218,10 @@ impl TradingWallet { validated_payout_psbt: PartiallySignedTransaction, key_agg_context: KeyAggContext, agg_pub_nonce: AggNonce, + local_musig_state: &MuSigData, ) -> Result { let payout_tx = validated_payout_psbt.extract_tx(); - let sig_hash_cache = SighashCache::new(payout_tx); + let mut sig_hash_cache = SighashCache::new(payout_tx); let utxo = validated_payout_psbt .iter_funding_utxos() @@ -232,8 +235,22 @@ impl TradingWallet { .context("Failed to create keyspend sighash")? .as_byte_array(); - panic!("Implement keyspend signing"); + let secret_nonce = local_musig_state.nonce.get_sec_for_signing()?; + let seckey = local_musig_state.secret_key; - Ok(signed_psbt) + let keyspend_sig: musig2::PartialSignature = musig2::sign_partial( + &key_agg_context, + seckey, + secret_nonce, + &agg_pub_nonce, + keyspend_sig_hash_msg, + )?; + + match keyspend_sig { + MaybeScalar::Valid(s) => Ok(s.encode_hex()), + MaybeScalar::Zero => { + return Err(anyhow!("keyspend sig maybe scalar is Zero")); + } + } } } diff --git a/taptrade-cli-demo/trader/src/wallet/musig2.rs b/taptrade-cli-demo/trader/src/wallet/musig2_utils.rs similarity index 83% rename from taptrade-cli-demo/trader/src/wallet/musig2.rs rename to taptrade-cli-demo/trader/src/wallet/musig2_utils.rs index 6e19498..d916711 100644 --- a/taptrade-cli-demo/trader/src/wallet/musig2.rs +++ b/taptrade-cli-demo/trader/src/wallet/musig2_utils.rs @@ -3,14 +3,11 @@ use crate::wallet::{wallet_utils::get_seed, KeychainKind}; use anyhow::{anyhow, Error, Result}; use bdk::bitcoin::secp256k1::PublicKey; use bdk::{ - bitcoin::{ - bip32::ExtendedPrivKey, - secp256k1::{All, SecretKey}, - }, + bitcoin::{bip32::ExtendedPrivKey, secp256k1::All}, keys::{DescriptorPublicKey, DescriptorSecretKey}, template::{Bip86, DescriptorTemplate}, }; -use musig2::{PubNonce, SecNonce, SecNonceBuilder}; +use musig2::{secp256k1::SecretKey as MusigSecretKey, PubNonce, SecNonce, SecNonceBuilder}; use std::time::{SystemTime, UNIX_EPOCH}; // https://docs.rs/musig2/latest/musig2/ @@ -19,7 +16,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; pub struct MuSigData { pub nonce: MusigNonce, pub public_key: PublicKey, - pub secret_key: SecretKey, + pub secret_key: MusigSecretKey, } // secret nonce has to be used only one time! @@ -70,10 +67,13 @@ impl MuSigData { let nonce = MusigNonce::generate()?; let keypair = xprv.to_owned().to_keypair(secp_ctx); // double check keypair, which derivation should we use? + // convert from bdk secp to musig crate secp for the traits needed to do sig agg + let musig_type_secret_key = MusigSecretKey::from_slice(&keypair.secret_bytes())?; + Ok(MuSigData { nonce, public_key: keypair.public_key(), - secret_key: keypair.secret_key(), + secret_key: musig_type_secret_key, }) } }