Skip to content

Commit

Permalink
Bump bitcoin to v0.32.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Aug 3, 2024
1 parent 1d0f7ff commit 6a4b901
Show file tree
Hide file tree
Showing 18 changed files with 441 additions and 459 deletions.
712 changes: 343 additions & 369 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions payjoin-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ v2 = ["payjoin/v2", "payjoin/io"]
[dependencies]
anyhow = "1.0.70"
async-trait = "0.1"
bip21 = "0.3.1"
bitcoincore-rpc = "0.17.0"
bip21 = "0.5.0"
bitcoincore-rpc = "0.19.0"
clap = { version = "~4.0.32", features = ["derive"] }
config = "0.13.3"
env_logger = "0.9.0"
Expand All @@ -44,7 +44,7 @@ tokio = { version = "1.12.0", features = ["full"] }
url = { version = "2.3.1", features = ["serde"] }

[dev-dependencies]
bitcoind = { version = "0.31.1", features = ["0_21_2"] }
bitcoind = { version = "0.36.0", features = ["0_21_2"] }
http = "1"
ohttp-relay = "0.0.8"
once_cell = "1"
Expand Down
5 changes: 4 additions & 1 deletion payjoin-cli/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl AppConfig {

#[cfg(feature = "v2")]
let builder = {
use payjoin::base64::prelude::BASE64_URL_SAFE_NO_PAD;
use payjoin::base64::Engine;
builder
.set_override_option(
"pj_directory",
Expand All @@ -100,7 +102,8 @@ impl AppConfig {
.set_override_option(
"ohttp_keys",
matches.get_one::<String>("ohttp_keys").and_then(|s| {
payjoin::base64::decode_config(s, payjoin::base64::URL_SAFE_NO_PAD)
BASE64_URL_SAFE_NO_PAD
.decode(s)
.map_err(|e| {
log::error!("Failed to decode ohttp_keys: {}", e);
ConfigError::Message(format!("Invalid ohttp_keys: {}", e))
Expand Down
2 changes: 1 addition & 1 deletion payjoin-cli/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn try_contributing_inputs(

// calculate receiver payjoin outputs given receiver payjoin inputs and original_psbt,
let txo_to_contribute = bitcoin::TxOut {
value: selected_utxo.amount.to_sat(),
value: selected_utxo.amount,
script_pubkey: selected_utxo.script_pub_key.clone(),
};
let outpoint_to_contribute =
Expand Down
18 changes: 8 additions & 10 deletions payjoin-cli/src/app/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl AppTrait for App {
.with_context(|| "HTTP request failed")?;
let fallback_tx = Psbt::from_str(&body)
.map_err(|e| anyhow!("Failed to load PSBT from base64: {}", e))?
.extract_tx();
println!("Sent fallback transaction txid: {}", fallback_tx.txid());
.extract_tx()?;
println!("Sent fallback transaction txid: {}", fallback_tx.compute_txid());
println!(
"Sent fallback transaction hex: {:#}",
payjoin::bitcoin::consensus::encode::serialize_hex(&fallback_tx)
Expand Down Expand Up @@ -243,7 +243,10 @@ impl App {
let payjoin_proposal = self.process_v1_proposal(proposal)?;
let psbt = payjoin_proposal.psbt();
let body = psbt.to_string();
println!("Responded with Payjoin proposal {}", psbt.clone().extract_tx().txid());
println!(
"Responded with Payjoin proposal {}",
psbt.clone().extract_tx_unchecked_fee_rate().compute_txid()
);
Ok(Response::new(Body::from(body)))
}

Expand All @@ -254,12 +257,7 @@ impl App {
let _to_broadcast_in_failure_case = proposal.extract_tx_to_schedule_broadcast();

// The network is used for checks later
let network = bitcoind
.get_blockchain_info()
.map_err(|e| Error::Server(e.into()))
.and_then(|info| {
bitcoin::Network::from_core_arg(&info.chain).map_err(|e| Error::Server(e.into()))
})?;
let network = bitcoind.get_blockchain_info().map_err(|e| Error::Server(e.into()))?.chain;

// Receive Check 1: Can Broadcast
let proposal = proposal.check_broadcast_suitability(None, |tx| {
Expand Down Expand Up @@ -334,7 +332,7 @@ impl App {
let payjoin_proposal_psbt = payjoin_proposal.psbt();
println!(
"Responded with Payjoin proposal {}",
payjoin_proposal_psbt.clone().extract_tx().txid()
payjoin_proposal_psbt.clone().extract_tx_unchecked_fee_rate().compute_txid()
);
Ok(payjoin_proposal)
}
Expand Down
10 changes: 2 additions & 8 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl App {
let payjoin_psbt = payjoin_proposal.psbt().clone();
println!(
"Response successful. Watch mempool for successful Payjoin. TXID: {}",
payjoin_psbt.extract_tx().clone().txid()
payjoin_psbt.extract_tx_unchecked_fee_rate().clone().compute_txid()
);
self.db.clear_recv_session()?;
Ok(())
Expand Down Expand Up @@ -280,13 +280,7 @@ impl App {
let _to_broadcast_in_failure_case = proposal.extract_tx_to_schedule_broadcast();

// The network is used for checks later
let network = bitcoind
.get_blockchain_info()
.map_err(|e| Error::Server(e.into()))
.and_then(|info| {
bitcoin::Network::from_core_arg(&info.chain).map_err(|e| Error::Server(e.into()))
})?;

let network = bitcoind.get_blockchain_info().map_err(|e| Error::Server(e.into()))?.chain;
// Receive Check 1: Can Broadcast
let proposal = proposal.check_broadcast_suitability(None, |tx| {
let raw_tx = bitcoin::consensus::encode::serialize_hex(&tx);
Expand Down
2 changes: 1 addition & 1 deletion payjoin-cli/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod e2e {
use std::env;
use std::process::Stdio;

use bitcoind::bitcoincore_rpc::core_rpc_json::AddressType;
use bitcoincore_rpc::json::AddressType;
use bitcoind::bitcoincore_rpc::RpcApi;
use log::{log_enabled, Level};
use payjoin::bitcoin::Amount;
Expand Down
2 changes: 1 addition & 1 deletion payjoin-directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ danger-local-https = ["hyper-rustls", "rustls"]

[dependencies]
anyhow = "1.0.71"
bitcoin = { version = "0.30.0", features = ["base64"] }
bitcoin = { version = "0.32.2", features = ["base64"] }
bhttp = { version = "=0.5.1", features = ["http"] }
futures = "0.3.17"
hyper = { version = "0.14", features = ["full"] }
Expand Down
9 changes: 5 additions & 4 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use bitcoin::{self, base64};
use bitcoin::base64::prelude::BASE64_URL_SAFE_NO_PAD;
use bitcoin::base64::Engine;
use hyper::header::{HeaderValue, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, Server, StatusCode, Uri};
Expand Down Expand Up @@ -102,7 +103,7 @@ fn init_ohttp() -> Result<ohttp::Server> {
// create or read from file
let server_config = ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC))?;
let encoded_config = server_config.encode()?;
let b64_config = base64::encode_config(encoded_config, base64::URL_SAFE_NO_PAD);
let b64_config = BASE64_URL_SAFE_NO_PAD.encode(encoded_config);
info!("ohttp-keys server config base64 UrlSafe: {:?}", b64_config);
Ok(ohttp::Server::new(server_config)?)
}
Expand Down Expand Up @@ -243,8 +244,8 @@ async fn post_session(body: Body) -> Result<Response<Body>, HandlerError> {
hyper::body::to_bytes(body).await.map_err(|e| HandlerError::BadRequest(e.into()))?;
let base64_id =
String::from_utf8(bytes.to_vec()).map_err(|e| HandlerError::BadRequest(e.into()))?;
let pubkey_bytes: Vec<u8> = base64::decode_config(base64_id, base64::URL_SAFE_NO_PAD)
.map_err(|e| HandlerError::BadRequest(e.into()))?;
let pubkey_bytes: Vec<u8> =
BASE64_URL_SAFE_NO_PAD.decode(base64_id).map_err(|e| HandlerError::BadRequest(e.into()))?;
let pubkey = bitcoin::secp256k1::PublicKey::from_slice(&pubkey_bytes)
.map_err(|e| HandlerError::BadRequest(e.into()))?;
tracing::info!("Initialized session with pubkey: {:?}", pubkey);
Expand Down
6 changes: 3 additions & 3 deletions payjoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ io = ["reqwest/rustls-tls"]
danger-local-https = ["io", "reqwest/rustls-tls", "rustls"]

[dependencies]
bitcoin = { version = "0.30.0", features = ["base64"] }
bip21 = "0.3.1"
bitcoin = { version = "0.32.2", features = ["base64"] }
bip21 = "0.5.0"
chacha20poly1305 = { version = "0.10.1", optional = true }
log = { version = "0.4.14"}
http = { version = "1", optional = true }
Expand All @@ -38,7 +38,7 @@ url = "2.2.2"
serde_json = "1.0.108"

[dev-dependencies]
bitcoind = { version = "0.31.1", features = ["0_21_2"] }
bitcoind = { version = "0.36.0", features = ["0_21_2"] }
http = "1"
payjoin-directory = { path = "../payjoin-directory", features = ["danger-local-https"] }
ohttp-relay = "0.0.8"
Expand Down
25 changes: 15 additions & 10 deletions payjoin/src/input_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,12 @@ impl std::error::Error for InputTypeError {}
mod tests {
use bitcoin::psbt::Input as PsbtInput;
use bitcoin::script::PushBytesBuf;
use bitcoin::{PublicKey, ScriptBuf};
use bitcoin::{Amount, PublicKey, ScriptBuf};

use super::*;

static FORTY_TWO: Amount = Amount::from_sat(42);

fn wrap_p2sh_script(script: &Script) -> ScriptBuf {
let bytes: PushBytesBuf = script
.to_bytes()
Expand All @@ -227,21 +229,21 @@ mod tests {

#[test]
fn test_p2pk() {
let input_type = InputType::from_spent_input(&TxOut { script_pubkey: ScriptBuf::new_p2pk(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap()), value: 42, }, &Default::default()).unwrap();
let input_type = InputType::from_spent_input(&TxOut { script_pubkey: ScriptBuf::new_p2pk(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap()), value: FORTY_TWO, }, &Default::default()).unwrap();
assert_eq!(input_type, InputType::P2Pk);
}

#[test]
fn test_p2pkh() {
let input_type = InputType::from_spent_input(&TxOut { script_pubkey: ScriptBuf::new_p2pkh(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap().pubkey_hash()), value: 42, }, &Default::default()).unwrap();
let input_type = InputType::from_spent_input(&TxOut { script_pubkey: ScriptBuf::new_p2pkh(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap().pubkey_hash()), value: FORTY_TWO, }, &Default::default()).unwrap();
assert_eq!(input_type, InputType::P2Pkh);
}

#[test]
fn test_p2sh() {
let script = ScriptBuf::new_op_return(&[42]);
let input_type = InputType::from_spent_input(
&TxOut { script_pubkey: ScriptBuf::new_p2sh(&script.script_hash()), value: 42 },
&TxOut { script_pubkey: ScriptBuf::new_p2sh(&script.script_hash()), value: FORTY_TWO },
&PsbtInput { final_script_sig: Some(script), ..Default::default() },
)
.unwrap();
Expand All @@ -250,15 +252,18 @@ mod tests {

#[test]
fn test_p2wpkh() {
let input_type = InputType::from_spent_input(&TxOut { script_pubkey: ScriptBuf::new_v0_p2wpkh(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap().wpubkey_hash().expect("WTF, the key is uncompressed")), value: 42, }, &Default::default()).unwrap();
let input_type = InputType::from_spent_input(&TxOut { script_pubkey: ScriptBuf::new_p2wpkh(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap().wpubkey_hash().expect("WTF, the key is uncompressed")), value: FORTY_TWO, }, &Default::default()).unwrap();
assert_eq!(input_type, InputType::SegWitV0 { ty: SegWitV0Type::Pubkey, nested: false });
}

#[test]
fn test_p2wsh() {
let script = ScriptBuf::new_op_return(&[42]);
let input_type = InputType::from_spent_input(
&TxOut { script_pubkey: ScriptBuf::new_v0_p2wsh(&script.wscript_hash()), value: 42 },
&TxOut {
script_pubkey: ScriptBuf::new_p2wsh(&script.wscript_hash()),
value: FORTY_TWO,
},
&PsbtInput { final_script_sig: Some(script), ..Default::default() },
)
.unwrap();
Expand All @@ -267,12 +272,12 @@ mod tests {

#[test]
fn test_p2sh_p2wpkh() {
let segwit_script = ScriptBuf::new_v0_p2wpkh(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap().wpubkey_hash().expect("WTF, the key is uncompressed"));
let segwit_script = ScriptBuf::new_p2wpkh(&PublicKey::from_slice(b"\x02\x50\x86\x3A\xD6\x4A\x87\xAE\x8A\x2F\xE8\x3C\x1A\xF1\xA8\x40\x3C\xB5\x3F\x53\xE4\x86\xD8\x51\x1D\xAD\x8A\x04\x88\x7E\x5B\x23\x52").unwrap().wpubkey_hash().expect("WTF, the key is uncompressed"));
let segwit_script_hash = segwit_script.script_hash();
let script_sig = wrap_p2sh_script(&segwit_script);

let input_type = InputType::from_spent_input(
&TxOut { script_pubkey: ScriptBuf::new_p2sh(&segwit_script_hash), value: 42 },
&TxOut { script_pubkey: ScriptBuf::new_p2sh(&segwit_script_hash), value: FORTY_TWO },
&PsbtInput { final_script_sig: Some(script_sig), ..Default::default() },
)
.unwrap();
Expand All @@ -282,12 +287,12 @@ mod tests {
#[test]
fn test_p2sh_p2wsh() {
let script = ScriptBuf::new_op_return(&[42]);
let segwit_script = ScriptBuf::new_v0_p2wsh(&script.wscript_hash());
let segwit_script = ScriptBuf::new_p2wsh(&script.wscript_hash());
let segwit_script_hash = segwit_script.script_hash();
let script_sig = wrap_p2sh_script(&segwit_script);

let input_type = InputType::from_spent_input(
&TxOut { script_pubkey: ScriptBuf::new_p2sh(&segwit_script_hash), value: 42 },
&TxOut { script_pubkey: ScriptBuf::new_p2sh(&segwit_script_hash), value: FORTY_TWO },
&PsbtInput { final_script_sig: Some(script_sig), ..Default::default() },
)
.unwrap();
Expand Down
14 changes: 8 additions & 6 deletions payjoin/src/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) trait PsbtExt: Sized {
fn outputs_mut(&mut self) -> &mut [psbt::Output];
fn xpub_mut(
&mut self,
) -> &mut BTreeMap<bip32::ExtendedPubKey, (bip32::Fingerprint, bip32::DerivationPath)>;
) -> &mut BTreeMap<bip32::Xpub, (bip32::Fingerprint, bip32::DerivationPath)>;
fn proprietary_mut(&mut self) -> &mut BTreeMap<psbt::raw::ProprietaryKey, Vec<u8>>;
fn unknown_mut(&mut self) -> &mut BTreeMap<psbt::raw::Key, Vec<u8>>;
fn input_pairs(&self) -> Box<dyn Iterator<Item = InputPair<'_>> + '_>;
Expand All @@ -47,7 +47,7 @@ impl PsbtExt for Psbt {

fn xpub_mut(
&mut self,
) -> &mut BTreeMap<bip32::ExtendedPubKey, (bip32::Fingerprint, bip32::DerivationPath)> {
) -> &mut BTreeMap<bip32::Xpub, (bip32::Fingerprint, bip32::DerivationPath)> {
&mut self.xpub
}

Expand Down Expand Up @@ -95,11 +95,11 @@ impl PsbtExt for Psbt {
let mut total_inputs = bitcoin::Amount::ZERO;

for output in &self.unsigned_tx.output {
total_outputs += bitcoin::Amount::from_sat(output.value);
total_outputs += output.value;
}

for input in self.input_pairs() {
total_inputs += bitcoin::Amount::from_sat(input.previous_txout().unwrap().value);
total_inputs += input.previous_txout().unwrap().value;
}
log::debug!(" total_inputs: {}", total_inputs);
log::debug!("- total_outputs: {}", total_outputs);
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<'a> InputPair<'a> {
(None, None) if treat_missing_as_error =>
Err(PsbtInputError::PrevTxOut(PrevTxOutError::MissingUtxoInformation)),
(None, None) => Ok(()),
(Some(tx), None) if tx.txid() == self.txin.previous_output.txid => tx
(Some(tx), None) if tx.compute_txid() == self.txin.previous_output.txid => tx
.output
.get::<usize>(self.txin.previous_output.vout.try_into().map_err(|_| {
PrevTxOutError::IndexOutOfBounds {
Expand All @@ -156,7 +156,9 @@ impl<'a> InputPair<'a> {
.map(drop),
(Some(_), None) => Err(PsbtInputError::UnequalTxid),
(None, Some(_)) => Ok(()),
(Some(tx), Some(witness_txout)) if tx.txid() == self.txin.previous_output.txid => {
(Some(tx), Some(witness_txout))
if tx.compute_txid() == self.txin.previous_output.txid =>
{
let non_witness_txout = tx
.output
.get::<usize>(self.txin.previous_output.vout.try_into().map_err(|_| {
Expand Down
Loading

0 comments on commit 6a4b901

Please sign in to comment.