Skip to content

Commit

Permalink
Merge pull request #30 from LtbLightning/fix-build-issues
Browse files Browse the repository at this point in the history
Fix build issues
  • Loading branch information
BitcoinZavior authored Jul 18, 2024
2 parents 3b549b4 + 462595e commit 610aed9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

7 changes: 5 additions & 2 deletions src/receive/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ impl MaybeInputsOwned {
self.0
.clone()
.check_inputs_not_owned(|input| {
is_owned(&input.to_bytes()).map_err(|e| payjoin::receive::Error::Server(Box::new(e)))
is_owned(&input.to_bytes())
.map_err(|e| payjoin::receive::Error::Server(Box::new(e)))
})
.map_err(|e| e.into())
.map(|e| Arc::new(e.into()))
Expand Down Expand Up @@ -222,7 +223,9 @@ impl MaybeInputsSeen {
self.0
.clone()
.check_no_inputs_seen_before(|outpoint| {
is_known.callback(outpoint.clone().into()).map_err(|e| pdk::Error::Server(Box::new(e)))
is_known
.callback(outpoint.clone().into())
.map_err(|e| pdk::Error::Server(Box::new(e)))
})
.map_err(|e| e.into())
.map(|e| Arc::new(e.into()))
Expand Down
15 changes: 9 additions & 6 deletions src/receive/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl SessionInitializer {
#[cfg(feature = "uniffi")]
pub fn new(
address: String,
expire_after: u64,
expire_after: Option<u64>,
network: Network,
directory: Arc<Url>,
ohttp_keys: Arc<OhttpKeys>,
Expand All @@ -80,13 +80,13 @@ impl SessionInitializer {
(*directory).clone().into(),
(*ohttp_keys).clone().into(),
(*ohttp_relay).clone().into(),
Duration::from_secs(expire_after),
expire_after.map(|e| Duration::from_secs(e)),
)
.into())
}
pub fn new(
address: String,
expire_after: u64,
expire_after: Option<u64>,
network: Network,
directory: Arc<Url>,
ohttp_keys: Arc<OhttpKeys>,
Expand All @@ -99,7 +99,7 @@ impl SessionInitializer {
(*directory).clone().into(),
(*ohttp_keys).clone().into(),
(*ohttp_relay).clone().into(),
Duration::from_secs(expire_after),
expire_after.map(|e| Duration::from_secs(e)),
)
.into())
}
Expand Down Expand Up @@ -333,7 +333,8 @@ impl V2MaybeInputsOwned {
self.0
.clone()
.check_inputs_not_owned(|input| {
is_owned(&input.to_bytes()).map_err(|e| payjoin::receive::Error::Server(Box::new(e)))
is_owned(&input.to_bytes())
.map_err(|e| payjoin::receive::Error::Server(Box::new(e)))
})
.map_err(|e| e.into())
.map(|e| Arc::new(e.into()))
Expand Down Expand Up @@ -382,7 +383,9 @@ impl V2MaybeInputsSeen {
self.0
.clone()
.check_no_inputs_seen_before(|outpoint| {
is_known.callback(outpoint.clone().into()).map_err(|e| pdk::Error::Server(Box::new(e)))
is_known
.callback(outpoint.clone().into())
.map_err(|e| pdk::Error::Server(Box::new(e)))
})
.map(|e| Arc::new(e.into()))
.map_err(|e| e.into())
Expand Down
26 changes: 22 additions & 4 deletions src/uri.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::str::FromStr;
use std::time::{Duration, UNIX_EPOCH};

use payjoin::bitcoin::address::NetworkChecked;
use payjoin::UriExt;
Expand Down Expand Up @@ -35,10 +36,14 @@ impl Uri {

self.0.amount.map(|x| x.to_btc())
}
pub fn check_pj_supported(&self) -> Result<PjUri,PayjoinError > {
match self.0.clone().check_pj_supported(){
pub fn check_pj_supported(&self) -> Result<PjUri, PayjoinError> {
match self.0.clone().check_pj_supported() {
Ok(e) => Ok(e.into()),
Err(_) => Err(PayjoinError::PjNotSupported{message:"Uri doesn't support payjoin".to_string()})
Err(_) => {
Err(PayjoinError::PjNotSupported {
message: "Uri doesn't support payjoin".to_string(),
})
}
}
}
pub fn as_string(&self) -> String {
Expand Down Expand Up @@ -117,13 +122,25 @@ impl From<payjoin::PjUriBuilder> for PjUriBuilder {
#[cfg(not(feature = "uniffi"))]
impl PjUriBuilder {
///Create a new PjUriBuilder with required parameters.
/// Parameters
// address: Represents a bitcoin address.
// origin: Represents either the payjoin endpoint in v1 or the directory in v2.
// ohttp_keys: Optional OHTTP keys for v2 (only available if the "v2" feature is enabled).
// expiry: Optional non-default duration_since epoch expiry for the payjoin session (only available if the "v2" feature is enabled).
pub fn new(
address: String,
pj: Url,
ohttp_keys: Option<OhttpKeys>,
expiry: Option<u64>,
) -> Result<Self, PayjoinError> {
let address = payjoin::bitcoin::Address::from_str(&address)?.assume_checked();
Ok(payjoin::PjUriBuilder::new(address, pj.into(), ohttp_keys.map(|e| e.0)).into())
Ok(payjoin::PjUriBuilder::new(
address,
pj.into(),
ohttp_keys.map(|e| e.0),
expiry.map(|e| UNIX_EPOCH + Duration::from_secs(e)),
)
.into())
}
///Accepts the amount you want to receive in sats and sets it in btc .
pub fn amount(&self, amount: u64) -> Self {
Expand Down Expand Up @@ -168,6 +185,7 @@ mod tests {
address.to_string(),
Url::from_str(pj.to_string()).unwrap(),
None,
None,
)
.unwrap();
let uri = builder
Expand Down
6 changes: 1 addition & 5 deletions tests/bdk_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,6 @@ fn handle_proposal(proposal: UncheckedProposal, receiver: Wallet) -> Arc<Payjoin
.contribute_witness_input(txo_to_contribute, outpoint_to_contribute)
.expect("contribute_witness_input error");

let receiver_substitute_address = receiver.get_address(AddressIndex::New).to_string();
payjoin
.substitute_output_address(receiver_substitute_address)
.expect("substitute_output_address error");
let payjoin_proposal = payjoin
.finalize_proposal(
|e| {
Expand Down Expand Up @@ -355,7 +351,7 @@ mod v1 {
let psbt = build_original_psbt(&sender, &pj_uri)?;
println!("\nOriginal sender psbt: {:#?}", psbt.to_string());

let req_ctx = RequestBuilder::from_psbt_and_uri(psbt.to_string(), Arc::new(pj_uri))?
let req_ctx = RequestBuilder::from_psbt_and_uri(psbt.to_string(), Arc::new(pj_uri.check_pj_supported().unwrap()))?
.build_with_additional_fee(10000, None, 0, false)?
.extract_v1()?;
let headers = Headers::from_vec(req_ctx.request.body.clone());
Expand Down
8 changes: 3 additions & 5 deletions tests/bitcoin_core_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ fn v1_to_v1_full_cycle() -> Result<(), BoxError> {
pj_receiver_address.to_string(),
Url::from_str("https://example.com".to_string())?,
None,
None,
)?
.amount(0.0083285)
.amount(832_850)
.build()
.as_string();
print!("pj_uri {}", pj_uri_string);
Expand Down Expand Up @@ -60,7 +61,7 @@ fn v1_to_v1_full_cycle() -> Result<(), BoxError> {
.psbt;
let psbt_base64 = sender.wallet_process_psbt(&psbt, None, None, None)?.psbt;
eprintln!("Original psbt: {:#?}", psbt_base64);
let req_ctx = RequestBuilder::from_psbt_and_uri(psbt_base64, Arc::new(pj_uri))?
let req_ctx = RequestBuilder::from_psbt_and_uri(psbt_base64, Arc::new(pj_uri.check_pj_supported().unwrap()))?
.build_with_additional_fee(10000, None, 0, false)?
.extract_v1()?;
let req = req_ctx.request;
Expand Down Expand Up @@ -175,9 +176,6 @@ fn handle_pj_proposal(proposal: UncheckedProposal, receiver: Arc<Client>) -> Arc
.contribute_witness_input(txo_to_contribute, outpoint_to_contribute)
.expect("contribute_witness_input error");

let receiver_substitute_address =
receiver.get_new_address(None, None).unwrap().assume_checked();
payjoin.substitute_output_address(receiver_substitute_address.to_string()).unwrap();
let payjoin_proposal = payjoin
.finalize_proposal(
|e| {
Expand Down

0 comments on commit 610aed9

Please sign in to comment.