#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
pub mod wallet;
use bytes::Buf;
use http::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE};
use prost::{DecodeError, Message};
use thiserror::Error;
#[allow(missing_docs)]
pub mod bip70 {
include!(concat!(env!("OUT_DIR"), "/bip70.rs"));
}
use bip70::Payment;
#[derive(Debug, Error)]
pub enum PreprocessingError {
#[error("missing accept header")]
MissingAcceptHeader,
#[error("invalid content-type")]
MissingContentTypeHeader,
#[error("payment decoding failure: {0}")]
PaymentDecode(DecodeError),
}
pub async fn preprocess_payment<B: Buf>(
headers: HeaderMap,
body: B,
) -> Result<Payment, PreprocessingError> {
let bch_content_type_value = HeaderValue::from_static("application/bitcoincash-payment");
let bch_accept_value = HeaderValue::from_static("application/bitcoincash-paymentack");
if !headers
.get_all(CONTENT_TYPE)
.iter()
.any(|header_val| header_val == bch_content_type_value)
{
return Err(PreprocessingError::MissingContentTypeHeader);
}
if !headers
.get_all(ACCEPT)
.iter()
.any(|header_val| header_val == bch_accept_value)
{
return Err(PreprocessingError::MissingAcceptHeader);
}
let payment = bip70::Payment::decode(body).map_err(PreprocessingError::PaymentDecode)?;
Ok(payment)
}