working on client musig partitial sig

This commit is contained in:
fbock
2024-08-14 18:38:31 +02:00
parent fda9af518d
commit 6a5a10546a
3 changed files with 31 additions and 14 deletions

View File

@ -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 {

View File

@ -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<String> {
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"));
}
}
}
}

View File

@ -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,
})
}
}