@resolverworks/ezccip
TypeScript icon, indicating that this package has built-in type declarations

0.0.10 • Public • Published

ezccip.js

Turnkey CCIP-Read Handler for ENS and arbitrary functions.

npm i @resolverworks/ezccip

Usage

Create an instance and register some handlers.

import {EZCCIP} from '@resolverworks/ezccip';

let ezccip = new EZCCIP();

// implement an arbitrary function
ezccip.register('add(uint256, uint256) returns (uint256)', ([a, b]) => [a + b]);

// implement a wildcard ENSIP-10 resolver
// which handles resolve() automatically
ezccip.enableENSIP10(async (name, context) => {
    return {
        async text(key) {
            switch (key) {
                case 'name': return 'Raffy';
                case 'avatar': return 'https://raffy.antistupid.com/ens.jpg';
            }
        },
    };
});

// more complicated example
let abi = new ethers.Interface([
	'function f(bytes32 x) return (string)',
	'function g(uint256 a, uint256 b) return (uint256)',
]);
ezccip.register(abi, { // register multiple functions at once using existing ABI
	async ['f()']([x], context, history) { // match function by signature
		history.show = [context.sender]; // replace arguments of f(...) in logger 
		history.name = 'Chonk'; // rename f() to Chonk() in logger
		return [context.calldata]; // return call data as hex
	},
	async ['0xe2179b8e']([a, b], context) {  // match by selector
		context.protocol = "tor"; // override signing protocol
		return ethers.toBeHex(1337n, 32); // return raw encoded result
	}
});

When your server has a request for CCIP-Read, use EZCCIP to produce a response.

let {sender, data: calldata} = JSON.parse(req.body);
let {data} = await ezccip.handleRead(sender, calldata, {
    protocol: 'tor', // default, tor requires signingKey + resolver
    signingKey, // your private key
    resolver, // address of the TOR
    // most other values are considered "context"
    // which is passed to each handler
    // number: 1,
	// string: "abc",
});
reply.json({data});

Demo

  1. npm run start — starts a CCIP-Read server for TOR protocol
  2. setText("ccip.context", "0xd00d726b2aD6C81E894DC6B87BE6Ce9c5572D2cd http://localhost:8016")

Note:

  • serve() will bind requests to the sender if the protocol needs a target.
  • Provide a resolvers mapping to pair endpoint suffixes to specific contract deployments.
    • The demo uses s to correspond to the Sepolia deployment, which makes requests to the modified endpoint http://localhost:8016/s target that contract, regardless of sender.
  • An endpointcontract pairing is required to support wrapped/recursive CCIP calls!

Examples

  • DNS: ezccip.raffy.xyz
    • Context: 0xd00d726b2aD6C81E894DC6B87BE6Ce9c5572D2cd https://raffy.xyz/ezccip/
  • ENS: ezccip.eth
    • Context: 0xd00d726b2aD6C81E894DC6B87BE6Ce9c5572D2cd https://raffy.xyz/ezccip/s

Package Sidebar

Install

npm i @resolverworks/ezccip

Weekly Downloads

65

Version

0.0.10

License

MIT

Unpacked Size

35.3 kB

Total Files

6

Last publish

Collaborators

  • dsinghbailey
  • adraffy
  • slobodnik