Skip to content

Repository files navigation

ENSKit

A swift utility to resolve Ethereum Domain Names (ENS).

Quick Start

Setup:

// Use default options with Cloudflare Ethereum Gateway
letenskit=ENSKit()
// Use a built-in public RPC
letflashbot=ENSKit(jsonrpcClient:EthereumAPI.Flashbots)
// Use Infura Ethereum API
letinfuraURL=URL(string:"https://mainnet.infura.io/v3/<projectid>")!
letinfura=ENSKit(jsonrpcClient:InfuraEthereumAPI(url: infuraURL))
// Use Infura Ethereum API with project secret
letinfuraSecret="<projectsecret>"letinfuraWithProjectSecret=ENSKit(jsonrpcClient:InfuraEthereumAPI(url: infuraURL, projectSecret: infuraSecret))
// Use Infura Ethereum API with JWT token
letinfuraJWT="<JWT>"letinfuraWithJWT=ENSKit(jsonrpcClient:InfuraEthereumAPI(url: infuraURL, jwt: infuraJWT))

Get contenthash URL:

// in async function
if let url =tryawait enskit.contenthash(name:"<your_ens>.eth"){
// try fetch the content from IPFS/IPNS/Swarm URL
}

Get avatar:

// in async function
letavatar=tryawait enskit.avatar(name:"<your_ens>.eth")iflet avatar = avatar,let image =NSImage(data: avatar){ // or UIImage
// use image in your application
}

Get text information associated with ENS:

// in async function
if let email =tryawait enskit.text(name:"<your_ens>.eth", key:"email"){
// use email in your application
}

Resolve address to name:

// in async function
if let name =tryawait enskit.name(addr:"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"){
// 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 would resolve to vitalik.eth
}

Advanced Usage

ENSResolver Interface

It is recommended to use ENSResolver if you would like to query several records of an ENS at once.

For example, use ENSResolver to get the Ethereum wallet address, email, avatar, and contenthash associated with an ENS:

// in async function
letenskit=ENSKit()
if let resolver =tryawait enskit.resolver(name:"<your_ens>.eth"){letaddress=tryawait resolver.addr()letemail=tryawait resolver.text(key:"email")letavatar=tryawait resolver.avatar()letcontenthash=tryawait resolver.contenthash()
// process information in your application
}

Methods in resolver reuse the same public resolver contract when the instance is created. This can save a few requests to Ethereum API.

In addition, all methods in resolver are marked with throw keyword. An empty result from resolver means a successful query, not an error interacting with Ethereum. In contrast, convenience methods in ENSKit main class do NOT distinguish between empty records and error results.

Contract Events

ENSKit supports contract events. You can search for history of contenthash changes and Ethereum wallet changes of an ENS:

// Cloudflare Ethereum Gateway (default) does not support full history of events
letenskit=ENSKit(jsonrpcClient:EthereumAPI.Flashbots)
if let resolver =tryawait enskit.resolver(name:"<your_ens>.eth"){iflet addrHistory =tryawait resolver.searchAddrHistory(),
!addrHistory.isEmpty {letcurrentAddressChangedOn=addrHistory[0].date
letaddress=addrHistory[0].addr // this address should be the same as calling resolver.addr()
}iflet contenthashHistory =tryawait resolver.searchContenthashHistory(),
contenthashHistory.count >1{letpreviousContenthashChangedOn=addrHistory[1].date
letpreviousContenthash=addrHistory[1].contenthash
}}

History entries are sorted by newest first.

If you only concern about the latest change, there are also convenience methods in ENSKit:

letenskit=ENSKit(jsonrpcClient:EthereumAPI.Flashbots)letlastAddrChange=await enskit.lastAddrChange(name:"<your_ens>.eth")

Extensibility

You can provide your own Ethereum node to interact:

letclient=EthereumAPI(url:URL("https://rpc.myethnode")!)letenskit=ENSKit(jronrpcClient: client)

You can also provide your own JSONRPC implementation if EthereumAPI does not cover your need.

ENSKit uses OpenSea as a default NFT API provider to fetch NFT avatars that do not store metadata on chain. You can provide your own NFTPlatform implementation to use other services.

ENSKit uses Cloudflare IPFS Gateway to resolve IPFS content when fetching ENS avatar data. You can use another IPFS Gateway by passing a base URL:

letipfs=IPFSGatewayClient(baseURL:URL("http://localhost:8080")!)letenskit=ENSKit(ipfsClient: ipfs)

You can also provide your own IPFSClient implementation to fetch IPFS content.

License

MIT

About

Swift library for Ethereum Name Service

Topics

Resources

Stars

21 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages