Skip to main content

Quickstart

Get started with KILT by following this guide, which teaches you to:

  1. Import the KILT SDK into your project
  2. Connect to the KILT blockchain
  3. Query a web3name to retrieve its DID
  4. Verify a credential using a DID service
Prerequisites

This quickstart guide provides hands-on experience to enhance your understanding of KILT. Basic knowledge of JavaScript and command-line tools is recommended.

Setup

Create a new project and directory and move into the directory by running mkdir kilt-rocks && cd kilt-rocks.

Inside the kilt-rocks project directory, install the KILT SDK, Typescript, ts-node, and Axios dependencies:

npm init -y
npm install @kiltprotocol/sdk-js ts-node typescript axios

With the required dependencies installed, create a TypeScript file with touch quickstart.ts.

Declare an async main function in the quickstart.ts file that executes the rest of the code in this quickstart and call the main() function by default:

async function main() {
}

main()

With the setup completed, let's get started! 🔥

Import the KILT SDK

Begin by importing the KILT SDK and Axios at the top of the file:

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

Now, you can access the SDK and all its functionality. The next step is connecting to the KILT blockchain.

Connect to the KILT Blockchain

To perform operations that rely on the KILT blockchain, such as querying and verifying a credential, you must first connect to the KILT blockchain.

Within the main function, configure the SDK to connect to a KILT node using the Kilt.connect() method:

Peregrine is the development blockchain. Connect to this network for testing and development purposes.

let api = await Kilt.connect('wss://peregrine.kilt.io/')

To ensure proper cleanup, call the Kilt.disconnect() function at the bottom of the main() function. You should add all other code before this function call:

await Kilt.disconnect()

By adding await Kilt.disconnect(), you ensure that the connection to the blockchain node is properly closed when the script finishes executing, which helps maintain the integrity of your application and is a good practice to follow.

Run the code by calling the name of the file. If you set up everything correctly, you should see no output showing that your code connected to the KILT blockchain.

yarn ts-node quickstart.ts

As you add to the code in this file, you can always run it with the same command.

Congratulations! 🔥

You have connected to a KILT blockchain node. The next step is to start querying data from the blockchain.

Query a KILT Identity

The following code queries information related to a web3name (kiltnerd123) and uses it to retrieve the KILT DID linked to it.

Between the Kilt.connect() and Kilt.disconnect() lines, add the following code:

let apiConfig = Kilt.ConfigService.get('api')
const encodedJohnDoeDetails =
await apiConfig.call.did.queryByWeb3Name('kiltnerd123')

// This function will throw if johnDoeOwner does not exist
const {
document: { uri }
} = Kilt.Did.linkedInfoFromChain(encodedJohnDoeDetails)
console.log(`My name is kiltnerd123 and this is my DID: "${uri}"`)

Try running the code and check the result.

Did you get the DID? You now have kiltnerd123's DID. The next step is to see if kiltnerd123 has any publicly linked KILT credentials to retrieve and verify.

Retrieve and Verify a Credential

A KILT DID can expose services that allow external resources to be linked to the DID. KILT credentials represent one type of external resource.

You can retrieve the services attached to John Doe's DID and see if they link to any public credentials to query and verify.

Add the following code after the code you added in the previous step but before the await Kilt.disconnect(). It retrieves the services exposed by the DID found for kiltnerd123:

const johnDoeDidDocument = await Kilt.Did.resolve(uri)
console.log(`John Doe's DID Document:`)
console.log(JSON.stringify(johnDoeDidDocument, null, 2))

const endpoints = johnDoeDidDocument?.document?.service
if (!endpoints) {
console.log('No endpoints for the DID.')
return []
}

console.log('Endpoints:')
console.log(JSON.stringify(endpoints, null, 2))

The code should print endpoints as JSON.

The next step is to see if you can find a credential among them. You do this by selecting one of the endpoints and querying the URL to see if it returns a KILT credential collection as described in the KiltPublishedCredentialCollectionV1 specification.

Add the following code after the code you added in the previous step but before await Kilt.disconnect():

const {
data: [{ credential }]
} = await axios.get<Kilt.KiltPublishedCredentialCollectionV1>(
endpoints[0].serviceEndpoint[0]
)
console.log(`Credentials: ${JSON.stringify(credential, null, 2)}`)

If the script completes without errors, you retrieved the published credential using the URL specified in the service.

The next step is to make sure the credential is valid and has a valid structure.

The following code outputs a string depending on whether the credential is valid, revoked, or not valid. Add it before await Kilt.disconnect():

try {
const { attester, revoked } =
await Kilt.Credential.verifyCredential(credential)

// Verify that the credential is not revoked. Exception caught by the catch {} block below.
if (revoked) {
throw new Error('The credential has been revoked, hence it is not valid.')
}
console.log(
`John Doe's credential is valid and has been attested by ${attester}!`
)
} catch {
console.log("John Doe's credential is not valid.")
}

Run the code and wait to see if you can retrieve and verify one of John Doe's credentials!

Next steps