Skip to content
LogoLogo

Core types

Challenge, Credential, and Receipt primitives

These types map directly to HTTP headers—WWW-Authenticate, Authorization, and Payment-Receipt—and can be used independently of the higher-level client and server APIs.

use mpp::{PaymentChallenge, PaymentCredential, Receipt};

Parse a Challenge

Parse a WWW-Authenticate header into a typed PaymentChallenge:

use mpp::parse_www_authenticate;
 
let header = r#"Payment id="abc", realm="api.example.com", method="tempo", intent="charge", request="eyJhbW91bnQiOiIxMDAwIn0""#;
let challenge = parse_www_authenticate(header)?;
 
println!("Method: {}", challenge.method);
println!("Intent: {}", challenge.intent);

On the wire, request and optional opaque are base64url-encoded JSON strings in the WWW-Authenticate header. mpp-rs stores them as Base64UrlJson, so you can decode them when you need structured values.

Decode the base64url-encoded request to a typed struct:

use mpp::ChargeRequest;
 
let request: ChargeRequest = challenge.request.decode()?;
println!("Amount: {}", request.amount);

Create a Credential

Build a PaymentCredential from a Challenge echo and payment proof:

use mpp::{PaymentCredential, PaymentPayload, format_authorization};
 
let credential = PaymentCredential::with_source(
    challenge.to_echo(),
    "did:pkh:eip155:42431:0xa726a1...",
    PaymentPayload::transaction("0xf86c..."),
);
 
let header = format_authorization(&credential)?;
// header = "Payment eyJ..."

challenge.to_echo() preserves the original wire values. In the serialized Credential, challenge.request and challenge.opaque remain base64url strings inside the Authorization payload.

Parse a Receipt

Parse the Payment-Receipt response header:

use mpp::parse_receipt;
 
let receipt = parse_receipt(receipt_header)?;
println!("Status: {:?}", receipt.status);
println!("Reference: {}", receipt.reference);

Serialize a Receipt back to a header value:

use mpp::format_receipt;
 
let header = format_receipt(&receipt)?;

Type reference

TypeDescription
PaymentChallengeServer Challenge parsed from WWW-Authenticate
PaymentCredentialClient Credential for the Authorization header
PaymentPayloadPayment proof—either transaction (signed tx) or hash (tx hash)
ChallengeEchoEchoed Challenge fields in a Credential
ReceiptServer Receipt parsed from Payment-Receipt
ChargeRequestDecoded charge intent request data
SessionRequestDecoded session intent request data
Base64UrlJsonJSON value encoded as base64url

Header functions

FunctionDescription
format_authorizationSerialize a Credential to an Authorization header value
format_receiptSerialize a Receipt to a Payment-Receipt header value
format_www_authenticateSerialize a Challenge to a WWW-Authenticate header value
parse_authorizationParse an Authorization header into a Credential
parse_receiptParse a Payment-Receipt header into a Receipt
parse_www_authenticateParse a WWW-Authenticate header into a Challenge