-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
63 lines (59 loc) · 2.72 KB
/
gatsby-node.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
"use strict";
const fetch = require('node-fetch');
const queryString = require('query-string');
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
const { hashElement } = require('folder-hash');
const jsonfile = require('jsonfile')
const report = require('gatsby-cli/lib/reporter');
exports.onPostBuild = async function onPostBuild(nodeOptions, pluginOptions) {
// console.log(JSON.stringify({ nodeOptions, pluginOptions }, null, 2));
if (pluginOptions.apiKey) {
// Get all pages and content from site public folder then hash it
//TODO create function to format multiple hashes instead of just top of tree and prep for the cryptowerk api call
const xyz = await hashnow(pluginOptions.hashoutputfile);
const createdSealID = await createSeal(xyz.hash, pluginOptions.apiKey, pluginOptions.outputfile);
//TODO archive public and the seal
}
};
async function hashnow(hashoutputfile) {
const options = {
algo: 'sha256', // see crypto.getHashes() for options
encoding: 'hex',
folders: { include: ['**.*', '**'] }, // Fine tune selection https://github.com/marc136/node-folder-hash#options-object-properties
files: { include: ['**.*'] }
};
const hashTree = await hashElement('public', options);
// console.log(` hashTree.hash ${hashTree.hash}\n`);
report.info(`Cryptowerk-Blockchain plugin: Content of public folder was hashed into ${hashTree.hash}`);
if (hashoutputfile) {
jsonfile.writeFileSync(hashoutputfile, hashTree, { spaces: 2 }, function (err) {
if (err) report.panic('writing file failed:', error);
});
report.info(`Cryptowerk-Blockchain plugin: Hash tree was stored here ${hashoutputfile}`);
}
return hashTree;
}
async function createSeal(hashforsealing, cwapikey, storesealhere) {
const qstring = '?version=6&hashes=' + hashforsealing;
// TODO Add name of site and other metadata about build consider adding callback too webhook
const options = {
headers: {
"Accept": "application/json",
"X-API-Key": cwapikey
}
};
const apiUrl = `https://developers.cryptowerk.com/platform/API/v6/register?${qstring}`;
const response = await fetch(apiUrl, options)
.then(res => res.json())
.catch(error => {
report.panic('sealing failed:', error);
return console.error('sealing failed:', error);
});
jsonfile.writeFileSync(storesealhere, response, { spaces: 2 }, function (err) {
if (err) report.panic('writing file failed:', error);
});
report.info(`Cryptowerk-Blockchain plugin: Retrieve SealID info manually https://www.sealwitness.com/platform/portal/list.html?text=${response.documents[0].retrievalId}`);
report.info(`Cryptowerk-Blockchain plugin: Seal info file ${storesealhere}`);
return response;
}