Skip to main content
TokenSwap is a product and protocol design in development. The TEE Gateway, usage receipt protocol, and usage-capacity settlement described on this page are not shipped yet.

Overview

TokenSwap is the first service-matching application built with OpenContract. It matches Buyers that need AI inference capacity with Sellers that can provide otherwise-idle API, subscription, bundle, or committed capacity during a defined future time window. The traded object is an AI Usage Ticket, not an API key and not a financial token. A ticket is a non-transferable right to consume a bounded AI inference service under an agreed policy:
  • a provider and model tier;
  • a start and end time window;
  • a maximum spend and/or usage allowance;
  • reserved throughput, rate limits, and concurrency;
  • allowed use and content policy;
  • availability, refund, and penalty rules;
  • evidence, receipt, and logging requirements.
TokenSwap uses OpenContract for commitments, money, disputes, and settlement. Actual model invocation and metering happen off-chain inside a Usage Gateway, ultimately a Trusted Execution Environment (TEE) Gateway.
In TokenSwap, usage tokens are model input, cached-input, and output units. They are unrelated to any future OpenContract protocol or governance token.

Why TokenSwap Uses OpenContract

A conventional API proxy can route requests and collect payment, but it does not create a neutral, auditable agreement between a capacity Buyer and Seller. TokenSwap applies OpenContract’s service-contract primitives to continuous machine-delivered work: TokenSwap is not forced into the ordinary one-time-delivery settlement formula. It is a specialized usage_capacity contract whose payment is calculated from signed usage, reserved availability, and SLA credits.

Participants

Each participant has a defined responsibility and a corresponding trust boundary: facts or decisions that cannot be established by that participant’s claim alone. The Gateway Operator is a distinct protocol role. OpenContract may operate the first Gateway, but the contract must not assume that OpenContract and the Gateway Operator are permanently the same entity. Future tickets may select an OpenContract-operated TEE, a qualified third-party TEE, or another approved gateway implementation.

Separation of Control

TokenSwap separates money and adjudication from secrets and execution:

OpenContract controls

  • ticket creation, matching, and lifecycle state;
  • Buyer escrow, Seller Stake, and Dispute Bond;
  • the committed policy hash and approved TEE measurement;
  • receipt checkpoints and the final evidence commitment;
  • deterministic settlement and escalation to dispute resolution;
  • refunds, payouts, and slashing.

TEE Gateway controls

  • the Seller’s scoped upstream credential;
  • Buyer request authentication and replay protection;
  • request and response plaintext inside the protected execution boundary;
  • time-window, quota, rate-limit, and model-policy enforcement;
  • provider invocation;
  • quota state and receipt sequence;
  • receipt signing and evidence-log commitments.
The OpenContract application and its ordinary backend must never receive the Seller credential or Buyer prompt in plaintext in the TEE design.

AI Usage Ticket

A ticket is a policy-bound capacity contract. Its canonical serialization is hashed, and policyHash is committed through OpenContract before credential provisioning or usage begins.

Core ticket fields

maxCost is required even when a ticket also specifies token allowances. Input, cached-input, output, and multimodal units may have different prices and throughput weights; a token count alone is not a safe financial limit.

Pricing model

The preferred contract separates capacity reservation from actual use:
  • The Reservation Fee compensates the Seller for holding capacity available, even if the Buyer does not use it.
  • The Usage Charge is calculated only from verified receipts.
  • Unused Usage Escrow is refunded.
  • Seller failure reduces the Availability Factor and may trigger SLA credits or stake slashing.
  • Seller Stake should cover credible replacement-cost exposure, not only a fixed percentage of ticket price.

TEE Attestation and Credential Provisioning

The Seller does not encrypt a credential to a permanent platform key. It provisions the credential only after verifying a fresh TEE instance: The Seller-provided credential should be:
  • dedicated to one ticket or a narrowly bounded ticket pool;
  • restricted to agreed models, endpoints, projects, and regions where supported;
  • protected by an upstream hard spend and throughput limit;
  • short-lived or immediately revocable;
  • deleted or revoked when the ticket closes.
