Merge branch 'release/1.2'

This commit is contained in:
Jose Rivera 2020-06-17 15:14:04 -05:00
commit 6e4eb2fbb6
7 changed files with 324 additions and 137 deletions

3
.envSample Normal file
View File

@ -0,0 +1,3 @@
APIKEY=
APIMODE=
# Available modes: dev, startup, grow

3
.gitignore vendored
View File

@ -1,4 +1,7 @@
/node_modules /node_modules
/orbitdb /orbitdb
/receipts
/proyectsReceipts
package-lock.json package-lock.json
.env .env
.directory

View File

@ -15,39 +15,40 @@ DECA's Carbon Credits OrbitDB Code and configs, everything you need for setting
## Carbon Credit Structure ## Carbon Credit Structure
```sh ```sh
{
/* /*
CC - Carbon credits CC - Carbon credits
Unix Time is date and time based in the UTC 0 Unix Time is date and time based in the UTC 0
example for the first carbon credit that is 1365209 example for the first carbon credit that is 1365209
*/ */
[
{
//CCID: unique Carbon Credit Hash in DECAs registry (used as receipt and control)
CCID: 'a67178fa3cb20e49f748050871f4f10784693bde1b9ec805740ff9c63b93860d',
SerialNo: 'GS1-1-MX-GS2441-16-2018-17438-328-328', SerialNo: 'GS1-1-MX-GS2441-16-2018-17438-328-328',
CCAddress: 'https://registry.goldstandard.org/credit-blocks/details/107995', //Address GS Registry ccAddress: 'https://registry.goldstandard.org/credit-blocks/details/107995', //Address GS Registry
issueDate: 1552953600, // CC creation UnixTime issueDate: 1552953600, // CC creation UnixTime
cancelDate: 1587214800, // CC cancelation cancelation UnixTime cancelDate: 1587168000, // CC cancelation cancelation UnixTime
ccVintageStart: 1514764800, // CC what year it was created in UnixTime
ccVintageEnd: 1514764800, // CC what year it was created in UnixTime
ccCategory: 'Energy Efficiency - Domestic', // CC category agricultural ccCategory: 'Energy Efficiency - Domestic', // CC category agricultural
ccMeasurement: 'VER(TCO2e)', //CC Measurement ccStandard: 'GS', // CC standard GoldStandard Carbon Credit
ccStandard: 'GS', // CC standard Woodland Carbon Credit cancelPrice: { qty: '19.5', divisa: 'USD' },
countryCode: 'MX' // Country code ccProjectID: 'GS2441', // CC project ID in the original backlog
ccProjectID: 'GS2441', // CC project ID in the original backlog is it unique? countryCode: 'MX', // Country code
ccID: 17438-328-328,
CCAdquisitionRecipt: 'QmWWQSuPMS6aXCbZKpEjPHPUZN2NjB3YrhJTHsV4X3vb2t', //Recipt of CC in IPFS
cancelPrice: {
qty: 19.50,
divisa : 'USD'},
conversionPrice:{
USD: 19.50,
EUR: 17.93,
CNY: 137.93,
BTC: 0.0027,
LTC: 0.45,
ETH: 0.11,
},
cancelRemaks: 'Cancel by account 1067262', cancelRemaks: 'Cancel by account 1067262',
ccVintageEnd: 1546214400, // CC what end date it was created in UnixTime
ccMeasurement: 'VER(TCO2e)', //CC Measurement
ccVintageStart: 1514764800, // CC what date year it was created in UnixTime
conversionPrice: {
BTC: 0.0027239430402556876,
CNY: 137.92740063195478,
ETH: 0.10952545708624091,
EUR: 17.93351483640976,
GBP: 15.588145399888809,
LTC: 0.45310691929120134,
USD: 19.5
},
CCAdquisitionRecipt: 'QmYw6JTNABDDfXvc3U2BfURjF5Zzx9eG3N43wJqHc4upDL' //Recipt of CC in IPFS
} }
]
``` ```
## Requirements ## Requirements
@ -281,3 +282,37 @@ WantedBy=multi-user.target
``` ```
**NOTE: service must be set as active (running), if not please verify the preview steps** **NOTE: service must be set as active (running), if not please verify the preview steps**
# IPFS pin projects receipts
**You can help us by pinning DECA's project receipts into IPFS which will give deeper trust to DECA.**
We have a script in order to pin (replicate into your node) the projects receipts that DECA got, so that
any one can have a proof that we bought this receipts as public information. Also you can store a copy of this receipts which will be added by IPFS hash into this script.
> run the IPFS pin script
```sh
$ /receipts.sh
connect Qmd4Cv2fNwixP6cabEnTVFkF57GUGD6VBEcDhUkqHPG4X9 success
connect QmfBASmqe3Az9AUjCxx3dtomSmbZEiJCsXusPxznjNnjU5 success
connect QmQBAsbA49q7QrKhetJpbo5gKxQQiL6sxVXCep5skmuHsq success
connect QmZL1otpiCzWMEJTHXbQ5Hb4aFE7TKLjAuuBAAet1WAgtD success
IPFS PIN DECA's Carbon Credits Receipts?:
Enter Y for Yes, N for No:Y
Pinning Receipts by IPFS hash
pinned QmYw6JTNABDDfXvc3U2BfURjF5Zzx9eG3N43wJqHc4upDL recursively
IPFS GET(Download) DECA's Carbon Credits Receipts?:
Enter Y for Yes, N for No:Y
Getting Receipts by IPFS hash
Default receipts file is ./receipts...
Saving file(s) to ./proyectsReceipts
135.34 KiB / 135.34 KiB [======================================================================================================================================] 100.00% 0s
Bye ;)
$ ls proyectsReceipts
QmYw6JTNABDDfXvc3U2BfURjF5Zzx9eG3N43wJqHc4upDL
```
As you can see in the above example we pin the receipt by running " ./receipts.sh ", also we Download this receipt with should be allocated in the directory projects Receipts.

