Skip to content

Save signatures with more information to db #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ mod tests {
use crate::builder::transaction::output::UnspentTxOut;
use crate::builder::transaction::TxHandlerBuilder;
use crate::config::BridgeConfig;
use crate::rpc::clementine::NormalSignatureKind;
use crate::utils::{initialize_logger, SECP};
use crate::{
actor::WinternitzDerivationPath, builder::transaction::TxHandler,
Expand All @@ -324,6 +325,7 @@ mod tests {
let (op_addr, op_spend) =
create_taproot_address(&[], Some(actor.xonly_public_key), Network::Regtest);
let builder = TxHandlerBuilder::new().add_input(
NormalSignatureKind::AlreadyDisproved1,
SpendableTxIn::new(
OutPoint::default(),
TxOut {
Expand All @@ -333,6 +335,7 @@ mod tests {
vec![],
Some(op_spend),
),
crate::builder::script::SpendPath::Unknown,
Sequence::ENABLE_RBF_NO_LOCKTIME,
);
builder
Expand Down
2 changes: 1 addition & 1 deletion core/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Aggregator {
agg_nonce: &MusigAggNonce,
partial_sigs: Vec<MusigPartialSignature>,
) -> Result<schnorr::Signature, BridgeError> {
let mut tx = builder::transaction::create_move_to_vault_txhandler(
let tx = builder::transaction::create_move_to_vault_txhandler(
deposit_outpoint,
evm_address,
recovery_taproot_address,
Expand Down
7 changes: 7 additions & 0 deletions core/src/builder/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ use bitvm::signatures::winternitz::{Parameters, PublicKey};
use std::any::Any;
use std::fmt::Debug;

#[derive(Debug, Clone)]
pub enum SpendPath {
ScriptSpend(usize),
KeySpend,
Unknown,
}

pub trait SpendableScript: Send + Sync + 'static + std::any::Any {
fn as_any(&self) -> &dyn Any;

Expand Down
186 changes: 126 additions & 60 deletions core/src/builder/sighash.rs

Large diffs are not rendered by default.

43 changes: 40 additions & 3 deletions core/src/builder/transaction/challenge.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::builder;
use crate::builder::script::{PreimageRevealScript, TimelockScript, WinternitzCommit};
use crate::builder::script::{PreimageRevealScript, SpendPath, TimelockScript, WinternitzCommit};
use crate::builder::transaction::output::UnspentTxOut;
use crate::builder::transaction::txhandler::{TxHandler, DEFAULT_SEQUENCE};
use crate::builder::transaction::*;
use crate::constants::{BLOCKS_PER_WEEK, OPERATOR_CHALLENGE_AMOUNT};
use crate::errors::BridgeError;
use crate::rpc::clementine::{NormalSignatureKind, WatchtowerSignatureKind};
use bitcoin::{Amount, ScriptBuf, Sequence, TxOut, XOnlyPublicKey};
use bitvm::signatures::winternitz;
use std::sync::Arc;
Expand All @@ -20,7 +21,9 @@ pub fn create_watchtower_challenge_kickoff_txhandler(
network: bitcoin::Network,
) -> Result<TxHandler, BridgeError> {
let mut builder = TxHandlerBuilder::new().add_input(
NormalSignatureKind::WatchtowerChallengeKickoff,
kickoff_tx_handler.get_spendable_output(0)?,
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);

Expand Down Expand Up @@ -56,7 +59,9 @@ pub fn create_watchtower_challenge_kickoff_txhandler_simplified(
watchtower_challenge_addresses: &[ScriptBuf],
) -> Result<TxHandler, BridgeError> {
let mut builder = TxHandlerBuilder::new().add_input(
NormalSignatureKind::WatchtowerChallengeKickoff,
kickoff_tx_handler.get_spendable_output(0)?,
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);
for i in 0..num_watchtowers {
Expand Down Expand Up @@ -94,7 +99,12 @@ pub fn create_watchtower_challenge_txhandler(
network: bitcoin::Network,
) -> Result<TxHandler, BridgeError> {
let builder = TxHandlerBuilder::new().add_input(
(
WatchtowerSignatureKind::WatchtowerNotStored,
watchtower_idx as i32,
),
wcp_txhandler.get_spendable_output(watchtower_idx)?,
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);

Expand Down Expand Up @@ -126,14 +136,28 @@ pub fn create_watchtower_challenge_txhandler(
/// prevent the operator from sending `assert_begin_tx`.
pub fn create_operator_challenge_nack_txhandler(
watchtower_challenge_txhandler: &TxHandler,
watchtower_idx: usize,
kickoff_txhandler: &TxHandler,
) -> Result<TxHandler, BridgeError> {
Ok(TxHandlerBuilder::new()
.add_input(
(
WatchtowerSignatureKind::OperatorChallengeNack1,
watchtower_idx as i32,
),
watchtower_challenge_txhandler.get_spendable_output(0)?,
SpendPath::ScriptSpend(1),
Sequence::from_height(BLOCKS_PER_WEEK / 2),
)
.add_input(kickoff_txhandler.get_spendable_output(2)?, DEFAULT_SEQUENCE)
.add_input(
(
WatchtowerSignatureKind::OperatorChallengeNack2,
watchtower_idx as i32,
),
kickoff_txhandler.get_spendable_output(2)?,
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
)
.add_output(UnspentTxOut::from_partial(
builder::transaction::anchor_output(),
))
Expand All @@ -149,11 +173,15 @@ pub fn create_already_disproved_txhandler(
) -> Result<TxHandler, BridgeError> {
Ok(TxHandlerBuilder::new()
.add_input(
NormalSignatureKind::AlreadyDisproved1,
assert_end_txhandler.get_spendable_output(1)?,
SpendPath::ScriptSpend(1),
Sequence::from_height(BLOCKS_PER_WEEK * 2),
)
.add_input(
NormalSignatureKind::AlreadyDisproved2,
sequential_collateral_txhandler.get_spendable_output(0)?,
SpendPath::KeySpend,
DEFAULT_SEQUENCE,
)
.add_output(UnspentTxOut::from_partial(
Expand All @@ -171,11 +199,15 @@ pub fn create_disprove_txhandler(
) -> Result<TxHandler, BridgeError> {
Ok(TxHandlerBuilder::new()
.add_input(
NormalSignatureKind::NotStored,
assert_end_txhandler.get_spendable_output(0)?,
SpendPath::Unknown,
DEFAULT_SEQUENCE,
)
.add_input(
NormalSignatureKind::Disprove2,
sequential_collateral_txhandler.get_spendable_output(0)?,
SpendPath::KeySpend,
DEFAULT_SEQUENCE,
)
.add_output(UnspentTxOut::from_partial(
Expand All @@ -192,7 +224,12 @@ pub fn create_challenge_txhandler(
operator_reimbursement_address: &bitcoin::Address,
) -> Result<TxHandler, BridgeError> {
Ok(TxHandlerBuilder::new()
.add_input(kickoff_txhandler.get_spendable_output(1)?, DEFAULT_SEQUENCE)
.add_input(
NormalSignatureKind::Challenge,
kickoff_txhandler.get_spendable_output(1)?,
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
)
.add_output(UnspentTxOut::from_partial(TxOut {
value: OPERATOR_CHALLENGE_AMOUNT,
script_pubkey: operator_reimbursement_address.script_pubkey(),
Expand Down
14 changes: 13 additions & 1 deletion core/src/builder/transaction/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::builder::address::create_taproot_address;
use crate::builder::script::SpendableScript;
use crate::builder::{address::create_taproot_address, script::SpendPath};
use crate::rpc::clementine::tagged_signature::SignatureId;
use bitcoin::{
taproot::{LeafVersion, TaprootSpendInfo},
Amount, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Witness, WitnessProgram, XOnlyPublicKey,
Expand Down Expand Up @@ -153,6 +154,7 @@ impl SpendableTxIn {
}
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct SpentTxIn {
spendable: SpendableTxIn,
Expand All @@ -166,18 +168,24 @@ pub struct SpentTxIn {
///
/// Has to be Some(_) when the transaction is signed.
witness: Option<Witness>,
spend_path: SpendPath,
input_id: SignatureId,
}

impl SpentTxIn {
pub fn from_spendable(
input_id: SignatureId,
spendable: SpendableTxIn,
spend_path: SpendPath,
sequence: Sequence,
witness: Option<Witness>,
) -> SpentTxIn {
SpentTxIn {
spendable,
sequence,
witness,
spend_path,
input_id,
}
}

Expand All @@ -189,6 +197,10 @@ impl SpentTxIn {
&self.witness
}

pub fn get_signature_id(&self) -> SignatureId {
self.input_id
}

pub fn set_witness(&mut self, witness: Witness) {
self.witness = Some(witness);
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/builder/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::sync::Arc;

use super::script::SpendPath;
use super::script::{CheckSig, DepositScript, TimelockScript};
pub use crate::builder::transaction::challenge::*;
use crate::builder::transaction::input::SpendableTxIn;
Expand All @@ -15,6 +16,7 @@ use crate::builder::transaction::output::UnspentTxOut;
pub use crate::builder::transaction::txhandler::*;
use crate::constants::ANCHOR_AMOUNT;
use crate::errors::BridgeError;
use crate::rpc::clementine::NormalSignatureKind;
use crate::EVMAddress;
use bitcoin::address::NetworkUnchecked;
use bitcoin::opcodes::all::{OP_PUSHNUM_1, OP_RETURN};
Expand Down Expand Up @@ -97,13 +99,15 @@ pub fn create_move_to_vault_txhandler(
));

let builder = TxHandlerBuilder::new().add_input(
NormalSignatureKind::NotStored,
SpendableTxIn::from_scripts(
deposit_outpoint,
bridge_amount_sats,
vec![deposit_script, script_timelock],
None,
network,
),
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);

Expand Down
23 changes: 21 additions & 2 deletions core/src/builder/transaction/operator_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use crate::builder::transaction::txhandler::TxHandler;
pub use crate::builder::transaction::*;
use crate::constants::{BLOCKS_PER_WEEK, MIN_TAPROOT_AMOUNT, PARALLEL_ASSERT_TX_CHAIN_SIZE};
use crate::errors::BridgeError;
use crate::rpc::clementine::NormalSignatureKind;
use crate::utils::SECP;
use bitcoin::hashes::Hash;
use bitcoin::taproot::TaprootBuilder;
Expand All @@ -28,7 +29,9 @@ pub fn create_assert_begin_txhandler(

// Add input from kickoff tx
builder = builder.add_input(
NormalSignatureKind::NotStored,
kickoff_txhandler.get_spendable_output(2)?,
builder::script::SpendPath::ScriptSpend(1),
Sequence::from_height(BLOCKS_PER_WEEK / 2 * 5), // 2.5 weeks
);

Expand Down Expand Up @@ -58,6 +61,7 @@ pub fn create_mini_assert_tx(

builder
.add_input(
NormalSignatureKind::NotStored,
SpendableTxIn::new_partial(
OutPoint {
txid: prev_txid,
Expand All @@ -68,6 +72,7 @@ pub fn create_mini_assert_tx(
script_pubkey: out_script.clone(),
},
),
builder::script::SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
)
.add_output(UnspentTxOut::from_partial(TxOut {
Expand Down Expand Up @@ -155,10 +160,20 @@ pub fn create_assert_end_txhandler(
let mut builder = TxHandlerBuilder::new();

for txhandler in mini_tx_handlers.into_iter() {
builder = builder.add_input(txhandler.get_spendable_output(0)?, DEFAULT_SEQUENCE);
builder = builder.add_input(
NormalSignatureKind::NotStored,
txhandler.get_spendable_output(0)?,
builder::script::SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);
}

builder = builder.add_input(kickoff_txhandler.get_spendable_output(3)?, DEFAULT_SEQUENCE);
builder = builder.add_input(
NormalSignatureKind::AssertEndLast,
kickoff_txhandler.get_spendable_output(3)?,
builder::script::SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);

let disprove_taproot_spend_info = TaprootBuilder::new()
.add_hidden_node(0, TapNodeHash::from_byte_array(*root_hash))
Expand Down Expand Up @@ -207,11 +222,15 @@ pub fn create_disprove_timeout_txhandler(
) -> Result<TxHandler<Unsigned>, BridgeError> {
Ok(TxHandlerBuilder::new()
.add_input(
NormalSignatureKind::DisproveTimeout1,
assert_end_txhandler.get_spendable_output(0)?,
builder::script::SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
)
.add_input(
NormalSignatureKind::DisproveTimeout2,
assert_end_txhandler.get_spendable_output(1)?,
builder::script::SpendPath::ScriptSpend(0),
Sequence::from_height(BLOCKS_PER_WEEK),
)
.add_output(UnspentTxOut::from_scripts(
Expand Down
12 changes: 12 additions & 0 deletions core/src/builder/transaction/operator_collateral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn create_sequential_collateral_txhandler(
) -> Result<TxHandler, BridgeError> {
let (op_address, op_spend) = create_taproot_address(&[], Some(operator_xonly_pk), network);
let mut builder = TxHandlerBuilder::new().add_input(
NormalSignatureKind::NotStored,
SpendableTxIn::new(
OutPoint {
txid: input_txid,
Expand All @@ -61,6 +62,7 @@ pub fn create_sequential_collateral_txhandler(
vec![],
Some(op_spend.clone()),
),
SpendPath::KeySpend,
DEFAULT_SEQUENCE,
);
let max_withdrawal_time_locked_script = Arc::new(TimelockScript::new(
Expand Down Expand Up @@ -123,11 +125,15 @@ pub fn create_reimburse_generator_txhandler(
) -> Result<TxHandler, BridgeError> {
let mut builder = TxHandlerBuilder::new()
.add_input(
NormalSignatureKind::NotStored,
sequential_collateral_txhandler.get_spendable_output(0)?,
SpendPath::KeySpend,
DEFAULT_SEQUENCE,
)
.add_input(
NormalSignatureKind::NotStored,
sequential_collateral_txhandler.get_spendable_output(1)?,
SpendPath::ScriptSpend(0),
Sequence::from_height(max_withdrawal_time_block_count),
);

Expand Down Expand Up @@ -163,7 +169,9 @@ pub fn create_kickoff_utxo_timeout_txhandler(
kickoff_idx: usize,
) -> Result<TxHandler, BridgeError> {
let builder = TxHandlerBuilder::new().add_input(
NormalSignatureKind::NotStored,
sequential_collateral_txhandler.get_spendable_output(2 + kickoff_idx)?,
SpendPath::ScriptSpend(1),
DEFAULT_SEQUENCE,
);

Expand All @@ -184,11 +192,15 @@ pub fn create_kickoff_timeout_txhandler(
) -> Result<TxHandler, BridgeError> {
let builder = TxHandlerBuilder::new()
.add_input(
NormalSignatureKind::KickoffTimeout1,
kickoff_tx_handler.get_spendable_output(3)?,
SpendPath::ScriptSpend(1),
DEFAULT_SEQUENCE,
)
.add_input(
NormalSignatureKind::KickoffTimeout2,
sequential_collateral_txhandler.get_spendable_output(0)?,
SpendPath::KeySpend,
DEFAULT_SEQUENCE,
);
Ok(builder
Expand Down
Loading
Loading