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
/orbitdb
/receipts
/proyectsReceipts
package-lock.json
.env
.directory

View File

@ -15,39 +15,40 @@ DECA's Carbon Credits OrbitDB Code and configs, everything you need for setting
## Carbon Credit Structure
```sh
{
/*
CC - Carbon credits
Unix Time is date and time based in the UTC 0
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',
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
cancelDate: 1587214800, // CC cancelation cancelation UnixTime
ccVintageStart: 1514764800, // CC what year it was created in UnixTime
ccVintageEnd: 1514764800, // CC what year it was created in UnixTime
cancelDate: 1587168000, // CC cancelation cancelation UnixTime
ccCategory: 'Energy Efficiency - Domestic', // CC category agricultural
ccMeasurement: 'VER(TCO2e)', //CC Measurement
ccStandard: 'GS', // CC standard Woodland Carbon Credit
countryCode: 'MX' // Country code
ccProjectID: 'GS2441', // CC project ID in the original backlog is it unique?
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,
},
ccStandard: 'GS', // CC standard GoldStandard Carbon Credit
cancelPrice: { qty: '19.5', divisa: 'USD' },
ccProjectID: 'GS2441', // CC project ID in the original backlog
countryCode: 'MX', // Country code
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
@ -281,3 +282,37 @@ WantedBy=multi-user.target
```
**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');
require('dotenv').config()
require('dotenv').config();
// Source: https://apiv2.bitcoinaverage.com/
const divisaTraceback = async(timestamp, divisa, amount) => {
let crypto = ['BTC', 'ETH', 'LTC']
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) {
opt.headers = cryptoHeaders;
let divisaTraceback = {};
let resolution = await getResolution(timestamp);
if(crypto.includes(divisa)) {
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(crypto, 'USD', false, timestamp, divisaTraceback['USD']));
Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount, resolution));
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);
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount));
Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC']));
}
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount, resolution));
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);
} else return('The amount must be greater that 0');
} else return('Currency target invalid');
} else return('Date out of range')
} catch(e) {
return(e);
}
} else throw new Error('The amount must be 0 or higher');
};
const requestIteration = async(set, divisa, flag, timestamp, amount) => {
const requestIteration = async(set, divisa, flag, timestamp, amount, resolution) => {
timestamp = timestamp.format('X');
let opt = {
method: 'GET',
json: true,
headers: {
'X-Testing': 'testing',
'x-ba-key': process.env.APIKEY
}
};
let res = {};
for (element in set) {
if(flag) {
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=hour`;
let { average } = await request(opt);
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`;
let { average, time } = await request(opt);
res[set[element]] = average * amount;
}
else {
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=hour`;
let { average } = await request(opt);
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`;
let { average, time } = await request(opt);
res[set[element]] = amount / average;
}
}
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;

View File

@ -3,7 +3,7 @@ const IpfsClient = require('ipfs-http-client');
const OrbitDB = require('orbit-db');
//Instance of ipfs locally in ipfs daemon
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 createReplicate() {

View File

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

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"