Skip to content

Commit

Permalink
Erce/send btc change to utxo address (#525)
Browse files Browse the repository at this point in the history
* Send bitcoin change to address taken from utxos

* Lint
  • Loading branch information
ercecan authored May 9, 2024
1 parent 6163fc0 commit f6d44ee
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
1 change: 0 additions & 1 deletion bin/citrea/configs/bitcoin-regtest/rollup_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ node_username = ""
# fill here
node_password = ""
network = "regtest"
address = "bcrt1q02g8qhycr0v8cnflt86kksfe2sqhm486fdkx4l"

[storage]
# The path to the rollup's data directory. Paths that do not begin with `/` are interpreted as relative paths.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ node_username = ""
# fill here
node_password = ""
network = "regtest"
address = "bcrt1q02g8qhycr0v8cnflt86kksfe2sqhm486fdkx4l"
sequencer_da_private_key = "E9873D79C6D87DC0FB6A5778633389F4453213303DA61F20BD67FC233AA33262"
[storage]
# The path to the rollup's data directory. Paths that do not begin with `/` are interpreted as relative paths.
Expand Down
36 changes: 9 additions & 27 deletions crates/bitcoin-da/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use core::time::Duration;

// use std::sync::Arc;
use async_trait::async_trait;
use bitcoin::address::NetworkUnchecked;
use bitcoin::consensus::encode;
use bitcoin::hashes::{sha256d, Hash};
use bitcoin::secp256k1::SecretKey;
Expand Down Expand Up @@ -37,7 +36,6 @@ pub struct BitcoinService {
client: BitcoinNode,
rollup_name: String,
network: bitcoin::Network,
address: Address<NetworkUnchecked>,
sequencer_da_private_key: Option<SecretKey>,
reveal_tx_id_prefix: Vec<u8>,
}
Expand All @@ -53,10 +51,6 @@ pub struct DaServiceConfig {
// network of the bitcoin node
pub network: String,

// taproot address that holds the funds of the sequencer
// will be used as the change address for the inscribe transaction
pub address: String,

// da private key of the sequencer
pub sequencer_da_private_key: Option<String>,

Expand All @@ -80,8 +74,6 @@ impl BitcoinService {
network,
);

let address = Address::from_str(&config.address).expect("Invalid bitcoin address");

let private_key = config
.sequencer_da_private_key
.map(|pk| SecretKey::from_str(&pk).expect("Invalid private key"));
Expand All @@ -90,7 +82,6 @@ impl BitcoinService {
client,
chain_params.rollup_name,
network,
address,
private_key,
chain_params.reveal_tx_id_prefix,
)
Expand All @@ -109,8 +100,6 @@ impl BitcoinService {
network,
);

let address = Address::from_str(&config.address).expect("Invalid bitcoin address");

let private_key = config
.sequencer_da_private_key
.map(|pk| SecretKey::from_str(&pk).expect("Invalid private key"));
Expand All @@ -119,7 +108,6 @@ impl BitcoinService {
client,
rollup_name: chain_params.rollup_name,
network,
address,
sequencer_da_private_key: private_key,
reveal_tx_id_prefix: chain_params.reveal_tx_id_prefix,
}
Expand All @@ -129,16 +117,9 @@ impl BitcoinService {
client: BitcoinNode,
rollup_name: String,
network: bitcoin::Network,
address: Address<NetworkUnchecked>,
sequencer_da_private_key: Option<SecretKey>,
reveal_tx_id_prefix: Vec<u8>,
) -> Self {
// We can't store address with the network check because it's not serializable
address
.clone()
.require_network(network)
.expect("Invalid address for network!");

let wallets = client
.list_wallets()
.await
Expand All @@ -152,7 +133,6 @@ impl BitcoinService {
client,
rollup_name,
network,
address,
sequencer_da_private_key,
reveal_tx_id_prefix,
}
Expand All @@ -167,11 +147,7 @@ impl BitcoinService {

let blob = blob.to_vec();
let network = self.network;
let address = self
.address
.clone()
.require_network(network)
.expect("Invalid network for address");

let rollup_name = self.rollup_name.clone();
let sequencer_da_private_key = self.sequencer_da_private_key.expect("No private key set");

Expand All @@ -180,6 +156,14 @@ impl BitcoinService {

// get all available utxos
let utxos: Vec<UTXO> = client.get_utxos().await?;
if utxos.is_empty() {
return Err(anyhow::anyhow!("No UTXOs left for transaction"));
}
// get address from a utxo
let address = Address::from_str(&utxos[0].address.clone())
.unwrap()
.require_network(network)
.expect("Invalid network for address");

// sign the blob for authentication of the sequencer
let (signature, public_key) = sign_blob_with_private_key(&blob, &sequencer_da_private_key)
Expand Down Expand Up @@ -520,7 +504,6 @@ mod tests {
node_username: "chainway".to_string(),
node_password: "topsecret".to_string(),
network: "regtest".to_string(),
address: "bcrt1qy85zdv5se9d9ceg9nvay36t6j86z95fny4rdzu".to_string(),
sequencer_da_private_key: Some(
"E9873D79C6D87DC0FB6A5778633389F4453213303DA61F20BD67FC233AA33262".to_string(), // Test key, safe to publish
),
Expand Down Expand Up @@ -636,7 +619,6 @@ mod tests {
node_username: "chainway".to_string(),
node_password: "topsecret".to_string(),
network: "regtest".to_string(),
address: "bcrt1qy85zdv5se9d9ceg9nvay36t6j86z95fny4rdzu".to_string(),
sequencer_da_private_key: Some(
"E9873D79C6D87DC0FB6A5778633389F4453213303DA61F20BD67FC233AA33261".to_string(), // Test key, safe to publish
),
Expand Down

0 comments on commit f6d44ee

Please sign in to comment.