/** * Base + USDC Session (mpp-base) * * 客户端仅做 EIP-712(transferAuth:EIP-3009 + openAuth:OpenAuthorization + voucher:AgentPaymentChannel);上链由服务端完成。 * 首次 402 建立通道时 payload action 为 `open`:含 openAuth(托管合约)、transferAuth(USDC)、voucher;USDC EIP-712 domain.name 与链上 `name()` 一致(默认 RPC 读 Base/Sepolia,可选 `usdcDomainRpcUrl`)。 */ import { type Address, type Hex } from 'viem'; import { Challenge, Receipt } from 'mppx'; import { Session as TempoSession } from 'mppx/tempo'; export type PaymentReceipt = ReturnType; /** * 从成功响应解析 `Payment-Receipt`(base64url JSON);无头或解析失败时返回 undefined。 * 与 mppx {@link Receipt.fromResponse} 不同:后者在无头时会抛错。 */ export declare function parsePaymentReceipt(response: Response): PaymentReceipt | undefined; /** * Base Session 配置 */ export interface BaseSessionParams { account: { address: Address; signTypedData: (params: unknown) => Promise; signTransaction?: (tx: unknown) => Promise; }; maxDeposit?: string; decimals?: number; chainId?: number; escrowContract?: Address; authorizedSigner?: Address; onChannelUpdate?: (entry: ChannelEntry) => void; /** * 与 mppx `Fetch.from` 一致:首次请求未显式带 `Accept-Payment` 时注入,便于与服务端协商。 * 默认 `tempo/session`;设为空字符串则完全不注入。 */ acceptPayment?: string; /** 默认 `globalThis.fetch` */ fetchImpl?: typeof fetch; /** * 收到 402 并解析 challenge 后、使用默认 open/voucher 凭证前调用;返回非空 string 则作为 Authorization 替代自动签名。 */ onChallenge?: (challenge: Challenge.Challenge, helpers: { createCredential: () => Promise; }) => Promise; /** 2xx 响应且存在合法 `Payment-Receipt` 时触发(与 mppx Receipt 模块一致) */ onPaymentReceipt?: (receipt: PaymentReceipt) => void; /** * 非 Base / Base Sepolia 时,用于 RPC 读取 USDC `name()` 以构造 EIP-712 domain(与 MPP_BASE_RPC_URL 等可对齐)。 */ usdcDomainRpcUrl?: string; } /** * Channel 条目 */ export interface ChannelEntry { channelId: Hex; salt: Hex; cumulativeAmount: bigint; escrowContract: Address; chainId: number; opened: boolean; } /** * EIP-3009(ReceiveWithAuthorization digest):仅传 message + signature(与顶层 voucher 的 `signature` 区分);上链须调 `receiveWithAuthorization` 与之对应。 */ export interface TransferAuthEip712 { message: { from: Address; to: Address; /** 最小单位,十进制字符串 */ value: string; validAfter: string; validBefore: string; nonce: Hex; }; signature: Hex; } /** * 托管合约 EIP-712 OpenAuthorization:与 `openWithAuthorization` 参数一致;auth 字段须与 transferAuth.message 的 validAfter、validBefore、nonce 一致 */ export interface OpenAuthEip712 { message: { payer: Address; payee: Address; token: Address; /** 最小单位,十进制字符串(uint128) */ deposit: string; salt: Hex; authorizedSigner: Address; authValidAfter: string; authValidBefore: string; authNonce: Hex; }; signature: Hex; } /** * Session Credential Payload(与 mppx `tempo/session/Types` 对齐:`voucher` / `close` 无 `type` 字段) */ export type SessionCredentialPayload = { action: 'open'; type: 'signature'; channelId: Hex; cumulativeAmount: string; /** Voucher(累计支付承诺)签名 */ signature: Hex; transaction?: Hex; /** 托管合约 OpenAuthorization 签名(与 transferAuth 配套) */ openAuth: OpenAuthEip712; transferAuth?: TransferAuthEip712; authorizedSigner?: Address; } | { action: 'voucher'; channelId: Hex; cumulativeAmount: string; signature: Hex; } | { action: 'close'; channelId: Hex; cumulativeAmount: string; signature: Hex; }; /** 与 mppx `sessionManager.close()` HTTP 分支返回的 `Payment-Receipt`(session 语义)一致 */ export type SessionCloseReceipt = TempoSession.Types.SessionReceipt; /** * Base Session Manager */ export interface BaseSessionManager { readonly opened: boolean; readonly channelId: Hex | null; readonly cumulative: bigint; /** 与 `resolvedEscrow` / EIP-712 一致 */ readonly chainId: number; readonly escrowContract: Address; fetch(input: RequestInfo | URL, init?: RequestInit): Promise; /** 与 `mppx` `sessionManager().close()` 一致:签 close 凭证并向 `fetch` 用过的 URL POST,解析 `Payment-Receipt` */ close(): Promise; /** * 链上调用托管合约 `requestClose(channelId)`(需已打开 channel)。 * 不清理本地 channel、不调用 HTTP close;与 {@link close} 独立。 */ requestCloseOnChain(params: { rpcUrl: string; }): Promise<{ txHash: Hex; }>; /** * 链上调用托管合约 `withdraw(channelId)`。须在链上 `requestClose` 且经过合约规定的等待期之后调用。 * 可传 `channelId` 覆盖(例如 HTTP {@link close} 已清空本地 channel 后仍凭已知 id 提现)。 */ withdrawOnChain(params: { rpcUrl: string; channelId?: Hex; }): Promise<{ txHash: Hex; }>; } /** * 创建 Base Session */ export declare function baseSession(params: BaseSessionParams): BaseSessionManager; //# sourceMappingURL=index.d.ts.map