begin with musig

This commit is contained in:
Felix
2024-06-10 16:56:24 +00:00
parent 364c86269a
commit dc0943cc08
7 changed files with 113 additions and 68 deletions

View File

@ -119,6 +119,12 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "base16ct"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf"
[[package]]
name = "base64"
version = "0.13.1"
@ -186,7 +192,7 @@ dependencies = [
"bitcoin-private",
"bitcoin_hashes",
"hex_lit",
"secp256k1",
"secp256k1 0.27.0",
"serde",
]
@ -257,46 +263,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.66",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.1"
@ -370,6 +336,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
@ -602,12 +569,6 @@ version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -620,6 +581,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "http"
version = "1.1.0"
@ -849,6 +819,21 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "musig2"
version = "0.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bed08befaac75bfb31ca5e87678c4e8490bcd21d0c98ccb4f12f4065a7567e83"
dependencies = [
"base16ct",
"hmac",
"once_cell",
"secp",
"secp256k1 0.28.2",
"sha2",
"subtle",
]
[[package]]
name = "native-tls"
version = "0.2.12"
@ -1242,6 +1227,18 @@ dependencies = [
"untrusted",
]
[[package]]
name = "secp"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1507279bb0404bb566f85523e48fcf37a158daa5380577ee0d93f3ef4df39ccc"
dependencies = [
"base16ct",
"once_cell",
"secp256k1 0.28.2",
"subtle",
]
[[package]]
name = "secp256k1"
version = "0.27.0"
@ -1250,10 +1247,19 @@ checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f"
dependencies = [
"bitcoin_hashes",
"rand",
"secp256k1-sys",
"secp256k1-sys 0.8.1",
"serde",
]
[[package]]
name = "secp256k1"
version = "0.28.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10"
dependencies = [
"secp256k1-sys 0.9.2",
]
[[package]]
name = "secp256k1-sys"
version = "0.8.1"
@ -1263,6 +1269,15 @@ dependencies = [
"cc",
]
[[package]]
name = "secp256k1-sys"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5d1746aae42c19d583c3c1a8c646bfad910498e2051c551a7f2e3c0c9fbb7eb"
dependencies = [
"cc",
]
[[package]]
name = "security-framework"
version = "2.11.0"
@ -1388,10 +1403,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "strsim"
version = "0.11.1"
name = "subtle"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
@ -1573,10 +1588,11 @@ dependencies = [
"anyhow",
"base91",
"bdk",
"clap",
"dotenv",
"env_logger",
"log",
"musig2",
"rand_core",
"reqwest",
"serde",
"sha2",

View File

@ -7,10 +7,11 @@ edition = "2021"
anyhow = "1.0.86"
base91 = "0.1.0"
bdk = "0.29.0"
clap = { version = "4.5.4", features = ["derive", "cargo"] }
dotenv = "0.15.0"
env_logger = "0.11.3"
log = "0.4.21"
musig2 = "0.0.11"
rand_core = "0.6.4"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
serde = "1.0.203"
sha2 = "0.10.8"

View File

@ -13,4 +13,11 @@ pub struct OrderRequest {
pub order_type: String, // buy or sell
pub bond_ratio: u8 // [2, 50]
}
#[derive(Serialize)]
pub struct BondSubmissionRequest {
pub serialized_bond_tx_hex: String,
pub robohash_base91: String,
pub payout_address: String,
pub frost_public_nonce_hex: String,
}

View File

@ -2,22 +2,34 @@
// use taker_utils;
// use utils;
use std::borrow::Borrow;
use anyhow::Result;
use bdk::bitcoin::block;
use crate::cli::TraderSettings;
use crate::communication::api::OfferCreationResponse;
use crate::wallet::{load_wallet, bond::Bond};
use bdk::blockchain::{Blockchain, ElectrumBlockchain};
use bdk::electrum_client::Client;
pub fn run_maker(maker_config: &TraderSettings) -> Result<()> {
let client = Client::new(&maker_config.electrum_endpoint)?;
let blockchain = ElectrumBlockchain::from(client);
// let offer_conditions = OfferCreationResponse::fetch(maker_config)?;
let offer_conditions = OfferCreationResponse {
let offer_conditions = OfferCreationResponse { // hardcoded for testing, locking_address is owned by .env xprv
locking_amount: 90000,
bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm".to_string(),
};
let wallet = load_wallet(maker_config)?;
let wallet = load_wallet(maker_config, &blockchain)?; // initialize the wallet with xprv
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
dbg!(&bond.extract_tx().txid());
let bond = Bond::assemble(&wallet, &offer_conditions, maker_config)?;
dbg!(bond.serialize_hex());
Ok(())
}

View File

@ -1,15 +1,17 @@
pub mod bond;
pub mod wallet_utils;
pub mod musig2;
use bdk::{bitcoin, keys::DescriptorPublicKey, miniscript::Descriptor, template::{Bip86, DescriptorTemplate}, KeychainKind, SyncOptions, Wallet};
use bdk::database::MemoryDatabase;
use bdk::blockchain::ElectrumBlockchain;
use bdk::bitcoin::{Network, secp256k1::rand::{self, RngCore}, bip32::ExtendedPrivKey};
use bdk::bitcoin::{Network, bip32::ExtendedPrivKey};
use bdk::electrum_client::Client;
use anyhow::Result;
use wallet_utils::get_seed;
use std::str::FromStr;
use crate::cli::TraderSettings;
// https://github.com/bitcoindevkit/book-of-bdk
pub struct WalletDescriptors {
pub descriptor: Bip86<ExtendedPrivKey>,
@ -23,19 +25,14 @@ pub fn get_wallet_xprv(xprv_input: Option<String>) -> Result<ExtendedPrivKey> {
if let Some(xprv_i) = xprv_input {
xprv = ExtendedPrivKey::from_str(&xprv_i)?;
} else {
let mut seed: [u8; 32] = [0u8; 32];
rand::thread_rng().fill_bytes(&mut seed); // verify this is secure randomness!
xprv = ExtendedPrivKey::new_master(network, &seed)?;
xprv = ExtendedPrivKey::new_master(network, &get_seed())?;
dbg!("Generated xprv: ", xprv.to_string());
}
Ok(xprv)
}
pub fn load_wallet(trader_config: &TraderSettings) -> Result<Wallet<MemoryDatabase>> {
let client = Client::new(&trader_config.electrum_endpoint)?;
let blockchain = ElectrumBlockchain::from(client);
pub fn load_wallet(trader_config: &TraderSettings, blockchain: &ElectrumBlockchain) -> Result<Wallet<MemoryDatabase>> {
let wallet = Wallet::new(
Bip86(trader_config.wallet_xprv.clone(), KeychainKind::External),
Some(Bip86(trader_config.wallet_xprv.clone(), KeychainKind::Internal)),
@ -43,7 +40,7 @@ pub fn load_wallet(trader_config: &TraderSettings) -> Result<Wallet<MemoryDataba
MemoryDatabase::default(), // non-permanent storage
)?;
wallet.sync(&blockchain, SyncOptions::default())?;
wallet.sync(blockchain, SyncOptions::default())?;
println!("Descriptor balance: {} SAT", wallet.get_balance()?);
Ok(wallet)
}

View File

@ -0,0 +1,4 @@
use musig2::{AggNonce, FirstRound, PartialSignature, PubNonce, SecNonceSpices, SecondRound};
// https://docs.rs/musig2/latest/musig2/

View File

@ -0,0 +1,8 @@
use rand_core::{RngCore, OsRng};
// 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
}