Skip to content

Commit

Permalink
chore: upgrade bitcoin cargo dependencies (#1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard authored Sep 12, 2024
1 parent 76eba28 commit fb70931
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 120 deletions.
100 changes: 13 additions & 87 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions components/clarinet-deployments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ stacks-codec = { path = "../stacks-codec", optional = true }

# CLI
reqwest = { workspace = true }
bitcoin = { version = "0.29.2", optional = true }
bitcoincore-rpc = { version = "0.16.0", optional = true }
bitcoincore-rpc-json = { version = "0.16.0", optional = true }
bitcoin = { version = "0.31.2", optional = true }
bitcoincore-rpc = { version = "0.18.0", optional = true }
bitcoincore-rpc-json = { version = "0.18.0", optional = true }
base58 = { version = "0.2.0", optional = true }
base64 = "0.21.3"
tiny-hderive = { version = "0.3.0", optional = true }
Expand Down
59 changes: 35 additions & 24 deletions components/clarinet-deployments/src/onchain/bitcoin_deployment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::str::FromStr;

use base58::FromBase58;
use bitcoin::absolute::LockTime;
use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::script::Builder;
use bitcoin::consensus::encode;
use bitcoin::hashes::Hash;
use bitcoin::script::PushBytes;
use bitcoin::sighash::SighashCache;
use bitcoin::transaction::Version;
use bitcoin::{
OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Txid, Witness,
Amount, OutPoint, PubkeyHash, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Witness,
};
use bitcoincore_rpc::bitcoin::secp256k1::{Message, PublicKey, Secp256k1, SecretKey};
use bitcoincore_rpc::bitcoin::Address;
Expand All @@ -21,8 +26,8 @@ pub fn build_transaction_spec(
utxos: &mut Vec<ListUnspentResultEntry>,
) -> (Transaction, Vec<ListUnspentResultEntry>) {
let mut transaction = Transaction {
version: 1,
lock_time: PackedLockTime(0),
version: Version::ONE,
lock_time: LockTime::ZERO,
input: vec![],
output: vec![],
};
Expand Down Expand Up @@ -52,28 +57,22 @@ pub fn build_transaction_spec(
let utxo = utxos.remove(index);
let input = TxIn {
previous_output: OutPoint {
txid: Txid::from_hash(utxo.txid.as_hash()),
txid: utxo.txid,
vout: utxo.vout,
},
script_sig: Script::new(),
script_sig: ScriptBuf::default(),
sequence: Sequence(0xFFFFFFFD), // allow RBF
witness: Witness::new(),
};
transaction.input.push(input);
selected_utxos.push(utxo);
}

// Prepare Recipient output
let address = {
match Address::from_str(&tx_spec.recipient) {
Ok(address) => address,
Err(e) => panic!("{:?}", e),
}
};
let address = Address::from_str(&tx_spec.recipient).unwrap_or_else(|e| panic!("{:?}", e));

let txout = TxOut {
value: tx_spec.sats_amount,
script_pubkey: address.script_pubkey(),
value: Amount::from_sat(tx_spec.sats_amount),
script_pubkey: address.assume_checked_ref().script_pubkey(),
};
transaction.output.push(txout);

Expand All @@ -82,12 +81,15 @@ pub fn build_transaction_spec(
.expected_sender
.from_base58()
.expect("Unable to get bytes sender btc address");

let pubkey_hash = PubkeyHash::from_slice(&sender_pub_key_hash[1..21]).expect("Invalid hash");

let txout = TxOut {
value: cumulated_amount - tx_spec.sats_amount - tx_fee,
value: Amount::from_sat(cumulated_amount - tx_spec.sats_amount - tx_fee),
script_pubkey: Builder::new()
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(&sender_pub_key_hash[1..21])
.push_slice(pubkey_hash)
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script(),
Expand All @@ -104,23 +106,27 @@ pub fn sign_transaction(
) {
for (i, utxo) in utxos.into_iter().enumerate() {
let sig_hash_all = 0x01;
let script_pub_key = Script::from(utxo.script_pub_key.into_bytes());
let sig_hash = transaction.signature_hash(i, &script_pub_key, sig_hash_all);
let script_pub_key = ScriptBuf::from(utxo.script_pub_key.into_bytes());
let sig_hash = SighashCache::new(transaction.clone())
.legacy_signature_hash(i, &script_pub_key, sig_hash_all)
.unwrap();

let (sig_der, public_key) = {
let sig_hash_bytes = sig_hash.as_hash();
let sig_hash_bytes = sig_hash;
let message =
Message::from_slice(&sig_hash_bytes[..]).expect("Unable to create Message");
Message::from_digest_slice(&sig_hash_bytes[..]).expect("Unable to create Message");
let secp = Secp256k1::new();
let signature = secp.sign_ecdsa_recoverable(&message, signer);
let public_key = PublicKey::from_secret_key(&secp, signer);
let sig_der = signature.to_standard().serialize_der();
(sig_der, public_key)
};

let sig_slice = [&*sig_der, &[sig_hash_all as u8][..]].concat();

transaction.input[i].script_sig = Builder::new()
.push_slice(&[&*sig_der, &[sig_hash_all as u8][..]].concat())
.push_slice(&public_key.serialize())
.push_slice(<&PushBytes>::try_from(sig_slice.as_slice()).unwrap())
.push_slice(public_key.serialize())
.into_script();
}
}
Expand All @@ -134,10 +140,15 @@ pub fn send_transaction_spec(
// In this v1, we're assuming that the bitcoin node is indexing sender's UTXOs.
let sender_address =
Address::from_str(&tx_spec.expected_sender).expect("Unable to parse address");
let addresses = vec![&sender_address];

let mut utxos = bitcoin_wallet_rpc
.list_unspent(None, None, Some(&addresses), None, None)
.list_unspent(
None,
None,
Some(&[sender_address.assume_checked_ref()]),
None,
None,
)
.expect("Unable to retrieve UTXOs");

let (mut transaction, selected_utxos) = build_transaction_spec(tx_spec, &mut utxos);
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ libsecp256k1 = "0.7.0"
toml = { version = "0.5.6", features = ["preserve_order"] }
url = { version = "2.2.2", features = ["serde"] }
tiny-hderive = "0.3.0"
bitcoin = { version = "0.29.2", optional = true }
bitcoin = { version = "0.31.2", optional = true }
lazy_static = { workspace = true}

clarity = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions components/stacks-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ edition = "2021"
atty = "0.2.14"
ansi_term = "0.12.1"
bollard = "0.17"
bitcoin = "0.29.2"
bitcoincore-rpc = "0.16.0"
bitcoincore-rpc = "0.18.0"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1.0.79", features = ["preserve_order"] }
serde_derive = "1"
Expand Down
6 changes: 3 additions & 3 deletions components/stacks-network/src/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2849,7 +2849,7 @@ events_keys = ["*"]

let mut error_count = 0;
loop {
let descriptor = format!("addr({})", miner_address);
let descriptor = format!("addr({})", miner_address.assume_checked_ref());
let rpc_result: JsonValue = base_builder(
&bitcoin_node_url,
&devnet_config.bitcoin_node_username,
Expand Down Expand Up @@ -2926,7 +2926,7 @@ events_keys = ["*"]

let mut error_count = 0;
loop {
let descriptor = format!("addr({})", faucet_address);
let descriptor = format!("addr({})", faucet_address.assume_checked_ref());
let rpc_result: JsonValue = base_builder(
&bitcoin_node_url,
&devnet_config.bitcoin_node_username,
Expand Down Expand Up @@ -3007,7 +3007,7 @@ events_keys = ["*"]

let mut error_count = 0;
loop {
let descriptor = format!("addr({})", address);
let descriptor = format!("addr({})", address.assume_checked_ref());
let rpc_result: JsonValue = base_builder(
&bitcoin_node_url,
&devnet_config.bitcoin_node_username,
Expand Down

0 comments on commit fb70931

Please sign in to comment.