View File

@ -1,66 +1,66 @@
const moment = require('moment');
const request = require('request-promise'); const request = require('request-promise');
require('dotenv').config();
require('dotenv').config()
// Source: https://apiv2.bitcoinaverage.com/ // Source: https://apiv2.bitcoinaverage.com/
const divisaTraceback = async(timestamp, divisa, amount) => { const divisaTraceback = async(timestamp, divisa, amount) => {
let crypto = ['BTC', 'ETH', 'LTC'] let crypto = ['BTC', 'ETH', 'LTC']
let normal = ['USD', 'GBP', 'CNY', 'EUR']; let normal = ['USD', 'GBP', 'CNY', 'EUR'];
let opt = { method: 'GET', json: true };
let cryptoHeaders = {
'X-Testing': 'testing',
'x-ba-key': process.env.APIKEY
}
try {
if((Date.now() - 7776000000) <= (timestamp * 1000)) {
if(crypto.includes(divisa) || normal.includes(divisa)) {
if(amount > 0) { if(amount > 0) {
opt.headers = cryptoHeaders;
let divisaTraceback = {}; let divisaTraceback = {};
let resolution = await getResolution(timestamp);
if(crypto.includes(divisa)) { if(crypto.includes(divisa)) {
divisaTraceback[crypto.splice(crypto.indexOf(divisa), 1)[0]] = Number(amount); divisaTraceback[crypto.splice(crypto.indexOf(divisa), 1)[0]] = Number(amount);
Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount)); Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount, resolution));
Object.assign(divisaTraceback, await requestIteration(crypto, 'USD', false, timestamp, divisaTraceback['USD'])); Object.assign(divisaTraceback, await requestIteration(crypto, 'USD', false, timestamp, divisaTraceback['USD'], resolution));
} }
else { else if (normal.includes(divisa)) {
divisaTraceback[normal.splice(normal.indexOf(divisa), 1)[0]] = Number(amount); divisaTraceback[normal.splice(normal.indexOf(divisa), 1)[0]] = Number(amount);
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount)); Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount, resolution));
Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC'])); Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC'], resolution));
} } else throw new Error('Currency target invalid');
console.log(` Resolution: ${resolution}`);
return(divisaTraceback); return(divisaTraceback);
} else return('The amount must be greater that 0'); } else throw new Error('The amount must be 0 or higher');
} else return('Currency target invalid');
} else return('Date out of range')
} catch(e) {
return(e);
}
}; };
const requestIteration = async(set, divisa, flag, timestamp, amount) => { const requestIteration = async(set, divisa, flag, timestamp, amount, resolution) => {
timestamp = timestamp.format('X');
let opt = { let opt = {
method: 'GET', method: 'GET',
json: true, json: true,
headers: { headers: {
'X-Testing': 'testing',
'x-ba-key': process.env.APIKEY 'x-ba-key': process.env.APIKEY
} }
}; };
let res = {}; let res = {};
for (element in set) { for (element in set) {
if(flag) { if(flag) {
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=hour`; opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`;
let { average } = await request(opt); let { average, time } = await request(opt);
res[set[element]] = average * amount; res[set[element]] = average * amount;
} }
else { else {
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=hour`; opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`;
let { average } = await request(opt); let { average, time } = await request(opt);
res[set[element]] = amount / average; res[set[element]] = amount / average;
} }
} }
return res; return res;
}; };
const getResolution = async(timestamp) => {
let resolution = 'day';
if(process.env.APIMODE == 'dev') {
if(timestamp >= moment.utc().subtract(2, 'hours')) resolution = 'minute';
else if(timestamp >= moment().subtract(10, 'days')) resolution = 'hour';
} else if(process.env.APIMODE == 'startup' || process.env.APIMODE == 'grow') {
if(timestamp >= moment.utc().subtract(24, 'hours')) resolution = 'minute';
else if(timestamp >= moment.utc().subtract(31, 'days')) resolution = 'hour';
} else throw new Error('Bad config at .env file');
return resolution;
}
module.exports = divisaTraceback; module.exports = divisaTraceback;

