apxtrib/api/models/Pagans.js

127 lines
4.0 KiB
JavaScript
Raw Normal View History

2023-04-27 04:17:20 +00:00
const glob = require("glob");
const path = require("path");
2023-05-12 05:59:32 +00:00
const dayjs = require("dayjs");
2023-04-27 04:17:20 +00:00
const fs = require("fs-extra");
2023-05-12 05:59:32 +00:00
const axios = require("axios");
const openpgp = require("openpgp");
2023-05-16 08:31:27 +00:00
/*if (fs.existsSync("../../nationchains/tribes/conf.json")) {
2023-05-12 05:59:32 +00:00
conf = require("../../nationchains/tribes/conf.json");
2023-05-16 08:31:27 +00:00
}*/
const conf = require(`${process.env.dirtown}/conf.json`);
2023-05-12 05:59:32 +00:00
console.log(conf);
2023-04-27 04:17:20 +00:00
/**
2023-05-12 05:59:32 +00:00
* Pagan Management numeric Identity and Person (Person = Pagan Id + tribe)
*
*
*
2023-04-27 04:17:20 +00:00
*/
2023-05-12 05:59:32 +00:00
const Pagans = {};
2023-04-27 04:17:20 +00:00
2023-05-12 05:59:32 +00:00
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 = {};
2023-05-16 08:31:27 +00:00
if (fs.existsSync(`${__dirapi}/nationchains/pagans/idx/alias_all.json`)) {
2023-05-12 05:59:32 +00:00
apxpagans = fs.readJsonSync(
2023-05-16 08:31:27 +00:00
`${__dirapi}/nationchains/pagans/idx/alias_all.json`
2023-05-12 05:59:32 +00:00
);
}
apxpagans[alias] = { alias, publicKey };
fs.outputJsonSync(
2023-05-16 08:31:27 +00:00
`${conf.dirapi}/nationchains/pagans/idx/alias_all.json`,
2023-05-12 05:59:32 +00:00
apxpagans
);
2023-05-16 08:31:27 +00:00
fs.outputJsonSync(`${conf.dirapi}/nationchains/pagans/itm/${alias}.json`, {
2023-05-12 05:59:32 +00:00
alias,
publicKey,
});
return { status: 200, data: { alias, publicKey } };
};
2023-04-27 04:17:20 +00:00
2023-05-12 05:59:32 +00:00
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" },
2023-04-27 04:17:20 +00:00
};
2023-05-16 08:31:27 +00:00
if (fs.existsSync(`${process.env.dirtown}/tribes/${tribe}/person/itm/${alias}.json`)) {
2023-05-12 05:59:32 +00:00
person = fs.readJsonSync(
2023-05-16 08:31:27 +00:00
`${process.env.dirtown}/tribes/${tribe}/person/itm/${alias}.json`
2023-05-12 05:59:32 +00:00
);
person.dt_update = dayjs();
}
Object.keys(persondata).forEach((d) => {
person[d] = persondata[d];
});
2023-05-16 08:31:27 +00:00
//const checkjson= Checkjson.schema.data = (fs.readJsonSync(`${conf.dirapi}/nationchains/schema/person.json`, person, false)
2023-05-12 05:59:32 +00:00
// if checkjson.status==200 create /update with odmdb to update index data
// see odmdb that did all and return standard message
fs.outputJSONSync(
2023-05-16 08:31:27 +00:00
`${process.env.dirtown}/tribes/${tribe}/person/itm/${alias}.json`,
2023-05-12 05:59:32 +00:00
person,
{
space: 2,
2023-04-27 04:17:20 +00:00
}
2023-05-12 05:59:32 +00:00
);
return {
status: 200,
ref: "Pagans",
msg: "successfullupdate",
data: { tribe: tribe },
2023-04-27 04:17:20 +00:00
};
2023-05-12 05:59:32 +00:00
};
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;