started verifying bonds

This commit is contained in:
aaravm
2024-06-25 10:03:57 +05:30
parent 3b43f8476d
commit afcb7de43e
5 changed files with 94 additions and 1 deletions

View File

@ -66,6 +66,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anyhow"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "async-trait"
version = "0.1.80"
@ -380,10 +386,12 @@ checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
name = "coordinator"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"bdk",
"clap",
"frost-secp256k1",
"hex",
"reqwest",
"serde",
"tokio",

View File

@ -11,3 +11,5 @@ frost-secp256k1 = "1.0.0"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
serde = "1.0.203"
tokio = "1.38.0"
anyhow = "1.0.86"
hex = "0.4"

View File

@ -1,6 +1,6 @@
pub mod api;
use reqwest::StatusCode;
use verify_bond::verify_psbt;
use axum::{routing::post, Json, Router, response::{IntoResponse, Response}, };
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

View File

@ -0,0 +1 @@
pub mod verify_bond;

View File

@ -0,0 +1,82 @@
use bdk::bitcoin::consensus::encode::deserialize;
use bdk::bitcoin::psbt::PartiallySignedTransaction;
use bdk::bitcoin::Transaction;
use bdk::Wallet;
use bdk::database::MemoryDatabase;
use bdk::SignOptions;
use crate::communication::api::BondSubmissionRequest;
use crate::communication::api::OrderActivatedResponse;
use anyhow::{Result, anyhow};
use hex;
pub fn verify_and_respond(
bond_submission: BondSubmissionRequest,
wallet: &Wallet<MemoryDatabase>,
) -> Result<OrderActivatedResponse> {
// Deserialize the signed bond hex
let tx: Transaction = deserialize(hex::decode(bond_submission.signed_bond_hex)?.as_slice())?;
// Verify the transaction (this example assumes you've implemented your own verification logic)
let is_valid = verify_psbt(&tx, &wallet, &bond_submission)?;
if !is_valid {
return Err(anyhow!("Invalid PSBT"));
}
// Create the response (you may need additional logic to generate order_id_hex and timestamp)
let response = OrderActivatedResponse {
order_id_hex: generate_order_id(&tx)?, // Assuming you have a function to generate this
bond_locked_until_timestamp: calculate_bond_lock_time()?, // Assuming you have a function for this
};
Ok(response)
}
pub fn verify_psbt(
tx: &Transaction,
wallet: &Wallet<MemoryDatabase>,
bond_submission: &BondSubmissionRequest,
) -> Result<bool> {
// Example verification logic
// Check if the payout address matches
// let payout_address = bond_submission.payout_address.parse();
// let output = tx.output.iter().find(|output| outputvc
// .script_pubkey == payout_address.script_pubkey());
// if output.is_none() {
// return Ok(false);
// }
// Check if the transaction is signed correctly
let mut psbt = PartiallySignedTransaction::from_unsigned_tx(tx.clone())?;
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
if !finalized {
return Ok(false);
}
// Validate MuSig data (assuming you have methods for this)
let musig_data_valid = validate_musig_data(&bond_submission.musig_pubkey_hex, &bond_submission.musig_pub_nonce_hex)?;
if !musig_data_valid {
return Ok(false);
}
Ok(true)
}
fn generate_order_id(tx: &Transaction) -> Result<String> {
// Example logic to generate an order ID from the transaction
Ok(tx.txid().to_string())
}
fn calculate_bond_lock_time() -> Result<u128> {
// Example logic to calculate the bond lock time
// This might depend on the current block height or a specific timestamp
Ok(12345678901234567890) // Placeholder value
}
fn validate_musig_data(pubkey_hex: &str, nonce_hex: &str) -> Result<bool> {
// Example logic to validate MuSig data
// This might involve parsing the hex strings and ensuring they match expected values
Ok(true) // Placeholder validation
}