Skip to content
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
17 changes: 12 additions & 5 deletions examples/union/participants/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use bitvmx_client::{
variables::PartialUtxo,
},
spv_proof::BtcTxSPVProof,
types::OutgoingBitVMXApiMessages::*,
types::{OutgoingBitVMXApiMessages::*, OutputPatternFilter, RSK_PEGIN_TAG},
};

const KEY_SPEND_FEE: u64 = 135;
Expand Down Expand Up @@ -106,9 +106,16 @@ impl User {
) -> Result<(Txid, Transaction)> {
info!(id = self.id, "Requesting pegin");

// Enable RSK pegin monitoring using the public API
// Enable output pattern monitoring for RSK pegin transactions
//TOOD: Define proper confirmation threshold
self.bitvmx.subscribe_to_rsk_pegin(Some(1))?;
self.bitvmx.subscribe_to_output_pattern(
OutputPatternFilter {
output_index: 1,
tag: RSK_PEGIN_TAG.to_vec(),
max_outputs: None,
},
Some(1),
)?;

// Create a proper RSK pegin transaction and send it as if it was a user transaction
let packet_number = 0;
Expand Down Expand Up @@ -145,8 +152,8 @@ impl User {
request_pegin_txid
);

// Wait for Bitvmx news PeginTransactionFound message
let (_, _) = wait_until_msg!(&self.bitvmx, PeginTransactionFound(_txid, _tx_status) => (_txid, _tx_status));
// Wait for Bitvmx news OutputPatternTransactionFound message
let (_, _, _) = wait_until_msg!(&self.bitvmx, OutputPatternTransactionFound(_txid, _tx_status, _tag) => (_txid, _tx_status, _tag));
info!("RSK request pegin completed");
info!("Waiting for SPV proof...");

Expand Down
41 changes: 31 additions & 10 deletions src/bitvmx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
signature_verifier::SignatureVerifier,
types::{
IncomingBitVMXApiMessages, OutgoingBitVMXApiMessages, ProgramContext, ProgramStatus,
PROGRAM_TYPE_AGGREGATED_KEY,
RSK_PEGIN_TAG, PROGRAM_TYPE_AGGREGATED_KEY,
},
};
use bitcoin::secp256k1::Message;
Expand Down Expand Up @@ -609,14 +609,25 @@ impl BitVMX {
context_data,
));
}
MonitorNews::RskPeginTransaction(tx_id, tx_status) => {
let data = serde_json::to_string(
&OutgoingBitVMXApiMessages::PeginTransactionFound(tx_id, tx_status),
)?;
MonitorNews::OutputPatternTransaction(tx_id, tx_status, tag) => {
if tag == RSK_PEGIN_TAG {
let legacy = OutgoingBitVMXApiMessages::PeginTransactionFound(tx_id, tx_status.clone());
let data = serde_json::to_string(&legacy)?;
self.program_context
.broker_channel
.send(&self.config.components.l2, data)?;
}
let outgoing = OutgoingBitVMXApiMessages::OutputPatternTransactionFound(
tx_id,
tx_status,
tag.clone(),
);
let data = serde_json::to_string(&outgoing)?;
self.program_context
.broker_channel
.send(&self.config.components.l2, data)?;
ack_news = AckNews::Monitor(AckMonitorNews::RskPeginTransaction(tx_id));
ack_news =
AckNews::Monitor(AckMonitorNews::OutputPatternTransaction(tx_id, tag));
}
MonitorNews::NewBlock(block_height, block_hash) => {
debug!("New block: {} {}", block_height, block_hash);
Expand Down Expand Up @@ -1232,14 +1243,14 @@ impl BitVMX {
Ok(())
}

fn subscribe_to_rsk_pegin(
fn subscribe_to_output_pattern(
&mut self,
filter: bitcoin_coordinator::OutputPatternFilter,
confirmation_threshold: Option<u32>,
) -> Result<(), BitVMXError> {
// Enable RSK pegin transaction monitoring
self.program_context
.bitcoin_coordinator
.monitor(TypesToMonitor::RskPegin(confirmation_threshold))?;
.monitor(TypesToMonitor::OutputPattern(filter, confirmation_threshold))?;
Ok(())
}

Expand Down Expand Up @@ -1496,8 +1507,18 @@ impl BitVMX {
txid,
confirmation_threshold,
) => self.subscribe_to_tx(from, uuid, txid, confirmation_threshold)?,
IncomingBitVMXApiMessages::SubscribeToOutputPattern(filter, confirmation_threshold) => {
self.subscribe_to_output_pattern(filter, confirmation_threshold)?
}
IncomingBitVMXApiMessages::SubscribeToRskPegin(confirmation_threshold) => {
self.subscribe_to_rsk_pegin(confirmation_threshold)?
self.subscribe_to_output_pattern(
bitcoin_coordinator::OutputPatternFilter {
output_index: 1,
tag: RSK_PEGIN_TAG.to_vec(),
max_outputs: None,
},
confirmation_threshold,
)?
}
IncomingBitVMXApiMessages::GetSPVProof(txid) => self.get_spv_proof(from, txid)?,

Expand Down
13 changes: 12 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
participant::CommsAddress,
variables::{VariableTypes, WitnessTypes},
},
types::{IncomingBitVMXApiMessages, OutgoingBitVMXApiMessages},
types::{IncomingBitVMXApiMessages, OutgoingBitVMXApiMessages, OutputPatternFilter},
};
use anyhow::Result;
use bitcoin::{PublicKey, Transaction, Txid};
Expand Down Expand Up @@ -158,6 +158,17 @@ impl BitVMXClient {
))
}

