This guide covers the did:ens method integration for TrustWeave. The did:ens plugin provides human-readable DID resolution using Ethereum Name Service (ENS).
Overview
The did/plugins/ens module provides an implementation of TrustWeave’s DidMethod interface using the Ethereum Name Service (ENS) resolver. This integration enables you to:
Resolve human-readable DID identifiers (e.g., did:ens:example.eth)
Map ENS domain names to Ethereum addresses
Integrate with ENS resolver contracts
Convert ENS names to did:ethr DIDs for resolution
Installation
Add the did:ens module to your dependencies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
dependencies{implementation("org.trustweave:did-plugins-ens:0.6.0")implementation("org.trustweave:did-did-core:0.6.0")implementation("org.trustweave:did-plugins-base:0.6.0")implementation("org.trustweave:did-plugins-ethr:0.6.0")implementation("org.trustweave:anchors-anchor-core:0.6.0")implementation("org.trustweave:distribution-all:0.6.0")// Web3j for Ethereum blockchainimplementation("org.web3j:core:4.10.0")// Optional: Polygon client for EVM-compatible chainsimplementation("org.trustweave:anchors-plugins-polygon:0.6.0")}
importorg.trustweave.did.identifiers.Didimportorg.trustweave.did.resolver.DidResolutionResultvalconfig=EnsDidConfig.mainnet("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")valanchorClient=PolygonBlockchainAnchorClient(config.chainId,config.toMap())valkms=InMemoryKeyManagementService()valmethod=EnsDidMethod(kms,anchorClient,config)// Resolve ENS domain to DID documentvaldid=Did("did:ens:example.eth")valresult=method.resolveDid(did)when(result){isDidResolutionResult.Success->{println("Resolved: ${result.document.id}")println("Verification methods: ${result.document.verificationMethod.size}")}isDidResolutionResult.Failure.NotFound->{println("DID not found: ${result.did.value}")}else->println("Resolution failed")}
How it Works
Extract ENS domain: From did:ens:example.eth, extract example.eth
Resolve ENS to address: Query ENS registry to resolve example.eth to Ethereum address
Convert to did:ethr: Convert Ethereum address to did:ethr:0x...
Resolve did:ethr: Use did:ethr method to resolve the DID document
Return did:ens document: Return document with did:ens:example.eth as the ID
importorg.trustweave.trust.TrustWeaveimportorg.trustweave.did.identifiers.Didimportorg.trustweave.did.resolver.DidResolutionResultimportorg.trustweave.ensdid.*importorg.trustweave.kms.inmemory.InMemoryKeyManagementServicevalconfig=EnsDidConfig.mainnet("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")valkms=InMemoryKeyManagementService()valtrustWeave=TrustWeave.build{customKms(kms)anchor{chain(config.chainId){provider("polygon")// OptionsBuilder uses an infix String.to(value); list keys explicitly so the// call resolves to the builder rather than kotlin.Pair.options{"rpcUrl"toconfig.rpcUrl"chainId"toconfig.chainId"ensRegistryAddress"toconfig.ensRegistryAddressconfig.privateKey?.let{"privateKey"toit}config.network?.let{"network"toit}}}}did{method("ens"){}}}valdid=Did("did:ens:example.eth")when(valresolved=trustWeave.resolveDid(did)){isDidResolutionResult.Success->println("Resolved: ${resolved.document.id}")else->println("Resolve failed: $resolved")}
Error Handling
Common errors and solutions:
Error
Cause
Solution
ensRegistryAddress is required
Missing ENS registry
Provide ENS registry contract address
rpcUrl is required
Missing RPC endpoint
Provide Ethereum RPC URL
chainId is required
Missing chain ID
Specify chain ID (eip155:1, etc.)
did:ens does not support DID creation
Trying to create DID
Register ENS domain first, then resolve
ENS domain not found
Domain not registered
Verify ENS domain exists and is registered
DID document not found
Address has no did:ethr
Ensure address has a did:ethr DID
Testing
For testing without actual ENS resolution:
1
2
3
4
5
6
7
8
importorg.trustweave.testkit.anchor.InMemoryBlockchainAnchorClientvalconfig=EnsDidConfig.mainnet("https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY")valanchorClient=InMemoryBlockchainAnchorClient(config.chainId)valmethod=EnsDidMethod(kms,anchorClient,config)// Note: ENS resolution requires actual ENS registry interaction// For full testing, use a testnet or local Ethereum node
Best Practices
Use ENS for readability: did:ens provides human-readable identifiers
Link to did:ethr: Ensure ENS domain is linked to an address with did:ethr DID
Test on testnets: Use Sepolia testnet for development
Cache resolutions: Cache ENS-to-address mappings for performance
Error handling: Handle cases where ENS domain doesn’t exist or isn’t linked