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
| Type | Description |
|---|---|
PaymentChallenge | Server Challenge parsed from WWW-Authenticate |
PaymentCredential | Client Credential for the Authorization header |
PaymentPayload | Payment proof—either transaction (signed tx) or hash (tx hash) |
ChallengeEcho | Echoed Challenge fields in a Credential |
Receipt | Server Receipt parsed from Payment-Receipt |
ChargeRequest | Decoded charge intent request data |
SessionRequest | Decoded session intent request data |
Base64UrlJson | JSON value encoded as base64url |
Header functions
| Function | Description |
|---|---|
format_authorization | Serialize a Credential to an Authorization header value |
format_receipt | Serialize a Receipt to a Payment-Receipt header value |
format_www_authenticate | Serialize a Challenge to a WWW-Authenticate header value |
parse_authorization | Parse an Authorization header into a Credential |
parse_receipt | Parse a Payment-Receipt header into a Receipt |
parse_www_authenticate | Parse a WWW-Authenticate header into a Challenge |