Skip to main content

Credential Issuance

As for traditional KILT credentials, public credentials also have their structure defined by a CType, although CTypes that can be used to represent information about assets would probably differ from the ones used to represent information about people.

As mentioned in the section about credentials, the creation of a CType in KILT involves two steps: the definition of a CType and the anchoring of its hash on the KILT blockchain.

We will not cover the creation of a CType, please refer to the CType creation

Create and Issue the Credential

Using the existing CType, the new public credential object can be created with the actual content, and then written to the chain for the rest of the KILT users (and beyond) to consume.

Creating a public credential is as simple as creating an object that conforms to the required structure of the CType:

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

// CType definition.
const ctype = Kilt.CType.fromProperties(`NFT Collection Certification CType`, {
name: {
type: 'string'
},
pieces: {
type: 'integer'
},
creationDate: {
type: 'string'
},
artistIdentity: {
type: 'string'
}
})

export function createNftCollectionCredential(
assetDid: Kilt.AssetDidUri,
artistDid: Kilt.DidUri
): Kilt.IPublicCredentialInput {
const claimProperties: Kilt.IClaimContents = {
name: 'Awesome NFT drop',
// NFT collection only has 100 pieces in total
pieces: 100,
// NFT collection was released on Jan 1st, 2023
creationDate: new Date(2023, 0, 1).toISOString(),
artistIdentity: artistDid
}
const fullClaim: Kilt.IAssetClaim = {
contents: claimProperties,
cTypeHash: Kilt.CType.idToHash(ctype.$id),
subject: assetDid
}

return Kilt.PublicCredential.fromClaim(fullClaim)
}
note

The creation of the credential object does not require any interaction with the blockchain per se. This also means that, until the object is written to the blockchain (see below), it cannot be used/retrieved/verified by anyone else, so it is, by all means, not existing.

Once the credential object is created, it must be written to the blockchain for other people to be able to use it.

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

export async function issueCredential(
attester: Kilt.DidUri,
submitterAccount: Kilt.KiltKeyringPair,
signCallback: Kilt.SignExtrinsicCallback,
credential: Kilt.IPublicCredentialInput
): Promise<void> {
const api = Kilt.ConfigService.get('api')

const credentialCreationTx = api.tx.publicCredentials.add(
Kilt.PublicCredential.toChain(credential)
)

// Same as for traditional KILT credentials
const authorizedAttestationTx = await Kilt.Did.authorizeTx(
attester,
credentialCreationTx,
signCallback,
submitterAccount.address
)
await Kilt.Blockchain.signAndSubmitTx(
authorizedAttestationTx,
submitterAccount
)
}
Credential has to be CBOR-encoded!

Given a public credential object, the SDK internally CBOR-encodes it before firing the extrinsic to the blockchain! This is to save space on credentials that actually benefit from CBOR compression (e.g., if they contain a lot of binary information). Hence, creating public credentials without the SDK requires the credential to be CBOR-encoded!