View File

@ -3,7 +3,7 @@ const IpfsClient = require('ipfs-http-client');
const OrbitDB = require('orbit-db'); const OrbitDB = require('orbit-db');
//Instance of ipfs locally in ipfs daemon //Instance of ipfs locally in ipfs daemon
const node = IpfsClient('http://localhost:5001'); const node = IpfsClient('http://localhost:5001');
const addressDB = 'decaCCDB'; const addressDB = '/orbitdb/zdpuAykPJ4qtBg2toS2vxr5eaPfGEBJmvGerM7V7x8qn5c8hW/decaCCDB';
//Async function to replicate or create a doc database //Async function to replicate or create a doc database
async function createReplicate() { async function createReplicate() {

View File

@ -5,6 +5,7 @@ let inquirer = require('inquirer');
let moment = require('moment'); let moment = require('moment');
let hasher = require('node-object-hash') let hasher = require('node-object-hash')
const fs = require('fs'); const fs = require('fs');
const path = require('path');
const addressDB = 'decaCCDB'; const addressDB = 'decaCCDB';
@ -17,6 +18,9 @@ const hashSortCoerce = hasher({ sort: true, coerce: true });
//Function to start application //Function to start application
const main = async () => { const main = async () => {
if (!fs.existsSync('./receipts')){
fs.mkdirSync('./receipts');
}
const orbitdb = await OrbitDB.createInstance(ipfs) const orbitdb = await OrbitDB.createInstance(ipfs)
const db = await orbitdb.docs(addressDB, { indexBy: 'CCID' }); const db = await orbitdb.docs(addressDB, { indexBy: 'CCID' });
await db.load(); await db.load();
@ -36,8 +40,10 @@ const main = async () => {
}]); }]);
if (mainMenu == 'Show carbon credit records') { if (mainMenu == 'Show carbon credit records') {
let data = db.get(''); let data = db.get('');
console.log(` ${data.length} records found`);
while (true) { while (true) {
let { showMenu } = await inquirer.prompt([{ const fileName = moment.utc().format();
let { showMenu } = await inquirer.prompt({
type: 'list', type: 'list',
name: 'showMenu', name: 'showMenu',
message: 'Select an option', message: 'Select an option',
@ -45,24 +51,33 @@ const main = async () => {
'Show in terminal', 'Show in terminal',
'Save as JSON', 'Save as JSON',
'Save as CSV', 'Save as CSV',
'Save as TXT',
'Back to main menu' 'Back to main menu'
] ]
}]); });
if (showMenu == 'Show in terminal') { if (showMenu == 'Show in terminal') {
for (element in data) { for (element in data) {
showData(data[element], 0); showData(data[element]);
console.log('-------------------------------------\n'); console.log('-------------------------------------\n');
} }
} else if (showMenu == 'Save as JSON') { } else if (showMenu == 'Save as JSON') {
fs.writeFileSync(`/tmp/${moment().format()}.json`, JSON.stringify(data)); fs.writeFileSync(path.resolve('receipts', `${fileName}.json`), JSON.stringify(data));
console.log(`File saved in /tmp/${moment().format()}.json`); console.log(`File saved in ${path.resolve('receipts', fileName + '.json')}`);
} else if (showMenu == 'Save as CSV') { } else if (showMenu == 'Save as CSV') {
let csv = ''; let csv = await JSONToCSV(data);
for (element in data) csv += await JSONToCSV(data[element]); fs.writeFileSync(path.resolve('receipts', `${fileName}.csv`), csv);
fs.writeFileSync(`/tmp/${moment().format()}.csv`, csv); console.log(`File saved in ${path.resolve('receipts', fileName + '.csv')}`);
console.log(`File saved in /tmp/${moment().format()}.csv`); } else if (showMenu == 'Save as TXT') {
let stream = fs.createWriteStream(path.resolve('receipts', `${fileName}.txt`));
let result = await elemInsert(data);
stream.once('open', function (fd) {
for (let i = 0; i < result.length; i++) {
stream.write(result[i] + '\n');
} }
else if (showMenu == 'Back to main menu') { stream.end();
});
console.log(`File saved in ${path.resolve('receipts', fileName + '.txt')}`);
} else if (showMenu == 'Back to main menu') {
break; break;
} }
} }
@ -91,7 +106,7 @@ const main = async () => {
} }
while (true) { while (true) {
await init(); await init();
console.clear(); //console.clear();
console.log('Select one to modify the field.\n'); console.log('Select one to modify the field.\n');
let carbonCreditMenu = await generateData(carbonCredit, 0); let carbonCreditMenu = await generateData(carbonCredit, 0);
let { insertMenu } = await inquirer.prompt([{ let { insertMenu } = await inquirer.prompt([{
@ -111,16 +126,18 @@ const main = async () => {
}]); }]);
if (insertMenu == 'Insert current register') { if (insertMenu == 'Insert current register') {
if (await validateInsertion(carbonCredit)) { if (await validateInsertion(carbonCredit)) {
try{
carbonCredit.conversionPrice = await divisaTraceback( carbonCredit.conversionPrice = await divisaTraceback(
moment.utc(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix(), moment.utc(carbonCredit.cancelDate, 'DD-MM-YYYY HH:mm Z'),
carbonCredit.cancelPrice.divisa, carbonCredit.cancelPrice.divisa,
carbonCredit.cancelPrice.qty carbonCredit.cancelPrice.qty
); );
showData(carbonCredit, 0); if(carbonCredit.conversionPrice === 'error') throw new Error('Error at getting currencies price.');
showData(carbonCredit);
serialNoPart=carbonCredit.SerialNo.split('-'); serialNoPart=carbonCredit.SerialNo.split('-');
serialFloor=serialNoPart[7]; serialFloor=serialNoPart[7];
serialTop=serialNoPart[8]; serialTop=serialNoPart[8];
var totalCC = 1+parseInt(serialTop)-parseInt(serialFloor); let totalCC = 1+parseInt(serialTop)-parseInt(serialFloor);
let { input } = await inquirer.prompt([{ let { input } = await inquirer.prompt([{
type: 'input', type: 'input',
name: 'input', name: 'input',
@ -131,22 +148,33 @@ const main = async () => {
carbonCredit.cancelDate = moment(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix(); carbonCredit.cancelDate = moment(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
carbonCredit.ccVintageStart = moment(carbonCredit.ccVintageStart + ' +0000', 'DD-MM-YYYY HH:mm Z').unix(); carbonCredit.ccVintageStart = moment(carbonCredit.ccVintageStart + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
carbonCredit.ccVintageEnd = moment(carbonCredit.ccVintageEnd + ' +0000', 'DD-MM-YYYY HH:mm Z').unix(); carbonCredit.ccVintageEnd = moment(carbonCredit.ccVintageEnd + ' +0000', 'DD-MM-YYYY HH:mm Z').unix();
const fileName = moment.utc().format();
let stream = fs.createWriteStream(path.resolve('receipts', fileName + '.txt'));
stream.once('open', async function (fd) {
carbonCredit.CCID = await hashSortCoerce.hash(carbonCredit); carbonCredit.CCID = await hashSortCoerce.hash(carbonCredit);
const hash = await db.put(carbonCredit); const hash = await db.put(carbonCredit);
console.log(`Successful insertion, CCID: ${carbonCredit.CCID}, hash: ${hash}.`); stream.write(`CCID: ${carbonCredit.CCID} SerialNo: ${carbonCredit.SerialNo} \n`);
for(var i=parseInt(serialFloor)+1; i<=serialTop; i++){ for (let i = parseInt(serialFloor) + 1; i <= serialTop; i++) {
var tempCarbonCredit = {...carbonCredit}; let tempCarbonCredit = {
...carbonCredit
};
tempCarbonCredit.SerialNo = await asyncCCSerialNo(serialNoPart, i) tempCarbonCredit.SerialNo = await asyncCCSerialNo(serialNoPart, i)
tempCarbonCredit.CCID = await hashSortCoerce.hash(tempCarbonCredit); tempCarbonCredit.CCID = await hashSortCoerce.hash(tempCarbonCredit);
const hash = await db.put(tempCarbonCredit); const hash = await db.put(tempCarbonCredit);
console.log(`Successful insertion, CCID: ${tempCarbonCredit.CCID}, hash: ${hash}.`); stream.write(`CCID: ${tempCarbonCredit.CCID} SerialNo: ${tempCarbonCredit.SerialNo} \n`);
} }
stream.end();
});
console.log(`Inserted ${totalCC} carbon credits.`); console.log(`Inserted ${totalCC} carbon credits.`);
console.log(`Receipts saved in ${path.resolve('receipts', fileName + '.txt')}`);
break; break;
} else { } else {
console.clear(); console.clear();
console.log('Insertion aborted'); console.log('Insertion aborted');
break; continue;
}
} catch(e) {
console.log(`Error at divisaTraceback: ${e.message}`);
} }
} }
else { else {
@ -182,12 +210,15 @@ const main = async () => {
if (input == '') console.log('Input mustn\'t be empty'); if (input == '') console.log('Input mustn\'t be empty');
else if (insertMenu == 'issueDate' || insertMenu == 'cancelDate' || insertMenu == 'ccVintageStart' || insertMenu == 'ccVintageEnd') { else if (insertMenu == 'issueDate' || insertMenu == 'cancelDate' || insertMenu == 'ccVintageStart' || insertMenu == 'ccVintageEnd') {
const date = moment.utc(input + ' +0000', 'DD-MM-YYYY HH:mm'); const date = moment.utc(input + ' +0000', 'DD-MM-YYYY HH:mm');
if (date.isValid() && insertMenu == 'cancelDate' && moment() >= date && date >= moment().subtract(10, 'days')) { console.log(date);
if(date.isValid() && moment.utc() >= date) {
if(insertMenu != 'cancelDate') carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
else if((date >= moment.utc().subtract(3, 'months') && process.env.APIMODE == 'dev') ||
(date >= moment.utc().subtract(1, 'years') && process.env.APIMODE == 'startup') ||
(date >= moment.utc().subtract(7, 'years') && process.env.APIMODE == 'grow')) {
carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z'); carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
} } else console.error(' Date is too old to get pricing data');
else if (date.isValid() && insertMenu != 'cancelDate' && moment() >= date) { } else console.error(' Date must follow next syntax: dd-mm-yyyy hh-mm and can\'t be in the future');
carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
} else console.log(' Date must follow next syntax: dd-mm-yyyy hh-mm and cancel date must be 10 days old at most');
} }
else if (insertMenu == 'qty') { else if (insertMenu == 'qty') {
if (parseFloat(input)) carbonCredit.cancelPrice[insertMenu] = input; if (parseFloat(input)) carbonCredit.cancelPrice[insertMenu] = input;
@ -204,11 +235,13 @@ const main = async () => {
else if (mainMenu == 'Search carbon credits') { else if (mainMenu == 'Search carbon credits') {
let { searchMenu } = await inquirer.prompt([{ let { searchMenu } = await inquirer.prompt([{
type: 'list', type: 'list',
pageSize: 24,
name: 'searchMenu', name: 'searchMenu',
message: 'Search carbon credit by:', message: 'Search carbon credit by:',
choices: [ choices: [
'Category', 'Category',
'CCID', 'CCID',
'SerialNo',
'Standard', 'Standard',
'Project Id', 'Project Id',
'Vintage Date', 'Vintage Date',
@ -237,8 +270,12 @@ const main = async () => {
message: 'Select CCID', message: 'Select CCID',
choices: choicesSearch choices: choicesSearch
}]).then(answer => { }]).then(answer => {
console.log(db.get(answer.CCID)) let query = db.get(answer.CCID);
}) for (element in query) {
showData(query[element]);
console.log('-------------------------------------\n');
}
});
} else { } else {
console.clear(); console.clear();
console.log("No registries found") console.log("No registries found")
@ -248,6 +285,28 @@ const main = async () => {
console.log('Search canceled'); console.log('Search canceled');
} }
} }
else if (searchMenu == 'SerialNo') {
let { input } = await inquirer.prompt([{
type: 'input',
name: 'input',
message: 'SerialNo: ',
}]);
let { confirm } = await inquirer.prompt([{
type: 'input',
name: 'confirm',
message: 'Confirm y | n: ',
}]);
if (confirm == 'y') {
const data = db.query((doc) => doc.SerialNo == input);
for (element in data) {
showData(data[element]);
console.log('-------------------------------------\n');
}
} else {
console.clear();
console.log('Search canceled');
}
}
else if (searchMenu == 'CCID') { else if (searchMenu == 'CCID') {
let { input } = await inquirer.prompt([{ let { input } = await inquirer.prompt([{
type: 'input', type: 'input',
@ -260,7 +319,8 @@ const main = async () => {
message: 'Confirm y | n: ', message: 'Confirm y | n: ',
}]); }]);
if (confirm == 'y') { if (confirm == 'y') {
console.log(db.get(input)) showData(db.get(input)[0]);
console.log('-------------------------------------\n');
} else { } else {
console.clear(); console.clear();
console.log('Search canceled'); console.log('Search canceled');
@ -287,7 +347,11 @@ const main = async () => {
message: 'Select CCID', message: 'Select CCID',
choices: choicesSearch choices: choicesSearch
}]).then(answer => { }]).then(answer => {
console.log(db.get(answer.CCID)) let query = db.get(answer.CCID);
for (element in query) {
showData(query[element]);
console.log('-------------------------------------\n');
}
}) })
} else { } else {
console.clear(); console.clear();
@ -319,7 +383,11 @@ const main = async () => {
message: 'Select CCID', message: 'Select CCID',
choices: choicesSearch choices: choicesSearch
}]).then(answer => { }]).then(answer => {
console.log(db.get(answer.CCID)) let query = db.get(answer.CCID);
for (element in query) {
showData(query[element]);
console.log('-------------------------------------\n');
}
}) })
} else { } else {
console.clear(); console.clear();
@ -333,6 +401,7 @@ const main = async () => {
else if (searchMenu == 'Vintage Date') { else if (searchMenu == 'Vintage Date') {
let { vintageMenu } = await inquirer.prompt([{ let { vintageMenu } = await inquirer.prompt([{
type: 'list', type: 'list',
pageSize: 24,
name: 'vintageMenu', name: 'vintageMenu',
message: 'Search carbon credit by:', message: 'Search carbon credit by:',
choices: [ choices: [
@ -364,7 +433,11 @@ const main = async () => {
message: 'Select CCID', message: 'Select CCID',
choices: choicesSearch choices: choicesSearch
}]).then(answer => { }]).then(answer => {
console.log(db.get(answer.CCID)) let query = db.get(answer.CCID);
for (element in query) {
showData(query[element]);
console.log('-------------------------------------\n');
}
}) })
} else { } else {
console.clear(); console.clear();
@ -397,7 +470,11 @@ const main = async () => {
message: 'Select CCID', message: 'Select CCID',
choices: choicesSearch choices: choicesSearch
}]).then(answer => { }]).then(answer => {
console.log(db.get(answer.CCID)) let query = db.get(answer.CCID);
for (element in query) {
showData(query[element]);
console.log('-------------------------------------\n');
}
}) })
} else { } else {
console.clear(); console.clear();
@ -433,7 +510,11 @@ const main = async () => {
message: 'Select CCID', message: 'Select CCID',
choices: choicesSearch choices: choicesSearch
}]).then(answer => { }]).then(answer => {
console.log(db.get(answer.CCID)) let query = db.get(answer.CCID);
for (element in query) {
showData(query[element]);
console.log('-------------------------------------\n');
}
}) })
} else { } else {
console.clear(); console.clear();
@ -514,6 +595,14 @@ const elemSearch = async (objectSearch) => {
return elems; return elems;
} }
const elemInsert = async (objectShow) => {
let elems = [];
for (let key in objectShow) {
elems.push('CCID: ' + objectShow[key]['CCID'] + ' SerialNumber: ' + objectShow[key]['SerialNo']);
}
return elems;
}
const generateData = async (data, n) => { const generateData = async (data, n) => {
let menu = []; let menu = [];
let tab = ' '.repeat(n); let tab = ' '.repeat(n);
@ -522,7 +611,7 @@ const generateData = async (data, n) => {
menu.push({ name: element, disabled: ' ' }); menu.push({ name: element, disabled: ' ' });
menu = menu.concat(await generateData(data[element], n + 1)); menu = menu.concat(await generateData(data[element], n + 1));
} else { } else {
let space = ' '.repeat(20 - element.length); let space = ' '.repeat(25 - element.length);
space = space.substring(0, space.length - tab.length); space = space.substring(0, space.length - tab.length);
menu.push(`${tab}${element}${space}${data[element]}`); menu.push(`${tab}${element}${space}${data[element]}`);
} }
@ -536,7 +625,7 @@ const asyncCCSerialNo = async (serialNoPart, index) => {
} }
//Show carbon credit data //Show carbon credit data
const showData = async (data, n) => { const showData = async (data, n = 0) => {
for (element in data) { for (element in data) {
for (let i = 0; i < n; i++) process.stdout.write('\t'); for (let i = 0; i < n; i++) process.stdout.write('\t');
if (typeof (data[element]) == 'object') { if (typeof (data[element]) == 'object') {
@ -575,14 +664,28 @@ const showErrorData = async (data) => {
//Parse json to csv //Parse json to csv
const JSONToCSV = async (data, n) => { const JSONToCSV = async (data, n) => {
let csv = ''; let csv;
for (element in data) { for (element in data[0]) {
if (typeof (data[element]) == 'object') { console.log(typeof(data[0][element]));
validateInsertion(data[element]); console.log(data[0][element]);
if (typeof data[0][element] == 'object') {
console.log('Entro aqui')
for(subelement in data[0][element]) csv += `${element} - ${subelement}, `;
} }
else csv += `${data[element]}, ` else csv += `${element}, `;
}
csv = csv.slice(0, -2);
csv += '\n';
for(record in data) {
for (element in data[record]) {
if (typeof data[record][element] == 'object') {
for(subelement in data[0][element]) csv += `${data[record][element][subelement]}, `;
}
else csv += `${data[record][element]}, `;
}
csv = csv.slice(0, -2);
csv += '\n';
} }
if (n == 0) csv += '\n';
return csv; return csv;
} }

43
receipts.sh Executable file
View File

@ -0,0 +1,43 @@
#!/bin/bash
./connectNodes.sh
# Receipts and Files to PIN
FilesHashList=("QmYw6JTNABDDfXvc3U2BfURjF5Zzx9eG3N43wJqHc4upDL" ) # \
#"" \
#"" \)
echo ""
echo "IPFS PIN DECA's Carbon Credits Receipts?:"
read -p "Enter Y for Yes, N for No:" choice
case "$choice" in
Y | y) echo "Pinning Receipts by IPFS hash";
for val in ${FilesHashList[*]}; do
/usr/local/bin/ipfs pin add $val
done
;;
N | n) ;;
*) echo "no option selected";;
esac
receiptsRoute=./proyectsReceipts
echo ""
echo "IPFS GET(Download) DECA's Carbon Credits Receipts?:"
read -p "Enter Y for Yes, N for No:" choice
case "$choice" in
Y | y) echo "Getting Receipts by IPFS hash";
echo "Default receipts file is ./receipts..."
mkdir $receiptsRoute
for val in ${FilesHashList[*]}; do
/usr/local/bin/ipfs get $val -o $receiptsRoute
done
;;
N | n) ;;
*) echo "no option selected";;
esac
echo "Bye ;) $USERNAME"