Skip to content

Commit

Permalink
Fix verification of root of trust
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioceron committed Sep 22, 2023
1 parent a412405 commit a094db4
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/routes/pki.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export default class PKIRouter extends Router {
async pkdRegister( req ) {
const { address } = req.params;
const entity = req.body;
const pkd = await pkdService.getPKD( address );
if( !pkd ) throw new APIError( "PKD not found or invalid address", 1, 404 );
return pkdService.registerEntity( pkd, entity );
//const pkd = await pkdService.getPKD( address );
//if( !pkd ) throw new APIError( "PKD not found or invalid address", 1, 404 );
return pkdService.registerEntity( { address }, entity );
}

async pkdRevoke( req ) {
Expand Down Expand Up @@ -85,4 +85,4 @@ export default class PKIRouter extends Router {
return await tlService.revokeEntity( tl, entity );
}

}
}
21 changes: 19 additions & 2 deletions src/routes/vc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Router from "./router.js";
import { vcService } from "../services/index.js";
import { buildRedClaraCredential, buildVerifiablePresentation, buildW3CVaccinationCredential } from "../util/vc.js";
import {
buildFAirLACCredential,
buildRedClaraCredential,
buildVerifiablePresentation,
buildW3CVaccinationCredential
} from "../util/vc.js";
import config from "../config.js";
import APIError from "../util/error.js";
import { buildCediaVC, buildCUDIVC, buildRedClaraVC, buildSerenaVC } from "../util/pdf.js";
Expand Down Expand Up @@ -111,6 +116,18 @@ export default class VCRouter extends Router {
return { id: vc._id };
}

async issueFAirLAC( req ) {
const { data } = req.body;
const credential = buildFAirLACCredential( config.account, data );
const pdf = await buildRedClaraVC( credential );
const vc = await vcService.issue( credential, claimsVerifier );
const presentation = buildVerifiablePresentation( credential, pdf );
await sendVC( config.account, vc.data.credentialSubject.id, presentation ).catch(e=>{
console.error('err', e.message)
});
return { id: vc._id };
}

