x402-chain-eip155 1.5.6

EIP-155 (EVM) chain support for the x402 payment protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use alloy_primitives::{Address, B256, Bytes, TxHash, U256};
use alloy_provider::bindings::IMulticall3;
use alloy_provider::{MULTICALL3_ADDRESS, MulticallItem, Provider};
use alloy_rpc_types_eth::TransactionReceipt;
use alloy_sol_types::{SolCall, SolStruct, eip712_domain};
use x402_types::chain::ChainProviderOps;
use x402_types::proto::{PaymentVerificationError, v2};
use x402_types::scheme::X402SchemeFacilitatorError;

use super::eip2612::{self, Permit2PaymentPayloadExt};

#[cfg(feature = "telemetry")]
use tracing::Instrument;
#[cfg(feature = "telemetry")]
use tracing::instrument;

use crate::chain::erc20::IERC20;
use crate::chain::permit2::{EXACT_PERMIT2_PROXY_ADDRESS, PERMIT2_ADDRESS};
use crate::chain::{Eip155ChainReference, Eip155MetaTransactionProvider, MetaTransaction};
use crate::v1_eip155_exact::{
    Eip155ExactError, StructuredSignature, VALIDATOR_ADDRESS, Validator6492, assert_enough_value,
    assert_time, is_contract_deployed, tx_hash_from_receipt,
};
use crate::v2_eip155_exact::eip3009::assert_requirements_match;
use crate::v2_eip155_exact::types::{
    ISignatureTransfer, Permit2PaymentPayload, Permit2PaymentRequirements,
    PermitWitnessTransferFrom, X402ExactPermit2Proxy, x402ExactPermit2Proxy,
};

#[cfg_attr(feature = "telemetry", instrument(skip_all, err))]
pub async fn verify_permit2_payment<P: Eip155MetaTransactionProvider + ChainProviderOps>(
    provider: &P,
    eip2612_gas_sponsoring: bool,
    payment_payload: &Permit2PaymentPayload,
    payment_requirements: &Permit2PaymentRequirements,
) -> Result<v2::VerifyResponse, Eip155ExactError> {
    // 1. Verify offchain constraints
    assert_offchain_valid(payment_payload, payment_requirements)?;

    // 2. Verify onchain constraints
    let authorization = &payment_payload.payload.permit_2_authorization;
    let payer: Address = authorization.from.into();

    // Check if the client provided EIP-2612 gas-sponsoring extension data
    let eip2612_gas_sponsoring_payload = payment_payload.eip2612_gas_sponsoring();
    if let Some(eip2612_gas_sponsoring_payload) = &eip2612_gas_sponsoring_payload {
        // Reject EIP-2612 gas sponsoring if not enabled in config
        if !eip2612_gas_sponsoring {
            return Err(PaymentVerificationError::eip2612_gas_sponsoring_not_enabled().into());
        }
        eip2612::assert_eip2612_offchain_valid(eip2612_gas_sponsoring_payload, payment_payload)?;
        eip2612::assert_onchain_exact_permit2_with_eip2612(
            provider.inner(),
            provider.chain(),
            payment_payload,
            eip2612_gas_sponsoring_payload,
        )
        .await?;
    } else {
        assert_onchain_exact_permit2(provider.inner(), provider.chain(), payment_payload).await?;
    }

    Ok(v2::VerifyResponse::valid(payer.to_string()))
}

