Skip to main content

Request an Attestation

In this section, we'll create a Claim and a Credential. But a credential in itself has no value. To become valid in the eyes of Verifiers, it needs to be attested by an entity they can trust: an Attester.

info

KILT is an open system. Anyone/anything can make a claim about themselves and attest it. But a credential only has value if the Verifier trusts the Attester.

Create Credentialโ€‹

We'll use provided light DID, ctype and Claimer provided content to generate the Claim object.

A claim is composed of attributes that we claim to be true about us.

claimer/createClaim.ts
import * as Kilt from '@kiltprotocol/sdk-js'

// Create a Claim object from light DID, CType and given content.
export function createClaim(
lightDid: Kilt.DidUri,
ctype: Kilt.ICType,
content: Kilt.IClaim['contents']
): Kilt.IClaim {
const claim = Kilt.Claim.fromCTypeAndClaimContents(ctype, content, lightDid)

return claim
}

Since we want to receive an attestation for those claims, we build a Credential, which happens in the generateCredential function below. The credential contains all necessary information, so that the Attester can attest it for us.

The main function puts it all together. There we load our light DID, create a claim and finally the credential.

claimer/generateCredential.ts
import { config as envConfig } from 'dotenv'

import * as Kilt from '@kiltprotocol/sdk-js'

import { createClaim } from './createClaim'
import { generateLightDid } from './generateLightDid'
import { getCtypeSchema } from '../attester/ctypeSchema'

export function generateCredential(
claimerDid: Kilt.DidUri,
claimAttributes: Kilt.IClaim['contents']
): Kilt.ICredential {
// Create claim.
const ctype = getCtypeSchema()
const claim = createClaim(claimerDid, ctype, claimAttributes)

// Create credential and request attestation.
console.log('Claimer -> create request')
return Kilt.Credential.fromClaim(claim)
}

// Don't execute if this is imported by another file.
if (require.main === module) {
;(async () => {
envConfig()

try {
await Kilt.init()

const claimerDidMnemonic = process.env.CLAIMER_DID_MNEMONIC as string
const claimerDid = generateLightDid(claimerDidMnemonic)

const request = generateCredential(claimerDid.uri, {
age: 28,
name: 'Max Mustermann'
})
console.log(
'โš ๏ธ save this to ./claimer/_credential.json for testing โš ๏ธ\n\n'
)
console.log(JSON.stringify(request, null, 2))
} catch (e) {
console.log('Error while building credential')
throw e
}
})()
}

When Attestations are issued by Attesters, they are written to chain which requires a deposit. Each new Credential is unique. While we're testing, we can store and reuse credentials to avoid multiple attestations. To do this store the output into ./claimer/_credential.json. You can also share this credential with others in the workshop to see how they get denied from fraudulent senders.

Runโ€‹

Run it from command line:

yarn ts-node claimer/generateCredential.ts

OK, you've made a claim as a Claimer and created a credential from it. Let's finish up our Attester and get the credential attested!