This guide covers the did:polygon method integration for TrustWeave. The did:polygon plugin provides Polygon DID resolution with blockchain anchoring support, reusing the Ethereum DID pattern for EVM compatibility.
Overview
The did/plugins/polygon module provides an implementation of TrustWeave’s DidMethod interface using the Polygon blockchain. This integration enables you to:
Create and resolve DIDs on Polygon blockchain
Store DID documents via blockchain anchoring
Support Polygon mainnet and Mumbai testnet
Lower transaction costs compared to Ethereum mainnet
Reuse Ethereum DID patterns (EVM-compatible)
Installation
Add the did:polygon module to your dependencies:
1
2
3
4
5
6
7
8
9
10
11
12
13
dependencies{implementation("com.trustweave.did:polygon:1.0.0-SNAPSHOT")implementation("com.trustweave:trustweave-did:1.0.0-SNAPSHOT")implementation("com.trustweave.did:base:1.0.0-SNAPSHOT")implementation("com.trustweave:trustweave-anchor:1.0.0-SNAPSHOT")implementation("com.trustweave:trustweave-common:1.0.0-SNAPSHOT")// Web3j for Polygon blockchainimplementation("org.web3j:core:4.10.0")// Polygon anchor clientimplementation("com.trustweave.chains:polygon:1.0.0-SNAPSHOT")}
When the module is on the classpath, did:polygon is automatically available:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
importcom.trustweave.did.*importjava.util.ServiceLoader// Discover did:polygon providervalproviders=ServiceLoader.load(DidMethodProvider::class.java)valpolygonProvider=providers.find{it.supportedMethods.contains("polygon")}// Create method with required optionsvaloptions=didCreationOptions{property("rpcUrl","https://rpc-mumbai.maticvigil.com")property("chainId","eip155:80001")property("anchorClient",anchorClient)// Required: provide anchor client}valmethod=polygonProvider?.create("polygon",options)
Usage Examples
Creating a did:polygon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
valconfig=PolygonDidConfig.mumbai("https://rpc-mumbai.maticvigil.com")valanchorClient=PolygonBlockchainAnchorClient(config.chainId,config.toMap())valkms=InMemoryKeyManagementService()valmethod=PolygonDidMethod(kms,anchorClient,config)// Create DID (uses secp256k1 for EVM compatibility)valoptions=didCreationOptions{algorithm=KeyAlgorithm.SECP256K1purpose(KeyPurpose.AUTHENTICATION)purpose(KeyPurpose.ASSERTION)}valdocument=method.createDid(options)println("Created: ${document.id}")// did:polygon:mumbai:0x... or did:polygon:0x...
did:polygon stores DID documents on the Polygon blockchain using the BlockchainAnchorClient infrastructure. Documents are anchored via transactions, similar to did:ethr.
importcom.trustweave.TrustWeaveimportcom.trustweave.polygondid.*importcom.trustweave.anchor.*importcom.trustweave.polygon.PolygonBlockchainAnchorClientvalconfig=PolygonDidConfig.mumbai("https://rpc-mumbai.maticvigil.com")valanchorClient=PolygonBlockchainAnchorClient(config.chainId,config.toMap())valTrustWeave=TrustWeave.create{kms=InMemoryKeyManagementService()blockchain{register(config.chainId,anchorClient)}didMethods{+PolygonDidMethod(kms!!,anchorClient,config)}}// Use did:polygonvaldid=TrustWeave.createDid("polygon"){algorithm=KeyAlgorithm.SECP256K1}.getOrThrow()valresolved=TrustWeave.resolveDid(did.id).getOrThrow()
Error Handling
Common errors and solutions:
Error
Cause
Solution
rpcUrl is required
Missing RPC endpoint
Provide Polygon RPC URL
chainId is required
Missing chain ID
Specify chain ID (eip155:137, etc.)
BlockchainAnchorClient is required
Missing anchor client
Provide anchor client in options
Failed to anchor document
Transaction failed
Check private key, gas, network connectivity
Testing
For testing without actual blockchain:
1
2
3
4
5
6
7
8
9
importcom.trustweave.testkit.anchor.InMemoryBlockchainAnchorClientvalconfig=PolygonDidConfig.mumbai("https://rpc-mumbai.maticvigil.com")valanchorClient=InMemoryBlockchainAnchorClient(config.chainId)valmethod=PolygonDidMethod(kms,anchorClient,config)// Create and resolve (stored in memory)valdocument=method.createDid(options)valresult=method.resolveDid(document.id)
Best Practices
Use testnet for development: Mumbai testnet for testing
Lower costs: Take advantage of Polygon’s lower transaction costs
Private key security: Never hardcode private keys
Gas management: Monitor gas prices (much lower than Ethereum)
Chain ID validation: Always validate chain ID matches network
Comparison with did:ethr
did:polygon is similar to did:ethr but optimized for Polygon: