thirdweb Engine

repository·main·Indexed 42 days ago

https://github.com/thirdweb-dev/engine

An open-source backend HTTP server providing a production-ready interface to read, write, and deploy contracts on EVM blockchains and private subnets. It features high throughput (700+ TPS), managed backend wallets (local or KMS), gas optimization, smart account support with session tokens, gasless transactions via relayers, and webhook support.

Tokens
40.7K
Snippets
142
Records
182
Agent score
53%

What's inside thirdweb Engine

  1. Overview of thirdweb Engine

    main
    thirdweb Engine is an open-source, backend HTTP server designed to provide a production-ready interface for interacting with blockchain contracts. It allows developers to read, write, and deploy contracts across various EVM blockchains and private subnets. Key capabilities include high throughput (700+ transactions per second), managed backend wallets (local or KMS), gas optimization (retries, ceilings, and timeouts), smart account support with session tokens, gasless transactions via relayers, and webhook support for wallets and contracts.
  2. Understand Engine's nonce management concepts

    main

    Engine tracks nonces using several distinct states to ensure reliable transaction sequencing and to prevent nonce collisions or gaps:

    1. On-chain Nonce: The state of the blockchain. It is calculated as the transactionCount from the RPC minus 1 (representing the last successfully mined transaction).
    2. Last Used Nonce: The highest nonce that Engine has attempted to use and sent to the RPC.
    3. Sent Nonces: A queue of nonces that have been broadcast to the network but have not yet been included in a block (mined).
    4. Recycled Nonces: A pool of nonces that were reserved by Engine but failed to be successfully broadcast to the RPC. These are held in a pending state to be either retried (recycled) or cancelled to prevent gaps in the transaction sequence.
  3. Configure deployment headers and transaction modes

    main

    When calling deployment methods, you can provide several optional headers to control the transaction behavior:

    • xIdempotencyKey: Prevents duplicate transactions. Engine compares the last 100,000 transactions to de-duplicate.
    • xTransactionMode: Set to 'sponsored' for sponsored transactions. This is ignored if using a smart wallet.
    • xAccountAddress: The smart account address.
    • xAccountFactoryAddress: The smart account factory address. If omitted, Engine attempts to resolve it from the contract.
    • xAccountSalt: A string or hex salt used to predict the smart account address. Useful when creating multiple accounts with the same admin.
  4. Understand the ABI schema structure

    main

    The engine uses abiSchema and abiTypeSchema to validate ABI definitions. An ABI is an array of objects where each object represents an entry (function, event, etc.).

    Key fields in abiTypeSchema include:

    • type (Optional String)
    • name (Optional String)
    • internalType (Optional String)
    • components (Optional Array of objects with type, name, and internalType)

    Key fields in abiFunctionSchema include:

    • name (String)
    • inputs (Array of abiTypeSchema)
    • outputs (Array of abiTypeSchema)
    • signature (String)
    • stateMutability (String)
    • comment (Optional String)
  5. Understand Public Endpoints and Sandbox Mode

    main

    Certain routes are considered public and do not require authentication. These include:

    • GET /
    • GET /favicon.ico
    • GET /system/health
    • GET routes matching OpenAPI definitions
    • GET /auth/user*
    • GET /transaction/status*
    • POST /auth/payload, /auth/login, /auth/switch-account, /auth/logout
    • /relayer/{uuid} (Relay transaction endpoints)
    • Admin queue routes (which enforce their own specific auth)

    Additionally, if the ENGINE_MODE environment variable is set to sandbox, the authentication check is skipped entirely for all requests.

  6. Understand the structure of an NFT

    main

    The nftSchema defines the complete structure of an NFT as tracked by the engine. It includes:

    • metadata: The nftMetadataSchema (id, uri, name, description, etc.).
    • owner: The wallet address of the owner.
    • type: The contract standard, which must be one of ERC1155, ERC721, or metaplex.
    • supply: The total supply as a string.
    • quantityOwned: (Optional) The amount owned by the user.
  7. Understand the CustomError structure

    main

    The engine uses a specific CustomError type to handle expected operational errors. When you throw an object matching this shape, the middleware will catch it and return it as a structured JSON response.

    Type Definition:

    type CustomError = {
      message: string;
      statusCode: number;
      code: string;
      stack?: string;
    };

    API Response Format: When a CustomError is thrown, the client receives a response with the following JSON body:

    {
      "error": {
        "code": "STRING_CODE",
        "message": "Human readable message",
        "statusCode": 400,
        "stack": "..."
      }
    }
  8. Enable Keypair Authentication

    main

    Keypair authentication allows requests to be authenticated using a JWT signed by an ES256 private key that matches a configured public key.

    To use this feature, you must:

    1. Set the ENABLE_KEYPAIR_AUTH environment variable to true.
    2. Ensure the public key is added to Engine.
    3. (Optional) If the JWT contains a bodyHash, the request body must match the SHA256 hash provided in the token to prevent tampering.
  9. Configure IP Allowlisting for Security

    main

    Engine supports IP allowlisting to restrict access to specific IP addresses.

    • If the ipAllowlist in your configuration is empty, all IPs are allowed.
    • If configured, the middleware checks the request IP against the list. If TRUST_PROXY is enabled (or if ENGINE_TIER is set), the middleware will use the cf-connecting-ip header (Cloudflare) to identify the client IP.
    • If an IP is not in the allowlist, the request is rejected with a 401 Unauthorized error.
  10. Configure authentication with `withAuth`

    main

    The withAuth function is a Fastify middleware used to set up authentication and permission handling in an Engine server. It integrates the @thirdweb-dev/auth/fastify plugin, registers an /auth router, and adds a preValidation hook to validate incoming requests.

    When configured, it allows for several authentication patterns:

    • Access Tokens: Non-revoked tokens for Owners or Admins.
    • Keypair Authentication: JWTs signed by an ES256 private key (requires ENABLE_KEYPAIR_AUTH environment variable).
    • Dashboard Auth: JWTs issued by the Thirdweb Dashboard.
    • Secret Key: Using the THIRDWEB_API_SECRET_KEY in the Authorization header.
    • Auth Webhooks: External verification via configured webhooks.

    In non-sandbox mode, if authentication fails, the middleware returns a 401 Unauthorized response with an error message.

    import { withAuth } from './path/to/auth';
    
    // Inside your Fastify server setup
    await withAuth(server);