-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supply & parse receiver response errors #120
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ use std::str::FromStr; | |
use bitcoin::address::NetworkChecked; | ||
use bitcoin::psbt::Psbt; | ||
use bitcoin::{FeeRate, Script, ScriptBuf, Sequence, TxOut, Weight}; | ||
pub use error::{CreateRequestError, ValidationError}; | ||
pub use error::{CreateRequestError, ResponseError, ValidationError}; | ||
pub(crate) use error::{InternalCreateRequestError, InternalValidationError}; | ||
#[cfg(feature = "v2")] | ||
use serde::{ | ||
|
@@ -639,7 +639,7 @@ pub struct Request { | |
/// | ||
/// This type is used to process the response. Get it from [`RequestBuilder`](crate::send::RequestBuilder)'s build methods. | ||
/// Then you only need to call [`.process_response()`](crate::send::Context::process_response()) on it to continue BIP78 flow. | ||
#[derive(Debug)] | ||
#[derive(Debug, Clone)] | ||
pub struct ContextV1 { | ||
original_psbt: Psbt, | ||
disable_output_substitution: bool, | ||
|
@@ -712,12 +712,11 @@ impl ContextV1 { | |
pub fn process_response( | ||
self, | ||
response: &mut impl std::io::Read, | ||
) -> Result<Psbt, ValidationError> { | ||
) -> Result<Psbt, ResponseError> { | ||
let mut res_str = String::new(); | ||
response.read_to_string(&mut res_str).map_err(InternalValidationError::Io)?; | ||
let proposal = Psbt::from_str(&res_str).map_err(InternalValidationError::PsbtParse)?; | ||
|
||
// process in non-generic function | ||
let proposal = | ||
Psbt::from_str(&res_str).or_else(|_| Err(ResponseError::from_str(&res_str)))?; | ||
self.process_proposal(proposal).map(Into::into).map_err(Into::into) | ||
} | ||
|
||
|
@@ -1120,18 +1119,15 @@ fn serialize_url( | |
mod test { | ||
const ORIGINAL_PSBT: &str = "cHNidP8BAHMCAAAAAY8nutGgJdyYGXWiBEb45Hoe9lWGbkxh/6bNiOJdCDuDAAAAAAD+////AtyVuAUAAAAAF6kUHehJ8GnSdBUOOv6ujXLrWmsJRDCHgIQeAAAAAAAXqRR3QJbbz0hnQ8IvQ0fptGn+votneofTAAAAAAEBIKgb1wUAAAAAF6kU3k4ekGHKWRNbA1rV5tR5kEVDVNCHAQcXFgAUx4pFclNVgo1WWAdN1SYNX8tphTABCGsCRzBEAiB8Q+A6dep+Rz92vhy26lT0AjZn4PRLi8Bf9qoB/CMk0wIgP/Rj2PWZ3gEjUkTlhDRNAQ0gXwTO7t9n+V14pZ6oljUBIQMVmsAaoNWHVMS02LfTSe0e388LNitPa1UQZyOihY+FFgABABYAFEb2Giu6c4KO5YW0pfw3lGp9jMUUAAA="; | ||
|
||
#[test] | ||
fn official_vectors() { | ||
const PAYJOIN_PROPOSAL: &str = "cHNidP8BAJwCAAAAAo8nutGgJdyYGXWiBEb45Hoe9lWGbkxh/6bNiOJdCDuDAAAAAAD+////jye60aAl3JgZdaIERvjkeh72VYZuTGH/ps2I4l0IO4MBAAAAAP7///8CJpW4BQAAAAAXqRQd6EnwadJ0FQ46/q6NcutaawlEMIcACT0AAAAAABepFHdAltvPSGdDwi9DR+m0af6+i2d6h9MAAAAAAQEgqBvXBQAAAAAXqRTeTh6QYcpZE1sDWtXm1HmQRUNU0IcBBBYAFMeKRXJTVYKNVlgHTdUmDV/LaYUwIgYDFZrAGqDVh1TEtNi300ntHt/PCzYrT2tVEGcjooWPhRYYSFzWUDEAAIABAACAAAAAgAEAAAAAAAAAAAEBIICEHgAAAAAAF6kUyPLL+cphRyyI5GTUazV0hF2R2NWHAQcXFgAUX4BmVeWSTJIEwtUb5TlPS/ntohABCGsCRzBEAiBnu3tA3yWlT0WBClsXXS9j69Bt+waCs9JcjWtNjtv7VgIge2VYAaBeLPDB6HGFlpqOENXMldsJezF9Gs5amvDQRDQBIQJl1jz1tBt8hNx2owTm+4Du4isx0pmdKNMNIjjaMHFfrQABABYAFEb2Giu6c4KO5YW0pfw3lGp9jMUUIgICygvBWB5prpfx61y1HDAwo37kYP3YRJBvAjtunBAur3wYSFzWUDEAAIABAACAAAAAgAEAAAABAAAAAAA="; | ||
|
||
fn create_v1_context() -> super::ContextV1 { | ||
use std::str::FromStr; | ||
|
||
use bitcoin::psbt::Psbt; | ||
use bitcoin::FeeRate; | ||
|
||
use crate::input_type::{InputType, SegWitV0Type}; | ||
use crate::psbt::PsbtExt; | ||
|
||
let proposal = "cHNidP8BAJwCAAAAAo8nutGgJdyYGXWiBEb45Hoe9lWGbkxh/6bNiOJdCDuDAAAAAAD+////jye60aAl3JgZdaIERvjkeh72VYZuTGH/ps2I4l0IO4MBAAAAAP7///8CJpW4BQAAAAAXqRQd6EnwadJ0FQ46/q6NcutaawlEMIcACT0AAAAAABepFHdAltvPSGdDwi9DR+m0af6+i2d6h9MAAAAAAQEgqBvXBQAAAAAXqRTeTh6QYcpZE1sDWtXm1HmQRUNU0IcBBBYAFMeKRXJTVYKNVlgHTdUmDV/LaYUwIgYDFZrAGqDVh1TEtNi300ntHt/PCzYrT2tVEGcjooWPhRYYSFzWUDEAAIABAACAAAAAgAEAAAAAAAAAAAEBIICEHgAAAAAAF6kUyPLL+cphRyyI5GTUazV0hF2R2NWHAQcXFgAUX4BmVeWSTJIEwtUb5TlPS/ntohABCGsCRzBEAiBnu3tA3yWlT0WBClsXXS9j69Bt+waCs9JcjWtNjtv7VgIge2VYAaBeLPDB6HGFlpqOENXMldsJezF9Gs5amvDQRDQBIQJl1jz1tBt8hNx2owTm+4Du4isx0pmdKNMNIjjaMHFfrQABABYAFEb2Giu6c4KO5YW0pfw3lGp9jMUUIgICygvBWB5prpfx61y1HDAwo37kYP3YRJBvAjtunBAur3wYSFzWUDEAAIABAACAAAAAgAEAAAABAAAAAAA="; | ||
|
||
let original_psbt = Psbt::from_str(ORIGINAL_PSBT).unwrap(); | ||
eprintln!("original: {:#?}", original_psbt); | ||
let payee = original_psbt.unsigned_tx.output[1].script_pubkey.clone(); | ||
|
@@ -1145,7 +1141,21 @@ mod test { | |
input_type: InputType::SegWitV0 { ty: SegWitV0Type::Pubkey, nested: true }, | ||
sequence, | ||
}; | ||
let mut proposal = Psbt::from_str(proposal).unwrap(); | ||
ctx | ||
} | ||
|
||
#[test] | ||
fn official_vectors() { | ||
use std::str::FromStr; | ||
|
||
use bitcoin::psbt::Psbt; | ||
|
||
use crate::psbt::PsbtExt; | ||
|
||
let original_psbt = Psbt::from_str(ORIGINAL_PSBT).unwrap(); | ||
eprintln!("original: {:#?}", original_psbt); | ||
let ctx = create_v1_context(); | ||
let mut proposal = Psbt::from_str(PAYJOIN_PROPOSAL).unwrap(); | ||
eprintln!("proposal: {:#?}", proposal); | ||
for output in proposal.outputs_mut() { | ||
output.bip32_derivation.clear(); | ||
|
@@ -1181,4 +1191,28 @@ mod test { | |
let deserialized = serde_json::from_str(&serialized).unwrap(); | ||
assert!(req_ctx == deserialized); | ||
} | ||
|
||
#[test] | ||
fn handle_known_errors() { | ||
let ctx = create_v1_context(); | ||
let known_json_error = serde_json::json!({ | ||
"errorCode": "version-unsupported", | ||
"message": "This version of payjoin is not supported." | ||
}) | ||
.to_string(); | ||
assert_eq!( | ||
ctx.process_response(&mut known_json_error.as_bytes()).unwrap_err().to_string(), | ||
"This version of payjoin is not supported." | ||
); | ||
let ctx = create_v1_context(); | ||
let invalid_json_error = serde_json::json!({ | ||
"err": "random", | ||
"message": "This version of payjoin is not supported." | ||
}) | ||
.to_string(); | ||
assert_eq!( | ||
ctx.process_response(&mut invalid_json_error.as_bytes()).unwrap_err().to_string(), | ||
"The receiver sent an invalid response: couldn't decode as PSBT or JSON" | ||
) | ||
Comment on lines
+1213
to
+1216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might check |
||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this could shortcut return directly to an unrecognized ResponseError insted of
Err(())
. Just a note in case this gets refactored in the future.