#[cfg_attr(feature = "telemetry", instrument(skip_all, err))]
pub async fn settle_permit2_payment<P, E>(
    provider: &P,
    eip2612_gas_sponsoring: bool,
    payment_payload: &Permit2PaymentPayload,
    payment_requirements: &Permit2PaymentRequirements,
) -> Result<v2::SettleResponse, X402SchemeFacilitatorError>
where
    P: Eip155MetaTransactionProvider<Error = E> + ChainProviderOps,
    Eip155ExactError: From<E>,
{
    // 1. Verify offchain constraints
    assert_offchain_valid(payment_payload, payment_requirements)?;

    // Check if the client provided EIP-2612 gas-sponsoring extension data
    let eip2612_gas_sponsoring_payload = payment_payload.eip2612_gas_sponsoring();

    // 2. Try settle (with or without EIP-2612 permit)
    let tx_hash = if let Some(eip2612_gas_sponsoring_payload) = &eip2612_gas_sponsoring_payload {
        // Reject EIP-2612 gas sponsoring if not enabled in config
        if !eip2612_gas_sponsoring {
            return Err(PaymentVerificationError::eip2612_gas_sponsoring_not_enabled().into());
        }
        eip2612::assert_eip2612_offchain_valid(eip2612_gas_sponsoring_payload, payment_payload)?;
        eip2612::settle_exact_permit2_with_eip2612(
            provider,
            payment_payload,
            eip2612_gas_sponsoring_payload,
        )
        .await?
    } else {
        settle_exact_permit2(provider, payment_payload).await?
    };

    let authorization = &payment_payload.payload.permit_2_authorization;
    let payer = authorization.from;
    let network = &payment_payload.accepted.network;

    Ok(v2::SettleResponse::Success {
        payer: payer.to_string(),
        transaction: tx_hash.to_string(),
        network: network.to_string(),
    })
}

#[cfg_attr(feature = "telemetry", instrument(skip_all, err))]
pub fn assert_offchain_valid(
    payment_payload: &Permit2PaymentPayload,
    payment_requirements: &Permit2PaymentRequirements,
) -> Result<(), PaymentVerificationError> {
    let payload = &payment_payload.payload;
    let accepted = &payment_payload.accepted;
    assert_requirements_match(accepted, payment_requirements)?;

    // Spender must be the x402ExactPermit2Proxy contract address
    let authorization = &payload.permit_2_authorization;
    if authorization.spender.0 != EXACT_PERMIT2_PROXY_ADDRESS {
        return Err(PaymentVerificationError::RecipientMismatch);
    }

    // Correct recipient
    let witness = &authorization.witness;
    if witness.to != accepted.pay_to {
        return Err(PaymentVerificationError::RecipientMismatch);
    }

    // Time validity
    let valid_after = witness.valid_after;
    let valid_before = authorization.deadline;
    assert_time(valid_after, valid_before)?;

    // Sufficient amount
    let amount_required = &accepted.amount;
    assert_enough_value(&authorization.permitted.amount, amount_required)?;

    // Same token
    if authorization.permitted.token != accepted.asset {
        return Err(PaymentVerificationError::AssetMismatch);
    }
    Ok(())
}

/// Generic pre-computed Permit2 settlement data.
///
/// This struct holds the shared preparation work — EIP-712 hash, parsed signature,
/// and the contract call arguments — used by both verify and settle code paths.
///
/// Both type parameters encode how the proxy contract types differ between schemes:
/// - `TPermitTransferFrom`: the `PermitTransferFrom` type from the scheme's `sol!`-generated proxy
/// - `TWitness`: the witness struct from the scheme's `sol!`-generated proxy
///
/// NOTE: `TPermitTransferFrom` types from exact and upto are structurally identical but
/// nominally distinct (separate `sol!` invocations). Unifying them requires merging the
/// `sol!` definitions in the types modules — a separate refactor.
pub struct PreparedPermit2<TPermitTransferFrom, TWitness> {
    pub payer: Address,
    pub eip712_hash: B256,
    pub structured_signature: StructuredSignature,
    pub permit_transfer_from: TPermitTransferFrom,
    pub witness: TWitness,
}

/// Type alias for the exact-scheme prepared data.
pub type PreparedExactPermit2 =
    PreparedPermit2<ISignatureTransfer::PermitTransferFrom, x402ExactPermit2Proxy::Witness>;

