Overview
This guide shows how to combine Magic’s Express Server Wallet with Alchemy’s Account Kit SDK to upgrade a TEE-managed EOA into an EIP-7702 smart wallet. The result is a server-side wallet that can send gas-sponsored and batched transactions on Base Sepolia — all without the end user managing keys or paying gas. The approach uses a Next.js API route as the integration layer: it authenticates the user, fetches the EOA from Magic’s TEE, builds a custom signer that delegates cryptographic operations back to the TEE, and uses Alchemy’sSmartWalletClient to send EIP-7702 calls.
This recipe uses EIP-7702 delegation, which temporarily upgrades an EOA to behave like a smart contract account for a single transaction bundle. Unlike ERC-4337, the user keeps the same address — no counterfactual deployment needed.
Prerequisites
To follow along, you’ll need:- A Magic Secret Key — from your Magic Dashboard
- An OIDC Provider ID — configured for your auth provider (setup guide)
- An Alchemy API Key — from your Alchemy Dashboard
- An Alchemy Gas Policy ID (optional) — to sponsor gas for your users (Alchemy gas manager configuration)
- A Next.js app with NextAuth configured for authentication
Install Dependencies
Environment Variables
Add the following to your.env.local:
.env.local
How It Works
The integration lives entirely in a single Next.js API route. Here’s the high-level flow:Authenticate the User
The API route validates the user’s session via NextAuth and extracts their JWT. This token is used to authenticate all subsequent calls to Magic’s TEE.
TypeScript
Fetch the EOA from the TEE
Call Magic’s Express API to get (or create) the user’s EOA wallet. The private key never leaves the TEE.
TypeScript
Build a Custom Signer
Alchemy’s The complete signer object:
SmartWalletClient expects a SmartAccountSigner interface. Since the private key lives in the TEE, we implement a custom signer that delegates all signing operations to Magic’s Express API.The signer needs three methods:signMessage — hash the message locally with hashMessage() (EIP-191), then send the raw hash to the TEE’s sign/data endpoint:TypeScript
signTypedData — same pattern, using hashTypedData() for EIP-712 structured data.signAuthorization — for EIP-7702 delegation, call the TEE’s dedicated sign/eip7702 endpoint:TypeScript
TypeScript
Create the Smart Wallet Client
Initialize Alchemy’s
SmartWalletClient with the custom signer, targeting Base Sepolia. If a gas policy ID is configured, transactions will be sponsored.TypeScript
Complete API Route
Here’s the full Next.js API route that ties everything together:TypeScript
Key Concepts
Why EIP-7702?
EIP-7702 allows an EOA to temporarily delegate to a smart contract implementation for a single transaction bundle. This means:- Same address — the user keeps their existing EOA address, no new counterfactual smart account needed
- Batched calls — multiple operations execute atomically in one transaction
- Gas sponsorship — a paymaster can cover gas fees on behalf of the user
- No permanent state change — the delegation is scoped to the transaction
Why a Custom Signer?
Since the private key lives inside Magic’s TEE (Trusted Execution Environment), we can’t pass it directly to Alchemy’s SDK. Instead, we implement theSmartAccountSigner interface with methods that proxy signing requests to the TEE’s API endpoints. This keeps the key secure while still integrating with Alchemy’s account abstraction tooling.
TEE Endpoints Used
| Endpoint | Purpose |
|---|---|
POST /v1/wallet | Get or create an EOA wallet |
POST /v1/wallet/sign/data | Sign a raw hash (used for EIP-191 and EIP-712) |
POST /v1/wallet/sign/eip7702 | Sign an EIP-7702 authorization |
Resources
Server Wallet Docs
Learn how to create and manage wallets with Magic’s TEE-based API.
Alchemy Smart Wallet Docs
Explore Alchemy’s smart wallet and account abstraction SDK.
EIP-7702 Specification
Read the EIP-7702 specification for EOA delegation.
Identity Provider Setup
Configure your OIDC provider for Magic’s Server Wallet.