forked from OpenCerts/certificate-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·174 lines (164 loc) · 4.97 KB
/
index.js
File metadata and controls
executable file
·174 lines (164 loc) · 4.97 KB
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
#!/usr/bin/env node
const fs = require("fs");
const mkdirp = require("mkdirp");
const yargs = require("yargs");
const {
validateSchema,
verifySignature,
obfuscateFields
} = 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
}
})
.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) => {
mkdirp.sync(batched);
return batchIssue(raw, batched)
.then(merkleRoot => {
logger.info(`Batch Certificate Root: 0x${merkleRoot}`);
return `${merkleRoot}`;
})
.catch(err => {
logger.error(err);
});
};
const verifyAll = async dir => {
const verified = await batchVerify(dir);
if (verified) {
logger.info(`All certificates in ${dir} is verified`);
} else {
logger.error("At least one certificate failed verification");
}
};
const verify = file => {
const certificateJson = JSON.parse(fs.readFileSync(file, "utf8"));
if (verifySignature(certificateJson) && validateSchema(certificateJson)) {
logger.info("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.info(`Obfuscated certificate saved to: ${output}`);
}
};
const main = async argv => {
const args = parseArguments(argv);
addConsole(args.logLevel);
logger.debug(`Parsed args: ${JSON.stringify(args)}`);
if (args._.length !== 1) {
yargs.showHelp("log");
return false;
}
switch (args._[0]) {
case "batch":
return batch(args.rawDir, args.batchedDir);
case "verify":
return verify(args.file);
case "verify-all":
return verifyAll(args.dir);
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(() => {
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);
});
}