impl PreparedExactPermit2 {
    /// Build the shared Permit2 data needed for both verify and settle operations.
    ///
    /// Constructs the EIP-712 domain, `PermitWitnessTransferFrom` struct, computes the
    /// signing hash, and parses the structured signature — eliminating the duplicated
    /// prep block that would otherwise appear in every verify/settle function.
    pub fn try_new(
        chain_reference: &Eip155ChainReference,
        payment_payload: &Permit2PaymentPayload,
    ) -> Result<Self, Eip155ExactError> {
        let authorization = &payment_payload.payload.permit_2_authorization;
        let payer: Address = authorization.from.into();

        let domain = eip712_domain! {
            name: "Permit2",
            chain_id: chain_reference.inner(),
            verifying_contract: PERMIT2_ADDRESS,
        };
        let permit_witness_transfer_from = PermitWitnessTransferFrom {
            permitted: ISignatureTransfer::TokenPermissions {
                token: authorization.permitted.token.into(),
                amount: authorization.permitted.amount,
            },
            spender: EXACT_PERMIT2_PROXY_ADDRESS,
            nonce: authorization.nonce,
            deadline: U256::from(authorization.deadline.as_secs()),
            witness: x402ExactPermit2Proxy::Witness {
                to: authorization.witness.to.into(),
                validAfter: U256::from(authorization.witness.valid_after.as_secs()),
            },
        };
        let eip712_hash = permit_witness_transfer_from.eip712_signing_hash(&domain);
        let structured_signature = StructuredSignature::try_from_bytes(
            payment_payload.payload.signature.clone(),
            payer,
            &eip712_hash,
        )?;
        let permit_transfer_from = ISignatureTransfer::PermitTransferFrom {
            permitted: permit_witness_transfer_from.permitted,
            nonce: permit_witness_transfer_from.nonce,
            deadline: permit_witness_transfer_from.deadline,
        };
        let witness = permit_witness_transfer_from.witness;

        Ok(Self {
            payer,
            eip712_hash,
            structured_signature,
            permit_transfer_from,
            witness,
        })
    }
}

/// Generic Permit2 settlement execution.
///
/// This function handles the common settlement dispatch logic for all Permit2-based
/// settlements. It matches on the signature type (EIP6492 / EOA / EIP1271) and dispatches
/// accordingly:
/// - EIP6492: Checks if the wallet is deployed, sends directly or via Multicall3
/// - EOA: Sends directly
/// - EIP1271: Sends directly
///
/// The `build_call` closure captures everything needed and only receives signature bytes.
pub async fn execute_permit2_settlement<P, E, Inner, BuildCall>(
    provider: &P,
    payer: Address,
    structured_signature: StructuredSignature,
    build_call: BuildCall,
) -> Result<TxHash, Eip155ExactError>
where
    P: Eip155MetaTransactionProvider<Error = E, Inner = Inner> + ChainProviderOps,
    Inner: Provider,
    Eip155ExactError: From<E>,
    BuildCall: FnOnce(Bytes) -> MetaTransaction,
{
    let receipt: TransactionReceipt = match structured_signature {
        StructuredSignature::EIP6492 {
            factory,
            factory_calldata,
            inner,
            original: _,
        } => {
            let is_contract_deployed = is_contract_deployed(provider.inner(), &payer).await?;
            let settle_call = build_call(inner.clone());
            if is_contract_deployed {
                let tx_fut = Eip155MetaTransactionProvider::send_transaction(provider, settle_call);
                #[cfg(feature = "telemetry")]
                let receipt = tx_fut
                    .instrument(tracing::info_span!(
                        "call_permit2_proxy_settle.EIP6492.deployed"
                    ))
                    .await?;
                #[cfg(not(feature = "telemetry"))]
                let receipt = tx_fut.await?;
                receipt
            } else {
                let deployment_call = IMulticall3::Call3 {
                    allowFailure: true,
                    target: factory,
                    callData: factory_calldata,
                };
                let transfer_with_authorization_call = IMulticall3::Call3 {
                    allowFailure: false,
                    target: settle_call.to,
                    callData: settle_call.calldata,
                };
                let aggregate_call = IMulticall3::aggregate3Call {
                    calls: vec![deployment_call, transfer_with_authorization_call],
                };
                let meta_tx =
                    MetaTransaction::new(MULTICALL3_ADDRESS, aggregate_call.abi_encode().into());
                let tx_fut = Eip155MetaTransactionProvider::send_transaction(provider, meta_tx);
                #[cfg(feature = "telemetry")]
                let receipt = tx_fut
                    .instrument(tracing::info_span!(
                        "call_permit2_proxy_settle.EIP6492.counterfactual"
                    ))
                    .await?;
                #[cfg(not(feature = "telemetry"))]
                let receipt = tx_fut.await?;
                receipt
            }
        }
        StructuredSignature::EOA(signature) => {
            let settle_call = build_call(signature.as_ref().as_bytes().into());
            let tx_fut = Eip155MetaTransactionProvider::send_transaction(provider, settle_call);
            #[cfg(feature = "telemetry")]
            let receipt = tx_fut
                .instrument(tracing::info_span!("call_permit2_proxy_settle.EOA"))
                .await?;
            #[cfg(not(feature = "telemetry"))]
            let receipt = tx_fut.await?;
            receipt
        }
        StructuredSignature::EIP1271(signature) => {
            let settle_call = build_call(signature);
            let tx_fut = Eip155MetaTransactionProvider::send_transaction(provider, settle_call);
            #[cfg(feature = "telemetry")]
            let receipt = tx_fut
                .instrument(tracing::info_span!("call_permit2_proxy_settle.EIP1271"))
                .await?;
            #[cfg(not(feature = "telemetry"))]
            let receipt = tx_fut.await?;
            receipt
        }
    };
    tx_hash_from_receipt(&receipt)
}