pub fn subscribe_to_output_pattern(
&self,
filter: OutputPatternFilter,
confirmation_threshold: Option<u32>,
) -> Result<()> {
self.send_message(IncomingBitVMXApiMessages::SubscribeToOutputPattern(
filter,
confirmation_threshold,
))
}

pub fn subscribe_to_rsk_pegin(&self, confirmation_threshold: Option<u32>) -> Result<()> {
self.send_message(IncomingBitVMXApiMessages::SubscribeToRskPegin(
confirmation_threshold,
Expand Down
10 changes: 9 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use crate::{
variables::{Globals, VariableTypes, WitnessTypes, WitnessVars},
},
};
pub use bitcoin_coordinator::OutputPatternFilter;
pub const RSK_PEGIN_TAG: &[u8] = b"RSK_PEGIN";

pub struct ProgramContext {
pub key_manager: Rc<KeyManager>,
Expand Down Expand Up @@ -93,6 +95,7 @@ pub enum IncomingBitVMXApiMessages {
GetHashedMessage(Uuid, String, u32, u32),
Setup(ProgramId, String, Vec<CommsAddress>, u16),
SubscribeToTransaction(Uuid, Txid, Option<u32>),
SubscribeToOutputPattern(OutputPatternFilter, Option<u32>),
SubscribeToRskPegin(Option<u32>),
GetSPVProof(Txid),
DispatchTransaction(Uuid, Transaction, Option<u32>),
Expand Down Expand Up @@ -131,7 +134,9 @@ pub enum OutgoingBitVMXApiMessages {
Pong(Uuid),
// response for transaction get and dispatch
Transaction(Uuid, TransactionStatus, Option<String>),
// Represents when pegin transactions is found
// Represents when a transaction matching a generic output pattern is found
OutputPatternTransactionFound(Txid, TransactionStatus, Vec<u8>),
// Represents when a RSK pegin transaction is found (kept for backward compatibility)
PeginTransactionFound(Txid, TransactionStatus),
// Represents when a spending utxo transaction is found
SpendingUTXOTransactionFound(Uuid, Txid, u32, TransactionStatus),
Expand Down Expand Up @@ -285,6 +290,9 @@ impl OutgoingBitVMXApiMessages {
match self {
OutgoingBitVMXApiMessages::Pong(_) => "Pong".to_string(),
OutgoingBitVMXApiMessages::Transaction(_, _, _) => "Transaction".to_string(),
OutgoingBitVMXApiMessages::OutputPatternTransactionFound(_, _, _) => {
"OutputPatternTransactionFound".to_string()
}
OutgoingBitVMXApiMessages::PeginTransactionFound(_, _) => {
"PeginTransactionFound".to_string()
}
Expand Down
Loading