TEE protection reduces trust in the Gateway Operator, but upstream limits still bound loss if the TEE, its policy, or provider adapter contains a bug.

Request Lifecycle

Every accepted request is authorized by the Buyer and processed against the committed policy: Rejected and failed requests also produce signed denial receipts. Standard status classes include:
  • outside_window;
  • invalid_signature;
  • nonce_replayed;
  • buyer_rate_limited;
  • quota_exhausted;
  • policy_rejected;
  • provider_rate_limited;
  • provider_error;
  • gateway_unavailable when externally provable.
Without denial receipts, a Seller or Gateway could omit failed attempts and present a misleading availability record.

Usage Receipts

Public usage receipt

The settlement-safe receipt excludes prompt and response plaintext:
Request and response commitments must include ticket-specific context, sequence, canonical content, and a random salt. An unsalted hash of a short or predictable prompt may permit dictionary guessing. The salt remains in the private evidence envelope unless disclosure is required.

Private evidence envelope

Evidence that may expose Buyer content is encrypted separately:
The evidence policy determines who can decrypt this envelope. Possible recipients include the Buyer, the active TEE, and a pre-agreed dispute resolver. OpenContract does not receive routine access to plaintext prompts merely because a dispute exists.

Evidence Storage and Checkpointing

Every receipt must be durable, but every full receipt does not need to be stored in OpenContract or posted on-chain.

Periodic checkpoint

The TEE submits a checkpoint after a configured number of receipts, elapsed time, or uncommitted value:
OpenContract verifies that:
  • the TEE signature and measurement are approved;
  • policyHash matches the ticket;
  • sequence ranges are continuous;
  • the receipt hash chain connects to the previous checkpoint;
  • cumulative usage never decreases;
  • remaining quota never increases except through an explicit contract amendment;
  • the checkpoint is newer than the last accepted state.
maxUncheckpointedExposure limits the value the TEE may consume without a successful checkpoint. When the threshold is reached, new calls pause until the checkpoint is accepted.

Final receipt

At expiry or exhaustion, the TEE revokes or deletes the credential and signs the final state:
The final receipt is the TokenSwap equivalent of a Working Contract delivery. The Buyer may approve it or dispute before the Review Deadline; silence may resolve optimistically according to the ticket policy.

Evidence Chain

The complete cooperation record is:
The hash chain establishes receipt order. Merkle proofs establish inclusion in a committed batch. Neither primitive independently proves that a log is complete; completeness additionally depends on Buyer-held receipts, monotonically increasing sequence numbers, TEE-enforced logging, bounded uncheckpointed exposure, and upstream billing reconciliation.

Crash-safe metering

The TEE persists a call_intent and reserves quota before invoking the provider. It finalizes the receipt only after receiving provider usage metadata. If the TEE crashes after the provider may have charged the Seller but before finalization, recovery exposes an indeterminate call rather than silently dropping it. The provider request ID and billing record are then used for reconciliation. TEE sealed storage protects confidentiality but does not by itself prevent an operator from restoring an old encrypted snapshot. Recovery must reject any quota or sequence state older than the latest OpenContract checkpoint. Upstream hard spending limits remain the final loss bound.

Verification and Disputes

TokenSwap resolves deterministic evidence before recruiting Backup Agents: Backup Agents do not vote on cryptographic facts such as whether a signature is valid or whether a timestamp falls inside a window. They adjudicate only evidence that deterministic verification cannot resolve. TEE attestation proves that approved code issued the invocation and receipt under the committed policy. It does not cryptographically prove which model weights an upstream provider executed internally. Model-integrity claims therefore use combined evidence rather than TEE evidence alone.

Settlement

