Credentials
Depending on the configuration of the Agent that has been created, credentials will be auto-accepted and added to the wallet or they will have to be manually accepted. Credentials are generally offered by an external Agent upon connection or other business action; in some rare cases the receiving Agent will request a credential.
If credentials are not auto accepted, you can use the credential events to determine when a credential has been offered.
Accepting an offered credential
The below code accepts the offer coming from an external issuing Agent. The issuer will then send a credential issuance message that needs final confirmation before the credential is saved and the issuer is notified of completion of issuance.
val credEvents = agent.events.getCredentialEvents()!! // Assuming agent initialized normally
val offers = credEvents.events.filter {
it.credentialExchangeRecord.state == CredentialState.OfferReceived
}
offers.onEach{
// present to user
if(userAccept)
agent.credentials.acceptOffer(it.credentialExchangeRecord.id)
}.collect()agent.events.registerCredentialHandler((event) => {
if(event.credentialExchangeRecord.state === CredentialState.OfferReceived)
// Present to user somehow
if(userAccept)
await agent.credentials.acceptOffer(event.credentialExchangeRecord.id)
})Confirming receipt of the issued credential
After the issuer sends the credential issuance message, your Agent must confirm that it accepts the issued credential. This second confirmation ensures the credential you were offered matches what you actually received. Accepting notifies the issuing Agent that the credential has been stored and the transaction should be recorded on the ledger.
val receivedCredentials = credEvents.events.filter{
it.credentialExchangeRecord.state == CredentialState.CredentialReceived
}
receivedCredentials.onEach{
val attributes = it.credentialExchangeRecord.attributes
// User reviews the attributes of the credential
if(userAccept)
agent.credentials.acceptCredential(it.credentialExchangeRecord.id)
else
agent.credentials.rejectCredential(it.credentialExchangeRecord.id)
}.collect()agent.events.registerCredentialHandler((event) => {
const attributes = event.credentialExchangeRecord.attributes
// Have user review credential
if(userAccept)
await agent.credentials.acceptCredential(event.credentialExchangeRecord.id)
else
await agent.credentials.rejectCredential(event.credentialExchangeRecord.id)
})Accessing stored credentials
Once the credential has been accepted it is stored in the wallet. The credentials module on the Agent has several accessor functions to get credential exchange records. The records have the previewed attributes and after being accepted will have a populated credential attribute that contains information about the accepted credential (for example: credential definition id or schema id).
val record = agent.credentials.findByRecordId("Some id")const record = await agent.credential.findByRecordId("Some id")Last updated
Was this helpful?