@adraffy/ezccip
v0.2.0
Published
Turnkey CCIP-Read Handler
Maintainers
Readme
ezccip.js
Turnkey EIP-3668: CCIP-Read handler for ENS and arbitrary functions.
bun add @adraffy/ezccip viem / npm i @adraffy/ezccip viem (Node 20+) ✓
- TypeScript, ESM-only, built on viem (peer dependency)
- handler arguments and return values are typed from the ABI
- runtime-agnostic core:
handleRequest(sender, calldata), plus HTTP adapters forfetchruntimes (Bun, Deno, Cloudflare Workers, Hono) andnode:http - signing protocols:
"ens"— ensdomains/offchain-resolver and ccip.tools"tor"— TheOffchainResolver (TOR)"raw"— unsigned response (EVM Gateway, testing, etc.)
enableENSIP10()wildcard resolver from plain record objects- supports Multicall-over-CCIP-Read (see Multicall)
serveEZCCIP()to quickly launch a server (Node, Bun, Deno),createFetchHandler()for everything else
Demo
bun run start starts a TOR CCIP-Read server on port 8016 (or PORT) (see scripts/demo.ts)
Usage
import { createFetchHandler, EZCCIP } from "@adraffy/ezccip";
import { serveEZCCIP } from "@adraffy/ezccip/serve";
import { getAbiItem } from "viem";
const ezccip = new EZCCIP({
signer: privateKey, // Hex private key or viem LocalAccount
// protocol: "tor", // default: "ens" with a signer, otherwise "raw"
// ttl: 60, // response lifetime in seconds
});
// implement an arbitrary function
// args and return type are inferred from the signature
ezccip.addFunctionHandler("function plus(uint256 a, uint256 b) returns (uint256)", ([a, b]) => [a + b]);
// or from an existing ABI
ezccip.addFunctionHandler(getAbiItem({ abi, name: "f" }), async ([x], ctx, history) => {
history.show = [ctx.sender]; // replace arguments of f(...) in logger
history.name = "Chonk"; // rename f() to Chonk() in logger
ctx.protocol = "raw"; // override signing protocol for this request
return "0x..."; // return already-encoded bytes
});
// or by name from an ABI: human-readable (which can declare structs) or JSON
// (the name must not be overloaded; for overloads, pass getAbiItem({ abi, name, args }) above)
const abi = ["struct Point { uint256 x; uint256 y; }", "function flip(Point p) returns (Point)"] as const;
ezccip.addAbiHandler(abi, "flip", ([{ x, y }]) => [{ x: y, y: x }]);
// implement a wildcard ENSIP-10 resolver
// which handles resolve() automatically
ezccip.enableENSIP10({
async resolve(name, ctx) {
// return nothing (undefined or null) if the name doesn't exist: reverts UnreachableName
// return {} if the name exists without records: every profile answers empty
return {
text(key) {
switch (key) {
case "name":
return "Raffy";
case "avatar":
return "https://raffy.antistupid.com/ens.jpg";
}
},
};
},
});
// signer and protocol can change later (the protocol isn't changed by setting a signer)
ezccip.signer = otherKey; // or undefined to clear it
ezccip.protocol = "tor";
// answer a request directly
const { data, history } = await ezccip.handleRequest(sender, calldata); // { origin, extra, ... } optional
console.log(history.toString()); // eg. resolve(raffy.eth).text(name)
// or serve it over HTTP
export default { fetch: createFetchHandler(ezccip) }; // Bun, Deno, Cloudflare Workers, ...
await serveEZCCIP(ezccip, { port: 8016 }); // Node, Bun, Deno (from "@adraffy/ezccip/serve")Handlers
(args, ctx, history) => result
args— decoded argumentsctx— aReadContext: one plain object shared by the whole request (everymulticallsubcall gets the same one)sender,calldata— the requestchainId— the origin's chain, if the request says (see Sender vs Origin)origin,protocol,signer,ttl— how the response is signed. Set them in any call to change the entire response; they're validated when signing. They start from thehandleRequest()options, then theEZCCIPdefaults. Settingctx.signerdoesn't changectx.protocolextra— custom values from the caller, eg.ctx.extra.ip, see Extra
history— this call's line in the log: setname, orshow(an array of values to print instead of the arguments, orfalseto hide them)result— tuple of outputs (ABI-encoded for you) or already-encodedHex | Uint8Array. Functions without outputs may return nothing
Errors
- throw a
CCIPReadErrorto send its message and status to the client, eg.new CCIPReadError("not found", { status: 404 }) handleRequest()throws; the HTTP adapters turn other errors into500{"message": "internal error"}, and log the real error whenlogis on- bad requests and calldata are
400, unknown functions are404 - inside
multicall(), each failed subcall is answered withError(string)using the same rule
Multicall
multicall(bytes[]) is built in (it isn't a handler, so it can't be replaced) and is answered only as the outer call; its subcalls cannot be multicall(). resolve() may contain one multicall() of its own:
multicall([f(), g()])multicall([resolve(name, ...), ...])resolve(name, multicall([...]))multicall([resolve(name, multicall([...])), ...])
HTTP
Both adapters answer POST {sender, data} and GET .../{sender}/{data}.json with {data}, or {message} and an HTTP status, and take the same options:
cors— add CORS headers and answerOPTIONSpreflight requests (default:true)parseOrigin: (url: URL) => { origin?, chainId? }— default:?origin=0x...&chainId=1, see Sender vs Originlog— log every response and error withconsole.log
serveEZCCIP()
Start a simple node:http server (works on Node, Bun, and Deno) for an EZCCIP instance, or for an ENSIP-10 resolve function: serveEZCCIP(resolve, options) is serveEZCCIP(new EZCCIP(options).enableENSIP10({ resolve }), options), signed with a random key unless signer is set. Request bodies over 1 MiB are rejected. It logs startup, every response, every error, and shutdown unless log: false.
import { serveEZCCIP } from "@adraffy/ezccip/serve";
const ccip = await serveEZCCIP(ezccip, { port: 8016 }); // default: random port
// 2026-01-01T00:00:00.000Z Serving "tor" 0xd00d726b2aD6C81E894DC6B87BE6Ce9c5572D2cd http://localhost:8016
// 2026-01-01T00:00:01.000Z ::1 1:0xABCD... resolve(raffy.eth).text(name) ({chainId}:{origin}, "?" if no chainId)
ccip.endpoint; // "http://localhost:8016"
ccip.context; // "{signer} {endpoint}"
await ccip.shutdown();
// 2026-01-01T00:00:02.000Z Shutdown<2000ms>
// minimal example: fixed text() for any name
await using ccip = await serveEZCCIP(() => ({ text: () => "Raffy" }));
// with ENSIP-10 options: enableENSIP10() returns the instance
await serveEZCCIP(new EZCCIP({ signer: privateKey }).enableENSIP10({ resolve, multicall: false }));
// every name exists: unknown names answer empty instead of UnreachableName
await serveEZCCIP((name) => db.get(name) ?? {});createFetchHandler()
Returns a fetch(request, extra?) => response handler for fetch-based runtimes.
import { createFetchHandler } from "@adraffy/ezccip";
// Bun / Cloudflare Workers / Deno
export default { fetch: createFetchHandler(ezccip) };
// or pass your own extra values
const handler = createFetchHandler(ezccip);
Bun.serve({ port: 8000, fetch: (req) => handler(req, { region: "us" }) });Extra
ctx.extra is a Record<string, unknown> of custom values for handlers (and resolve), filled by whoever calls handleRequest(). Handlers may read or add to it.
ezccip.addFunctionHandler("function whoami() returns (string)", (_, ctx) => [`${ctx.extra.ip}`]);
// serveEZCCIP(): the client's ip (x-forwarded-for, or the socket address), plus your own values
await serveEZCCIP(ezccip, { extra: { region: "us" } }); // ctx.extra = { region: "us", ip: "::1" }
// createFetchHandler(): the fields of its second argument, eg. Cloudflare's env
export default { fetch: createFetchHandler(ezccip) }; // ctx.extra = { ...env }
// directly
await ezccip.handleRequest(sender, calldata, { extra: { ip } });Sender vs Origin
- ⚠️
sendermay not be the contract that issued the request:OffchainLookuponly carriessenderand the URLs, and a lookup can be wrapped and rethrown (see recursive CCIP-Read) - so the gateway URL is the only thing that identifies the contract, and its network
- Best Solution: put them in the URL as query parameters (both optional):
https://my.server/?chainId=1&origin=0xABCD...(GET:https://my.server/{sender}/{data}.json?chainId=1&origin=0xABCD...)ctx.origin = 0xABCD...,ctx.chainId = 1
- this is what the HTTP adapters do by default (
originFromQuery);chainIdmust be decimal - pass
parseOrigin: (url: URL) => { origin?, chainId? }to read them some other way
- or, pass them to
handleRequest(sender, calldata, { origin, chainId }) - if there's no origin,
origin = sender; if there's no chain,chainIdisundefined originis signed ("ens","tor");chainIdis only for handlers, eg. to pick which network to read
Unknown names vs empty records
What resolve(name, ctx, history) returns decides how resolve() is answered:
- nothing (
undefinedornull) → the name doesn't exist: revertsUnreachableName(bytes) {}→ the name exists without records: every profile answers empty ("",0x, the zero address)- a record → each profile calls its method; missing methods and missing values answer empty
resolve: (name) => db.get(name); // unknown names revert UnreachableName
resolve: (name) => db.get(name) ?? {}; // every name existsprocessENSIP10()
Answers ENSIP-10 calldata from a record object and returns the ABI-encoded response. It's a standalone function, so you can use it without an EZCCIP instance.
const record = {
text(key) { if (key === "name") return "raffy"; },
addr(coinType) { if (coinType === 60n) return "0x51050ec063d393217b436747617ad1c2285aeeee"; },
};
const res = await processENSIP10(record, calldata); // (record, calldata, options?, history?)addr() values for EVM coin types (60, and ENSIP-11 chains including the default) must be 20 bytes or empty; anything else throws. Other coin types are returned as-is. Empty bytes ("0x") count as no address, so the ENSIP-19 default address still applies.
Verifying responses
Checks a signed response the way the onchain verifier would: throws if it has expired, or if signer is given and doesn't match.
import { verifyResponse } from "@adraffy/ezccip";
const { result, expires, signer } = await verifyResponse({ protocol: "ens", response, calldata, origin, signer: expected });Development
See AGENTS.md for layout and conventions.
bun install
bun test
bun run check # typecheck
bun run lint # biome
bun run build # dist/
bun run smoke # build, then run dist/ on NodeMigrating from 0.1
ethers→viem(peer dependency); values areHexstrings andbigint- default protocol is
"ens"when constructed with a signer (was"tor"), otherwise"raw"; change it later withezccip.protocol = ...and the signer withezccip.signer = ...(orundefined) signingKey→signer(private key or viemLocalAccount)serve()from@adraffy/ezccip/serve→serveEZCCIP(ezccip, options)orserveEZCCIP(resolve, options)(the default signing key is random;serveris still anode:httpserver;signer→ezccip.signerAddress;resolvers→parseOrigin); orcreateFetchHandler(ezccip)for fetch-based runtimesregister(abi, impl)→ one function per call:addFunctionHandler(signature | AbiFunction, (args, ctx, history) => ...)oraddAbiHandler(abi, functionName, ...); signatures must be valid human-readable ABI, starting withfunction- extra context keys (eg.
context.ip) →ctx.extra.ip ttlSec→ttl,signingKey→signer, andcontext.history→ the handler's third argumentenableENSIP10(fn, options)→enableENSIP10({ resolve: fn, ...options });throwErrors→throwUnsupportedaddr()values for EVM coin types must be 20 bytes (were truncated or padded)- unknown functions are
404and bad calldata400(both were500); non-CCIPReadErrormessages are hidden from clients handleRead(sender, calldata, config)→handleRequest(sender, calldata, { origin, chainId, protocol, signer, ttl, extra })log/formatErroroptions →log: booleanonserveEZCCIP(default on) andcreateFetchHandlerserveEZCCIPsetsctx.extra.ip;context.urlis gone (useparseOriginor callhandleRequest()yourself)- the origin is read from the query (
?origin=0x...&chainId=1), not the first/0x...path component; for existing URLs with the origin in the path, passparseOrigin: (url) => ({ origin: url.pathname.match(/\/(0x[0-9a-f]{40})(?=\/|$)/i)?.[1] }) recursionLimitremoved —multicallis only allowed at the top level (see Multicall)Historyonly builds the log line: setnameorshow(an array, orfalse);enter()→addChild(),then()→addInner(),next→innerENSRecord.ABI(contentTypes)receives abigintand returnstypeas abigint;pubkey()returnsxandyasbigintorHex(nonumber)unreachableAsEmptyremoved — return{}fromresolvefor names that exist without records (see Unknown names vs empty records)labels_from_dns_encoded→dnsDecodeLabels(ordnsDecodeNamefor the joined name),error_with→CCIPReadErrorRESOLVE_ABI→resolverAbi;multicallis no longer a handler (findHandler("multicall")isundefined)
