kikocis/gaiax-credentials-tool public
Changesets · Branches · Tags · Compare
| Changeset | a03137c1782172b46b5d0f48b73f9df711793526c9f593213f7092cb3a1e44ad |
|---|---|
| Author | Andrés García Mangas <andres.garcia@fundacionctic.org> · 2023-09-14 12:31 UTC |
| Committer | Andrés García Mangas <andres.garcia@fundacionctic.org> · 2023-09-14 12:31 UTC |
| Parents | bf1558263a74 |
7 files changed
deleted index.js +0 −323
| @@ -1,323 +0,0 @@ | ||
| 1 | -const jose = require("jose"); | |
| 2 | -const fs = require("node:fs/promises"); | |
| 3 | -const jsonld = require("jsonld"); | |
| 4 | -const crypto = require("node:crypto"); | |
| 5 | -const axios = require("axios"); | |
| 6 | -const chalk = require("chalk"); | |
| 7 | -const path = require("node:path"); | |
| 8 | - | |
| 9 | -const CMD_DID = "did"; | |
| 10 | -const CMD_VALIDATE = "validate"; | |
| 11 | -const ALGORITHM_RSASSA_PSS = "PS256"; | |
| 12 | -const ALGORITHM_URDNA2015 = "URDNA2015"; | |
| 13 | - | |
| 14 | -async function publicKeyMatchesCertificate(publicKeyJwk, certificatePem) { | |
| 15 | - try { | |
| 16 | - const pk = await jose.importJWK(publicKeyJwk); | |
| 17 | - const spki = await jose.exportSPKI(pk); | |
| 18 | - const x509 = await jose.importX509(certificatePem, ALGORITHM_RSASSA_PSS); | |
| 19 | - const spkiX509 = await jose.exportSPKI(x509); | |
| 20 | - return spki === spkiX509; | |
| 21 | - } catch (error) { | |
| 22 | - console.error(error); | |
| 23 | - throw new Error( | |
| 24 | - "Could not confirm X509 public key with certificate chain." | |
| 25 | - ); | |
| 26 | - } | |
| 27 | -} | |
| 28 | - | |
| 29 | -async function writeFile(filePath, obj) { | |
| 30 | - const data = JSON.stringify(obj, null, 2); | |
| 31 | - await fs.writeFile(filePath, data); | |
| 32 | -} | |
| 33 | - | |
| 34 | -function joinUrl(...parts) { | |
| 35 | - return parts.map((item) => item.trimEnd("/")).join("/"); | |
| 36 | -} | |
| 37 | - | |
| 38 | -function getIssuerDID() { | |
| 39 | - return process.env.DID_WEB_ID; | |
| 40 | -} | |
| 41 | - | |
| 42 | -function getParticipantUrl() { | |
| 43 | - return joinUrl(process.env.BASE_URL, process.env.FILENAME_PARTICIPANT); | |
| 44 | -} | |
| 45 | - | |
| 46 | -function getLegalRegistrationNumberUrl() { | |
| 47 | - return joinUrl(process.env.BASE_URL, process.env.FILENAME_LRN); | |
| 48 | -} | |
| 49 | - | |
| 50 | -function getTermsConditionsUrl() { | |
| 51 | - return joinUrl(process.env.BASE_URL, process.env.FILENAME_TC); | |
| 52 | -} | |
| 53 | - | |
| 54 | -async function createDIDFile() { | |
| 55 | - const certificatePem = await fs.readFile(process.env.PATH_CERTIFICATE, { | |
| 56 | - encoding: "utf8", | |
| 57 | - }); | |
| 58 | - | |
| 59 | - const x509 = await jose.importX509(certificatePem, ALGORITHM_RSASSA_PSS); | |
| 60 | - const publicKeyJwk = await jose.exportJWK(x509); | |
| 61 | - publicKeyJwk.alg = ALGORITHM_RSASSA_PSS; | |
| 62 | - publicKeyJwk.x5u = joinUrl(process.env.BASE_URL, process.env.FILENAME_X5U); | |
| 63 | - | |
| 64 | - // A sanity check to catch upstream errors in the Compliance API calls | |
| 65 | - if (!publicKeyMatchesCertificate(publicKeyJwk, certificatePem)) { | |
| 66 | - throw new Error("Public key does not match certificate"); | |
| 67 | - } | |
| 68 | - | |
| 69 | - const did = { | |
| 70 | - "@context": ["https://www.w3.org/ns/did/v1"], | |
| 71 | - id: process.env.DID_WEB_ID, | |
| 72 | - verificationMethod: [ | |
| 73 | - { | |
| 74 | - "@context": "https://w3c-ccg.github.io/lds-jws2020/contexts/v1/", | |
| 75 | - id: process.env.DID_WEB_ID, | |
| 76 | - type: "JsonWebKey2020", | |
| 77 | - publicKeyJwk, | |
| 78 | - }, | |
| 79 | - ], | |
| 80 | - assertionMethod: [`${process.env.DID_WEB_ID}#JWK2020`], | |
| 81 | - }; | |
| 82 | - | |
| 83 | - const filePath = path.join( | |
| 84 | - process.env.WEBSERVER_DIR, | |
| 85 | - process.env.FILENAME_DID | |
| 86 | - ); | |
| 87 | - | |
| 88 | - await writeFile(filePath, did); | |
| 89 | -} | |
| 90 | - | |
| 91 | -function sha256(input) { | |
| 92 | - return crypto.createHash("sha256").update(input).digest("hex"); | |
| 93 | -} | |
| 94 | - | |
| 95 | -async function canonize(doc) { | |
| 96 | - return await jsonld.canonize(doc, { | |
| 97 | - algorithm: ALGORITHM_URDNA2015, | |
| 98 | - }); | |
| 99 | -} | |
| 100 | - | |
| 101 | -async function sign(hash) { | |
| 102 | - const privkeyPem = await fs.readFile(process.env.PATH_PRIVATE_KEY, { | |
| 103 | - encoding: "utf8", | |
| 104 | - }); | |
| 105 | - | |
| 106 | - const rsaPrivateKey = await jose.importPKCS8( | |
| 107 | - privkeyPem, | |
| 108 | - ALGORITHM_RSASSA_PSS | |
| 109 | - ); | |
| 110 | - | |
| 111 | - const jws = await new jose.CompactSign(new TextEncoder().encode(hash)) | |
| 112 | - .setProtectedHeader({ | |
| 113 | - alg: ALGORITHM_RSASSA_PSS, | |
| 114 | - b64: false, | |
| 115 | - crit: ["b64"], | |
| 116 | - }) | |
| 117 | - .sign(rsaPrivateKey); | |
| 118 | - | |
| 119 | - return jws; | |
| 120 | -} | |
| 121 | - | |
| 122 | -async function createProof(doc) { | |
| 123 | - const canonized = await canonize(doc); | |
| 124 | - const hash = sha256(canonized); | |
| 125 | - const created = new Date().toISOString(); | |
| 126 | - | |
| 127 | - const proof = { | |
| 128 | - type: "JsonWebSignature2020", | |
| 129 | - created, | |
| 130 | - proofPurpose: "assertionMethod", | |
| 131 | - verificationMethod: `${process.env.DID_WEB_ID}#JWK2020`, | |
| 132 | - jws: await sign(hash), | |
| 133 | - }; | |
| 134 | - | |
| 135 | - return proof; | |
| 136 | -} | |
| 137 | - | |
| 138 | -async function buildParticipantVC() { | |
| 139 | - const issuanceDate = new Date().toISOString(); | |
| 140 | - | |
| 141 | - const doc = { | |
| 142 | - "@context": [ | |
| 143 | - "https://www.w3.org/2018/credentials/v1", | |
| 144 | - "https://w3id.org/security/suites/jws-2020/v1", | |
| 145 | - "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 146 | - ], | |
| 147 | - type: ["VerifiableCredential"], | |
| 148 | - id: getParticipantUrl(), | |
| 149 | - issuer: getIssuerDID(), | |
| 150 | - issuanceDate: issuanceDate, | |
| 151 | - credentialSubject: { | |
| 152 | - type: "gx:LegalParticipant", | |
| 153 | - "gx:legalName": process.env.LEGAL_NAME, | |
| 154 | - "gx:legalRegistrationNumber": { | |
| 155 | - id: getLegalRegistrationNumberUrl(), | |
| 156 | - }, | |
| 157 | - "gx:headquarterAddress": { | |
| 158 | - "gx:countrySubdivisionCode": process.env.COUNTRY_SUBDIVISION_CODE, | |
| 159 | - }, | |
| 160 | - "gx:legalAddress": { | |
| 161 | - "gx:countrySubdivisionCode": process.env.COUNTRY_SUBDIVISION_CODE, | |
| 162 | - }, | |
| 163 | - "gx-terms-and-conditions:gaiaxTermsAndConditions": | |
| 164 | - getTermsConditionsUrl(), | |
| 165 | - id: getParticipantUrl(), | |
| 166 | - }, | |
| 167 | - }; | |
| 168 | - | |
| 169 | - const proof = await createProof(doc); | |
| 170 | - Object.assign(doc, { proof }); | |
| 171 | - | |
| 172 | - const filePath = path.join( | |
| 173 | - process.env.WEBSERVER_DIR, | |
| 174 | - process.env.FILENAME_PARTICIPANT | |
| 175 | - ); | |
| 176 | - await writeFile(filePath, doc); | |
| 177 | - | |
| 178 | - return doc; | |
| 179 | -} | |
| 180 | - | |
| 181 | -async function buildLegalRegistrationNumberVC() { | |
| 182 | - const issuanceDate = new Date().toISOString(); | |
| 183 | - | |
| 184 | - const doc = { | |
| 185 | - "@context": [ | |
| 186 | - "https://www.w3.org/2018/credentials/v1", | |
| 187 | - "https://w3id.org/security/suites/jws-2020/v1", | |
| 188 | - ], | |
| 189 | - type: "VerifiableCredential", | |
| 190 | - id: getLegalRegistrationNumberUrl(), | |
| 191 | - issuer: getIssuerDID(), | |
| 192 | - issuanceDate: issuanceDate, | |
| 193 | - credentialSubject: { | |
| 194 | - id: getLegalRegistrationNumberUrl(), | |
| 195 | - "@context": | |
| 196 | - "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 197 | - type: "gx:legalRegistrationNumber", | |
| 198 | - "gx:vatID": process.env.VAT_ID, | |
| 199 | - "gx:vatID-countryCode": process.env.COUNTRY_CODE, | |
| 200 | - }, | |
| 201 | - }; | |
| 202 | - | |
| 203 | - const proof = await createProof(doc); | |
| 204 | - Object.assign(doc, { proof }); | |
| 205 | - | |
| 206 | - const filePath = path.join( | |
| 207 | - process.env.WEBSERVER_DIR, | |
| 208 | - process.env.FILENAME_LRN | |
| 209 | - ); | |
| 210 | - await writeFile(filePath, doc); | |
| 211 | - | |
| 212 | - return doc; | |
| 213 | -} | |
| 214 | - | |
| 215 | -async function buildTermsConditionsVC() { | |
| 216 | - const issuanceDate = new Date().toISOString(); | |
| 217 | - | |
| 218 | - const termsAndConditions = | |
| 219 | - "The PARTICIPANT signing the Self-Description agrees as follows:\n- " + | |
| 220 | - "to update its descriptions about any changes, " + | |
| 221 | - "be it technical, organizational, or legal - " + | |
| 222 | - "especially but not limited to contractual in regards to " + | |
| 223 | - "the indicated attributes present in the descriptions.\n\n" + | |
| 224 | - "The keypair used to sign Verifiable Credentials will be revoked " + | |
| 225 | - "where Gaia-X Association becomes aware of any inaccurate statements " + | |
| 226 | - "in regards to the claims which result in a non-compliance " + | |
| 227 | - "with the Trust Framework and policy rules defined " + | |
| 228 | - "in the Policy Rules and Labelling Document (PRLD)."; | |
| 229 | - | |
| 230 | - const doc = { | |
| 231 | - "@context": [ | |
| 232 | - "https://www.w3.org/2018/credentials/v1", | |
| 233 | - "https://w3id.org/security/suites/jws-2020/v1", | |
| 234 | - "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 235 | - ], | |
| 236 | - type: "VerifiableCredential", | |
| 237 | - id: getTermsConditionsUrl(), | |
| 238 | - issuer: getIssuerDID(), | |
| 239 | - issuanceDate: issuanceDate, | |
| 240 | - credentialSubject: { | |
| 241 | - "@context": | |
| 242 | - "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 243 | - type: "gx:GaiaXTermsAndConditions", | |
| 244 | - id: getTermsConditionsUrl(), | |
| 245 | - "gx:termsAndConditions": termsAndConditions, | |
| 246 | - }, | |
| 247 | - }; | |
| 248 | - | |
| 249 | - const proof = await createProof(doc); | |
| 250 | - Object.assign(doc, { proof }); | |
| 251 | - | |
| 252 | - const filePath = path.join( | |
| 253 | - process.env.WEBSERVER_DIR, | |
| 254 | - process.env.FILENAME_TC | |
| 255 | - ); | |
| 256 | - await writeFile(filePath, doc); | |
| 257 | - | |
| 258 | - return doc; | |
| 259 | -} | |
| 260 | - | |
| 261 | -async function requestCompliance() { | |
| 262 | - console.log("Building Participant Verifiable Credential"); | |
| 263 | - const vcParticipant = await buildParticipantVC(); | |
| 264 | - console.log(vcParticipant); | |
| 265 | - | |
| 266 | - console.log("Building Legal Registration Number Verifiable Credential"); | |
| 267 | - const vcLRN = await buildLegalRegistrationNumberVC(); | |
| 268 | - console.log(vcLRN); | |
| 269 | - | |
| 270 | - console.log("Building Terms and Conditions Verifiable Credential"); | |
| 271 | - const vcTC = await buildTermsConditionsVC(); | |
| 272 | - console.log(vcTC); | |
| 273 | - | |
| 274 | - const verifiablePresentation = { | |
| 275 | - "@context": "https://www.w3.org/2018/credentials/v1", | |
| 276 | - type: "VerifiablePresentation", | |
| 277 | - verifiableCredential: [vcParticipant, vcLRN, vcTC], | |
| 278 | - }; | |
| 279 | - | |
| 280 | - console.log("Sending Verifiable Presentation to Compliance API"); | |
| 281 | - console.log(`POST -> ${process.env.API_COMPLIANCE_CREDENTIAL_OFFER}`); | |
| 282 | - console.log(verifiablePresentation); | |
| 283 | - | |
| 284 | - try { | |
| 285 | - const res = await axios.post( | |
| 286 | - process.env.API_COMPLIANCE_CREDENTIAL_OFFER, | |
| 287 | - verifiablePresentation | |
| 288 | - ); | |
| 289 | - | |
| 290 | - console.log(chalk.green("✅ Compliance success")); | |
| 291 | - console.log(res.data); | |
| 292 | - | |
| 293 | - Object.assign(verifiablePresentation, { | |
| 294 | - verifiableCredential: [vcParticipant, vcLRN, vcTC, res.data], | |
| 295 | - }); | |
| 296 | - | |
| 297 | - const filePath = path.join( | |
| 298 | - process.env.WEBSERVER_DIR, | |
| 299 | - process.env.FILENAME_VP | |
| 300 | - ); | |
| 301 | - | |
| 302 | - console.log(`Writing resulting Verifiable Presentation to ${filePath}`); | |
| 303 | - | |
| 304 | - await writeFile(filePath, verifiablePresentation); | |
| 305 | - } catch (err) { | |
| 306 | - console.error(chalk.red("🔴 Compliance error")); | |
| 307 | - console.error(err.response.data); | |
| 308 | - } | |
| 309 | -} | |
| 310 | - | |
| 311 | -async function main() { | |
| 312 | - const subCommand = process.argv[2]; | |
| 313 | - | |
| 314 | - if (subCommand === CMD_DID) { | |
| 315 | - await createDIDFile(); | |
| 316 | - } else if (subCommand === CMD_VALIDATE) { | |
| 317 | - await requestCompliance(); | |
| 318 | - } else { | |
| 319 | - console.log("Unknown command"); | |
| 320 | - } | |
| 321 | -} | |
| 322 | - | |
| 323 | -main(); |
modified package-lock.json +9 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | "dependencies": { |
| 11 | 11 | "axios": "^1.5.0", |
| 12 | 12 | "chalk": "^4.1.2", |
| 13 | + "commander": "^11.0.0", | |
| 13 | 14 | "jose": "^4.14.4", |
| 14 | 15 | "jsonld": "^8.2.0" |
| 15 | 16 | } |
| @@ -125,6 +126,14 @@ | ||
| 125 | 126 | "node": ">= 0.8" |
| 126 | 127 | } |
| 127 | 128 | }, |
| 129 | + "node_modules/commander": { | |
| 130 | + "version": "11.0.0", | |
| 131 | + "resolved": "https://registry.npmjs.org/commander/-/commander-11.0.0.tgz", | |
| 132 | + "integrity": "sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==", | |
| 133 | + "engines": { | |
| 134 | + "node": ">=16" | |
| 135 | + } | |
| 136 | + }, | |
| 128 | 137 | "node_modules/data-uri-to-buffer": { |
| 129 | 138 | "version": "4.0.1", |
| 130 | 139 | "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", |
modified package.json +5 −3
| @@ -2,15 +2,17 @@ | ||
| 2 | 2 | "name": "gaiax-self-description-poc", |
| 3 | 3 | "private": true, |
| 4 | 4 | "version": "1.0.0", |
| 5 | + "type": "module", | |
| 5 | 6 | "scripts": { |
| 6 | - "build-did": "node index.js did", | |
| 7 | - "validate-compliance": "node index.js validate" | |
| 7 | + "build-did": "node ./src/index.js did", | |
| 8 | + "validate-compliance": "node ./src/index.js participant" | |
| 8 | 9 | }, |
| 9 | 10 | "author": "Andrés García Mangas <andres.garcia@fundacionctic.org>", |
| 10 | 11 | "dependencies": { |
| 11 | 12 | "axios": "^1.5.0", |
| 12 | 13 | "chalk": "^4.1.2", |
| 14 | + "commander": "^11.0.0", | |
| 13 | 15 | "jose": "^4.14.4", |
| 14 | 16 | "jsonld": "^8.2.0" |
| 15 | 17 | } |
| 16 | -} | |
| 18 | +} | |
| No newline at the end of the file |
added src/config.js +17 −0
| @@ -0,0 +1,17 @@ | ||
| 1 | +import { joinUrl } from "./utils.js"; | |
| 2 | + | |
| 3 | +export function getIssuerDID() { | |
| 4 | + return process.env.DID_WEB_ID; | |
| 5 | +} | |
| 6 | + | |
| 7 | +export function getParticipantUrl() { | |
| 8 | + return joinUrl(process.env.BASE_URL, process.env.FILENAME_PARTICIPANT); | |
| 9 | +} | |
| 10 | + | |
| 11 | +export function getLegalRegistrationNumberUrl() { | |
| 12 | + return joinUrl(process.env.BASE_URL, process.env.FILENAME_LRN); | |
| 13 | +} | |
| 14 | + | |
| 15 | +export function getTermsConditionsUrl() { | |
| 16 | + return joinUrl(process.env.BASE_URL, process.env.FILENAME_TC); | |
| 17 | +} |
added src/index.js +25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +import { Command } from "commander"; | |
| 2 | +import { writeDIDFile, writeParticipantCredentials } from "./participant.js"; | |
| 3 | + | |
| 4 | +const program = new Command(); | |
| 5 | + | |
| 6 | +program | |
| 7 | + .name("gaiax-credentials-cli") | |
| 8 | + .description( | |
| 9 | + "CLI to help in the process of building and signing Gaia-X credentials" | |
| 10 | + ) | |
| 11 | + .version("0.1.0"); | |
| 12 | + | |
| 13 | +program | |
| 14 | + .command("did") | |
| 15 | + .description( | |
| 16 | + "Build the DID document that represents the identity of the participant" | |
| 17 | + ) | |
| 18 | + .action(writeDIDFile); | |
| 19 | + | |
| 20 | +program | |
| 21 | + .command("participant") | |
| 22 | + .description("Build and sign the Verifiable Credentials of the participant") | |
| 23 | + .action(writeParticipantCredentials); | |
| 24 | + | |
| 25 | +program.parse(); |
added src/participant.js +232 −0
| @@ -0,0 +1,232 @@ | ||
| 1 | +import axios from "axios"; | |
| 2 | +import chalk from "chalk"; | |
| 3 | +import * as jose from "jose"; | |
| 4 | +import fs from "node:fs/promises"; | |
| 5 | +import path from "node:path"; | |
| 6 | +import { | |
| 7 | + getIssuerDID, | |
| 8 | + getLegalRegistrationNumberUrl, | |
| 9 | + getParticipantUrl, | |
| 10 | + getTermsConditionsUrl, | |
| 11 | +} from "./config.js"; | |
| 12 | +import { | |
| 13 | + ALGORITHM_RSASSA_PSS, | |
| 14 | + createProof, | |
| 15 | + joinUrl, | |
| 16 | + publicKeyMatchesCertificate, | |
| 17 | + writeFile, | |
| 18 | +} from "./utils.js"; | |
| 19 | + | |
| 20 | +export async function writeDIDFile() { | |
| 21 | + const certificatePem = await fs.readFile(process.env.PATH_CERTIFICATE, { | |
| 22 | + encoding: "utf8", | |
| 23 | + }); | |
| 24 | + | |
| 25 | + const x509 = await jose.importX509(certificatePem, ALGORITHM_RSASSA_PSS); | |
| 26 | + const publicKeyJwk = await jose.exportJWK(x509); | |
| 27 | + publicKeyJwk.alg = ALGORITHM_RSASSA_PSS; | |
| 28 | + publicKeyJwk.x5u = joinUrl(process.env.BASE_URL, process.env.FILENAME_X5U); | |
| 29 | + | |
| 30 | + // A sanity check to catch upstream errors in the Compliance API calls | |
| 31 | + if (!publicKeyMatchesCertificate(publicKeyJwk, certificatePem)) { | |
| 32 | + throw new Error("Public key does not match certificate"); | |
| 33 | + } | |
| 34 | + | |
| 35 | + const did = { | |
| 36 | + "@context": ["https://www.w3.org/ns/did/v1"], | |
| 37 | + id: process.env.DID_WEB_ID, | |
| 38 | + verificationMethod: [ | |
| 39 | + { | |
| 40 | + "@context": "https://w3c-ccg.github.io/lds-jws2020/contexts/v1/", | |
| 41 | + id: process.env.DID_WEB_ID, | |
| 42 | + type: "JsonWebKey2020", | |
| 43 | + publicKeyJwk, | |
| 44 | + }, | |
| 45 | + ], | |
| 46 | + assertionMethod: [`${process.env.DID_WEB_ID}#JWK2020`], | |
| 47 | + }; | |
| 48 | + | |
| 49 | + const filePath = path.join( | |
| 50 | + process.env.WEBSERVER_DIR, | |
| 51 | + process.env.FILENAME_DID | |
| 52 | + ); | |
| 53 | + | |
| 54 | + await writeFile(filePath, did); | |
| 55 | +} | |
| 56 | + | |
| 57 | +export async function buildParticipantVC() { | |
| 58 | + const issuanceDate = new Date().toISOString(); | |
| 59 | + | |
| 60 | + const doc = { | |
| 61 | + "@context": [ | |
| 62 | + "https://www.w3.org/2018/credentials/v1", | |
| 63 | + "https://w3id.org/security/suites/jws-2020/v1", | |
| 64 | + "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 65 | + ], | |
| 66 | + type: ["VerifiableCredential"], | |
| 67 | + id: getParticipantUrl(), | |
| 68 | + issuer: getIssuerDID(), | |
| 69 | + issuanceDate: issuanceDate, | |
| 70 | + credentialSubject: { | |
| 71 | + type: "gx:LegalParticipant", | |
| 72 | + "gx:legalName": process.env.LEGAL_NAME, | |
| 73 | + "gx:legalRegistrationNumber": { | |
| 74 | + id: getLegalRegistrationNumberUrl(), | |
| 75 | + }, | |
| 76 | + "gx:headquarterAddress": { | |
| 77 | + "gx:countrySubdivisionCode": process.env.COUNTRY_SUBDIVISION_CODE, | |
| 78 | + }, | |
| 79 | + "gx:legalAddress": { | |
| 80 | + "gx:countrySubdivisionCode": process.env.COUNTRY_SUBDIVISION_CODE, | |
| 81 | + }, | |
| 82 | + "gx-terms-and-conditions:gaiaxTermsAndConditions": | |
| 83 | + getTermsConditionsUrl(), | |
| 84 | + id: getParticipantUrl(), | |
| 85 | + }, | |
| 86 | + }; | |
| 87 | + | |
| 88 | + const proof = await createProof(doc); | |
| 89 | + Object.assign(doc, { proof }); | |
| 90 | + | |
| 91 | + const filePath = path.join( | |
| 92 | + process.env.WEBSERVER_DIR, | |
| 93 | + process.env.FILENAME_PARTICIPANT | |
| 94 | + ); | |
| 95 | + | |
| 96 | + await writeFile(filePath, doc); | |
| 97 | + | |
| 98 | + return doc; | |
| 99 | +} | |
| 100 | + | |
| 101 | +export async function buildLegalRegistrationNumberVC() { | |
| 102 | + const issuanceDate = new Date().toISOString(); | |
| 103 | + | |
| 104 | + const doc = { | |
| 105 | + "@context": [ | |
| 106 | + "https://www.w3.org/2018/credentials/v1", | |
| 107 | + "https://w3id.org/security/suites/jws-2020/v1", | |
| 108 | + ], | |
| 109 | + type: "VerifiableCredential", | |
| 110 | + id: getLegalRegistrationNumberUrl(), | |
| 111 | + issuer: getIssuerDID(), | |
| 112 | + issuanceDate: issuanceDate, | |
| 113 | + credentialSubject: { | |
| 114 | + id: getLegalRegistrationNumberUrl(), | |
| 115 | + "@context": | |
| 116 | + "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 117 | + type: "gx:legalRegistrationNumber", | |
| 118 | + "gx:vatID": process.env.VAT_ID, | |
| 119 | + "gx:vatID-countryCode": process.env.COUNTRY_CODE, | |
| 120 | + }, | |
| 121 | + }; | |
| 122 | + | |
| 123 | + const proof = await createProof(doc); | |
| 124 | + Object.assign(doc, { proof }); | |
| 125 | + | |
| 126 | + const filePath = path.join( | |
| 127 | + process.env.WEBSERVER_DIR, | |
| 128 | + process.env.FILENAME_LRN | |
| 129 | + ); | |
| 130 | + | |
| 131 | + await writeFile(filePath, doc); | |
| 132 | + | |
| 133 | + return doc; | |
| 134 | +} | |
| 135 | + | |
| 136 | +export async function buildTermsConditionsVC() { | |
| 137 | + const issuanceDate = new Date().toISOString(); | |
| 138 | + | |
| 139 | + const termsAndConditions = | |
| 140 | + "The PARTICIPANT signing the Self-Description agrees as follows:\n- " + | |
| 141 | + "to update its descriptions about any changes, " + | |
| 142 | + "be it technical, organizational, or legal - " + | |
| 143 | + "especially but not limited to contractual in regards to " + | |
| 144 | + "the indicated attributes present in the descriptions.\n\n" + | |
| 145 | + "The keypair used to sign Verifiable Credentials will be revoked " + | |
| 146 | + "where Gaia-X Association becomes aware of any inaccurate statements " + | |
| 147 | + "in regards to the claims which result in a non-compliance " + | |
| 148 | + "with the Trust Framework and policy rules defined " + | |
| 149 | + "in the Policy Rules and Labelling Document (PRLD)."; | |
| 150 | + | |
| 151 | + const doc = { | |
| 152 | + "@context": [ | |
| 153 | + "https://www.w3.org/2018/credentials/v1", | |
| 154 | + "https://w3id.org/security/suites/jws-2020/v1", | |
| 155 | + "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 156 | + ], | |
| 157 | + type: "VerifiableCredential", | |
| 158 | + id: getTermsConditionsUrl(), | |
| 159 | + issuer: getIssuerDID(), | |
| 160 | + issuanceDate: issuanceDate, | |
| 161 | + credentialSubject: { | |
| 162 | + "@context": | |
| 163 | + "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", | |
| 164 | + type: "gx:GaiaXTermsAndConditions", | |
| 165 | + id: getTermsConditionsUrl(), | |
| 166 | + "gx:termsAndConditions": termsAndConditions, | |
| 167 | + }, | |
| 168 | + }; | |
| 169 | + | |
| 170 | + const proof = await createProof(doc); | |
| 171 | + Object.assign(doc, { proof }); | |
| 172 | + | |
| 173 | + const filePath = path.join( | |
| 174 | + process.env.WEBSERVER_DIR, | |
| 175 | + process.env.FILENAME_TC | |
| 176 | + ); | |
| 177 | + | |
| 178 | + await writeFile(filePath, doc); | |
| 179 | + | |
| 180 | + return doc; | |
| 181 | +} | |
| 182 | + | |
| 183 | +export async function writeParticipantCredentials() { | |
| 184 | + console.log("Building Participant Verifiable Credential"); | |
| 185 | + const vcParticipant = await buildParticipantVC(); | |
| 186 | + console.log(vcParticipant); | |
| 187 | + | |
| 188 | + console.log("Building Legal Registration Number Verifiable Credential"); | |
| 189 | + const vcLRN = await buildLegalRegistrationNumberVC(); | |
| 190 | + console.log(vcLRN); | |
| 191 | + | |
| 192 | + console.log("Building Terms and Conditions Verifiable Credential"); | |
| 193 | + const vcTC = await buildTermsConditionsVC(); | |
| 194 | + console.log(vcTC); | |
| 195 | + | |
| 196 | + const verifiablePresentation = { | |
| 197 | + "@context": "https://www.w3.org/2018/credentials/v1", | |
| 198 | + type: "VerifiablePresentation", | |
| 199 | + verifiableCredential: [vcParticipant, vcLRN, vcTC], | |
| 200 | + }; | |
| 201 | + | |
| 202 | + console.log("Sending Verifiable Presentation to Compliance API"); | |
| 203 | + console.log(`POST -> ${process.env.API_COMPLIANCE_CREDENTIAL_OFFER}`); | |
| 204 | + console.log(verifiablePresentation); | |
| 205 | + | |
| 206 | + try { | |
| 207 | + const res = await axios.post( | |
| 208 | + process.env.API_COMPLIANCE_CREDENTIAL_OFFER, | |
| 209 | + verifiablePresentation | |
| 210 | + ); | |
| 211 | + | |
| 212 | + console.log(chalk.green("✅ Compliance success")); | |
| 213 | + console.log(res.data); | |
| 214 | + | |
| 215 | + Object.assign(verifiablePresentation, { | |
| 216 | + verifiableCredential: [vcParticipant, vcLRN, vcTC, res.data], | |
| 217 | + }); | |
| 218 | + | |
| 219 | + const filePath = path.join( | |
| 220 | + process.env.WEBSERVER_DIR, | |
| 221 | + process.env.FILENAME_VP | |
| 222 | + ); | |
| 223 | + | |
| 224 | + console.log(`Writing resulting Verifiable Presentation to ${filePath}`); | |
| 225 | + | |
| 226 | + await writeFile(filePath, verifiablePresentation); | |
| 227 | + } catch (err) { | |
| 228 | + console.error(chalk.red("🔴 Compliance error")); | |
| 229 | + console.error(err.response.data); | |
| 230 | + throw err; | |
| 231 | + } | |
| 232 | +} |
added src/utils.js +81 −0
| @@ -0,0 +1,81 @@ | ||
| 1 | +import * as jose from "jose"; | |
| 2 | +import jsonld from "jsonld"; | |
| 3 | +import crypto from "node:crypto"; | |
| 4 | +import fs from "node:fs/promises"; | |
| 5 | + | |
| 6 | +export const ALGORITHM_RSASSA_PSS = "PS256"; | |
| 7 | +export const ALGORITHM_URDNA2015 = "URDNA2015"; | |
| 8 | + | |
| 9 | +export async function publicKeyMatchesCertificate( | |
| 10 | + publicKeyJwk, | |
| 11 | + certificatePem | |
| 12 | +) { | |
| 13 | + try { | |
| 14 | + const pk = await jose.importJWK(publicKeyJwk); | |
| 15 | + const spki = await jose.exportSPKI(pk); | |
| 16 | + const x509 = await jose.importX509(certificatePem, ALGORITHM_RSASSA_PSS); | |
| 17 | + const spkiX509 = await jose.exportSPKI(x509); | |
| 18 | + return spki === spkiX509; | |
| 19 | + } catch (error) { | |
| 20 | + console.error(error); | |
| 21 | + throw new Error( | |
| 22 | + "Could not confirm X509 public key with certificate chain." | |
| 23 | + ); | |
| 24 | + } | |
| 25 | +} | |
| 26 | + | |
| 27 | +export async function writeFile(filePath, obj) { | |
| 28 | + const data = JSON.stringify(obj, null, 2); | |
| 29 | + await fs.writeFile(filePath, data); | |
| 30 | +} | |
| 31 | + | |
| 32 | +export function joinUrl(...parts) { | |
| 33 | + return parts.map((item) => item.trimEnd("/")).join("/"); | |
| 34 | +} | |
| 35 | + | |
| 36 | +export function sha256(input) { | |
| 37 | + return crypto.createHash("sha256").update(input).digest("hex"); | |
| 38 | +} | |
| 39 | + | |
| 40 | +export async function canonize(doc) { | |
| 41 | + return await jsonld.canonize(doc, { | |
| 42 | + algorithm: ALGORITHM_URDNA2015, | |
| 43 | + }); | |
| 44 | +} | |
| 45 | + | |
| 46 | +export async function sign(hash) { | |
| 47 | + const privkeyPem = await fs.readFile(process.env.PATH_PRIVATE_KEY, { | |
| 48 | + encoding: "utf8", | |
| 49 | + }); | |
| 50 | + | |
| 51 | + const rsaPrivateKey = await jose.importPKCS8( | |
| 52 | + privkeyPem, | |
| 53 | + ALGORITHM_RSASSA_PSS | |
| 54 | + ); | |
| 55 | + | |
| 56 | + const jws = await new jose.CompactSign(new TextEncoder().encode(hash)) | |
| 57 | + .setProtectedHeader({ | |
| 58 | + alg: ALGORITHM_RSASSA_PSS, | |
| 59 | + b64: false, | |
| 60 | + crit: ["b64"], | |
| 61 | + }) | |
| 62 | + .sign(rsaPrivateKey); | |
| 63 | + | |
| 64 | + return jws; | |
| 65 | +} | |
| 66 | + | |
| 67 | +export async function createProof(doc) { | |
| 68 | + const canonized = await canonize(doc); | |
| 69 | + const hash = sha256(canonized); | |
| 70 | + const created = new Date().toISOString(); | |
| 71 | + | |
| 72 | + const proof = { | |
| 73 | + type: "JsonWebSignature2020", | |
| 74 | + created, | |
| 75 | + proofPurpose: "assertionMethod", | |
| 76 | + verificationMethod: `${process.env.DID_WEB_ID}#JWK2020`, | |
| 77 | + jws: await sign(hash), | |
| 78 | + }; | |
| 79 | + | |
| 80 | + return proof; | |
| 81 | +} |