From f6d44ee4a165b13da8db9016612a252a63a31922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erce=20Can=20Bekt=C3=BCre?= <47954181+ercecan@users.noreply.github.com> Date: Thu, 9 May 2024 15:28:37 +0300 Subject: [PATCH] Erce/send btc change to utxo address (#525) * Send bitcoin change to address taken from utxos * Lint --- .../bitcoin-regtest/rollup_config.toml | 1 - .../sequencer_rollup_config.toml | 1 - crates/bitcoin-da/src/service.rs | 36 +++++-------------- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/bin/citrea/configs/bitcoin-regtest/rollup_config.toml b/bin/citrea/configs/bitcoin-regtest/rollup_config.toml index defbd175d..144488a8a 100644 --- a/bin/citrea/configs/bitcoin-regtest/rollup_config.toml +++ b/bin/citrea/configs/bitcoin-regtest/rollup_config.toml @@ -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. diff --git a/bin/citrea/configs/bitcoin-regtest/sequencer_rollup_config.toml b/bin/citrea/configs/bitcoin-regtest/sequencer_rollup_config.toml index 58608d26c..51a8e1c56 100644 --- a/bin/citrea/configs/bitcoin-regtest/sequencer_rollup_config.toml +++ b/bin/citrea/configs/bitcoin-regtest/sequencer_rollup_config.toml @@ -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. diff --git a/crates/bitcoin-da/src/service.rs b/crates/bitcoin-da/src/service.rs index dbe041f97..1a69d3885 100644 --- a/crates/bitcoin-da/src/service.rs +++ b/crates/bitcoin-da/src/service.rs @@ -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; @@ -37,7 +36,6 @@ pub struct BitcoinService { client: BitcoinNode, rollup_name: String, network: bitcoin::Network, - address: Address, sequencer_da_private_key: Option, reveal_tx_id_prefix: Vec, } @@ -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, @@ -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")); @@ -90,7 +82,6 @@ impl BitcoinService { client, chain_params.rollup_name, network, - address, private_key, chain_params.reveal_tx_id_prefix, ) @@ -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")); @@ -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, } @@ -129,16 +117,9 @@ impl BitcoinService { client: BitcoinNode, rollup_name: String, network: bitcoin::Network, - address: Address, sequencer_da_private_key: Option, reveal_tx_id_prefix: Vec, ) -> 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 @@ -152,7 +133,6 @@ impl BitcoinService { client, rollup_name, network, - address, sequencer_da_private_key, reveal_tx_id_prefix, } @@ -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"); @@ -180,6 +156,14 @@ impl BitcoinService { // get all available utxos let utxos: Vec = 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) @@ -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 ), @@ -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 ),