agentcash
Version:
Generic MCP server for calling x402-protected APIs with automatic payment handling
1,014 lines (992 loc) • 33 kB
JavaScript
import {
assertTempoUsdc,
createSessionChannelStore,
createTempoClient,
mppErr,
mppOk,
mppResultFromPromise,
safeCreateMppCredential,
safeGetMppChallenge,
safeGetMppReceipt,
sessionRequestFailedErr
} from "./chunk-H2JGMFEG.js";
import {
DEFAULT_MAX_AMOUNT,
getSettings
} from "./chunk-4TIYIVZF.js";
import {
buildRequest
} from "./chunk-IKPLMFAK.js";
import {
getBalance
} from "./chunk-EM6OE6WL.js";
import {
fetchErr,
fetchOk,
safeFetch,
safeReadResponseText
} from "./chunk-RAS5DZPQ.js";
import {
caip2ToNetwork,
networkToCaip2
} from "./chunk-HTGTKVXB.js";
import {
formatUsd,
getDepositLink,
tokenStringToNumber
} from "./chunk-U6FRXL3X.js";
import {
log
} from "./chunk-QZCSZB7E.js";
import {
BLACKLISTED_ORIGINS
} from "./chunk-YPFBBTA2.js";
import {
err,
ok,
resultFromPromise,
resultFromThrowable
} from "./chunk-YWNBUUBR.js";
// src/operations/fetch/auth.ts
import { encodeSIWxHeader } from "@x402/extensions/sign-in-with-x";
// src/shared/protocols/x402/index.ts
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { toClientEvmSigner } from "@x402/evm";
import { toClientSvmSigner } from "@x402/svm";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { UptoEvmScheme } from "@x402/evm/upto/client";
// src/shared/protocols/x402/builder-code.ts
import {
BUILDER_CODE_PATTERN,
BuilderCodeClientExtension
} from "@x402/extensions/builder-code";
var AGENTCASH_BUILDER_CODE = "agentcash";
var createBuilderCodeExtension = () => {
const extra = process.env.AGENTCASH_BUILDER_CODE_EXTRA?.trim();
const codes = extra && BUILDER_CODE_PATTERN.test(extra) ? [AGENTCASH_BUILDER_CODE, extra] : AGENTCASH_BUILDER_CODE;
return new BuilderCodeClientExtension(codes);
};
// src/shared/protocols/x402/patched-exact-svm-scheme.ts
import {
getSetComputeUnitLimitInstruction,
setTransactionMessageComputeUnitPrice
} from "@solana-program/compute-budget";
import { TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import {
fetchMint,
findAssociatedTokenPda,
getTransferCheckedInstruction,
TOKEN_2022_PROGRAM_ADDRESS
} from "@solana-program/token-2022";
import {
appendTransactionMessageInstructions,
createTransactionMessage,
getBase64EncodedWireTransaction,
partiallySignTransactionMessageWithSigners,
pipe,
prependTransactionMessageInstruction,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash
} from "@solana/kit";
import {
DEFAULT_COMPUTE_UNIT_LIMIT,
DEFAULT_COMPUTE_UNIT_PRICE_MICROLAMPORTS,
createRpcClient
} from "@x402/svm";
function parseLastValidBlockHeight(value) {
if (typeof value === "bigint") {
return value;
}
if (typeof value === "number" && Number.isFinite(value)) {
return BigInt(value);
}
if (typeof value === "string" && value.length > 0) {
return BigInt(value);
}
return void 0;
}
function asBlockhash(blockhash) {
return blockhash;
}
var PatchedExactSvmScheme = class {
constructor(signer, config) {
this.signer = signer;
this.config = config;
}
scheme = "exact";
async createPaymentPayload(x402Version, paymentRequirements) {
const rpc = createRpcClient(
paymentRequirements.network,
this.config?.rpcUrl
);
const tokenMint = await fetchMint(
rpc,
paymentRequirements.asset
);
const tokenProgramAddress = tokenMint.programAddress;
if (tokenProgramAddress.toString() !== TOKEN_PROGRAM_ADDRESS.toString() && tokenProgramAddress.toString() !== TOKEN_2022_PROGRAM_ADDRESS.toString()) {
throw new Error("Asset was not created by a known token program");
}
const [sourceATA] = await findAssociatedTokenPda({
mint: paymentRequirements.asset,
owner: this.signer.address,
tokenProgram: tokenProgramAddress
});
const [destinationATA] = await findAssociatedTokenPda({
mint: paymentRequirements.asset,
owner: paymentRequirements.payTo,
tokenProgram: tokenProgramAddress
});
const transferIx = getTransferCheckedInstruction(
{
source: sourceATA,
mint: paymentRequirements.asset,
destination: destinationATA,
authority: this.signer,
amount: BigInt(paymentRequirements.amount),
decimals: tokenMint.data.decimals
},
{ programAddress: tokenProgramAddress }
);
const extra = paymentRequirements.extra;
const feePayer = extra.feePayer;
if (!feePayer) {
throw new Error(
"feePayer is required in paymentRequirements.extra for SVM transactions"
);
}
const latestBlockhash = await this.resolveBlockhashLifetime(rpc, extra);
const tx = pipe(
createTransactionMessage({ version: 0 }),
(tx2) => setTransactionMessageComputeUnitPrice(
DEFAULT_COMPUTE_UNIT_PRICE_MICROLAMPORTS,
tx2
),
(tx2) => setTransactionMessageFeePayer(feePayer, tx2),
(tx2) => prependTransactionMessageInstruction(
getSetComputeUnitLimitInstruction({
units: DEFAULT_COMPUTE_UNIT_LIMIT
}),
tx2
),
(tx2) => appendTransactionMessageInstructions([transferIx], tx2),
(tx2) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx2)
);
const signedTransaction = await partiallySignTransactionMessageWithSigners(tx);
const base64EncodedWireTransaction = getBase64EncodedWireTransaction(signedTransaction);
const payload = {
transaction: base64EncodedWireTransaction
};
return {
x402Version,
payload
};
}
async resolveBlockhashLifetime(rpc, extra) {
const providedLastValidBlockHeight = parseLastValidBlockHeight(
extra.lastValidBlockHeight
);
if (typeof extra.recentBlockhash === "string") {
if (providedLastValidBlockHeight !== void 0) {
return {
blockhash: asBlockhash(extra.recentBlockhash),
lastValidBlockHeight: providedLastValidBlockHeight
};
}
const { value } = await rpc.getLatestBlockhash().send();
return {
blockhash: asBlockhash(extra.recentBlockhash),
lastValidBlockHeight: value.lastValidBlockHeight
};
}
if (extra.recentBlockhash && typeof extra.recentBlockhash === "object" && typeof extra.recentBlockhash.blockhash === "string") {
const lastValidBlockHeight = parseLastValidBlockHeight(extra.recentBlockhash.lastValidBlockHeight) ?? providedLastValidBlockHeight;
if (lastValidBlockHeight !== void 0) {
return {
blockhash: asBlockhash(extra.recentBlockhash.blockhash),
lastValidBlockHeight
};
}
const { value } = await rpc.getLatestBlockhash().send();
return {
blockhash: asBlockhash(extra.recentBlockhash.blockhash),
lastValidBlockHeight: value.lastValidBlockHeight
};
}
return (await rpc.getLatestBlockhash().send()).value;
}
};
// src/shared/protocols/x402/policies.ts
var MAX_UPTO_DEADLINE_SECONDS = 7 * 24 * 60 * 60;
var capUptoDeadline = (_x402Version, requirements) => requirements.map(
(r) => r.scheme === "upto" && r.maxTimeoutSeconds > MAX_UPTO_DEADLINE_SECONDS ? { ...r, maxTimeoutSeconds: MAX_UPTO_DEADLINE_SECONDS } : r
);
// src/shared/protocols/x402/index.ts
import { createSIWxPayload } from "@x402/extensions/sign-in-with-x";
import { createPublicClient, http } from "viem";
import { base } from "viem/chains";
var errorType = "x402";
var x402Ok = (value) => ok(value);
var x402Err = (cause, error) => err(errorType, cause, error);
var x402ResultFromPromise = (surface, promise, error) => resultFromPromise(errorType, surface, promise, error);
var x402ResultFromThrowable = (surface, fn, error) => resultFromThrowable(errorType, surface, fn, error);
var x402ProbeClient = new x402HTTPClient(new x402Client());
var safeGetPaymentRequired = (surface, response) => {
const probe = response.clone();
return x402ResultFromPromise(
surface,
probe.json().then(
(json) => x402ProbeClient.getPaymentRequiredResponse(
(name) => response.headers.get(name),
json
),
() => x402ProbeClient.getPaymentRequiredResponse(
(name) => response.headers.get(name)
)
),
(error) => ({
cause: "parse_payment_required",
message: error instanceof Error ? error.message : "Failed to parse payment required"
})
);
};
var getSiwxExtension = (extensions, paymentNetwork) => {
const siwx = extensions?.["sign-in-with-x"];
if (!siwx?.info) {
return void 0;
}
const chain = siwx.supportedChains?.find(
(c) => paymentNetwork ? c.chainId === networkToCaip2(paymentNetwork) : c.chainId.startsWith("eip155:")
);
return {
...siwx.info,
chainId: chain?.chainId ?? "eip155:8453",
type: chain?.type ?? "eip191",
signatureScheme: chain?.signatureScheme
};
};
var safeCreatePaymentPayload = (surface, wallets, paymentRequired, paymentRequirementsSelector) => {
const client = new x402HTTPClient(
x402Client.fromConfig({
schemes: [
{
network: networkToCaip2("base" /* BASE */),
client: new ExactEvmScheme(
toClientEvmSigner(
wallets.evm,
createPublicClient({
chain: base,
transport: http()
})
)
)
},
{
network: networkToCaip2("base" /* BASE */),
client: new UptoEvmScheme(
toClientEvmSigner(
wallets.evm,
createPublicClient({
chain: base,
transport: http()
})
)
)
},
{
network: networkToCaip2("solana" /* SOLANA */),
client: new PatchedExactSvmScheme(toClientSvmSigner(wallets.svm))
}
],
policies: [capUptoDeadline],
paymentRequirementsSelector
}).registerExtension(createBuilderCodeExtension())
);
return x402ResultFromPromise(
surface,
client.createPaymentPayload(paymentRequired),
(error) => ({
cause: "create_payment_payload",
message: error instanceof Error ? error.message : "Failed to create payment payload"
})
);
};
var safeGetPaymentSettlement = (surface, response) => {
return x402ResultFromThrowable(
surface,
() => x402ProbeClient.getPaymentSettleResponse(
(name) => response.headers.get(name)
),
(error) => ({
cause: "get_payment_settlement",
message: error instanceof Error ? error.message : "Failed to get payment settlement"
})
);
};
var safeCreateSIWxPayload = (surface, serverInfo, wallets) => {
return x402ResultFromPromise(
surface,
createSIWxPayload(
serverInfo,
serverInfo.chainId.startsWith("eip155:") ? wallets.evm : wallets.svm
),
(error) => ({
cause: "create_siwx_payload",
message: error instanceof Error ? error.message : "Failed to create SIWX payload"
})
);
};
// src/operations/fetch/auth.ts
async function attemptSiwxAuth(response, authRetryRequest, paymentRetryRequest, options) {
const { surface, wallets } = options;
const { timeout, paymentNetwork } = options.params;
if (!response.headers.has("payment-required")) {
return fetchOk({
response,
paymentRetryRequest
});
}
const paymentRequiredResult = await safeGetPaymentRequired(surface, response);
if (paymentRequiredResult.isErr()) {
return paymentRequiredResult;
}
const siwxExtension = getSiwxExtension(
paymentRequiredResult.value.extensions,
paymentNetwork
);
if (!siwxExtension) {
return fetchOk({
response,
paymentRetryRequest
});
}
const payloadResult = await safeCreateSIWxPayload(
surface,
siwxExtension,
wallets
);
if (payloadResult.isErr()) {
return payloadResult;
}
const siwxHeader = encodeSIWxHeader(payloadResult.value);
authRetryRequest.headers.set("SIGN-IN-WITH-X", siwxHeader);
paymentRetryRequest.headers.set("SIGN-IN-WITH-X", siwxHeader);
return (await safeFetch(surface, authRetryRequest, timeout)).andThen(
(authResponse) => fetchOk({
response: authResponse,
paymentRetryRequest
})
);
}
// src/shared/protocols/detect.ts
function detectPaymentProtocols(response) {
const protocols = [];
const wwwAuth = response.headers.get("WWW-Authenticate");
if (wwwAuth?.startsWith("Payment")) {
protocols.push("mpp" /* MPP */);
}
const paymentRequired = response.headers.get("payment-required");
if (paymentRequired) {
protocols.push("x402" /* X402 */);
}
if (protocols.length === 0) {
protocols.push("x402" /* X402 */);
}
return protocols;
}
// src/shared/protocols/mpp/handle-payment.ts
import { formatUnits as formatUnits2 } from "viem";
// src/shared/protocols/before-payment.ts
var beforePayment = async (props) => {
const { options, balanceInput, amount } = props;
const maxAmount = options.params.maxAmount ?? getSettings().maxAmount ?? DEFAULT_MAX_AMOUNT;
if (amount > maxAmount) {
return err("before_payment", options.surface, {
cause: "amount_exceeds_max_amount",
message: `Endpoint requested $${amount} which exceeds the maximum allowed amount of $${maxAmount}. Pass a higher maxAmount on this call, or use update_settings to raise the default permanently.`
});
}
const balanceResult = await getBalance(
options.surface,
balanceInput,
options.flags
);
if (balanceResult.isErr()) {
return balanceResult;
}
const balance = balanceResult.value.balance;
if (balance < amount) {
return err("before_payment", options.surface, {
cause: "insufficient_balance",
message: insufficientBalanceErrorMessage(props, balance)
});
}
return ok(true);
};
var insufficientBalanceErrorMessage = (props, balance) => {
const { options, balanceInput, amount } = props;
return [
`You are attempting to use an endpoint via ${props.protocol} that costs ${amount} USDC on ${balanceInput.network}.`,
`Your current balance is ${balance} USDC.`,
`You can bridge between accounts or deposit at ${getDepositLink(balanceInput.address, options.flags, balanceInput.network)} to top up your balance.`,
`Before bridging, you can check the users account with npx agentcash@latest accounts or if you are within the MCP, you can use the list_accounts tool.`
].join("\n");
};
// src/shared/protocols/mpp/session/handle-session-payment.ts
import { formatUnits } from "viem";
import { sessionManager } from "mppx/client";
// src/shared/protocols/mpp/session/run-request-session.ts
async function runRequestSession(surface, sm, url, init) {
const fetchResult = await mppResultFromPromise(
surface,
sm.fetch(url, init),
(e) => sessionRequestFailedErr(e)
);
if (fetchResult.isErr()) return fetchResult;
const paymentResponse = fetchResult.value;
if (paymentResponse.status === 402) {
const body = await safeReadResponseText(surface, paymentResponse).unwrapOr(
""
);
const detail = body ? `: ${body}` : "";
return mppErr(surface, {
cause: "mpp_payment_rejected",
message: `MPP session payment was rejected by the server (402)${detail}`
});
}
return mppOk({
response: paymentResponse,
receipt: paymentResponse.receipt
});
}
// src/shared/protocols/mpp/session/run-streaming-session.ts
var MAX_BUFFERED_BYTES = 50 * 1024 * 1024;
async function runStreamingSession(surface, sm, url, init) {
let latestReceipt = null;
const { signal, ...rest } = init;
const sseResult = await sm.sse(url, {
...rest,
...signal ? { signal } : {},
onReceipt: (receipt) => {
latestReceipt = receipt;
}
});
const chunks = [];
let bytes = 0;
for await (const chunk of sseResult) {
bytes += chunk.length;
chunks.push(chunk);
if (bytes > MAX_BUFFERED_BYTES) {
return mppErr(surface, {
cause: "mpp_session_response_too_large",
message: `Streaming response exceeded the ${MAX_BUFFERED_BYTES}-byte buffering cap (got ${bytes} bytes).`
});
}
}
const response = new Response(chunks.join("\n"), {
status: 200,
headers: {
"Content-Type": "text/plain",
"Cache-Control": "private"
}
});
return mppOk({ response, receipt: latestReceipt });
}
// src/shared/protocols/mpp/session/utils.ts
var USDC_DECIMALS = 6;
var DEFAULT_TICK_BUDGET = 10;
function resolveDepositUsd(suggestedDeposit, tickCostUsd, options) {
const maxAmount = options.params.maxAmount ?? getSettings().maxAmount ?? DEFAULT_MAX_AMOUNT;
const desired = suggestedDeposit !== void 0 ? tokenStringToNumber(suggestedDeposit, USDC_DECIMALS) : tickCostUsd * DEFAULT_TICK_BUDGET;
return Math.min(desired, maxAmount);
}
function formatDepositCap(depositUsd) {
if (!Number.isFinite(depositUsd) || depositUsd <= 0) {
return "0";
}
return depositUsd.toFixed(USDC_DECIMALS);
}
async function bufferRequestForReplay(request) {
const init = {
method: request.method,
headers: new Headers(request.headers),
credentials: request.credentials,
redirect: request.redirect,
referrer: request.referrer === "" ? void 0 : request.referrer,
integrity: request.integrity || void 0,
signal: request.signal
};
const hasBody = request.method !== "GET" && request.method !== "HEAD" && request.body !== null;
if (hasBody) {
init.body = await request.arrayBuffer();
}
return init;
}
// src/shared/protocols/mpp/session/handle-session-payment.ts
async function handleMppSessionPayment(challenge, { request, options }) {
const { surface, wallets } = options;
const tickCostAtomic = BigInt(challenge.request.amount);
const tickCostUsd = Number(formatUnits(tickCostAtomic, USDC_DECIMALS));
const tokenAddress = challenge.request.currency;
const tokenCheck = assertTempoUsdc(surface, tokenAddress);
if (tokenCheck.isErr()) return tokenCheck;
const depositUsd = resolveDepositUsd(
challenge.request.suggestedDeposit,
tickCostUsd,
options
);
const beforePaymentResult = await beforePayment({
options,
balanceInput: {
address: wallets.evm.address,
network: "tempo" /* TEMPO */,
tokenAddress
},
amount: depositUsd,
protocol: "mpp" /* MPP */
});
if (beforePaymentResult.isErr()) return beforePaymentResult;
const replayInitResult = await mppResultFromPromise(
surface,
bufferRequestForReplay(request),
(e) => sessionRequestFailedErr(
e,
"Failed to buffer request body for session replay"
)
);
if (replayInitResult.isErr()) return replayInitResult;
const sm = sessionManager({
account: wallets.evm,
client: createTempoClient(),
maxDeposit: formatDepositCap(depositUsd),
decimals: USDC_DECIMALS,
// Persist the channel (with its TIP-1034 descriptor) per wallet so it can be
// reused across requests/restarts and closed later via sessions_close — and
// only ever by the wallet that opened it.
channelStore: createSessionChannelStore(wallets.evm.address)
});
const isStreaming = challenge.meta?.streaming === "true" || options.params.stream === true;
const sessionResult = isStreaming ? await runStreamingSession(
surface,
sm,
request.url,
replayInitResult.value
) : await runRequestSession(surface, sm, request.url, replayInitResult.value);
if (sessionResult.isErr()) return sessionResult;
const { response: finalResponse, receipt } = sessionResult.value;
const price = receipt ? formatUsd(tokenStringToNumber(receipt.spent, USDC_DECIMALS)) : `from ${formatUsd(tickCostUsd)}/tick`;
return mppOk({
response: finalResponse,
paymentInfo: {
protocol: "mpp" /* MPP */,
network: "tempo" /* TEMPO */,
price,
payment: receipt ? { success: true, channelId: receipt.channelId } : null
}
});
}
// src/shared/protocols/mpp/handle-payment.ts
async function handleMppPayment(params) {
const { response, request, options } = params;
const { surface, wallets } = options;
const { timeout } = options.params;
if (request.headers.has("Authorization")) {
return mppErr(surface, {
cause: "mpp_payment_already_attempted",
message: "MPP payment already attempted"
});
}
const challengeResult = safeGetMppChallenge(surface, response);
if (challengeResult.isErr()) {
return challengeResult;
}
const challenge = challengeResult.value;
if (challenge.intent === "session") {
return handleMppSessionPayment(challenge, params);
}
const amount = Number(formatUnits2(BigInt(challenge.request.amount), 6));
const tokenAddress = challenge.request.currency;
const tokenCheck = assertTempoUsdc(surface, tokenAddress);
if (tokenCheck.isErr()) {
return tokenCheck;
}
const beforePaymentResult = await beforePayment({
options,
balanceInput: {
address: wallets.evm.address,
network: "tempo" /* TEMPO */,
tokenAddress
},
amount,
protocol: "mpp" /* MPP */
});
if (beforePaymentResult.isErr()) {
return beforePaymentResult;
}
const credentialResult = await safeCreateMppCredential(
surface,
wallets,
response
);
if (credentialResult.isErr()) {
return credentialResult;
}
const credential = credentialResult.value;
request.headers.set("Authorization", credential);
const paidFetchResult = await safeFetch(surface, request, timeout);
if (paidFetchResult.isErr()) {
return paidFetchResult;
}
const paidResponse = paidFetchResult.value;
if (paidResponse.status === 402) {
const body = await safeReadResponseText(surface, paidResponse).unwrapOr("");
const detail = body ? `: ${body}` : "";
return mppErr(surface, {
cause: "mpp_payment_rejected",
message: `MPP payment was rejected by the server (402)${detail}`
});
}
const receiptResult = safeGetMppReceipt(surface, paidResponse);
return mppOk({
response: paidResponse,
paymentInfo: {
protocol: "mpp" /* MPP */,
network: "tempo" /* TEMPO */,
price: formatUsd(amount),
payment: receiptResult.isOk() ? {
success: true,
transactionHash: receiptResult.value.reference
} : null
}
});
}
// src/shared/protocols/pick.ts
async function pickByBalance(response, options) {
const { surface, wallets, flags } = options;
const x402Balances = await Promise.all([
getBalance(
surface,
{
address: wallets.evm.address,
network: "base" /* BASE */
},
flags
),
getBalance(
surface,
{
address: wallets.svm.address,
network: "solana" /* SOLANA */
},
flags
)
]);
const x402Balance = x402Balances.reduce(
(acc, balance) => acc + (balance.isOk() ? balance.value.balance : 0),
0
);
let mppBalance = 0;
const challengeResult = safeGetMppChallenge(surface, response);
if (challengeResult.isOk()) {
const currency = challengeResult.value.request.currency;
if (currency) {
const tempoResult = await getBalance(
surface,
{
address: wallets.evm.address,
network: "tempo" /* TEMPO */
},
flags
);
if (tempoResult.isOk()) {
mppBalance = tempoResult.value.balance ?? 0;
}
}
}
log.info(`Protocol selection \u2014 x402: $${x402Balance}, mpp: $${mppBalance}`);
return x402Balance >= mppBalance ? "x402" /* X402 */ : "mpp" /* MPP */;
}
// src/shared/protocols/x402/choose-payment-requirement.ts
var preferUpto = (requirements) => {
return requirements.find((pr) => pr.scheme === "upto") ?? requirements[0];
};
var choosePaymentRequirement = async ({
paymentRequirements,
options
}) => {
const { surface, wallets, flags } = options;
const { paymentNetwork } = options.params;
if (paymentNetwork) {
const caip2 = networkToCaip2(paymentNetwork);
return preferUpto(paymentRequirements.filter((pr) => pr.network === caip2));
}
const requirementsWithBalance = await Promise.all(
paymentRequirements.map(async (pr) => {
const network = caip2ToNetwork(pr.network);
if (network === null) {
return {
balance: 0,
requirement: pr
};
}
if (network === "solana" /* SOLANA */) {
const balanceResult = await getBalance(
surface,
{
address: wallets.svm.address,
network
},
flags
);
return {
requirement: pr,
balance: balanceResult.isOk() ? balanceResult.value.balance : 0
};
} else {
const balanceResult = await getBalance(
surface,
{
address: wallets.evm.address,
network
},
flags
);
return {
requirement: pr,
balance: balanceResult.isOk() ? balanceResult.value.balance : 0
};
}
})
);
const bestNetwork = requirementsWithBalance.sort(
(a, b) => b.balance - a.balance
)[0].requirement.network;
return preferUpto(
paymentRequirements.filter((pr) => pr.network === bestNetwork)
);
};
// src/shared/protocols/x402/handle-payment.ts
var isPermit2AllowanceFailureBody = (body) => body.includes("permit2") && body.includes("allowance_required");
var buildPermit2AllowanceErrorMessage = (params) => {
const { walletAddress, scheme } = params;
return [
`Payment failed: this endpoint's \`${scheme}\` scheme settles via the Permit2 contract, but your agentcash wallet (${walletAddress}) has not approved Permit2 to spend USDC.`,
"",
"The clean fix is server-side. The merchant should adopt the EIP-2612 gas-sponsoring extension \u2014 agentcash will then auto-sign an off-chain permit on each call and the facilitator pays the approval gas, with no setup required from the user. See https://docs.x402.org/extensions/eip2612-gas-sponsoring",
"",
`Until the merchant adopts that extension, you can do a one-time \`USDC.approve(0x000000000022D473030F116dDEE9F6B43aC78BA3, MAX_UINT256)\` on Base from this wallet (requires a small amount of ETH for gas, ~$0.01\u20130.10). After that, all future Permit2-based payments from this wallet work without further on-chain transactions.`
].join("\n");
};
async function handleX402Payment({
response,
request,
options
}) {
const { surface, wallets } = options;
const { timeout } = options.params;
const paymentRequiredResult = await safeGetPaymentRequired(surface, response);
if (paymentRequiredResult.isErr()) {
return paymentRequiredResult;
}
const paymentRequired = paymentRequiredResult.value;
if (paymentRequired.x402Version === 1) {
return x402Err(surface, {
cause: "parse_payment_required",
message: "This endpoint uses the x402 v1 format, which is not supported by agentcash. Only x402 v2 servers (with an `accepts` array in the Payment-Required header) are supported."
});
}
if (!paymentRequired.accepts || !Array.isArray(paymentRequired.accepts)) {
return x402Err(surface, {
cause: "parse_payment_required",
message: "This endpoint has a missing or malformed accepts array in the Payment-Required header."
});
}
const accept = await choosePaymentRequirement({
options,
paymentRequirements: paymentRequired.accepts
});
if (accept) {
const amount = tokenStringToNumber(accept.amount);
const typedNetwork = caip2ToNetwork(accept.network);
if (!typedNetwork) {
return x402Err(surface, {
cause: "parse_payment_required",
message: `Invalid network: ${accept.network}`
});
}
const beforePaymentResult = await beforePayment({
options,
balanceInput: typedNetwork === "solana" /* SOLANA */ ? {
address: wallets.svm.address,
network: "solana" /* SOLANA */
} : {
address: wallets.evm.address,
network: typedNetwork
},
amount,
protocol: "x402" /* X402 */
});
if (beforePaymentResult.isErr()) {
return beforePaymentResult;
}
}
const paymentPayloadResult = await safeCreatePaymentPayload(
surface,
wallets,
paymentRequired,
accept ? () => accept : void 0
);
if (paymentPayloadResult.isErr()) {
return paymentPayloadResult;
}
const paymentPayload = paymentPayloadResult.value;
const paymentHeaders = x402ProbeClient.encodePaymentSignatureHeader(paymentPayload);
if (request.headers.has("PAYMENT-SIGNATURE") || request.headers.has("X-PAYMENT")) {
return x402Err(surface, {
cause: "payment_already_attempted",
message: "Payment already attempted"
});
}
for (const [key, value] of Object.entries(paymentHeaders)) {
request.headers.set(key, value);
}
request.headers.set(
"Access-Control-Expose-Headers",
"PAYMENT-RESPONSE,X-PAYMENT-RESPONSE"
);
const paidResult = await safeFetch(surface, request, timeout);
if (paidResult.isErr()) {
return paidResult;
}
const paidResponse = paidResult.value;
const permit2Error = await detectPermit2AllowanceError(
surface,
paidResponse,
{
walletAddress: wallets.evm.address,
scheme: paymentPayload.accepted.scheme
}
);
if (permit2Error) {
return x402Err(surface, permit2Error);
}
const settlementResult = safeGetPaymentSettlement(surface, paidResponse);
const settlement = settlementResult.isOk() ? settlementResult.value : null;
const maxAmount = tokenStringToNumber(paymentPayload.accepted.amount);
const settledAmount = settlement?.amount ? tokenStringToNumber(settlement.amount) : null;
const isUpTo = paymentPayload.accepted.scheme === "upto";
const price = settledAmount != null ? formatUsd(settledAmount) : isUpTo ? `up to ${formatUsd(maxAmount)}` : formatUsd(maxAmount);
return x402Ok({
response: paidResponse,
paymentInfo: {
protocol: "x402" /* X402 */,
network: caip2ToNetwork(paymentPayload.accepted.network),
price,
payment: settlement ? {
success: settlement.success,
transactionHash: settlement.transaction
} : null
}
});
}
async function detectPermit2AllowanceError(surface, response, context) {
if (response.ok) {
return null;
}
const body = await safeReadResponseText(surface, response).unwrapOr("");
if (!isPermit2AllowanceFailureBody(body)) {
return null;
}
return {
cause: "permit2_allowance_required",
message: buildPermit2AllowanceErrorMessage(context)
};
}
// src/operations/fetch/payment.ts
async function executePayment(response, request, options) {
const { paymentProtocol } = options.params;
const params = {
response,
request,
options
};
if (paymentProtocol) {
return handlerMap[paymentProtocol](params);
}
const available = detectPaymentProtocols(response);
const preferred = available.length === 1 ? available[0] : await pickByBalance(response, options);
const fallback = available.length > 1 ? preferred === "mpp" /* MPP */ ? "x402" /* X402 */ : "mpp" /* MPP */ : null;
const fallbackRetryRequest = fallback ? request.clone() : null;
const result = await handlerMap[preferred](params);
if (result.isErr() && fallback && fallbackRetryRequest) {
return handlerMap[fallback]({
...params,
request: fallbackRetryRequest
});
}
return result;
}
var handlerMap = {
["mpp" /* MPP */]: handleMppPayment,
["x402" /* X402 */]: handleX402Payment
};
// src/operations/fetch/index.ts
async function executeFetch(input, options) {
const request = buildRequest(input, options);
const { surface } = options;
const { timeout } = options.params;
const blocked = BLACKLISTED_ORIGINS.find(
(origin) => request.url.startsWith(origin)
);
if (blocked) {
return fetchErr(surface, {
cause: "network",
message: `${blocked} is no longer available. This origin has been deprecated.`
});
}
const authRetryRequest = request.clone();
const paymentRetryRequest = request.clone();
const probeResult = await safeFetch(surface, request, timeout);
if (probeResult.isErr()) {
return fetchErr(surface, probeResult.error);
}
const initialResponse = probeResult.value;
if (initialResponse.status !== 402) {
return fetchOk({
response: initialResponse,
paymentInfo: null
});
}
const authResult = await attemptSiwxAuth(
initialResponse,
authRetryRequest,
paymentRetryRequest,
options
);
if (authResult.isErr()) {
return authResult;
}
const latestResponse = authResult.value.response;
if (latestResponse.status !== 402) {
return fetchOk({
response: latestResponse,
paymentInfo: null
});
}
return executePayment(
latestResponse,
authResult.value.paymentRetryRequest,
options
);
}
function getFetchResultMetadata(response, paymentInfo) {
return {
...paymentInfo ?? {},
headers: Object.fromEntries(response.headers.entries())
};
}
export {
executeFetch,
getFetchResultMetadata
};
//# sourceMappingURL=chunk-YIECO2O4.js.map