const glob = require("glob"); const path = require("path"); const dayjs = require("dayjs"); const fs = require("fs-extra"); const axios = require("axios"); const openpgp = require("openpgp"); var conf = {}; if (fs.existsSync("../../nationchains/tribes/conf.json")) { conf = require("../../nationchains/tribes/conf.json"); } console.log(conf); /** * Pagan Management numeric Identity and Person (Person = Pagan Id + tribe) * * * */ const Pagans = {}; Pagans.create = (alias, publicKey) => { /** * @param {string} alias a unique alias that identify an identity * @param {string} publicKey a publicKey * @return {object} { status: 200, data: { alias, publicKey } } * xhash was checked by isauthenticated * @todo use Odmdb to add a pagan */ let apxpagans = {}; if (fs.existsSync(`${__base}nationchains/pagans/idx/alias_all.json`)) { apxpagans = fs.readJsonSync( `${__base}nationchains/pagans/idx/alias_all.json` ); } apxpagans[alias] = { alias, publicKey }; fs.outputJsonSync( `${__base}nationchains/pagans/idx/alias_all.json`, apxpagans ); fs.outputJsonSync(`${__base}nationchains/pagans/itm/${alias}.json`, { alias, publicKey, }); return { status: 200, data: { alias, publicKey } }; }; Pagans.personupdate = (alias, tribe, persondata) => { //later use Odmdb ans schema person to manage this /** * @Param {string} alias pagan unique id * @Param {string} tribe tribe id in this town * @Param {object} persondata that respect /nationchains/schema/person.json + nationchains/tribe/tribeid/schema/personextented.json * @return create or update a person /tribe/tribeid/person/alias.json */ let person = { alias: alias, dt_create: dayjs(), accessrights: { profil: "user" }, }; if (fs.existsSync(`${__base}tribes/${tribe}/person/itm/${alias}.json`)) { person = fs.readJsonSync( `${__base}tribes/${tribe}/person/itm/${alias}.json` ); person.dt_update = dayjs(); } Object.keys(persondata).forEach((d) => { person[d] = persondata[d]; }); //const checkjson= Checkjson.schema.data = (fs.readJsonSync(`${__base}}nationchains/schema/person.json`, person, false) // if checkjson.status==200 create /update with odmdb to update index data // see odmdb that did all and return standard message fs.outputJSONSync( `${__base}tribes/${tribe}/person/itm/${alias}.json`, person, { space: 2, } ); return { status: 200, ref: "Pagans", msg: "successfullupdate", data: { tribe: tribe }, }; }; Pagans.authenticatedetachedSignature = async ( alias, pubK, detachedSignature, message ) => { /** * Check that a message was signed with a privateKey from a publicKey * This is not necessary if isAuthenticated, but can be usefull to double check * @TODO finish it and implement it also in /apxpagan.js for browser * @alias {string} alias link to the publicKey * @pubK {string} publiKey text format * @detachedSignature {string} a detachedsignatured get from apx.detachedSignature * @message {string} the message signed * @return {boolean} true the message was signed by alias * false the message was not signed by alias */ const publicKey = await openpgp.readKey({ armoredKey: pubK }); const msg = await openpgp.createMessage({ text: message }); const signature = await openpgp.readSignature({ armoredSignature: detachedSignature, // parse detached signature }); const verificationResult = await openpgp.verify({ msg, // Message object signature, verificationKeys: publicKey, }); const { verified, keyID } = verificationResult.signatures[0]; try { await verified; // throws on invalid signature console.log("Signed by key id " + keyID.toHex()); return KeyId.toHex().alias == alias; } catch (e) { console.log("Signature could not be verified: " + e.message); return false; } }; module.exports = Pagans;