| 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 | import { getConfig } from "./config.js"; |
| 6 | import { logger } from "./log.js"; |
| 7 | |
| 8 | export const ALGORITHM_RSASSA_PSS = "PS256"; |
| 9 | export const ALGORITHM_URDNA2015 = "URDNA2015"; |
| 10 | |
| 11 | export async function publicKeyMatchesCertificate( |
| 12 | publicKeyJwk, |
| 13 | certificatePem |
| 14 | ) { |
| 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 | logger.error(error); |
| 23 | |
| 24 | throw new Error( |
| 25 | "Could not confirm X509 public key with certificate chain." |
| 26 | ); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | export async function writeFile(filePath, obj) { |
| 31 | const data = JSON.stringify(obj, null, 2); |
| 32 | await fs.writeFile(filePath, data); |
| 33 | } |
| 34 | |
| 35 | export function joinUrl(...parts) { |
| 36 | return parts.map((item) => item.trimEnd("/")).join("/"); |
| 37 | } |
| 38 | |
| 39 | export function sha256(input) { |
| 40 | return crypto.createHash("sha256").update(input).digest("hex"); |
| 41 | } |
| 42 | |
| 43 | const CACHE = {}; |
| 44 | |
| 45 | const customLoader = async (url) => { |
| 46 | if (url in CACHE) { |
| 47 | logger.debug(`Loaded ${url} from cache`); |
| 48 | return CACHE[url]; |
| 49 | } |
| 50 | |
| 51 | logger.info(`Loading '${url}' from remote source`); |
| 52 | const loaderResp = await jsonld.documentLoaders.node()(url); |
| 53 | CACHE[url] = loaderResp; |
| 54 | return loaderResp |
| 55 | }; |
| 56 | |
| 57 | export async function canonize(doc) { |
| 58 | return await jsonld.canonize(doc, { |
| 59 | algorithm: ALGORITHM_URDNA2015, |
| 60 | documentLoader: customLoader, |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | export async function sign(hash) { |
| 65 | const config = getConfig(); |
| 66 | |
| 67 | const privkeyPem = await fs.readFile(config.pathPrivateKey, { |
| 68 | encoding: "utf8", |
| 69 | }); |
| 70 | |
| 71 | const rsaPrivateKey = await jose.importPKCS8( |
| 72 | privkeyPem, |
| 73 | ALGORITHM_RSASSA_PSS |
| 74 | ); |
| 75 | |
| 76 | const jws = await new jose.CompactSign(new TextEncoder().encode(hash)) |
| 77 | .setProtectedHeader({ |
| 78 | alg: ALGORITHM_RSASSA_PSS, |
| 79 | b64: false, |
| 80 | crit: ["b64"], |
| 81 | }) |
| 82 | .sign(rsaPrivateKey); |
| 83 | |
| 84 | return jws; |
| 85 | } |
| 86 | |
| 87 | export async function createProof(doc) { |
| 88 | const config = getConfig(); |
| 89 | const canonized = await canonize(doc); |
| 90 | const hash = sha256(canonized); |
| 91 | const created = new Date().toISOString(); |
| 92 | |
| 93 | const proof = { |
| 94 | type: "JsonWebSignature2020", |
| 95 | created, |
| 96 | proofPurpose: "assertionMethod", |
| 97 | verificationMethod: `${config.didWebId}`, |
| 98 | jws: await sign(hash), |
| 99 | }; |
| 100 | |
| 101 | return proof; |
| 102 | } |
| 103 | |
| 104 | export function getProperty(obj, prop) { |
| 105 | const props = prop.split("."); |
| 106 | let value = obj; |
| 107 | |
| 108 | for (const p of props) { |
| 109 | if (value[p] === undefined) { |
| 110 | return undefined; |
| 111 | } |
| 112 | |
| 113 | value = value[p]; |
| 114 | } |
| 115 | |
| 116 | return value; |
| 117 | } |
| 118 | |
| 119 | export function buildVerifiablePresentation({ verifiableCredentials }) { |
| 120 | return { |
| 121 | "@context": "https://www.w3.org/2018/credentials/v1", |
| 122 | type: "VerifiablePresentation", |
| 123 | verifiableCredential: verifiableCredentials, |
| 124 | }; |
| 125 | } |