Skip to content

Commit

Permalink
Full withdrawal flow (#253)
Browse files Browse the repository at this point in the history
* Save operator take and slash or take sigs

* Operator can find kickoff utxo

* Operator can send kickoff tx onchain

* WIP verifiers and operators should use same db missing

* Fix bugs

* WIP Boths sigs verified but operator_take_tx fails

* WIP MuSig2 fix

* Fix withdrawal happy part test

* nits

* Fix musig test

* Use key aggregator trait

---------

Co-authored-by: ozankaymak <ozan.kaymak@boun.edu.tr>
  • Loading branch information
ekrembal and ozankaymak authored Aug 29, 2024
1 parent 74e0065 commit 3341e35
Show file tree
Hide file tree
Showing 19 changed files with 1,041 additions and 503 deletions.
21 changes: 21 additions & 0 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ impl Actor {
Ok(self.sign(sig_hash))
}

pub fn sign_taproot_pubkey_spend(
&self,
tx_handler: &mut TxHandler,
input_index: usize,
sighash_type: Option<TapSighashType>,
) -> Result<schnorr::Signature, BridgeError> {
let mut sighash_cache = SighashCache::new(&mut tx_handler.tx);
let sig_hash = sighash_cache.taproot_key_spend_signature_hash(
input_index,
&match sighash_type {
Some(TapSighashType::SinglePlusAnyoneCanPay) => bitcoin::sighash::Prevouts::One(
input_index,
tx_handler.prevouts[input_index].clone(),
),
_ => bitcoin::sighash::Prevouts::All(&tx_handler.prevouts),
},
sighash_type.unwrap_or(TapSighashType::Default),
)?;
self.sign_with_tweak(sig_hash, None)
}

pub fn sign_taproot_pubkey_spend_tx(
&self,
tx: &mut bitcoin::Transaction,
Expand Down
97 changes: 96 additions & 1 deletion core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use crate::{config::BridgeConfig, errors::BridgeError};
use crate::{EVMAddress, UTXO};
use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, OutPoint, Txid};
use secp256k1::schnorr;
use sqlx::{Pool, Postgres};
use std::fs;

use super::wrapper::{AddressDB, EVMAddressDB, OutPointDB, TxOutDB, TxidDB, UTXODB};
use super::wrapper::{AddressDB, EVMAddressDB, OutPointDB, SignatureDB, TxOutDB, TxidDB, UTXODB};

#[derive(Clone, Debug)]
pub struct Database {
Expand Down Expand Up @@ -401,6 +402,100 @@ impl Database {
Ok(())
}

pub async fn save_slash_or_take_sig(
&self,
deposit_outpoint: OutPoint,
kickoff_utxo: UTXO,
slash_or_take_sig: schnorr::Signature,
) -> Result<(), BridgeError> {
sqlx::query(
"UPDATE deposit_kickoff_utxos
SET slash_or_take_sig = $3
WHERE deposit_outpoint = $1 AND kickoff_utxo = $2;",
)
.bind(OutPointDB(deposit_outpoint))
.bind(sqlx::types::Json(UTXODB {
outpoint_db: OutPointDB(kickoff_utxo.outpoint),
txout_db: TxOutDB(kickoff_utxo.txout),
}))
.bind(SignatureDB(slash_or_take_sig))
.execute(&self.connection)
.await?;

Ok(())
}

pub async fn get_slash_or_take_sig(
&self,
deposit_outpoint: OutPoint,
kickoff_utxo: UTXO,
) -> Result<Option<schnorr::Signature>, BridgeError> {
let qr: Option<(SignatureDB,)> = sqlx::query_as(
"SELECT slash_or_take_sig
FROM deposit_kickoff_utxos
WHERE deposit_outpoint = $1 AND kickoff_utxo = $2;",
)
.bind(OutPointDB(deposit_outpoint))
.bind(sqlx::types::Json(UTXODB {
outpoint_db: OutPointDB(kickoff_utxo.outpoint),
txout_db: TxOutDB(kickoff_utxo.txout),
}))
.fetch_optional(&self.connection)
.await?;

match qr {
Some(sig) => Ok(Some(sig.0 .0)),
None => Ok(None),
}
}

pub async fn save_operator_take_sig(
&self,
deposit_outpoint: OutPoint,
kickoff_utxo: UTXO,
operator_take_sig: schnorr::Signature,
) -> Result<(), BridgeError> {
sqlx::query(
"UPDATE deposit_kickoff_utxos
SET operator_take_sig = $3
WHERE deposit_outpoint = $1 AND kickoff_utxo = $2;",
)
.bind(OutPointDB(deposit_outpoint))
.bind(sqlx::types::Json(UTXODB {
outpoint_db: OutPointDB(kickoff_utxo.outpoint),
txout_db: TxOutDB(kickoff_utxo.txout),
}))
.bind(SignatureDB(operator_take_sig))
.execute(&self.connection)
.await?;

Ok(())
}

pub async fn get_operator_take_sig(
&self,
deposit_outpoint: OutPoint,
kickoff_utxo: UTXO,
) -> Result<Option<schnorr::Signature>, BridgeError> {
let qr: Option<(SignatureDB,)> = sqlx::query_as(
"SELECT operator_take_sig
FROM deposit_kickoff_utxos
WHERE deposit_outpoint = $1 AND kickoff_utxo = $2;",
)
.bind(OutPointDB(deposit_outpoint))
.bind(sqlx::types::Json(UTXODB {
outpoint_db: OutPointDB(kickoff_utxo.outpoint),
txout_db: TxOutDB(kickoff_utxo.txout),
}))
.fetch_optional(&self.connection)
.await?;

match qr {
Some(sig) => Ok(Some(sig.0 .0)),
None => Ok(None),
}
}

pub async fn add_deposit_kickoff_generator_tx(
&self,
txid: Txid,
Expand Down
2 changes: 1 addition & 1 deletion core/src/database/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct EVMAddressDB(pub EVMAddress);
#[derive(Serialize, Deserialize)]
pub struct TxidDB(pub Txid);

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, sqlx::FromRow)]
pub struct SignatureDB(pub secp256k1::schnorr::Signature);

#[derive(Serialize, Deserialize, sqlx::FromRow)]
Expand Down
3 changes: 2 additions & 1 deletion core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! This module defines errors, returned by the library.

use bitcoin::{
consensus::encode::FromHexError,
merkle_tree::MerkleBlockError,
taproot::{TaprootBuilder, TaprootBuilderError},
};
Expand Down Expand Up @@ -157,7 +158,7 @@ pub enum BridgeError {
DepositInfoNotFound,

#[error("FromHexError: {0}")]
FromHexError(#[from] hex::FromHexError),
FromHexError(#[from] FromHexError),

#[error("FromSliceError: {0}")]
FromSliceError(#[from] bitcoin::hashes::FromSliceError),
Expand Down
4 changes: 2 additions & 2 deletions core/src/extended_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ where
self.client.get_transaction(txid, include_watchonly)
}

pub fn send_raw_transaction(
pub fn send_raw_transaction<T: bitcoincore_rpc::RawTx>(
&self,
tx: &Transaction,
tx: T,
) -> Result<bitcoin::Txid, bitcoincore_rpc::Error> {
self.client.send_raw_transaction(tx)
}
Expand Down
Loading

0 comments on commit 3341e35

Please sign in to comment.