pub async fn assert_onchain_allowance<P: Provider>(
    token_contract: &IERC20::IERC20Instance<P>,
    payer: Address,
    required_amount: U256,
) -> Result<(), Eip155ExactError> {
    let allowance_call = token_contract.allowance(payer, PERMIT2_ADDRESS);
    let allowance_fut = allowance_call.call().into_future();
    #[cfg(feature = "telemetry")]
    let allowance = allowance_fut
        .instrument(tracing::info_span!(
            "fetch_permit2_allowance",
            token_contract = %token_contract.address(),
            sender = %payer,
            otel.kind = "client"
        ))
        .await?;
    #[cfg(not(feature = "telemetry"))]
    let allowance = allowance_fut.await?;
    if allowance < required_amount {
        Err(PaymentVerificationError::InsufficientAllowance.into())
    } else {
        Ok(())
    }
}

pub async fn assert_onchain_balance<P: Provider>(
    token_contract: &IERC20::IERC20Instance<P>,
    payer: Address,
    required_amount: U256,
) -> Result<(), Eip155ExactError> {
    let balance_call = token_contract.balanceOf(payer);
    let balance_fut = balance_call.call().into_future();
    #[cfg(feature = "telemetry")]
    let balance = balance_fut
        .instrument(tracing::info_span!(
            "fetch_balance",
            token_contract = %token_contract.address(),
            sender = %payer,
            otel.kind = "client"
        ))
        .await?;
    #[cfg(not(feature = "telemetry"))]
    let balance = balance_fut.await?;
    if balance < required_amount {
        return Err(PaymentVerificationError::InsufficientFunds.into());
    }
    Ok(())
}

