application/docIssue.js
/**
 * Prepare data block to submit to the ledger.
 * @param {string} dirWallets Directory with wallet PGP.
 * @param {string} organizationName Organization name.
 * @param {string} walletName Wallet user name.
 * @param {string} filePath File path to load the file.
 * @param {array} destinationsArray Array with recipients list.
 */
Prepare data block to submit to the ledger.
@param {string} dirWallets Directory with wallet PGP.
@param {string} organizationName Organization name.
@param {string} walletName Wallet user name.
@param {string} filePath File path to load the file.
@param {array} destinationsArray Array with recipients list.
Prepare data block to submit to the ledger . @param { string } dirWallets Directory with wallet PGP . @param { string } organizationName Organization name . @param { string } walletName Wallet user name . @param { string } filePath File path to load the file . @param { array } destinationsArray Array with recipients list .
async function prepareDataToSubmit(dirWallets, organizationName, walletName, filePath, destinationsArray) {
    var docUtil = ClientUtil.newClientUtil();
    var issuerPGP = await docUtil.getUserWalletPGP(dirWallets, organizationName, walletName);
    logWrite('[prepareDataToSubmit] Issuer = ' + JSON.stringify(issuerPGP));
    var pathFile = filePath;
    var fileStat = fs.statSync(pathFile);
    var fileBuff = fs.readFileSync(pathFile);
    logWrite('[prepareDataToSubmit] fileBuff.length = ' + fileBuff.length);
    const msgFileContentBase64 = Buffer.from(fileBuff).toString(base64);
    if (logActive) fs.writeFileSync(FileBase64BeforeIssue, msgFileContentBase64.toString(), utf8);
    let msgHashFactory = createHash();
    var msgHashFileContent = msgHashFactory.update(msgFileContentBase64).digest(hex);
    logWrite('[prepareDataToSubmit] msgHashFileContent = ' + msgHashFileContent);
    var msgHashFileSigned = await EccUtil.signMessage(issuerPGP, msgHashFileContent);;
    logWrite('[prepareDataToSubmit] msgHashFileSigned = ' + msgHashFileSigned);
    const password = crypto.randomBytes(32);
    const passwordBase64 = Buffer.from(password).toString(base64);
    logWrite('[prepareDataToSubmit] passwordBase64 = ' + passwordBase64);
    const initVector = crypto.randomBytes(16);
    const initVectorBase64 = Buffer.from(initVector).toString(base64);
    logWrite('[prepareDataToSubmit] initVectorBase64 = ' + initVectorBase64);
    function createCipher() {
        return crypto.createCipheriv(algoritsymm, Buffer.from(password), initVector);
    }
    var cipher = createCipher();
    var msgEncrypted = cipher.update(msgFileContentBase64);
    msgEncrypted = Buffer.concat([msgEncrypted, cipher.final()]).toString(hex);
    cipher = createCipher();
    var msgHashFileSignedEncrypted = cipher.update(msgHashFileSigned);
    msgHashFileSignedEncrypted = Buffer.concat([msgHashFileSignedEncrypted, cipher.final()]).toString(hex);
    const msgFileName = path.basename(pathFile);
    cipher = createCipher();
    var msgFileNameEncrypted = cipher.update(msgFileName);
    msgFileNameEncrypted = Buffer.concat([msgFileNameEncrypted, cipher.final()]).toString(hex);
    const msgFileType = mime.getType(pathFile);
    cipher = createCipher();
    var msgFileTypeEncrypted = cipher.update(msgFileType);
    msgFileTypeEncrypted = Buffer.concat([msgFileTypeEncrypted, cipher.final()]).toString(hex);
    const msgFileExtName = path.extname(pathFile);
    cipher = createCipher();
    var msgFileExtNameEncrypted = cipher.update(msgFileExtName);
    msgFileExtNameEncrypted = Buffer.concat([msgFileExtNameEncrypted, cipher.final()]).toString(hex);
    const msgFileSize = fileStat.size.toString();
    cipher = createCipher();
    var msgFileSizeEncrypted = cipher.update(msgFileSize);
    msgFileSizeEncrypted = Buffer.concat([msgFileSizeEncrypted, cipher.final()]).toString(hex);
    var fileMessage = BeanFileMessage.newBeanFileMessage();
    fileMessage.setFileName(msgFileNameEncrypted);
    fileMessage.setFileType(msgFileTypeEncrypted);
    fileMessage.setFileExtName(msgFileExtNameEncrypted);
    fileMessage.setFileSize(msgFileSizeEncrypted);
    fileMessage.setHashSigned(msgHashFileSignedEncrypted);
    fileMessage.setMessage(msgEncrypted);
    fileMessage.setTransportType(BeanFileMessage.getTransport().ENCRYPTED);
    -----
    var buffHashDestination = '';
    async function prepareRecipientToAdd(persona) {
        let recipient = BeanUser.createBeanUser(persona);
        buffHashDestination = buffHashDestination + recipient.publicId.toString();
        let pwdEncryptedBase64 = await EccUtil.encryptMessage(issuerPGP, passwordBase64, recipient);
        logWrite('[prepareRecipientToAdd] pwdEncryptedBase64 = ' + pwdEncryptedBase64);
        recipient.setPassword(pwdEncryptedBase64);
        let initVecBase64Encrypted = await EccUtil.encryptMessage(issuerPGP, initVectorBase64, recipient);
        logWrite('[prepareRecipientToAdd] initVecBase64Encrypted = ' + initVecBase64Encrypted);
        recipient.setInitVector(initVecBase64Encrypted);
        recipient.clearSecurityData();
        return recipient;
    }
    var recipientsArray = new Array(1 + destinationsArray.length);
    var i = 0;
    for (var len = destinationsArray.length; i < len; i++) {
        recipientsArray[i] = await prepareRecipientToAdd(destinationsArray[i]);
    }
    recipientsArray[i] = await prepareRecipientToAdd(issuerPGP);
    -----
    logWrite('[prepareDataToSubmit] recipientsArray = ' + JSON.stringify(recipientsArray));
    let recipientsHashFactory = createHash();
    var recipientsHash = recipientsHashFactory.update(buffHashDestination.toString()).digest(hex);
    var blockData = BeanDataBlock.newBeanDataBlock();
    blockData.setRecipients(recipientsArray);
    blockData.setRecipientsHash(recipientsHash);
    blockData.setFileMessage(fileMessage);
    var blockDataIssuer = await processToAddIssuer(issuerPGP, blockData);
    blockDataIssuer.configDateTimeLedger();
    logWrite('[prepareDataToSubmit] BlockData ISSUED as JSON in client application');
    return blockDataIssuer;
}
async function prepareDataToSubmit ( dirWallets , organizationName , walletName , filePath , destinationsArray ) { var docUtil = ClientUtil . newClientUtil ( ) ; var issuerPGP = await docUtil . getUserWalletPGP ( dirWallets , organizationName , walletName ) ; logWrite ( '[prepareDataToSubmit] Issuer = ' + JSON . stringify ( issuerPGP ) ) ; var pathFile = filePath ; var fileStat = fs . statSync ( pathFile ) ; var fileBuff = fs . readFileSync ( pathFile ) ; logWrite ( '[prepareDataToSubmit] fileBuff.length = ' + fileBuff . length ) ; const msgFileContentBase64 = Buffer . from ( fileBuff ) . toString ( base64 ) ; if ( logActive ) fs . writeFileSync ( FileBase64BeforeIssue , msgFileContentBase64 . toString ( ) , utf8 ) ; let msgHashFactory = createHash ( ) ; var msgHashFileContent = msgHashFactory . update ( msgFileContentBase64 ) . digest ( hex ) ; logWrite ( '[prepareDataToSubmit] msgHashFileContent = ' + msgHashFileContent ) ; var msgHashFileSigned = await EccUtil . signMessage ( issuerPGP , msgHashFileContent ) ; ; logWrite ( '[prepareDataToSubmit] msgHashFileSigned = ' + msgHashFileSigned ) ; const password = crypto . randomBytes ( 32 ) ; const passwordBase64 = Buffer . from ( password ) . toString ( base64 ) ; logWrite ( '[prepareDataToSubmit] passwordBase64 = ' + passwordBase64 ) ; const initVector = crypto . randomBytes ( 16 ) ; const initVectorBase64 = Buffer . from ( initVector ) . toString ( base64 ) ; logWrite ( '[prepareDataToSubmit] initVectorBase64 = ' + initVectorBase64 ) ; function createCipher ( ) { return crypto . createCipheriv ( algoritsymm , Buffer . from ( password ) , initVector ) ; } var cipher = createCipher ( ) ; var msgEncrypted = cipher . update ( msgFileContentBase64 ) ; msgEncrypted = Buffer . concat ( [ msgEncrypted , cipher . final ( ) ] ) . toString ( hex ) ; cipher = createCipher ( ) ; var msgHashFileSignedEncrypted = cipher . update ( msgHashFileSigned ) ; msgHashFileSignedEncrypted = Buffer . concat ( [ msgHashFileSignedEncrypted , cipher . final ( ) ] ) . toString ( hex ) ; const msgFileName = path . basename ( pathFile ) ; cipher = createCipher ( ) ; var msgFileNameEncrypted = cipher . update ( msgFileName ) ; msgFileNameEncrypted = Buffer . concat ( [ msgFileNameEncrypted , cipher . final ( ) ] ) . toString ( hex ) ; const msgFileType = mime . getType ( pathFile ) ; cipher = createCipher ( ) ; var msgFileTypeEncrypted = cipher . update ( msgFileType ) ; msgFileTypeEncrypted = Buffer . concat ( [ msgFileTypeEncrypted , cipher . final ( ) ] ) . toString ( hex ) ; const msgFileExtName = path . extname ( pathFile ) ; cipher = createCipher ( ) ; var msgFileExtNameEncrypted = cipher . update ( msgFileExtName ) ; msgFileExtNameEncrypted = Buffer . concat ( [ msgFileExtNameEncrypted , cipher . final ( ) ] ) . toString ( hex ) ; const msgFileSize = fileStat . size . toString ( ) ; cipher = createCipher ( ) ; var msgFileSizeEncrypted = cipher . update ( msgFileSize ) ; msgFileSizeEncrypted = Buffer . concat ( [ msgFileSizeEncrypted , cipher . final ( ) ] ) . toString ( hex ) ; var fileMessage = BeanFileMessage . newBeanFileMessage ( ) ; fileMessage . setFileName ( msgFileNameEncrypted ) ; fileMessage . setFileType ( msgFileTypeEncrypted ) ; fileMessage . setFileExtName ( msgFileExtNameEncrypted ) ; fileMessage . setFileSize ( msgFileSizeEncrypted ) ; fileMessage . setHashSigned ( msgHashFileSignedEncrypted ) ; fileMessage . setMessage ( msgEncrypted ) ; fileMessage . setTransportType ( BeanFileMessage . getTransport ( ) . ENCRYPTED ) ; var buffHashDestination = '' ; async function prepareRecipientToAdd ( persona ) { let recipient = BeanUser . createBeanUser ( persona ) ; buffHashDestination = buffHashDestination + recipient . publicId . toString ( ) ; let pwdEncryptedBase64 = await EccUtil . encryptMessage ( issuerPGP , passwordBase64 , recipient ) ; logWrite ( '[prepareRecipientToAdd] pwdEncryptedBase64 = ' + pwdEncryptedBase64 ) ; recipient . setPassword ( pwdEncryptedBase64 ) ; let initVecBase64Encrypted = await EccUtil . encryptMessage ( issuerPGP , initVectorBase64 , recipient ) ; logWrite ( '[prepareRecipientToAdd] initVecBase64Encrypted = ' + initVecBase64Encrypted ) ; recipient . setInitVector ( initVecBase64Encrypted ) ; recipient . clearSecurityData ( ) ; return recipient ; } var recipientsArray = new Array ( 1 + destinationsArray . length ) ; var i = 0 ; for ( var len = destinationsArray . length ; i < len ; i ++ ) { recipientsArray [ i ] = await prepareRecipientToAdd ( destinationsArray [ i ] ) ; } recipientsArray [ i ] = await prepareRecipientToAdd ( issuerPGP ) ; logWrite ( '[prepareDataToSubmit] recipientsArray = ' + JSON . stringify ( recipientsArray ) ) ; let recipientsHashFactory = createHash ( ) ; var recipientsHash = recipientsHashFactory . update ( buffHashDestination . toString ( ) ) . digest ( hex ) ; var blockData = BeanDataBlock . newBeanDataBlock ( ) ; blockData . setRecipients ( recipientsArray ) ; blockData . setRecipientsHash ( recipientsHash ) ; blockData . setFileMessage ( fileMessage ) ; var blockDataIssuer = await processToAddIssuer ( issuerPGP , blockData ) ; blockDataIssuer . configDateTimeLedger ( ) ; logWrite ( '[prepareDataToSubmit] BlockData ISSUED as JSON in client application' ) ; return blockDataIssuer ; }
Prepare data block to submit to the ledger.
Prepare data block to submit to the ledger .
// Get Sender user //------------------------------------------------- // Load binary file (message) from the system /*
     * Transform from binary to Base64 string
     * https://stackoverflow.com/questions/28834835/readfile-in-base64-nodejs
     */ // Testing the contents of the transformed file from binary to base64, so that it is later encrypted /*
     * Hash the file in Base64.
     * This hash will then be signed by Issuer and then encrypted.
     */ /*
     * Issuer sign the file-message's hash with his secret key.
     * Reader will have to check this signature.
     */ // Generate random password for use with AES-256-CBC // Save password as Base64 // Generate random inicialization vector for use with AES-256-CBC // Save the inicialization vector as Base64 /**
     * Create AES encryption.
     */ /*
     * Encrypt the file with the symmetric password, and save.
     * Encrypt message with AES.
     */ // Encrypt hash of the message that was signed by Issuer // Encrypt message metadata // https://stackoverflow.com/questions/10431845/node-js-file-system-get-file-type-solution-around-year-2012 // https://stackoverflow.com/questions/10865347/node-js-get-file-extension // Create new message record //------------------------------------------------------ // Create a buffer to hash the Recipients list /**
     * Prepare a single recipient to add in the array of recipients.
     * @param {BeanUser} persona 
     */ // Get Recipient User // Concatenation of Recipients' public key hashes sequence // Encrypt the AES password using the Recipient public key // Add encrypted password to the Recipient object // Encrypt the inicialization vector using the Recipient's public key // Add encrypted inicialization vector to the Recipient object // Prevents the private dataset from being inside the user's data // Create Recipient object listing // Scroll through the list of Recipients // Add each individual recipient to the Recipients list // Add Issuer as an individual recipient to the Recipients list //------------------------------------------------------ /*
     * Build the general hash from the concatenation of the Recipient identifiers. 
     * This hash will be used in the Validation step.
     */ // Creates the empty data block // Recipients List // AES encrypted message with password // Add the Issuer data block