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 ProofID = require('@proofid/pid-ts-lib')
function requestForAttestationReconstructed(signedRequestForAttestation) {
return ProofID.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 ProofID = require('@proofid/pid-ts-lib')
async function verifyRequest(requestForAttestation) {
await ProofID.connect()
const isDataValid = requestForAttestation.verifyData()
const isSignatureValid = await requestForAttestation.verifySignature()
console.log('isDataValid: ', isDataValid)
console.log('isSignatureValid: ', isSignatureValid)
await ProofID.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 ProofID = require('@proofid/pid-ts-lib')
async function attestCredential(
attester,
attesterFullDid,
requestForAttestation,
keystore
) {
await ProofID.connect()
// build the attestation object
const attestation = ProofID.Attestation.fromRequestAndDid(
requestForAttestation,
attesterFullDid.details.did
)
if (await ProofID.Attestation.query(attestation.claimHash)) {
console.log('Attestation found on chain')
const credential = ProofID.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 ProofID.disconnect()
return credential
}
// store the attestation on chain
const tx = await attestation.store()
const authorizedTx = await attesterFullDid.details.authorizeExtrinsic(
tx,
keystore,
attester.address
)
await ProofID.BlockchainUtils.signAndSubmitTx(authorizedTx, attester, {
resolveOn: ProofID.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 = ProofID.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 ProofID.disconnect()
console.log('Disconnected from ProofID 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 pid-app
directory:
node index.js
You should see in your logs:
true
andtrue
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.