Skip to content

ZAP wire: decode nested x402 envelopes in a single decoder pass #76

@abhicris

Description

@abhicris

Problem

Today zap_transport.py (PR #21) decodes the outer ZAP envelope and returns the inner payload as opaque bytes. When the inner payload is itself an x402 envelope (the common case for agent payments — Warp/ZAP wraps x402 to traverse a cross-subnet bridge), callers have to:

  1. Decode the ZAP frame
  2. Re-instantiate the x402 decoder on payload
  3. Re-parse the x402 envelope

That's two passes over the same byte range and forces every consumer (AgentEscrow, oracle pushers, bridge receipts) to know both wire formats. We should surface nested x402 envelopes in a typed nestedPayload field with a discriminator tag, decoded in a single pass.

Proposed Approach

  1. Extend the ZAP frame header with a nested_tag byte:
    • 0x00 = no nesting
    • 0x01 = x402 nested
    • 0x02 = Warp 2.0 nested
    • 0x03..0xFF reserved
  2. Update zap_transport.py decode to return a structured tuple (version, scheme, header_digest, payload, nested_tag, nested_payload). When nested_tag != 0x00, nested_payload carries the inner envelope bytes already validated for length-fit.
  3. The decoder does not recursively decode the x402 envelope — callers that need x402 field access call the existing x402 decoder on nested_payload. This keeps the wire-layer scope tight and matches the LP-183 precompile's behavior so on-chain and off-chain code see the same shape.
  4. Round-trip test: build a Warp(x402(PaymentProof)) frame, decode in one pass, verify all three layers cleanly recoverable.

Acceptance Criteria

  • zap_transport.decode() returns the 6-tuple including nested_tag + nested_payload
  • nested_tag == 0x01 round-trips against x402_middleware parser
  • nested_tag == 0x02 reserved field present (decoder accepts; consumer code not required yet)
  • Reserved-tag inputs (0x03..0xFF) revert cleanly with RESERVED_NESTED_TAG
  • No-nesting frames (nested_tag == 0x00) round-trip identical to current behavior
  • Updated test vectors in conformance suite

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions