import * as z from 'zod/mini' import * as Schema from '../../internal/Schema.js' /** Stable MPP relay failure codes. */ export const relayErrorCode = { /** Credential or transaction already used. */ alreadyUsed: 'already_used', /** Transaction broadcast or confirmation failed. */ broadcastFailed: 'broadcast_failed', /** Challenge or payment authorization expired. */ expired: 'expired', /** Payload, signature, parties, or payment requirements are invalid. */ invalidPayment: 'invalid_payment', /** Sender lacks funds. */ insufficientFunds: 'insufficient_funds', /** Payment violates relay or account policy. */ policyDenied: 'policy_denied', /** Originator or recipient failed screening. */ screenRejected: 'screen_rejected', /** Transaction simulation failed before broadcast. */ simulationFailed: 'simulation_failed', /** Retry may succeed. */ temporarilyUnavailable: 'temporarily_unavailable', /** Method, intent, network, or asset is unsupported. */ unsupported: 'unsupported', /** Unexpected relay failure. */ unknown: 'unknown', } as const /** Stable MPP relay failure code. */ export type RelayErrorCode = (typeof relayErrorCode)[keyof typeof relayErrorCode] /** MPP relay error response. */ export type RelayError = { /** Stable machine-readable reason the relay rejected the credential. */ code: RelayErrorCode /** Human-readable explanation of the relay result. */ message?: string | undefined } /** MPP challenge accepted with a relay credential payload. */ export type Challenge = { /** Additional MPP challenge metadata, preserved verbatim for validation. */ [key: string]: unknown /** Human-readable payment description. */ description?: string | undefined /** When the payment challenge expires, as an ISO-8601 timestamp. */ expires?: string | undefined /** Payment challenge identifier. */ id: string /** MPP payment intent. */ intent: string /** MPP payment method. */ method: string /** Opaque data bound to the challenge by its issuer. */ opaque?: string | undefined /** MPP realm that issued the challenge. */ realm: string /** Payment request bound to the challenge. */ request: Record } /** MPP challenge and credential payload accepted by the relay. */ export type RelayInput = { /** Payment challenge supplied with the credential. */ challenge: Challenge /** Method-specific MPP credential payload. */ payload: unknown /** Optional payer identifier supplied with the credential. */ source?: string | undefined } /** Broadcast MPP payment receipt. */ export type Receipt = { /** Optional caller-provided payment reference. */ externalId?: string | undefined /** Payment method that settled the credential. */ method: string /** On-chain or payment-system settlement reference. */ reference: string /** When the payment settled, as an ISO-8601 timestamp. */ timestamp: string } /** MPP credential validation response. */ export type VerifyResponse = { success: true } | { error: RelayError; success: false } /** MPP credential broadcast response. */ export type BroadcastResponse = | { receipt: Receipt; success: true } | { error: RelayError; success: false } function errorCode(value: code, description: string) { return z.literal(value).check(z.describe(description), z.meta({ examples: [value] })) } /** MPP relay schemas. */ export namespace schema { /** Stable MPP relay failure codes. */ export const ErrorCode = Schema.describe( z.union([ errorCode(relayErrorCode.unsupported, 'Method, intent, network, or asset is unsupported.'), errorCode(relayErrorCode.policyDenied, 'Payment violates relay or account policy.'), errorCode( relayErrorCode.invalidPayment, 'Payload, signature, parties, or payment requirements are invalid.', ), errorCode(relayErrorCode.insufficientFunds, 'Sender lacks funds.'), errorCode(relayErrorCode.expired, 'Challenge or payment authorization expired.'), errorCode(relayErrorCode.alreadyUsed, 'Credential or transaction already used.'), errorCode(relayErrorCode.screenRejected, 'Originator or recipient failed screening.'), errorCode(relayErrorCode.simulationFailed, 'Transaction simulation failed before broadcast.'), errorCode(relayErrorCode.broadcastFailed, 'Transaction broadcast or confirmation failed.'), errorCode(relayErrorCode.temporarilyUnavailable, 'Retry may succeed.'), errorCode(relayErrorCode.unknown, 'Unexpected relay failure.'), ]), 'A stable machine-readable reason the MPP relay rejected or could not process a payment.', ) /** Error returned by the MPP relay. */ export const RelayError = Schema.describe( z .object({ code: ErrorCode, message: z .optional(z.string()) .check( z.describe('A human-readable explanation of the relay result.'), z.meta({ examples: ['MPP relay is not enabled yet.'] }), ), }) .check( z.meta({ examples: [ { code: relayErrorCode.temporarilyUnavailable, message: 'MPP relay is not enabled yet.', }, ], }), ), 'An error returned by the MPP relay.', ) /** MPP challenge accepted with a relay credential payload. */ export const Challenge = Schema.describe( z.looseObject({ description: z .optional(z.string()) .check( z.describe('A human-readable description of the payment.'), z.meta({ examples: ['Tempo API payment'] }), ), expires: z .optional(z.string()) .check( z.describe('When the payment challenge expires, as an ISO-8601 timestamp.'), z.meta({ examples: ['2026-07-21T21:30:00.000Z'] }), ), id: z .string() .check( z.describe('The payment challenge identifier.'), z.meta({ examples: ['ch_01j3j1k2l3m4n5p6q7r8s9t0u'] }), ), intent: z .string() .check(z.describe('The MPP payment intent.'), z.meta({ examples: ['charge'] })), method: z .string() .check(z.describe('The MPP payment method.'), z.meta({ examples: ['tempo'] })), opaque: z .optional(z.string()) .check( z.describe('Opaque data bound to the challenge by its issuer.'), z.meta({ examples: ['eyJhbGciOiJIUzI1NiJ9'] }), ), realm: z .string() .check( z.describe('The MPP realm that issued the challenge.'), z.meta({ examples: ['merchant.example'] }), ), request: z .record(z.string(), z.unknown()) .check( z.describe('The payment request bound to the challenge.'), z.meta({ examples: [{ amount: '1', currency: 'USD' }] }), ), }), 'An MPP payment challenge.', ) /** Request accepted by MPP relay verification and broadcast routes. */ export const RelayInput = Schema.describe( z.object({ challenge: Challenge, payload: z .custom((value) => value !== undefined) .check( z.describe('The method-specific MPP credential payload.'), z.meta({ examples: [{ signature: `0x${'aa'.repeat(65)}`, type: 'transaction' }] }), ), source: z .optional(z.string()) .check( z.describe('An optional payer identifier supplied with the credential.'), z.meta({ examples: ['did:pkh:eip155:4217:0x1234567890abcdef1234567890abcdef12345678'] }), ), }), 'An MPP payment credential and its challenge.', ) /** Receipt returned after the relay broadcasts a payment. */ export const Receipt = Schema.describe( z.object({ externalId: z .optional(z.string()) .check( z.describe('An optional caller-provided payment reference.'), z.meta({ examples: ['pi_01j3j1k2l3m4n5p6q7r8s9t0u'] }), ), method: z .string() .check( z.describe('The payment method that settled the credential.'), z.meta({ examples: ['tempo'] }), ), reference: z .string() .check( z.describe('The on-chain or payment-system reference for the settlement.'), z.meta({ examples: [`0x${'22'.repeat(32)}`] }), ), timestamp: z .string() .check( z.describe('When the payment settled, as an ISO-8601 timestamp.'), z.meta({ examples: ['2026-07-20T18:30:00.000Z'] }), ), }), 'A receipt for a broadcast MPP payment.', ) /** Read-only MPP credential validation result. */ export const VerifyResponse = Schema.describe( z.union([ z.object({ success: z .literal(true) .check( z.describe('Whether the credential passed relay checks.'), z.meta({ examples: [true] }), ), }), z.object({ error: RelayError.check( z.describe('The reason the credential did not pass relay checks.'), z.meta({ examples: [ { code: relayErrorCode.invalidPayment, message: 'Credential signature is invalid.' }, ], }), ), success: z .literal(false) .check( z.describe('Whether the credential passed relay checks.'), z.meta({ examples: [false] }), ), }), ]), 'The result of read-only MPP credential validation.', ) /** MPP credential broadcast result. */ export const BroadcastResponse = Schema.describe( z.union([ z.object({ receipt: Receipt.check( z.describe('The payment settlement receipt.'), z.meta({ examples: [ { method: 'tempo', reference: `0x${'22'.repeat(32)}`, timestamp: '2026-07-20T18:30:00.000Z', }, ], }), ), success: z .literal(true) .check( z.describe('Whether the relay broadcast the credential.'), z.meta({ examples: [true] }), ), }), z.object({ error: RelayError.check( z.describe('The reason the relay did not broadcast the credential.'), z.meta({ examples: [ { code: relayErrorCode.broadcastFailed, message: 'Transaction broadcast failed.' }, ], }), ), success: z .literal(false) .check( z.describe('Whether the relay broadcast the credential.'), z.meta({ examples: [false] }), ), }), ]), 'The result of broadcasting an MPP credential.', ) }