-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_refresh_diploma.ts
59 lines (52 loc) · 2.55 KB
/
01_refresh_diploma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
Bls12381G2KeyPair,
BbsBlsSignature2020
} from "@mattrglobal/jsonld-signatures-bbs";
import { sign, purposes } from "jsonld-signatures";
const config = require('./config');
import {documentLoader, cli} from './util';
import keyPairOptions from "./data/keyPair.json"; // keypair needed to create original VC
import fsp from 'fs/promises';
import path from 'path';
const refreshDiploma = async (credentialConfig): Promise<void> => {
const oldSignedDocument = JSON.parse(await fsp.readFile(path.resolve(__dirname, credentialConfig.path), 'utf8'));
const vcId = oldSignedDocument['id'];
// remove old proof
const { proof: oldProof, ...inputDocument } = oldSignedDocument as any;
// remove old contexts
let oldContexts = ['https://w3id.org/security/suites/ed25519-2020/v1', 'https://w3id.org/vc-revocation-list-2020/v1', 'https://www.w3.org/2018/credentials/v1'];
for (const oldContext of oldContexts) {
const oldProofContextIndex = inputDocument["@context"].indexOf(oldContext);
if (oldProofContextIndex !== -1) {
inputDocument["@context"].splice(oldProofContextIndex, 1);
}
}
// add new contexts
let newContexts = ['https://w3id.org/security/bbs/v1', 'https://www.w3.org/ns/credentials/v2'];
for (const newContext of newContexts) {
const newProofContextIndex = inputDocument["@context"].indexOf(newContext);
if (newProofContextIndex === -1) {
inputDocument["@context"].push(newContext);
}
}
// add status
inputDocument['credentialStatus'] = {
"id": `${config.status[vcId].listId}#${config.status[vcId].index}`,
"type": "BitstringStatusListEntry",
"statusPurpose": "revocation",
"statusListIndex": config.status[vcId].index,
"statusListCredential": config.status[vcId].listId
}
await fsp.writeFile(path.resolve(__dirname, credentialConfig.path_refresh), JSON.stringify(inputDocument, null, 2));
console.log(`Input document created for '${credentialConfig.description}'`);
//Sign the input document
const keyPair = await new Bls12381G2KeyPair(keyPairOptions);
const signedDocument = await sign(inputDocument, {
suite: new BbsBlsSignature2020({ key: keyPair }),
purpose: new purposes.AssertionProofPurpose(),
documentLoader,
});
await fsp.writeFile(path.resolve(__dirname, credentialConfig.path_refresh_signed), JSON.stringify(signedDocument, null, 2));
console.log(`Input document signed for '${credentialConfig.description}'`);
};
cli(config, refreshDiploma);