#[cfg_attr(feature = "telemetry", instrument(skip_all, err))]
pub async fn assert_onchain_exact_permit2<P: Provider>(
    provider: &P,
    chain_reference: &Eip155ChainReference,
    payment_payload: &Permit2PaymentPayload,
) -> Result<(), Eip155ExactError> {
    let authorization = &payment_payload.payload.permit_2_authorization;
    let required_amount = payment_payload.accepted.amount;
    let asset_address = payment_payload.accepted.asset.0;

    let token_contract = IERC20::new(asset_address, provider);

    // Allowance from payer to Permit2 contract is enough
    let onchain_allowance_fut =
        assert_onchain_allowance(&token_contract, authorization.from.0, required_amount);
    // User balance is enough
    let onchain_balance_fut =
        assert_onchain_balance(&token_contract, authorization.from.0, required_amount);
    tokio::try_join!(onchain_allowance_fut, onchain_balance_fut)?;

    // ... and below is a check if we can do the settle

    let PreparedExactPermit2 {
        payer,
        eip712_hash,
        structured_signature,
        permit_transfer_from,
        witness,
    } = PreparedExactPermit2::try_new(chain_reference, payment_payload)?;

    let exact_permit2_proxy = X402ExactPermit2Proxy::new(EXACT_PERMIT2_PROXY_ADDRESS, provider);
    match structured_signature {
        StructuredSignature::EIP6492 {
            factory: _,
            factory_calldata: _,
            inner,
            original,
        } => {
            let validator6492 = Validator6492::new(VALIDATOR_ADDRESS, provider);
            let is_valid_signature_call =
                validator6492.isValidSigWithSideEffects(payer, eip712_hash, original);
            let settle_call =
                exact_permit2_proxy.settle(permit_transfer_from, payer, witness, inner);
            let aggregate3 = provider
                .multicall()
                .add(is_valid_signature_call)
                .add(settle_call);
            let aggregate3_call = aggregate3.aggregate3();
            #[cfg(feature = "telemetry")]
            let (is_valid_signature_result, transfer_result) = aggregate3_call
                .instrument(tracing::info_span!("multi_call_settle_exact_permit2",
                    from = %payer,
                    to = %authorization.witness.to,
                    value = %authorization.permitted.amount,
                    valid_after = %authorization.witness.valid_after,
                    valid_before = %authorization.deadline,
                    nonce = %authorization.nonce,
                    token_contract = %authorization.permitted.token,
                    otel.kind = "client",
                ))
                .await?;
            #[cfg(not(feature = "telemetry"))]
            let (is_valid_signature_result, transfer_result) = aggregate3_call.await?;
            let is_valid_signature_result = is_valid_signature_result
                .map_err(|e| PaymentVerificationError::InvalidSignature(e.to_string()))?;
            if !is_valid_signature_result {
                return Err(PaymentVerificationError::InvalidSignature(
                    "Chain reported signature to be invalid".to_string(),
                )
                .into());
            }
            transfer_result
                .map_err(|e| PaymentVerificationError::TransactionSimulation(e.to_string()))?;
            Ok(())
        }
        StructuredSignature::EOA(signature) => {
            let settle_call = exact_permit2_proxy.settle(
                permit_transfer_from,
                payer,
                witness,
                signature.as_ref().as_bytes().into(),
            );
            let settle_call_fut = settle_call.call().into_future();
            #[cfg(feature = "telemetry")]
            settle_call_fut
                .instrument(tracing::info_span!("call_settle_exact_permit2",
                    from = %payer,
                    to = %authorization.witness.to,
                    value = %authorization.permitted.amount,
                    valid_after = %authorization.witness.valid_after,
                    valid_before = %authorization.deadline,
                    nonce = %authorization.nonce,
                    token_contract = %authorization.permitted.token,
                    otel.kind = "client",
                ))
                .await?;
            #[cfg(not(feature = "telemetry"))]
            settle_call_fut.await?;
            Ok(())
        }
        StructuredSignature::EIP1271(signature) => {
            let settle_call =
                exact_permit2_proxy.settle(permit_transfer_from, payer, witness, signature);
            let settle_call_fut = settle_call.call().into_future();
            #[cfg(feature = "telemetry")]
            settle_call_fut
                .instrument(tracing::info_span!("call_settle_exact_permit2",
                    from = %payer,
                    to = %authorization.witness.to,
                    value = %authorization.permitted.amount,
                    valid_after = %authorization.witness.valid_after,
                    valid_before = %authorization.deadline,
                    nonce = %authorization.nonce,
                    token_contract = %authorization.permitted.token,
                    otel.kind = "client",
                ))
                .await?;
            #[cfg(not(feature = "telemetry"))]
            settle_call_fut.await?;
            Ok(())
        }
    }
}

pub async fn settle_exact_permit2<P, E>(
    provider: &P,
    payment_payload: &Permit2PaymentPayload,
) -> Result<TxHash, Eip155ExactError>
where
    P: Eip155MetaTransactionProvider<Error = E> + ChainProviderOps,
    Eip155ExactError: From<E>,
{
    let PreparedExactPermit2 {
        payer,
        eip712_hash: _,
        structured_signature,
        permit_transfer_from,
        witness,
    } = PreparedExactPermit2::try_new(provider.chain(), payment_payload)?;

    let build_call = move |sig_bytes: Bytes| {
        let inner = provider.inner();
        let exact_permit2_proxy = X402ExactPermit2Proxy::new(EXACT_PERMIT2_PROXY_ADDRESS, inner);
        let call = exact_permit2_proxy.settle(permit_transfer_from, payer, witness, sig_bytes);
        MetaTransaction::new(call.target(), call.calldata().clone())
    };

    execute_permit2_settlement(provider, payer, structured_signature, build_call).await
}