importcom.trustweave.did.*importcom.trustweave.kms.*classMyCustomDidMethod(privatevalkms:KeyManagementService,privatevalconfig:MyDidConfig):DidMethod{overridevalmethod:String="mydid"overridesuspendfuncreateDid(options:DidCreationOptions):DidDocument{// Generate keyvalalgorithm=options.algorithm?:KeyAlgorithm.Ed25519valkey=kms.generateKey(algorithm.toAlgorithm())// Create DID identifiervalidentifier=generateDidIdentifier(key)valdid="did:mydid:$identifier"// Build DID documentreturnDidDocument(id=did,verificationMethod=listOf(VerificationMethod(id="$did#key-1",type="Ed25519VerificationKey2020",controller=did,publicKeyJwk=key.publicKey)),authentication=listOf("$did#key-1"))}overridesuspendfunresolveDid(did:String):DidResolutionResult{// Resolve DID from your backendvaldocument=resolveFromBackend(did)returnDidResolutionResult(didDocument=document,metadata=DidResolutionMetadata(contentType="application/did+json"))}overridesuspendfunupdateDid(did:String,updater:(DidDocument)->DidDocument):DidDocument{// Resolve current documentvalcurrent=resolveDid(did).didDocument?:throwNotFoundException()// Apply updatevalupdated=updater(current)// Persist updatepersistToBackend(did,updated)returnupdated}overridesuspendfundeactivateDid(did:String):Boolean{// Deactivate DID in backendreturndeactivateInBackend(did)}privatefungenerateDidIdentifier(key:Key):String{// Generate identifier from keyreturn/* implementation */}privatesuspendfunresolveFromBackend(did:String):DidDocument?{// Resolve from your backendreturn/* implementation */}privatesuspendfunpersistToBackend(did:String,document:DidDocument){// Persist to your backend/* implementation */}privatesuspendfundeactivateInBackend(did:String):Boolean{// Deactivate in your backendreturn/* implementation */}}
importcom.trustweave.did.spi.DidMethodProviderimportcom.trustweave.did.*classMyDidMethodProvider:DidMethodProvider{overridevalname:String="my-did-method"overridevalsupportedMethods:List<String>=listOf("mydid")overridefuncreate(method:String,options:DidCreationOptions):DidMethod?{if(method!="mydid")returnnull// Create KMSvalkms=createKms(options)// Create config from optionsvalconfig=MyDidConfig.fromOptions(options)returnMyCustomDidMethod(kms,config)}privatefuncreateKms(options:DidCreationOptions):KeyManagementService{// Create or get KMS instancereturn/* implementation */}}
Registering Provider
Create service file at src/main/resources/META-INF/services/com.trustweave.did.spi.DidMethodProvider:
importcom.trustweave.anchor.*classMyBlockchainAnchorClient(overridevalchainId:String,privatevalconfig:MyBlockchainConfig):BlockchainAnchorClient{overridesuspendfunwritePayload(payload:ByteArray):AnchorResult{// Submit transaction to blockchainvaltxHash=submitTransaction(payload)// Wait for confirmationvalblockHeight=waitForConfirmation(txHash)returnAnchorResult(anchorRef=AnchorRef(chainId=chainId,transactionHash=txHash,metadata=mapOf("blockHeight"toblockHeight,"timestamp"toSystem.currentTimeMillis())),payload=payload)}overridesuspendfunreadPayload(anchorRef:AnchorRef):ByteArray?{// Read transaction from blockchainreturnreadTransaction(anchorRef.transactionHash)}privatesuspendfunsubmitTransaction(payload:ByteArray):String{// Submit to blockchainreturn/* implementation */}privatesuspendfunwaitForConfirmation(txHash:String):Long{// Wait for confirmationreturn/* implementation */}privatesuspendfunreadTransaction(txHash:String):ByteArray?{// Read from blockchainreturn/* implementation */}}
importcom.trustweave.kms.*classMyKeyManagementService(privatevalconfig:MyKmsConfig):KeyManagementService{overridesuspendfungenerateKey(algorithm:Algorithm):Key{// Generate key in your KMSvalkeyId=generateKeyInKms(algorithm)valpublicKey=getPublicKey(keyId)returnKey(id=keyId,algorithm=algorithm,publicKey=publicKey)}overridesuspendfunsign(keyId:String,data:ByteArray):ByteArray{// Sign data using your KMSreturnsignWithKms(keyId,data)}overridesuspendfungetPublicKey(keyId:String):PublicKey{// Get public key from your KMSreturngetPublicKeyFromKms(keyId)}overridesuspendfundeleteKey(keyId:String):Boolean{// Delete key from your KMSreturndeleteKeyFromKms(keyId)}privatesuspendfungenerateKeyInKms(algorithm:Algorithm):String{// Generate in your KMSreturn/* implementation */}privatesuspendfunsignWithKms(keyId:String,data:ByteArray):ByteArray{// Sign with your KMSreturn/* implementation */}privatesuspendfungetPublicKeyFromKms(keyId:String):PublicKey{// Get from your KMSreturn/* implementation */}privatesuspendfundeleteKeyFromKms(keyId:String):Boolean{// Delete from your KMSreturn/* implementation */}}