UserOp Build and Sign: ERC-4337 Account Abstraction API for AI Agents

ERC-4337 Account Abstraction transforms how DeFi applications handle transactions, but building and signing UserOps manually is complex and error-prone. WAIaaS provides a simple REST API that abstracts the entire UserOp lifecycle — from building to signing to submission — letting your AI agents and DeFi applications focus on strategy rather than ERC-4337 implementation details.

Why Account Abstraction Matters for DeFi

Traditional externally owned accounts (EOAs) create friction in DeFi applications. Every transaction requires gas in ETH, users must manage private keys, and batch operations need complex coordination. ERC-4337 Account Abstraction solves these problems by introducing smart contract wallets that can:

However, ERC-4337's complexity has limited adoption. Building UserOps requires understanding the spec's intricate details, managing multiple contracts (EntryPoint, Account Factory, Paymaster), and handling gas estimation across different networks. WAIaaS eliminates this complexity with a unified API that works across all EVM chains.

The WAIaaS UserOp API

WAIaaS provides three endpoints that handle the complete UserOp lifecycle:

1. Build UserOp

The /v1/userop/build endpoint constructs a UserOperation from your transaction intent:

curl -X POST http://127.0.0.1:3100/v1/userop/build \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "transactions": [
      {
        "type": "ContractCall",
        "to": "0xA0b86a33E6441d78abf57Fe4e5dA14A6e7F8b57C",
        "data": "0xa9059cbb000000000000000000000000742b35Cc6Cf16CCBb0c5db86c5E5D12b06d67f2E0000000000000000000000000000000000000000000000000de0b6b3a7640000"
      }
    ],
    "gasOptions": {
      "maxFeePerGas": "20000000000",
      "maxPriorityFeePerGas": "1000000000"
    }
  }'

The API automatically:

2. Sign UserOp

The /v1/userop/sign endpoint signs the UserOperation hash:

curl -X POST http://127.0.0.1:3100/v1/userop/sign \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "userOp": {
      "sender": "0x1234...",
      "nonce": "0x1",
      "initCode": "0x",
      "callData": "0xa9059cbb...",
      "callGasLimit": "0x5208",
      "verificationGasLimit": "0x5208",
      "preVerificationGas": "0x5208",
      "maxFeePerGas": "0x4a817c800",
      "maxPriorityFeePerGas": "0x3b9aca00",
      "paymasterAndData": "0x",
      "signature": "0x"
    }
  }'

WAIaaS handles signature generation based on your smart account's validation scheme — whether it's ECDSA, multi-sig, or custom validation logic.

3. Combined Build and Sign

For convenience, you can build and sign in a single request:

curl -X POST http://127.0.0.1:3100/v1/userop/build-and-sign \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "transactions": [
      {
        "type": "TokenTransfer",
        "token": "0xA0b86a33E6441d78abf57Fe4e5dA14A6e7F8b57C",
        "to": "0x742b35Cc6Cf16CCBb0c5db86c5E5D12b06d67f2E",
        "amount": "1000000000000000000"
      }
    ],
    "submitToMempool": true
  }'

Setting submitToMempool: true automatically submits the signed UserOp to the bundler network.

Batch Operations Made Simple

One of ERC-4337's key benefits is batching multiple operations into a single UserOp. WAIaaS makes this trivial:

curl -X POST http://127.0.0.1:3100/v1/userop/build-and-sign \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "transactions": [
      {
        "type": "Approve",
        "token": "0xA0b86a33E6441d78abf57Fe4e5dA14A6e7F8b57C",
        "spender": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
        "amount": "1000000000000000000000000"
      },
      {
        "type": "ContractCall",
        "to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
        "data": "0x791ac947..."
      }
    ],
    "submitToMempool": true
  }'

This example batches a token approval with a Uniswap swap, executing both operations atomically in a single UserOp.

Integration with WAIaaS Policy Engine

UserOps flow through WAIaaS's 7-stage transaction pipeline, including policy validation. You can configure ERC-4337-specific policies:

curl -X POST http://127.0.0.1:3100/v1/policies \
  -H "Content-Type: application/json" \
  -H "X-Master-Password: <password>" \
  -d '{
    "walletId": "<wallet-uuid>",
    "type": "SPENDING_LIMIT",
    "rules": {
      "instant_max_usd": 100,
      "notify_max_usd": 1000,
      "delay_max_usd": 10000,
      "delay_seconds": 300
    }
  }'

The policy engine evaluates UserOps just like regular transactions, enabling sophisticated risk management for smart contract wallets.

Real-World DeFi Example: Automated Yield Strategy

Here's how a yield farming bot might use the UserOp API to execute a complex strategy:

# Step 1: Build a multi-step yield strategy UserOp
curl -X POST http://127.0.0.1:3100/v1/userop/build-and-sign \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer wai_sess_<token>" \
  -d '{
    "transactions": [
      {
        "type": "Approve",
        "token": "0xA0b86a33E6441d78abf57Fe4e5dA14A6e7F8b57C",
        "spender": "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9",
        "amount": "1000000000"
      },
      {
        "type": "ContractCall",
        "to": "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9",
        "data": "0xe8eda9df000000000000000000000000a0b86a33e6441d78abf57fe4e5da14a6e7f8b57c000000000000000000000000000000000000000000000000000000003b9aca000000000000000000000000001234567890123456789012345678901234567890000000000000000000000000000000000000000000000000000000000000000000"
      }
    ],
    "submitToMempool": true,
    "gasOptions": {
      "maxFeePerGas": "30000000000",
      "maxPriorityFeePerGas": "2000000000"
    }
  }'

This UserOp approves USDC for Aave and supplies it to earn yield, all in a single atomic operation.

Quick Start with ERC-4337

  1. Install WAIaaS CLI:

    npm install -g @waiaas/cli
    waiaas init
    waiaas start
    
  2. Create an EVM wallet:

    curl -X POST http://127.0.0.1:3100/v1/wallets \
      -H "Content-Type: application/json" \
      -H "X-Master-Password: <password>" \
      -d '{"name": "aa-wallet", "chain": "evm", "environment": "mainnet"}'
    
  3. Create a session for your application:

    curl -X POST http://127.0.0.1:3100/v1/sessions \
      -H "Content-Type: application/json" \
      -H "X-Master-Password: <password>" \
      -d '{"walletId": "<wallet-uuid>"}'
    
  4. Test UserOp building:

    curl -X POST http://127.0.0.1:3100/v1/userop/build \
      -H "Authorization: Bearer <session-token>" \
      -d '{"transactions": [{"type": "Transfer", "to": "0x...", "amount": "0.01"}]}'
    
  5. Check the interactive API docs: Open http://127.0.0.1:3100/reference to explore all UserOp endpoints.

The WAIaaS UserOp API eliminates the complexity of ERC-4337 Account Abstraction, letting you focus on building innovative DeFi applications rather than wrestling with UserOperation construction and signing. With support for batching, policy enforcement, and automatic gas estimation, it's the simplest way to add smart contract wallet capabilities to your AI agents and DeFi protocols.

Ready to simplify your ERC-4337 integration? Check out the full documentation at GitHub and start building with smart contract wallets at waiaas.ai.