import type { LocalAccount } from 'viem/accounts' import type * as Auth from '../../../internal/Auth.js' import type * as Db from '../../../db/Db.js' import * as Viem from '../../../internal/Viem.js' import * as Billing from '../../management/Billing.js' import type * as Mpp from '../Mpp.js' /** Resolves the chain declared by a Tempo charge challenge. */ export function chainId(input: Mpp.RelayInput): number | undefined { const request = input.challenge.request const details = record(request['methodDetails']) return number(details?.['chainId']) ?? number(request['chainId']) } /** Whether a Tempo charge credential requests platform fee sponsorship. */ export function requiresFeePayer(input: Mpp.RelayInput) { const request = input.challenge.request return record(request['methodDetails'])?.['feePayer'] === true && request['feePayer'] !== false } /** Refuses credentials outside the chains served by this API deployment. */ export function assertSupportedChain(options: assertSupportedChain.Options) { const { chainId, supportedChainIds } = options if (!supportedChainIds.has(chainId)) throw new UnsupportedChainError(chainId) } export declare namespace assertSupportedChain { /** Chain-selection inputs. */ type Options = { /** Resolved credential chain. */ chainId: number /** Chains served by this API deployment. */ supportedChainIds: ReadonlySet } } /** Authorizes managed fee sponsorship for one broadcast request. */ export async function authorizeFeePayer( options: authorizeFeePayer.Options, ): Promise { const { chainId, db, feePayer, input, principal } = options if (!feePayer || !requiresFeePayer(input)) return feePayer if (principal?.type !== 'api_key') throw new PolicyDeniedError('Fee sponsorship requires an API key.') if (!Viem.isMainnet(chainId)) return feePayer if (principal.environment !== 'production') throw new PolicyDeniedError('Mainnet fee sponsorship requires a production API key.') const status = await Billing.status(db, principal.orgId) if (status !== 'active') throw new PolicyDeniedError( status === 'past_due' ? 'Billing is past due.' : 'Active billing is required for mainnet fee sponsorship.', ) return feePayer } export declare namespace authorizeFeePayer { /** Dependencies used to authorize managed fee sponsorship. */ type Options = { /** Credential chain. */ chainId: number /** Primary database for authoritative billing enforcement. */ db: Db.Db /** Managed fee-payer account, when configured. */ feePayer?: LocalAccount | undefined /** MPP credential being broadcast. */ input: Mpp.RelayInput /** Authenticated request principal. */ principal?: Auth.Principal | undefined } } function record(value: unknown): Record | undefined { return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record) : undefined } function number(value: unknown): number | undefined { return typeof value === 'number' && Number.isSafeInteger(value) ? value : undefined } /** Raised when an MPP credential selects a chain this deployment does not serve. */ export class UnsupportedChainError extends Error { override name = 'Mpp.Policy.UnsupportedChainError' constructor(chainId: number) { super(`Unsupported chain id ${chainId}.`) } } /** Raised when a caller is not eligible for managed fee sponsorship. */ export class PolicyDeniedError extends Error { override name = 'Mpp.Policy.PolicyDeniedError' }