forked from apxtri/apxtrib
120 lines
4.6 KiB
JavaScript
120 lines
4.6 KiB
JavaScript
const glob = require("glob");
|
|
const path = require("path");
|
|
const fs = require("fs-extra");
|
|
const axios = require('axios');
|
|
const openpgp = require('openpgp');
|
|
const conf=require('../../nationchains/tribes/conf.json')
|
|
|
|
/**
|
|
* Pagan Management numeric Identity
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
|
|
const Pagans= {}
|
|
|
|
Pagans.createId = async (alias,passphrase) =>{
|
|
/**
|
|
* @param {string} alias a unique alias that identify an identity
|
|
* @param {string} passphrase a string to cipher the publicKey (can be empty, less secure but simpler)
|
|
* @return {publicKey,privateKey} with userIds = [{alias}]
|
|
*/
|
|
let apxpagans={};
|
|
if (fs.existsSync(`${conf.dirname}/nationchains/pagans/idx/alias_all.json`)){
|
|
apxpagans = fs.readJsonSync(
|
|
`${conf.dirname}/nationchains/pagans/idx/alias_all.json`
|
|
);
|
|
}
|
|
if (Object.keys(apxpagans).includes(alias)){
|
|
return {status:409,ref:"pagans",msg:"aliasalreadyexist"}
|
|
};
|
|
const {privateKey,publicKey} = await openpgp.generateKey({
|
|
type: "ecc", // Type of the key, defaults to ECC
|
|
curve: "curve25519", // ECC curve name, defaults to curve25519
|
|
userIDs: [{ alias: alias }], // you can pass multiple user IDs
|
|
passphrase: passphrase, // protects the private key
|
|
format: "armored", // output key format, defaults to 'armored' (other options: 'binary' or 'object')
|
|
});
|
|
console.log(privateKey)
|
|
console.log(publicKey)
|
|
apxpagans[alias]={alias,publicKey};
|
|
|
|
fs.outputJsonSync(`${conf.dirname}/nationchains/pagans/idx/alias_all.json`,apxpagans);
|
|
fs.outputJsonSync(`${conf.dirname}/nationchains/pagans/itm/${alias}.json`,{alias,publicKey});
|
|
return {status:200, data:{alias,privateKey,publicKey}}
|
|
}
|
|
|
|
Pagans.generateKey = async (alias, passphrase) => {
|
|
/**
|
|
* @param {string} alias a unique alias that identify an identity
|
|
* @param {string} passphrase a string to cipher the publicKey (can be empty, less secure but simpler)
|
|
* @return {publicKey,privateKey} with userIds = [{alias}]
|
|
*/
|
|
const { privateKey, publicKey } = await openpgp.generateKey({
|
|
type: "ecc", // Type of the key, defaults to ECC
|
|
curve: "curve25519", // ECC curve name, defaults to curve25519
|
|
userIDs: [{ alias: alias }], // you can pass multiple user IDs
|
|
passphrase: passphrase, // protects the private key
|
|
format: "armored", // output key format, defaults to 'armored' (other options: 'binary' or 'object')
|
|
});
|
|
// key start by '-----BEGIN PGP PRIVATE KEY BLOCK ... '
|
|
// get liste of alias:pubklickey await axios.get('api/v0/pagans')
|
|
// check alias does not exist
|
|
console.log(privateKey)
|
|
return { alias, privateKey, publicKey };
|
|
|
|
};
|
|
//console.log( Pagans.generateKey('toto',''))
|
|
Pagans.detachedSignature = async (pubK, privK, passphrase, message) => {
|
|
/**
|
|
* @pubK {string} a text public key
|
|
* @privK {string} a test priv key
|
|
* @passphrase {string} used to read privK
|
|
* @message {string} message to sign
|
|
* @Return a detached Signature of the message
|
|
*/
|
|
const publicKey = await openpgp.readKey({ armoredKey: pubK });
|
|
const privateKey = await openpgp.decryptKey({
|
|
privateKey: await openpgp.readPrivateKey({ armoredKey: privK }),
|
|
passphrase,
|
|
});
|
|
const msg = await openpgp.createMessage({ text: message });
|
|
return await openpgp.sign({ msg, signinKeys: privK, detached: true });
|
|
};
|
|
Pagans.checkdetachedSignature = async (
|
|
alias,
|
|
pubK,
|
|
detachedSignature,
|
|
message
|
|
) => {
|
|
/**
|
|
* @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; |