Skip to content

Commit

Permalink
test mixed input scripts v1
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebear21 committed Oct 11, 2024
1 parent d6cc4bf commit f1b0021
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod integration {
#[test]
fn v1_to_v1() -> Result<(), BoxError> {
init_tracing();
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;

// Receiver creates the payjoin URI
let pj_receiver_address = receiver.get_new_address(None, None)?.assume_checked();
Expand Down Expand Up @@ -79,6 +79,48 @@ mod integration {
);
Ok(())
}

#[test]
fn disallow_mixed_input_scripts() -> Result<(), BoxError> {
init_tracing();
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver(
Some(AddressType::Bech32),
Some(AddressType::P2shSegwit),
)?;

// Receiver creates the payjoin URI
let pj_receiver_address = receiver.get_new_address(None, None)?.assume_checked();
let pj_uri = PjUriBuilder::new(pj_receiver_address, EXAMPLE_URL.to_owned())
.amount(Amount::ONE_BTC)
.build();

// **********************
// Inside the Sender:
// Sender create a funded PSBT (not broadcasted) to address with amount given in the pj_uri
let uri = Uri::from_str(&pj_uri.to_string())
.unwrap()
.assume_checked()
.check_pj_supported()
.unwrap();
let psbt = build_original_psbt(&sender, &uri)?;
debug!("Original psbt: {:#?}", psbt);
let (req, ctx) = RequestBuilder::from_psbt_and_uri(psbt, uri)?
.build_with_additional_fee(Amount::from_sat(10000), None, FeeRate::ZERO, false)?
.extract_v1()?;
let headers = HeaderMock::new(&req.body, req.content_type);

// **********************
// Inside the Receiver:
// this data would transit from one party to another over the network in production
let response = handle_v1_pj_request(req, headers, &receiver, None, None, None);
// this response would be returned as http response to the sender

// **********************
// Inside the Sender:
// Sender checks error due to mixed input scripts
assert!(ctx.process_response(&mut response.as_bytes()).is_err());
Ok(())
}
}

#[cfg(feature = "danger-local-https")]
Expand Down Expand Up @@ -165,7 +207,7 @@ mod integration {
directory: Url,
cert_der: Vec<u8>,
) -> Result<(), BoxError> {
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;
let agent = Arc::new(http_agent(cert_der.clone())?);
wait_for_service_ready(ohttp_relay.clone(), agent.clone()).await.unwrap();
wait_for_service_ready(directory.clone(), agent.clone()).await.unwrap();
Expand Down Expand Up @@ -235,7 +277,7 @@ mod integration {
directory: Url,
cert_der: Vec<u8>,
) -> Result<(), BoxError> {
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;
let agent = Arc::new(http_agent(cert_der.clone())?);
wait_for_service_ready(ohttp_relay.clone(), agent.clone()).await.unwrap();
wait_for_service_ready(directory.clone(), agent.clone()).await.unwrap();
Expand Down Expand Up @@ -340,7 +382,7 @@ mod integration {
#[test]
fn v2_to_v1() -> Result<(), BoxError> {
init_tracing();
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;
// Receiver creates the payjoin URI
let pj_receiver_address = receiver.get_new_address(None, None)?.assume_checked();
let pj_uri = PjUriBuilder::new(pj_receiver_address, EXAMPLE_URL.to_owned(), None, None)
Expand Down Expand Up @@ -408,7 +450,7 @@ mod integration {
directory: Url,
cert_der: Vec<u8>,
) -> Result<(), BoxError> {
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (_bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;
let agent: Arc<Client> = Arc::new(http_agent(cert_der.clone())?);
wait_for_service_ready(ohttp_relay.clone(), agent.clone()).await?;
wait_for_service_ready(directory.clone(), agent.clone()).await?;
Expand Down Expand Up @@ -745,7 +787,7 @@ mod integration {
#[test]
fn receiver_consolidates_utxos() -> Result<(), BoxError> {
init_tracing();
let (bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;
// Generate more UTXOs for the receiver
let receiver_address =
receiver.get_new_address(None, Some(AddressType::Bech32))?.assume_checked();
Expand Down Expand Up @@ -833,7 +875,7 @@ mod integration {
#[test]
fn receiver_forwards_payment() -> Result<(), BoxError> {
init_tracing();
let (bitcoind, sender, receiver) = init_bitcoind_sender_receiver()?;
let (bitcoind, sender, receiver) = init_bitcoind_sender_receiver(None, None)?;
let third_party = bitcoind.create_wallet("third-party")?;

// Receiver creates the payjoin URI
Expand Down Expand Up @@ -929,6 +971,8 @@ mod integration {
}

fn init_bitcoind_sender_receiver(
sender_address_type: Option<AddressType>,
receiver_address_type: Option<AddressType>,
) -> Result<(bitcoind::BitcoinD, bitcoincore_rpc::Client, bitcoincore_rpc::Client), BoxError>
{
let bitcoind_exe =
Expand All @@ -938,10 +982,9 @@ mod integration {
let bitcoind = bitcoind::BitcoinD::with_conf(bitcoind_exe, &conf)?;
let receiver = bitcoind.create_wallet("receiver")?;
let receiver_address =
receiver.get_new_address(None, Some(AddressType::Bech32))?.assume_checked();
receiver.get_new_address(None, receiver_address_type)?.assume_checked();
let sender = bitcoind.create_wallet("sender")?;
let sender_address =
sender.get_new_address(None, Some(AddressType::Bech32))?.assume_checked();
let sender_address = sender.get_new_address(None, sender_address_type)?.assume_checked();
bitcoind.client.generate_to_address(1, &receiver_address)?;
bitcoind.client.generate_to_address(101, &sender_address)?;

Expand Down

0 comments on commit f1b0021

Please sign in to comment.