At ticket close:
  1. The TEE submits the final receipt and evidence root.
  2. OpenContract verifies the TEE signature, measurement, policy hash, checkpoint continuity, and deterministic SLA calculations.
  3. The Buyer approves or enters the optimistic review window.
  4. A dispute posts the configured Dispute Bond and identifies the contested receipt range or SLA claim.
  5. Deterministic checks run first; unresolved claims recruit qualified dispute resolvers.
  6. OpenContract pays verified usage and earned reservation fees, refunds unused escrow, applies SLA credits, returns or slashes Seller Stake, and resolves the Dispute Bond.
TokenSwap may report fully_met, partially_met, or none_met for compatibility with Working Contract outcomes, but the payout amount comes from usage-capacity settlement rules rather than an unweighted count of acceptance criteria. An all-unclear dispute must not default automatically to full Seller payment.

Protection and Verification Matrix

TokenSwap uses several complementary controls. Encryption protects confidentiality, isolation limits access, hashes bind data and state, signatures authenticate actors and receipts, and attestation authenticates the TEE execution environment. No single control proves that the complete system is correct.

Trust Model

Properties TokenSwap targets

  • The Buyer never receives the Seller credential.
  • The Seller does not receive Buyer prompt plaintext from the Gateway.
  • The ordinary OpenContract backend cannot read the credential, prompt, response, or TEE signing key.
  • The Buyer cannot create billable usage without signing a request.
  • The Seller cannot deny Buyer-authorized successful calls that have Buyer-held receipts.
  • The Gateway Operator cannot change code or policy without changing the attested measurement or policy hash.
  • OpenContract cannot create a valid TEE receipt without the attested receipt key.
  • Credential scope, upstream limits, checkpoints, escrow, and stakes bound the loss from failures that cryptography cannot prevent.

Residual trust and failure modes

TEE execution does not prevent:
  • the Gateway Operator from stopping the machine or blocking its network;
  • hardware, firmware, side-channel, or attestation-service failures;
  • bugs in approved Gateway code or provider adapters;
  • the Seller revoking a credential during the service window;
  • the upstream provider misreporting or internally changing a model;
  • policy-compliant but harmful or illegal requests.
These risks are handled with redundancy, monitoring, scoped credentials, provider reconciliation, SLA remedies, stake, dispute resolution, and supplier eligibility rules rather than by claiming the system is fully trustless.

MVP and Evolution

Closed MVP

The first test may use one Buyer and one Seller, one provider adapter, test-value escrow, and an OpenContract-operated Gateway. Its purpose is to validate the ticket lifecycle and evidence semantics:
  • create and match an AI Usage Ticket;
  • commit a policy hash;
  • provision a dedicated, capped, revocable credential;
  • authenticate Buyer requests;
  • enforce window, quota, and rate limits;
  • generate usage and denial receipts;
  • build a receipt hash chain and periodic roots;
  • submit a final receipt;
  • exercise approval, a simulated dispute, and settlement.
The MVP may initially substitute a process-isolated Gateway for a production TEE, but it must preserve the same interfaces for secret storage, attestation, signing, policy evaluation, receipt storage, and checkpointing. Its trust assumptions must be disclosed as gatewayType: platform.

TEE-Ledger

Move receipt signing, quota state, sequence state, and checkpoint construction into an attested environment. Add sealed-state recovery and rollback protection.

TEE Gateway

Move credential provisioning and the complete Buyer-to-provider request path into the TEE. Buyer requests and provider responses remain encrypted outside the attested environment.

Verification TEE and open supply

Add pre-listing and pre-window verification of Seller access, multiple qualified Gateway Operators, provider billing reconciliation, supplier authorization classes, and redundant TEE deployments.

Non-goals

TokenSwap is not:
  • an API-key marketplace;
  • an account- or subscription-sharing product;
  • a transferable claim on future cash flows;
  • a prediction market;
  • a secondary market for an OpenContract protocol token;
  • proof that an upstream provider internally executed particular model weights;
  • a replacement for provider terms, supplier authorization, privacy review, or applicable regulation.
The open-market version should admit only supply whose authorization permits the Seller to provide the contracted downstream service. The closed MVP validates product mechanics, not the legal eligibility of every future supply source.