mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-08-10 03:50:03 +00:00
add musig nonce builder
This commit is contained in:
@ -10,12 +10,13 @@ use anyhow::{anyhow, Result};
|
||||
use cli::CliSettings;
|
||||
|
||||
fn start_trade_pipeline(cli_input: &CliSettings) -> Result<()> {
|
||||
if let CliSettings::Maker(maker_data) = cli_input {
|
||||
Ok(trading::run_maker(maker_data)?)
|
||||
} else if let CliSettings::Taker(taker_data) = cli_input {
|
||||
// trading::run_taker(taker_data)?;
|
||||
} else {
|
||||
Err(anyhow!("Wrong mode selected!"))
|
||||
match cli_input {
|
||||
CliSettings::Maker(maker_config) => trading::run_maker(maker_config),
|
||||
CliSettings::Taker(taker_config) => trading::run_taker(taker_config),
|
||||
_ => Err(anyhow!(
|
||||
"Wrong trading mode selected, not implemented: {:?}",
|
||||
cli_input
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,11 +6,15 @@ use std::borrow::Borrow;
|
||||
|
||||
use crate::cli::TraderSettings;
|
||||
use crate::communication::api::OfferCreationResponse;
|
||||
use crate::wallet::musig2::MusigNonce;
|
||||
use crate::wallet::{bond::Bond, load_wallet};
|
||||
use anyhow::Result;
|
||||
use bdk::bitcoin::block;
|
||||
use bdk::blockchain::{Blockchain, ElectrumBlockchain};
|
||||
use bdk::electrum_client::Client;
|
||||
use bdk::{
|
||||
bitcoin::block,
|
||||
blockchain::{Blockchain, ElectrumBlockchain},
|
||||
electrum_client::Client,
|
||||
wallet::AddressIndex::LastUnused,
|
||||
};
|
||||
|
||||
pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
|
||||
let blockchain = ElectrumBlockchain::from(Client::new(&maker_config.electrum_endpoint)?);
|
||||
@ -25,12 +29,19 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
|
||||
|
||||
let bond = Bond::assemble(&wallet, &offer_conditions, maker_config)?; // assemble the Bond transaction for offer creation
|
||||
// blockchain.broadcast(&bond.extract_tx())?; // publish bond to be mined for testing
|
||||
let payout_pubkey = wallet.get_address(bdk::wallet::AddressIndex::LastUnused)?;
|
||||
|
||||
let musig_nonce: MusigNonce = MusigNonce::generate()?; // will be moved to a more suitable place
|
||||
|
||||
dbg!(&bond.extract_tx().txid());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_taker(taker_config: &TraderSettings) -> Result<()> {
|
||||
let blockchain = ElectrumBlockchain::from(Client::new(&taker_config.electrum_endpoint)?);
|
||||
|
||||
// panic!("Taker to be implemented!");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,3 +1,48 @@
|
||||
use musig2::{AggNonce, FirstRound, PartialSignature, PubNonce, SecNonceSpices, SecondRound};
|
||||
use crate::wallet::wallet_utils::get_seed;
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use musig2::{PubNonce, SecNonce, SecNonceBuilder};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
// https://docs.rs/musig2/latest/musig2/
|
||||
|
||||
// secret nonce has to be used only one time!
|
||||
pub struct MusigNonce {
|
||||
secret_nonce: SecNonce,
|
||||
accessed_for_signing: bool,
|
||||
accessed_for_sharing: bool,
|
||||
}
|
||||
|
||||
impl MusigNonce {
|
||||
pub fn generate() -> Result<MusigNonce> {
|
||||
let timestamp_salt = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)?
|
||||
.as_nanos()
|
||||
.to_le_bytes();
|
||||
|
||||
// more salt can be added e.g. pubkey or secret key
|
||||
let secret_nonce = SecNonceBuilder::new(get_seed())
|
||||
.with_extra_input(×tamp_salt)
|
||||
.build();
|
||||
Ok(MusigNonce {
|
||||
secret_nonce,
|
||||
accessed_for_sharing: false,
|
||||
accessed_for_signing: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_sec_for_signing(mut self) -> Result<SecNonce> {
|
||||
if self.accessed_for_signing {
|
||||
return Err(anyhow!("MuSig nonce has already been used for signing!"));
|
||||
}
|
||||
self.accessed_for_signing = true;
|
||||
Ok(self.secret_nonce)
|
||||
}
|
||||
|
||||
pub fn get_pub_for_sharing(&mut self) -> Result<PubNonce> {
|
||||
if self.accessed_for_sharing || self.accessed_for_signing {
|
||||
return Err(anyhow!("MuSig nonce reused!"));
|
||||
}
|
||||
self.accessed_for_sharing = true;
|
||||
Ok(self.secret_nonce.public_nonce())
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use rand_core::{OsRng, RngCore};
|
||||
|
||||
// uses operating system rng which is secure for cryptography
|
||||
pub fn get_seed() -> [u8; 32] {
|
||||
let mut key = [0u8; 32];
|
||||
OsRng.fill_bytes(&mut key);
|
||||
key
|
||||
let mut seed = [0u8; 32];
|
||||
OsRng.fill_bytes(&mut seed);
|
||||
seed
|
||||
}
|
||||
|
Reference in New Issue
Block a user