-
Notifications
You must be signed in to change notification settings - Fork 3
Verifiable Credentials Step By Step
How to generate Verifiable Credentials to assert privilages.
Firstly generate a ed25519 key-pair which can be found at Transmute DID Key:
import * as ed25519 from '@transmute/did-key-ed25519';
import crypto from 'crypto';
const seed = crypto.randomBytes(32).toString('hex');
let key = await Ed25519KeyPair.generate({
secureRandom: () => {
return Buffer.from(seed, 'hex');
},
});Then you generate a DID document using the key-pair generated above. In order to generate the document, you first have to install npm i @transmute/did-key-common@latest --save and then pass the key generated above to the keyToDidDoc function.
import crypto from 'crypto';
import * as ed25519 from '@transmute/did-key-ed25519';
import { keyToDidDoc } from '@transmute/did-key-common';
const seed = crypto.randomBytes(32).toString('hex');
let ed25519Key = await Ed25519KeyPair.generate({
secureRandom: () => {
return Buffer.from(seed, 'hex');
},
});
const diddoc = await keyToDidDoc(ed25519Key);If you consoled the diddoc object, you will see something similar to the following:
{
'@context': [
'https://www.w3.org/ns/did/v1',
{
'@base': 'did:key:z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y'
}
],
id: 'did:key:z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y',
verificationMethod: [
{
id: '#z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y',
type: 'Ed25519VerificationKey2018',
controller: 'did:key:z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y',
publicKeyBase58: 'BuaohKYEHLtSQoCSyxB5NgvxgH7gvF6N9xub9K4ZDDJb'
},
{
id: '#z6LSku5fknwx7iJCvPG4n5KgCv9Gu5xVLP84AEM49nMX2pJy',
type: 'X25519KeyAgreementKey2019',
controller: 'did:key:z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y',
publicKeyBase58: 'ADuWEV962FaTpztJFRoitKvo3wRNdmwuHFdNfKhzKSYD'
}
],
authentication: [ '#z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y' ],
assertionMethod: [ '#z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y' ],
capabilityInvocation: [ '#z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y' ],
capabilityDelegation: [ '#z6MkqMqrHZnfctNuXJ39fX8vDnUxVrPYL8LiqypWyb2a8S5y' ],
keyAgreement: [ '#z6LSku5fknwx7iJCvPG4n5KgCv9Gu5xVLP84AEM49nMX2pJy' ]
}The did doc is something that can be stored somewhere. In most scenario, the did doc is something that the actor will generate and then give to a credentialing authority that will use it to generate a verifiable credential. This credential can then be used to 'prove' the credential holder is in custody of the key material used to generate the DID document.
First thing you need to do is install npm install @transmute/ed25519-signature-2018 --save and npm install @transmute/vc.js --save. As the credentialing authority (perhaps a server of some sort), you have to do the following to generate a Verifiable Credential:
const cred = {
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"id": "http://example.gov/credentials/3732",
"type": ["VerifiableCredential", "UniversityDegreeCredential"],
"issuer": { "id": "did:example:123" },
"issuanceDate": "2020-03-10T04:24:12.164Z",
"credentialSubject": {
"id": "did:example:456",
"degree": {
"type": "BachelorDegree",
"name": "Bachelor of Science and Arts"
}
}
};
key.id = key.controller + key.id;
const credential = {
...cred,
issuer: { id: didDoc.id },
credentialSubject: {
...cred.credentialSubject,
id: didDoc.id,
},
};