x401-mpp lets your mppx route require proof of identity from the agent that pays it.
When an agent requests the route, the server answers 402 with the MPP payment Challenge and an
x401 proof request bound to that Challenge. The user presents
their credential from their Proof Cloud Wallet, the agent pays, and the server verifies the proof
before it settles the payment.
To present the credential on the agent side, use x401-agent.
npm install @proof.com/x401-mpp mppxmppx 0.22.1 or later is required.
Add the payment check to an mppx charge route. request builds the signed OpenID4VP request for
one payment Challenge, with the Challenge id as nonce. verify checks the presentation and returns
the nonce it is bound to.
import * as X401 from "@proof.com/x401-mpp";
import { Mppx, tempo } from "mppx/server";
const proof = X401.Server.paymentCheck({
request: ({ challenge }) => signedRequest({ nonce: challenge.id }),
verify: ({ proofResponse }) => verifyPresentation(proofResponse),
});
const mppx = Mppx.create({ secretKey, methods: [tempo({ recipient })] });
const charge = mppx.charge({
amount: "1",
expires: new Date(Date.now() + 15 * 60_000).toISOString(),
identity: { payment: proof },
});
export async function route(request: Request): Promise<Response> {
const result = await charge(request);
if (result.status === 402) return proof.challenge(result.challenge);
return result.withReceipt(
Response.json({ holder: result.identity?.get(proof) }),
);
}The route answers an unpaid request with 402, WWW-Authenticate: Payment, Identity-Required: x401
and a PROOF-REQUEST. The paid retry carries Authorization: Payment and a PROOF-RESPONSE, and
the route answers 200 with a Payment-Receipt:
{ "holder": { "given_name": "Ada", "family_name": "Lovelace" } }request and verify use your credential library. With
proof-vc-server, bind the request to the payment with
transaction data, and check that the presented credential names the paid Challenge:
import * as X401 from "@proof.com/x401-mpp";
import { DC_API_PROTOCOL, verifier as x401 } from "@proof.com/x401-node";
import {
agentsTrustListUrl,
createClient,
createVerifier,
transactionData,
TX_DATA_TYPE,
} from "@proof.com/proof-vc-server";
const client = createClient({
environment: "sandbox",
clientId,
useSecuredAuthorizationRequest: true,
privateKeyFactory,
});
const verifier = createVerifier({
environment: "sandbox",
clientId,
privateKeyFactory,
});
const proof = X401.Server.paymentCheck({
async request({ challenge }) {
const request = await client.signedDcApiRequest({
scope: "urn:proof:params:scope:verifiable-credentials:basic",
nonce: challenge.id,
expectedOrigins: [agentsTrustListUrl("sandbox")],
transactionData: transactionData.mppPaymentChallenge({
challenge_id: challenge.id,
realm: challenge.realm,
method: challenge.method,
intent: challenge.intent,
amount: "1.00",
currency: "USD",
recipient,
}),
});
return { protocol: DC_API_PROTOCOL.SIGNED, data: { request } };
},
async verify({ challenge, proofResponse }) {
const { credential_result_uri } = x401.decodeResultArtifact(proofResponse);
if (
credential_result_uri === undefined ||
new URL(credential_result_uri).origin !== "https://api.fairfax.proof.com"
) {
return { error: "invalid_response" };
}
const result = await fetch(credential_result_uri).then((r) => r.json());
const presentation = await verifier.verifyVPToken({
encodedVPToken: result.data.vp_token,
aud: clientId,
});
const [credential] = presentation.proof_id_default ?? [];
const nonce = credential?.getNonce();
const paid = credential?.getTransactionData(
TX_DATA_TYPE.MPP_PAYMENT_CHALLENGE,
);
if (
credential === undefined ||
nonce === undefined ||
paid?.challenge_id !== challenge.id
) {
return { error: "invalid_nonce" };
}
return { nonce, value: credential.toJSON() };
},
});verify must accept a presentation once only. Proof's credential result can be read once.
Set the route's expires to the time a user needs to approve the request. A Challenge lasts
5 minutes by default.
On the agent side, add the prover to the mppx client. present receives the request whose nonce is
the Challenge the client pays, and returns where the server reads the result. With x401-agent, the
user approves the request in their Proof Cloud Wallet:
import { awaitPresentation, createPresentation } from "@proof.com/x401-agent";
import * as X401 from "@proof.com/x401-mpp";
import { Mppx, tempo } from "mppx/client";
import { privateKeyToAccount } from "viem/accounts";
const proof = {
environment: "sandbox",
accessToken: process.env.PROOF_ACCESS_TOKEN!,
} as const;
const account = privateKeyToAccount(
process.env.MPPX_PRIVATE_KEY as `0x${string}`,
);
const mppx = Mppx.create({
methods: [tempo({ account })],
identity: [
X401.Client.prover({
scope: ["https://api.example"],
present: async ({ requirement }) =>
awaitPresentation(proof, await createPresentation(proof, requirement)),
}),
],
});
const response = await mppx.fetch("https://api.example/paid");
console.log(response.status);
// => 200The prover presents before the client creates the payment Credential. When the user approves after the Challenge expired, the client throws and pays nothing.
A missing, rejected or wrongly bound proof answers 401 with a PROOF-RESULT, no new Challenge,
and nothing settles. On the agent side, decode the header with x401-node:
import { agent } from "@proof.com/x401-node";
const response = await mppx.fetch("https://api.example/paid");
if (response.status === 401) {
const result = response.headers.get("PROOF-RESULT");
if (result) console.log(agent.decodeErrorObject(result).error);
// => "invalid_nonce"
}X401.Server.paymentCheck(config):
| Option | Type | Description |
|---|---|---|
request |
({ challenge }) => CredentialRequest |
Returns the Digital Credentials API request for one Challenge, with challenge.id as nonce. |
verify |
({ challenge, credential, proofResponse }) => { nonce, value } | { error, error_description? } |
Verifies the PROOF-RESPONSE once. Returns the nonce the presentation is bound to and the value the route reads, or an x401 error code. |
The check returns an mppx payment check with one more function, challenge(response), which adds
the PROOF-REQUEST to a 402 response. result.identity.get(proof) returns the value of verify.
X401.Client.prover(config):
| Option | Type | Description |
|---|---|---|
scope |
string[] |
Origins that may receive a proof. |
present |
({ challenge, requirement }) => { result } | { resultUri, expiresAt? } |
Presents the credential requirement asks for. Returns the wallet's result inline, or the URI where the server reads it. |
The prover throws X401.Client.ProofRequestError before it pays when the 402 carries no request for
the selected Challenge.
| Status | Headers | Meaning |
|---|---|---|
402 |
WWW-Authenticate: Payment, Identity-Required: x401, PROOF-REQUEST |
Pay one of the Challenges and present the request that carries its id. |
200 |
Payment-Receipt |
The proof verified and the payment settled. |
401 |
PROOF-RESULT |
The proof is missing, rejected or bound to another Challenge. Nothing settled. |
- Present credentials from an agent with x401-agent
- Protect a Cloudflare Worker with x401-workers
- Verify presentations with proof-vc-server
- Read the x401 specification
- Read the mppx identity documentation
See CONTRIBUTING.md.