testutil

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// USDCBaseSepolia is the USDC contract address on Base Sepolia.
	USDCBaseSepolia = "0x036CbD53842c5426634e7929541eC2318f3dCF7e"
)

Variables

This section is empty.

Functions

func AnvilKeyAddress

func AnvilKeyAddress(t *testing.T, hexKey string) common.Address

AnvilKeyAddress returns the Ethereum address for an Anvil private key.

func ClusterHostAddress

func ClusterHostAddress() string

ClusterHostAddress returns the hostname for reaching the host from inside k3d. On macOS: host.docker.internal. On Linux: host.k3d.internal.

func ClusterHostIP

func ClusterHostIP(t *testing.T) string

ClusterHostIP returns an IP address that k3d containers can use to reach the host machine. Used for creating EndpointSlice objects (which require IPs).

On macOS: Docker Desktop VM gateway (192.168.65.254) On Linux: docker0 bridge interface IP (typically 172.17.0.1)

func ParseAnvilKey

func ParseAnvilKey(t *testing.T, hexKey string) *ecdsa.PrivateKey

ParseAnvilKey converts a hex-encoded private key string to an ecdsa.PrivateKey.

func PatchVerifierFacilitator

func PatchVerifierFacilitator(t *testing.T, kubectlBin, kubeconfig, newURL string)

PatchVerifierFacilitator patches the x402-pricing ConfigMap to use the given facilitator URL, restarts the x402-verifier deployment, waits for the new pod to log the updated URL, and registers t.Cleanup to restore the original ConfigMap contents.

kubectlBin is the absolute path to the kubectl binary. kubeconfig is the absolute path to the kubeconfig file. newURL is the facilitator URL to inject (e.g. "http://host.docker.internal:54321").

func SignPaymentHeaderDirect

func SignPaymentHeaderDirect(signerKeyHex, payTo, amount string, chainID int64) string

SignPaymentHeaderDirect is a non-test version of SignRealPaymentHeader. It panics on error instead of calling t.Fatal.

func SignRealPaymentHeader

func SignRealPaymentHeader(t *testing.T, signerKeyHex string, payTo string, amount string, chainID int64) string

SignRealPaymentHeader constructs a real EIP-712 TransferWithAuthorization (ERC-3009) payment header and returns it as a base64-encoded string compatible with the x402 v2 wire format.

The signerKey is the buyer's private key (signs the authorization). payTo is the seller's address (from ServiceOffer payment.payTo). amount is the USDC amount in micro-units (e.g. "1000000" = 1 USDC). chainID is the EVM chain ID (84532 for Base Sepolia).

Critical x402-rs wire format requirements:

  • validAfter/validBefore must be STRINGS (x402-rs UnixTimestamp deserializes from string)
  • value must be a STRING (x402-rs U256 uses decimal_u256 serde)
  • nonce must be a hex-encoded 32-byte value with 0x prefix

func TestPaymentHeader

func TestPaymentHeader(t *testing.T, payTo string) string

TestPaymentHeader constructs a base64-encoded x402 V1 payment header that the mock facilitator will accept.

func USDCMicroUnits

func USDCMicroUnits(usdc float64) *big.Int

USDCMicroUnits converts a USDC amount (e.g. 1.0) to micro-units (1000000).

Types

type AnvilAccount

type AnvilAccount struct {
	Address    string
	PrivateKey string
}

AnvilAccount is one of the 10 deterministic Anvil accounts.

type AnvilFork

type AnvilFork struct {
	Port     int
	RPCURL   string
	Accounts []AnvilAccount
	// contains filtered or unexported fields
}

AnvilFork represents a running Anvil instance forking a live chain.

func StartAnvilFork

func StartAnvilFork(t *testing.T) *AnvilFork

StartAnvilFork starts anvil forking Base Sepolia on a free port. Skips the test if anvil is not installed. Registers t.Cleanup to kill the process.

func StartAnvilForkWithURL

func StartAnvilForkWithURL(t *testing.T, forkURL string) *AnvilFork

StartAnvilForkWithURL forks from a custom RPC URL. Uses BASE_SEPOLIA_RPC_URL env var or falls back to https://sepolia.base.org.

func (*AnvilFork) ClearCode

func (f *AnvilFork) ClearCode(t *testing.T, addr string)

ClearCode removes contract code from an address on Anvil. Required for deterministic Anvil accounts that have proxy contracts on Base Sepolia — USDC's SignatureChecker sees code → tries EIP-1271 instead of ecrecover.

func (*AnvilFork) FundETH

func (f *AnvilFork) FundETH(t *testing.T, addr string, amount *big.Int)

FundETH sets the ETH balance for an address on the Anvil fork using anvil_setBalance.

func (*AnvilFork) GetUSDCBalance

func (f *AnvilFork) GetUSDCBalance(t *testing.T, addr string) *big.Int

GetUSDCBalance returns the USDC balance for an address via eth_call.

func (*AnvilFork) MintUSDC

func (f *AnvilFork) MintUSDC(t *testing.T, to string, amount *big.Int)

MintUSDC sets the USDC balance for the given address on the Anvil fork using anvil_setStorageAt. This writes directly to the ERC-20 balanceOf mapping in the USDC proxy contract.

USDC on Base Sepolia: 0x036CbD53842c5426634e7929541eC2318f3dCF7e Balance mapping slot: 9 (FiatTokenV2 uses slot 9 for balances)

type MockFacilitator

type MockFacilitator struct {
	Server     *httptest.Server
	Port       int
	ClusterURL string // e.g. "http://host.k3d.internal:54321"

	VerifyCalls atomic.Int32
	SettleCalls atomic.Int32
}

MockFacilitator wraps an httptest.Server that speaks the x402 facilitator protocol. Accessible from inside k3d cluster via http://host.k3d.internal:<port> (Linux) or http://host.docker.internal:<port> (macOS).

func StartMockFacilitator

func StartMockFacilitator(t *testing.T) *MockFacilitator

StartMockFacilitator starts a mock facilitator on a free port. Follows the pattern from x402/go/test/mocks/cash/cash.go. Handles: GET /supported, POST /verify, POST /settle. Registers t.Cleanup to stop the server.

type RealFacilitator

type RealFacilitator struct {
	Port       int
	ClusterURL string // e.g. "http://host.docker.internal:4040"
	// contains filtered or unexported fields
}

RealFacilitator wraps a running x402-rs facilitator process. Unlike MockFacilitator, this validates real EIP-712 signatures against an Anvil fork of Base Sepolia.

func StartRealFacilitator

func StartRealFacilitator(t *testing.T, anvil *AnvilFork) *RealFacilitator

StartRealFacilitator discovers/builds the x402-rs facilitator binary, generates a config pointing at the given Anvil fork, starts the facilitator on a free port, and waits for it to become ready.

Binary discovery order:

  1. X402_FACILITATOR_BIN env var (explicit path to binary)
  2. Pre-built binary at $X402_RS_DIR/target/release/x402-facilitator (or the legacy $X402_RS_DIR/target/release/facilitator)
  3. cargo build --release in $X402_RS_DIR (if Cargo.toml exists)
  4. Skip test

Registers t.Cleanup to kill the process and remove temp config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL