Skip to main content

Build DID Extrinsics

DID keys can be used to sign extrinsic. But not every extrinsic can be signed using a DID. The Spiritnet blockchain offers two types of extrinsics.

The first type can only be called using an account. We call them account extrinsic. The second callable type are DID extrinsics. They must be used for all KILT features like creating CTypes, issue attestations, etc. Since every extrinsic requires fees to be paid, this type needs to be wrapped inside an account extrinsic. Accounts hold balances and can therefore pay fees and provide deposits.

This document describes how to sign the DID extrinsics. The KILT SDK provides two functions for signing DID extrinsics. The first function signs a single extrinsic while the second one batches multiple extrinsics together.

Single extrinsics

To sign a single extrinsic, you need to provide:

import * as Kilt from '@kiltprotocol/sdk-js'
// Just a helper to get an extrinsic
import getExtrinsic from '../utils/getExtrinsic'

export async function signAndSubmitDidExtrinsic(
submitterAccount: Kilt.KiltKeyringPair,
fullDid: Kilt.DidUri,
signCallback: Kilt.SignExtrinsicCallback
): Promise<void> {
const extrinsic = getExtrinsic()

// This results in a DID-signed tx that can be signed and submitted to
// the KILT blockchain by the account authorized in this operation (the `submitterAccount`).
const didSignedExtrinsic = await Kilt.Did.authorizeTx(
fullDid,
extrinsic,
signCallback,
submitterAccount.address
)

// Wrap the DID extrinsic in an account extrinsic.
await Kilt.Blockchain.signAndSubmitTx(didSignedExtrinsic, submitterAccount)
}

Batch multiple extrinsics

Full DIDs can also be used to batch multiple extrinsics that require the signature of the DID. For instance, a batch could create multiple services with a single submission to the blockchain. This would save the user the time of generating one additional signature, as multiple extrinsics are batched and signed at once. The extrinsics are also submitted and executed in the same block. For more information, see the official Substrate documentation.

An example of a batch using the authorizeBatch is provided below.

import * as Kilt from '@kiltprotocol/sdk-js'
// Just a helper to get an extrinsic
import getExtrinsic from '../utils/getExtrinsic'

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

// Build two extrinsics
const extrinsic1 = getExtrinsic()
const extrinsic2 = getExtrinsic()

// Create the DID-signed batch.
const authorizedBatch = await Kilt.Did.authorizeBatch({
batchFunction: api.tx.utility.batchAll,
did: fullDid,
extrinsics: [extrinsic1, extrinsic2],
sign: signCallback,
submitter: submitterAccount.address
})

// Wrap the DID extrinsic in an account extrinsic.
await Kilt.Blockchain.signAndSubmitTx(authorizedBatch, submitterAccount)
}

DIDs have different keys that posses different capabilities. Each key can only be used to authorize a specific subset of extrinsics. If extrinsics are batched together that require different DID keys, the authorizeBatch function will call the sign callback multiple times.