Complete guide for implementing credential exchange protocols in TrustWeave.
Overview
This guide covers how to implement new credential exchange protocols using the protocol abstraction layer. All protocols implement the CredentialExchangeProtocol interface, allowing them to be used interchangeably.
packagecom.trustweave.credential.yourprotocol.exchangeimportcom.trustweave.credential.exchange.*classYourProtocolExchangeProtocol(privatevalservice:YourProtocolService):CredentialExchangeProtocol{overridevalprotocolName="yourprotocol"overridevalsupportedOperations=setOf(ExchangeOperation.OFFER_CREDENTIAL,ExchangeOperation.REQUEST_CREDENTIAL,ExchangeOperation.ISSUE_CREDENTIAL)overridesuspendfunofferCredential(request:CredentialOfferRequest):CredentialOfferResponse{valoffer=service.createOffer(issuerDid=request.issuerDid,holderDid=request.holderDid,preview=request.credentialPreview)returnCredentialOfferResponse(offerId=offer.id,offerData=offer,protocolName=protocolName)}// Implement other operations...}
packagecom.trustweave.credential.yourprotocol.exchange.spiimportcom.trustweave.credential.exchange.CredentialExchangeProtocolimportcom.trustweave.credential.exchange.spi.CredentialExchangeProtocolProviderclassYourProtocolExchangeProtocolProvider:CredentialExchangeProtocolProvider{overridevalname="yourprotocol"overridevalsupportedProtocols=listOf("yourprotocol")overridefuncreate(protocolName:String,options:Map<String,Any?>):CredentialExchangeProtocol?{if(protocolName!="yourprotocol")returnnullvalservice=YourProtocolService(// Initialize from options)returnYourProtocolExchangeProtocol(service)}}
@Testfun`testcompleteexchangeflow`()=runTest{valregistry=CredentialExchangeProtocolRegistry()registry.register(YourProtocolExchangeProtocol(service))// Test full flowvaloffer=registry.offerCredential("yourprotocol",offerRequest)valrequest=registry.requestCredential("yourprotocol",requestRequest)valissue=registry.issueCredential("yourprotocol",issueRequest)assertNotNull(issue.credential)}
Best Practices
Error Handling: Always validate inputs and provide clear error messages
Type Safety: Use strongly-typed models for protocol-specific data
Documentation: Document protocol-specific options and limitations
Testing: Write comprehensive tests for all operations
SPI Registration: Always register SPI providers for auto-discovery
// Convert common preview to protocol-specificvalprotocolPreview=ProtocolPreview(attributes=request.credentialPreview.attributes.map{attr->ProtocolAttribute(name=attr.name,value=attr.value,mimeType=attr.mimeType)})
Handling Optional Operations
1
2
3
4
5
6
7
8
9
10
overridesuspendfunrequestProof(request:ProofRequestRequest):ProofRequestResponse{if(!supportedOperations.contains(ExchangeOperation.REQUEST_PROOF)){throwUnsupportedOperationException("Protocol '$protocolName' does not support proof requests")}// Implementation...}