Seal and open
You rarely seal by hand: PaymentGate and createPq402Fetch do it for every paid call. This page shows the four
functions they use, so you can check what travels and build your own transport.
Sealing uses QoreSeal envelopes in ML-KEM-1024 mode. Each envelope is bound to what it carries:
- a request is bound to the method, the path and the content type, so it cannot be replayed to another route;
- a response is bound to the hash of the request it answers and its content type, so it cannot be moved to another request.
/** * Sealing and opening by hand, without the server or client packages: what * PaymentGate and createPq402Fetch do for every paid call. * * npx tsx seal.ts */import { generateSealKeys, openRequest, openResponse, requestHash, sealRequest, sealResponse, utf8,} from "@pq402/core";
const chainId = "qorechain-diana";const server = generateSealKeys(); // on the testnet: read from the KEM key registryconst client = generateSealKeys();
// The client seals the request body to the server's ML-KEM-1024 public key,// bound to the method, path and content type.const request = { method: "POST", target: "/v1/answer", contentType: "application/json" };const body = utf8(JSON.stringify({ question: "What is 6 x 7?" }));const sealedRequest = sealRequest({ chainId, gatewayKey: server.mlkemPublicKey, ...request, body });console.log(`request: ${body.length} bytes of JSON, ${sealedRequest.length} bytes on the wire`);
// The server opens it with its secret key. The same envelope presented for another path fails.const opened = openRequest({ chainId, keys: server, ...request, envelope: sealedRequest });console.log(`server reads: ${new TextDecoder().decode(opened)}`);try { openRequest({ chainId, keys: server, ...request, target: "/v1/other", envelope: sealedRequest });} catch { console.log("opening it as a request for another path fails");}
// The answer is sealed to the client's key and bound to the request it answers.const hash = requestHash({ method: request.method, target: request.target, body: sealedRequest });const sealedAnswer = sealResponse({ chainId, payerKey: client.mlkemPublicKey, requestHash: hash, contentType: "text/plain", body: utf8("42"),});const answer = openResponse({ chainId, keys: client, requestHash: hash, contentType: "text/plain", envelope: sealedAnswer,});console.log(`client reads: ${new TextDecoder().decode(answer)}`);npx tsx seal.tsrequest: 29 bytes of JSON, 1717 bytes on the wireserver reads: {"question":"What is 6 x 7?"}opening it as a request for another path failsclient reads: 42An envelope is about 1.7 KB larger than its plaintext: most of it is the ML-KEM-1024 ciphertext.
On the wire
Section titled “On the wire”A sealed body travels with Content-Type: application/vnd.pq402.sealed, and its original type in the
PQ402-Content-Type header. Paths, sizes and timing stay visible: only bodies are sealed. See the
wire format.