This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
executable file
·190 lines (179 loc) · 5.5 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
const fs = require("fs");
const mkdirp = require("mkdirp");
const yargs = require("yargs");
const {
validateSchema,
verifySignature,
obfuscateFields,
schemas
} = require("@govtechsg/open-certificate");
const batchVerify = require("./src/batchVerify");
const { batchIssue } = require("./src/batchIssue");
const { logger, addConsole } = require("./lib/logger");
const { version } = require("./package.json");
// Pass argv with $1 and $2 sliced
const parseArguments = argv =>
yargs
.version(version)
.usage("Certificate issuing, verification and revocation tool.")
.strict()
.epilogue(
"The common subcommands you might be interested in are:\n" +
"- batch\n" +
"- verify\n" +
"- verify-all\n" +
"- filter"
)
.options({
"log-level": {
choices: ["error", "warn", "info", "verbose", "debug", "silly"],
default: "info",
description: "Set the log level",
global: true
},
schema: {
choices: Object.keys(schemas),
description: "Set the schema to use",
default: Object.keys(schemas)[Object.keys(schemas).length - 1],
global: true,
type: "string"
}
})
.command({
command: "filter <source> <destination> [fields..]",
description: "Obfuscate fields in the certificate",
builder: sub =>
sub
.positional("source", {
description: "Source signed certificate filename",
normalize: true
})
.positional("destination", {
description: "Destination to write obfuscated certificate file to",
normalize: true
})
})
.command({
command: "verify [options] <file>",
description: "Verify the certificate",
builder: sub =>
sub.positional("file", {
description: "Certificate file to verify",
normalize: true
})
})
.command({
command: "verify-all [options] <dir>",
description: "Verify all certiifcate in a directory",
builder: sub =>
sub.positional("dir", {
description: "Directory with all certificates to verify",
normalize: true
})
})
.command({
command: "batch [options] <raw-dir> <batched-dir>",
description:
"Combine a directory of certificates into a certificate batch",
builder: sub =>
sub
.positional("raw-dir", {
description:
"Directory containing the raw unissued and unsigned certificates",
normalize: true
})
.positional("batched-dir", {
description: "Directory to output the batched certificates to.",
normalize: true
})
})
.parse(argv);
const batch = async (raw, batched, schemaVersion) => {
mkdirp.sync(batched);
return batchIssue(raw, batched, schemaVersion)
.then(merkleRoot => {
logger.debug(`Batch Certificate Root: 0x${merkleRoot}`);
return `0x${merkleRoot}`;
})
.catch(err => {
logger.error(err);
});
};
const verifyAll = async (dir, schemaVersion) => {
const verified = await batchVerify(dir, schemaVersion);
if (verified) {
logger.debug(`All certificates in ${dir} is verified`);
return true;
}
logger.error("At least one certificate failed verification");
return false;
};
const verify = (file, schemaVersion) => {
const certificateJson = JSON.parse(fs.readFileSync(file, "utf8"));
if (
verifySignature(certificateJson) &&
validateSchema(certificateJson, schemaVersion)
) {
logger.debug("Certificate's signature is valid!");
logger.warn(
"Warning: Please verify this certificate on the blockchain with the issuer's certificate store."
);
} else {
logger.error("Certificate's signature is invalid");
}
return true;
};
const obfuscate = (input, output, fields) => {
const certificateJson = JSON.parse(fs.readFileSync(input, "utf8"));
const obfuscatedCertificate = obfuscateFields(certificateJson, fields);
const isValid =
verifySignature(obfuscatedCertificate) &&
validateSchema(obfuscatedCertificate);
if (!isValid) {
logger.error(
"Privacy filtering caused document to fail schema or signature validation"
);
} else {
fs.writeFileSync(output, JSON.stringify(obfuscatedCertificate, null, 2));
logger.debug(`Obfuscated certificate saved to: ${output}`);
}
};
const main = async argv => {
const args = parseArguments(argv);
addConsole(args.logLevel);
logger.debug(`Parsed args: ${JSON.stringify(args)}`);
const selectedSchema = schemas[args.schema];
if (args._.length !== 1) {
yargs.showHelp("log");
return false;
}
switch (args._[0]) {
case "batch":
return batch(args.rawDir, args.batchedDir, selectedSchema);
case "verify":
return verify(args.file, selectedSchema);
case "verify-all":
return verifyAll(args.dir, selectedSchema);
case "filter":
return obfuscate(args.source, args.destination, args.fields);
default:
throw new Error(`Unknown command ${args._[0]}. Possible bug.`);
}
};
if (typeof require !== "undefined" && require.main === module) {
main(process.argv.slice(2))
.then(res => {
// eslint-disable-next-line no-console
if (res) console.log(res);
process.exit(0);
})
.catch(err => {
logger.error(`Error executing: ${err}`);
if (typeof err.stack !== "undefined") {
logger.debug(err.stack);
}
logger.debug(JSON.stringify(err));
process.exit(1);
});
}