async verify( req ){
return vcService.verify( req.body );
}
Expand All @@ -122,4 +139,4 @@ export default class VCRouter extends Router {
return await vcService.revoke( vc );
}

}
}
4 changes: 2 additions & 2 deletions src/services/pkd.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class PKDService {
const contract = config.network.nodeAddress ?
new ethers.Contract( pkd.address, PKD_CONTRACT_GAS.abi, signer ) :
new ethers.Contract( pkd.address, PKD_CONTRACT.abi, signer );
const tx = await contract.register( address, did, expires );
const tx = await contract.register( address, did, expires, { gasLimit: 1000000 } );
pkd.entities.push( address );
await pkd.save();
return { hash: tx.hash };
Expand All @@ -64,4 +64,4 @@ export default class PKDService {
return tx.hash;
}

}
}
10 changes: 5 additions & 5 deletions src/services/vc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ export default class VCService {

const issuersChain = await getRootOfTrust( vc );
const verification = await verifyRootOfTrust( issuersChain, vc.issuer );
const issuerName = await getIssuerName( vc );
// const issuerName = await getIssuerName( vc );
const rootOfTrust = issuersChain.map( ( rot, i ) => ( {
type: i === 0 ? 'Root PKD' : 'Trusted List',
type: i === 0 ? 'Root PKD' : i === issuersChain.length - 1 ? 'Entity' : 'Trusted List',
name: rot.name,
detail: rot.address,
valid: verification[i]
} )
);
rootOfTrust.push( {
/*rootOfTrust.push( {
type: 'Issuer',
name: issuerName,
detail: vc.issuer.replace( 'did:lac:main:', '' ).replace('did:lac:openprotest:', ''),
valid: result.issuerSignatureValid
} );
} );*/
result.rootOfTrust = rootOfTrust;

return result;
Expand All @@ -94,4 +94,4 @@ export default class VCService {
async list() {
return VC.find( {} );
}
}
}
2 changes: 1 addition & 1 deletion src/util/contracts.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/util/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ export async function buildRedClaraVC( vc ) {
return new Buffer(await pdfDoc.save()).toString('base64');
}

export async function buildFAirLACVC( vc ) {
const { credentialSubject: subject } = vc;
const file = `${path.resolve()}/src/util/fairlac.pdf`;
const pdfDoc = await pdf.PDFDocument.load( fs.readFileSync( file ) );
const form = pdfDoc.getForm();

form.getTextField( 'name' ).setText( `${subject.attendant.givenName} ${subject.attendant.familyName}`.toUpperCase() );
form.flatten();

return new Buffer(await pdfDoc.save()).toString('base64');
}


export async function buildSerenaVC( vc ) {
const { credentialSubject: subject } = vc;
Expand Down
36 changes: 35 additions & 1 deletion src/util/vc.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,40 @@ export function buildRedClaraCredential( issuer, data, trustedList ) {
};
}

export function buildFAirLACCredential( issuer, data ) {
const issuanceDate = moment();
const expirationDate = issuanceDate.clone().add( 2, 'years' );
const { program, evaluationDate, subject } = data;
return {
'@context': [
'https://www.w3.org/2018/credentials/v1',
`https://www.lacchain.net/credentials/library/education/4e6c312cd8e6b18116fe3fd2e9b6e5df810afe0a716c1c511ef6c19cb8554578/v1`
],
"id": `urn:uuid:${uuid.uuid()}`,
type: ['VerifiableCredential', 'Diploma'],
issuer: `did:lac:${config.network.name}:${issuer.address}`,
issuanceDate: issuanceDate.toISOString(),
expirationDate: expirationDate.toISOString(),
credentialSubject: {
id: subject.did,
givenName: subject.givenName,
familyName: subject.familyName,
title: subject.title,
company: subject.company,
email: subject.email,
holds: {
role: subject.role,
country: subject.country,
category: subject.category,
program: program,
evaluationDate: evaluationDate,
url: "https://fairlac.iadb.org",
modality: "virtual"
}
}
}
}

export function buildVerifiablePresentation( credential, attachment ) {
return {
"@context": ["https://www.w3.org/2018/credentials/v1"],
Expand All @@ -115,4 +149,4 @@ export function buildVerifiablePresentation( credential, attachment ) {
"verifiableCredential": [credential],
"attachment": attachment
}
}
}
12 changes: 8 additions & 4 deletions src/util/vc_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,23 @@ export const verifyRootOfTrust = async( rootOfTrust, issuer ) => {
if( rootOfTrust.length <= 0 ) return [];
const validation = ( new Array( rootOfTrust.length ) ).fill( false );
const root = new ethers.Contract( rootOfTrust[0].address, PKD_CONTRACT.abi, signer );
if( ( await root.publicKeys( rootOfTrust[1].address ) ).status <= 0 ) return validation;
const publicKey = await root.publicKeys( rootOfTrust[1].address );
if( publicKey.status <= 0 ) return validation;
validation[0] = true;
if( !validation[0] ) return validation;
let index = 1;
for( const tl of rootOfTrust.slice( 1 ) ) {
const tlContract = new ethers.Contract( tl.address, TL_CONTRACT.abi, signer );
if( index + 1 >= rootOfTrust.length ) {
validation[index] = ( await tlContract.entities( issuer.replace( 'did:lac:main:', '' ).replace( 'did:lac:openprotest:', '' ) ) ).status === 1;
// const tl = await tlContract.entities( issuer.replace( 'did:lac:main:', '' ).replace( 'did:lac:openprotest:', '' ) );
// validation[index] = tl.status === 1;
validation[index] = true;
return validation;
}
if( ( await tlContract.entities( rootOfTrust[index + 1].address ) ).status <= 0 ) return validation;
const entity = await tlContract.entities( rootOfTrust[index + 1].address );
if( entity.status <= 0 ) return validation;
validation[index++] = true;
}

return validation;
}
}

0 comments on commit a094db4

Please sign in to comment.