Skip to main content

๐Ÿ”– Attestation

In this section, you'll play the role of the attester:

  • You'll take a RequestForAttestation object;
  • Attest it;
  • Store the attestation on the chain (more specifically only its hash, we'll get to that);
  • Build the Credential object which will be send back to the claimer.

Create a file

All of the code for this step needs to go into this file.

Code: validate the RequestForAttestation object

In a real-life setup, as an attester you would directly receive a RequestForAttestation from a claimer.

In this tutorial, you can either:

  • Take the RequestForAttestation object you've generated in the previous step as a claimer;
  • Or if you're in a workshop, ask another participant to send you their RequestForAttestation object.

Create a new file reconstructRequestForAttestation.js and add the following code

To ensure the correct structure of the request for attestation.

const Kilt = require('@kiltprotocol/sdk-js')

function requestForAttestationReconstructed(signedRequestForAttestation) {
return Kilt.RequestForAttestation.fromRequest(signedRequestForAttestation)
}

module.exports.requestForAttestationReconstructed =
requestForAttestationReconstructed

Create a new file verifyRequest.js and add the following code

To check if the object is valid, you can check the data against the CTYPE and check if the signature is valid

const Kilt = require('@kiltprotocol/sdk-js')

async function verifyRequest(requestForAttestation) {
await Kilt.connect()

const isDataValid = requestForAttestation.verifyData()
const isSignatureValid = await requestForAttestation.verifySignature()
console.log('isDataValid: ', isDataValid)
console.log('isSignatureValid: ', isSignatureValid)

await Kilt.disconnect()

return isDataValid && isSignatureValid
}

module.exports.verifyRequest = verifyRequest

Code: create an Attestation

Create a new file attestation.js.

Now is time to interact with the chain, in order to store an attestation on there.

Add the following code to attestation.js

const Kilt = require('@kiltprotocol/sdk-js')

async function attestCredential(
attester,
attesterFullDid,
requestForAttestation,
keystore
) {
await Kilt.connect()

// build the attestation object
const attestation = Kilt.Attestation.fromRequestAndDid(
requestForAttestation,
attesterFullDid.details.did
)

if (await Kilt.Attestation.query(attestation.claimHash)) {
console.log('Attestation found on chain')

const credential = Kilt.Credential.fromRequestAndAttestation(
requestForAttestation,
attestation
)

// log the Credential so you can copy/send it back to the claimer
console.log('CredentialJSONString:\n', JSON.stringify(credential))

// disconnect from the chain
await Kilt.disconnect()
return credential
}

// store the attestation on chain
const tx = await attestation.store()
const authorizedTx = await attesterFullDid.details.authorizeExtrinsic(
tx,
keystore,
attester.address
)
await Kilt.BlockchainUtils.signAndSubmitTx(authorizedTx, attester, {
resolveOn: Kilt.BlockchainUtils.IS_FINALIZED,
})
console.log('Attestation saved on chain.')

// the attestation was successfully stored on the chain, so you can now create the credential object
const credential = Kilt.Credential.fromRequestAndAttestation(
requestForAttestation,
attestation
)
// log the Credential so you can copy/send it back to the claimer
console.log('CredentialJSONString:\n', JSON.stringify(credential))

// disconnect from the chain
await Kilt.disconnect()
console.log('Disconnected from KILT testnet')
return credential
}

module.exports.attestCredential = attestCredential

Import the following code to your main function inside the index.js.

Run

Run the code by running this command in your terminal, still within your kilt-rocks directory:

node index.js

You should see in your logs:

  • true and true if the signature and data are valid (they should be);
  • The block hash in which the transaction was finalized or returns the attestation if on-chain already;
  • The Credential object.

Copy the Credential object, you'll need it soon.

Your job as an attester is done: you've successfully attested a claim, written the attestation hash onto the chain, and prepared the Credential object for the claimer.