Quickstart
Get started with KILT by following this guide, which teaches you to:
- Import the KILT SDK into your project
- Connect to the KILT blockchain
- Query a web3name to retrieve its DID
- Verify a credential using a DID service
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
.
- Typescript
- Javascript
Inside the kilt-rocks
project directory, install the KILT SDK, Typescript, ts-node, and Axios dependencies:
- npm
- Yarn
- pnpm
npm init -y
npm install @kiltprotocol/sdk-js ts-node typescript axios
yarn init -y
yarn add @kiltprotocol/sdk-js ts-node typescript axios
pnpm init -y
pnpm add @kiltprotocol/sdk-js ts-node typescript axios
With the required dependencies installed, create a TypeScript file with touch quickstart.ts
.
From inside the kilt-rocks
project directory, install the KILT SDK, Node, and Axios dependencies:
- npm
- Yarn
- pnpm
npm init -y
npm install @kiltprotocol/sdk-js node axios
yarn init -y
yarn add @kiltprotocol/sdk-js node axios
pnpm init -y
pnpm add @kiltprotocol/sdk-js node axios
With the required dependencies installed, create a JavaScript file with touch quickstart.js
.
To enable ES modules in your project, add "type": "module"
to the package.json
file.
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 (Testnet)
- Spiritnet (Production)
Peregrine is the development blockchain. Connect to this network for testing and development purposes.
let api = await Kilt.connect('wss://peregrine.kilt.io/')
Spiritnet is the production blockchain. When you are ready to publish your DApp, connect to the Spiritnet network for production purposes.
let api = await Kilt.connect('wss://spiritnet.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.
- Typescript
- Javascript
yarn ts-node quickstart.ts
node quickstart.js
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 encodedKiltnerd123Details =
await apiConfig.call.did.queryByWeb3Name('kiltnerd123')
// This function will throw if kiltnerd123 does not exist
const {
document: { uri }
} = Kilt.Did.linkedInfoFromChain(encodedKiltnerd123Details)
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 kiltnerd123'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 kiltnerd123DidDocument = await Kilt.Did.resolve(uri)
console.log(`kiltnerd123's DID Document:`)
console.log(JSON.stringify(kiltnerd123DidDocument, null, 2))
const endpoints = kiltnerd123DidDocument?.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()
:
- Typescript
- Javascript
const {
data: [{ credential }]
} = await axios.get<Kilt.KiltPublishedCredentialCollectionV1>(
endpoints[0].serviceEndpoint[0]
)
console.log(`Credentials: ${JSON.stringify(credential, null, 2)}`)
# loading code...
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(
`kiltnerd123's credential is valid and has been attested by ${attester}!`
)
} catch {
console.log("kiltnerd123's credential is not valid.")
}
Run the code and wait to see if you can retrieve and verify one of kiltnerd123's credentials!
- If you want to explore more of KILT's features, read our Concepts section.
- If you want to dive deeper into the SDK, read the next section, the KILT Cookbook.