As we explored in the previous section, Self provides a simplified interface for handling Verifiable Credentials, taking care of the complex underlying processes.
Now, let's delve into one of the core functionalities provided by Self - Credential Presentation Requests.
This key operation allows you to access and validate the credentials you need, all while maintaining the robust security and privacy standards inherent in the Verifiable Credentials system.
err=selfAccount.MessageSend(responderAddress,content)iferr!=nil{log.Fatal("failed to send credential request message","error",err)}// Store the presentation completer in a map to handle the async responsepresentationCompleter:=make(chan*message.CredentialPresentationResponse,1)requests.Store(hex.EncodeToString(content.ID()),presentationCompleter,)
response:=<-presentationCompleter// Validate the presentationsfor_,p:=rangeresponse.Presentations(){err=p.Validate()iferr!=nil{log.Warn("failed to validate presentation","error",err)continue}// Check the presentation references the address we are communicating withif!p.Holder().Address().Matches(responderAddress){log.Warn("received a presentation response for a different holder address")continue}// Process credentials...}
ContentType.CREDENTIAL_PRESENTATION_RESPONSE->{valcredentialResponse=CredentialPresentationResponse.decode(content)println("Response received with status:${credentialResponse.status().name}")credentialResponse.presentations().forEach{presentation->valcredentials=presentation.credentials()credentials.forEach{credential->valclaims=credential.credentialSubjectClaims()claims.forEach{println("credential value"+"\ncredentialType:${credential.credentialType()}"+"\nfield:${it.key}"+"\nvalue:${it.value}")}}}}
account.setOnResponseListener{msg->when(msg){isCredentialResponse->{println("Response received with status:${msg.status().name}")msg.credentials().forEach{credential->println("crendential types: ${credential.types()}")credential.claims().forEach{claim->println("claim ${claim.subject()}:${claim.value()}")}}}}}
account.setOnResponseListener{messageinprint("setOnResponseListener: \(message)")switchmessage{caseisCredentialResponse:letresponse=messageas!CredentialResponseprint("Handle credential response: \(response)")default:print("TODO: Handle For other response: \(message)")break;}}
for_,c:=rangep.Credentials(){// Validate the credentialerr=c.Validate()// Check if the credential is currently validifc.ValidFrom().After(time.Now()){log.Warn("credential is intended to be used in the future")continue}// Additional checks for non-key-based issuersifc.Issuer().Method()!=credential.MethodKey{// Verify the issuer's validity at the time of issuancedocument,err:=selfAccount.IdentityResolve(c.Issuer().Address())if!document.HasRolesAt(c.Issuer().SigningKey(),identity.RoleAssertion,c.Created()){log.Warn("credential signing key was not valid at the time of issuance")continue}}// Extract and process the claimsclaims,err:=c.CredentialSubjectClaims()fork,v:=rangeclaims{log.Info("credential value","credentialType",c.CredentialType(),"field",k,"value",v,)}}
This process ensures that: 1. The presentation and credentials are cryptographically valid. 2. The credentials are from the expected user. 3. The credentials are currently valid (not future-dated). 4. For non-key-based issuers, the issuer was authorized to create the credential at the time of issuance. 5. The actual claim values are extracted and can be used in your application.
By following these steps, you can securely request, receive, and verify credentials from users, enabling trust in the information provided without needing to store sensitive data yourself.
Implementing Verifiable Credentials using Self's SDK provides a robust and secure way to handle sensitive user information. This approach offers several benefits:
Enhanced Security: Cryptographic validation ensures the integrity and authenticity of credentials.
Privacy Preservation: You can verify claims without storing sensitive user data.
Flexibility: The system can handle various types of credentials and claims.
Compliance: Adheres to W3C standards, ensuring interoperability and future-proofing.