You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 lines
8.3 KiB

#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.2020
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
###########################################################################################
# tag_OP.sh OPERATION ON G1TAG...
# sudo pip3 install duniterpy
###########################################################################################
# ipfs ls /ipns/QmY6ooFcsGXY4VSindqKMz9GvrprFXY32RrsTmqvVTYQX5
# QmZ3RfWk1u5LEGYLHA633B5TNJy3Du27K6Fny9wcxpowGS 4 TAG_amount
# QmXizDydPuiBd1V7JBVY5kdppShm9xdk2aoYj7c8V8Foxe 47 TAG_chain
# QmeHHBZKtLJHr11rwXKeimwDE4i3nLMy2VPkWZsNaFasg1 4 TAG_currency
# QmSjBDpjnoxnQT5RWRnyLxqwE3ovSRVb7xjwZoiufZAv3E 39 TAG_id
# QmUQcSjQx2bg4cSe2rUZyQi6F8QtJFJb74fWL7D784UWf9 2 TAG_n
# QmdbdBWv4SygqaaeWHAvABiQAa3faf4ncfSyzRnx8BDdCc 20 TAG_nanodate
# QmbN6gNM2vjLj7yaJ6xJcySDxmuSK2eB9ZDv5dpj2bo5pM 47 TAG_nodeid
# QmXaMzRqhEs4cQMRN18FWaCeqGmrDBfV8hTkbfQuBCXnnA 1243 TAG_publishkey.swarm.crypt
# QmWHsRZCTSjSxnp1Qm8WcmHNNQTJwDS2ukLE7NTccc3Kp9 1304 TAG_publishkey.B.gpg
# ...
# TAG_passenger, TAG_file is used to attach G1Tag to file.
#
# TRANSFERT ZEN FROM G1Tag to G1Tag
# G1Tag J References IPNS G1 Tag link
JSOURCE=$1
JDEST=$2
# Zen Amount to transfert
VALUE=$3
# Needed to Open ipfs publish Key
KEYFILE=$4
############################################
############## INIT & root check
NANODATE=$(date -u +%s%N)
echo "__SUB:tag_OP.sh: TRANSFER from $JSOURCE (1) to $JDEST (2) G1Tag = $VALUE (3) using \"$KEYFILE\" (4)... GO... $NANODATE"
if [[ "$USER" != "root" ]]; then echo "__SUB:tag_OP.sh: Hey, $USER you must at least be admin of your system. sudo -s ?"; exit; fi
#############################################
############## CHECK ipfs daemon user
YOU=$(ps aux --sort=+utime | grep ipfs | tail -n 1 | cut -d " " -f 1)
if [[ "$YOU" == "" || "$YOU" == "root" ]]; then echo "__SUB:tag_OP.sh: BAD IPFS. Aucune installation IPFS satisfaisante ici... Ciao $YOU !"; exit; fi
IPFSNODEID=$(su $YOU -c "ipfs id -f='<id>\n'")
#############################################
############## SOURCE EXTRACTION
JSOURCERR=$(su $YOU -c "ipfs cat /ipns/$JSOURCE/TAG_id | sed s/\ //g");
#############################################
############## GOOD or BAD ?
if [[ "$JSOURCERR" == "" ]]; then echo "__SUB:tag_OP.sh: G1TAG DETRUIT. Aucun G1Tag /ipns/$JSOURCERR ici... Ciao."; exit; fi
echo "__SUB:tag_OP.sh: LOG. SOURCE G1Tag OK!! GREAT... Retrieving it all: ipfs get --output=./TAG/${JSOURCERR}/ /ipns/$JSOURCE"
mkdir -p ./TAG/${JSOURCERR}; rm -f ./TAG/${JSOURCERR}/*
chown -R $YOU ./TAG/${JSOURCERR}/
su $YOU -c "ipfs get --output=./TAG/${JSOURCERR}/ /ipns/$JSOURCE"
JSOURCEPUBLISHKEY="./TAG/${JSOURCERR}/TAG_publishkey.swarm.crypt"
JSOURCEVALUE=$(cat ./TAG/${JSOURCERR}/TAG_amount);
#############################################
############## No JDEST, then return $JSOURCEVALUE
if [[ "$JDEST" == "" ]]; then echo "__SUB:tag_OP.sh: OK. G1Tag $JSOURCE VALUE = $JSOURCEVALUE Zen"; exit; fi
#############################################
############## TEST IF $VALUE IS AVAILABLE
FINALSOURCE=$( echo "${JSOURCEVALUE} - ${VALUE}" | bc -l )
if [[ $FINALSOURCE -lt 0 ]]; then echo "__SUB:tag_OP.sh: KO. La valeur de ce G1Tag ne peut pas être : $FINALSOURCE Zen... Ciao $YOU !"; exit; fi
#############################################
############## EXTRACT $JSOURCERR PUBLISH KEY
if [[ -f $KEYFILE && ! -f "/home/$YOU/.ipfs/keystore/$JSOURCERR" ]]; then
./shell/natools.py decrypt -k "$KEYFILE" -i "$JSOURCEPUBLISHKEY" -o "/home/$YOU/.ipfs/keystore/$JSOURCERR"
else
if [[ ! -f $KEYFILE ]]; then
echo "__SUB:tag_OP.sh: KO. La clef de dévérouillage pour $JSOURCERR est inexistante. Contact: https://g1sms.fr"
fi
fi
#############################################
############## DESTINATION EXTRACTION
JDESTRR=$(su $YOU -c "ipfs cat /ipns/$JDEST/TAG_id | sed s/\ //g");
if [[ "$JDESTRR" == "" ]]; then echo "__SUB:tag_OP.sh: BAD G1TAG. Aucun G1Tag /ipns/$JDESTRR ici... Ciao."; exit; fi
mkdir -p ./TAG/${JDESTRR}; rm -f ./TAG/${JDESTRR}/*
echo "__SUB:tag_OP.sh: LOG. Hey You get a G1Tag!! GREAT... ipfs get --output=./TAG/${JDESTRR}/ /ipns/$JDEST"
chown -R $YOU ./TAG/${JDESTRR}/
su $YOU -c "ipfs get --output=./TAG/${JDESTRR}/ /ipns/$JDEST"
JDESTPUBLISHKEY="./TAG/${JDESTRR}/TAG_publishkey.swarm.crypt"
JDESTVALUE=$(cat ./TAG/${JDESTRR}/TAG_amount);
FINALDEST=$(echo "${JDESTVALUE} + ${VALUE}" | bc -l)
# EXTRACT $JSOURCERR PUBLISH KEY to "/home/$YOU/.ipfs/keystore/JSOURCE"
# EXTRACT PUBLISH KEY
if [[ -f $KEYFILE && ! -f "/home/$YOU/.ipfs/keystore/$JDESTRR" ]]; then
./shell/natools.py decrypt -k "$KEYFILE" -i "$JDESTPUBLISHKEY" -o "/home/$YOU/.ipfs/keystore/$JDESTRR"
else
if [[ ! -f $KEYFILE ]]; then
echo "__SUB:tag_OP.sh: KO. La clef de dévérouillage pour $JDESTRR est inexistante. Contact: https://g1sms.fr"
fi
fi
######################################################################################################
if [[ -f "/home/$YOU/.ipfs/keystore/$JDESTRR" && -f "/home/$YOU/.ipfs/keystore/$JSOURCERR" ]]; then
# IF PUBLISH key for BOTH G1TAG exists
#############################################
############## SOURCE OPERATION
NN=$(cat ./TAG/${JSOURCERR}/TAG_n); ((NN++)); echo "$NN" > "./TAG/${JSOURCERR}/TAG_n"
#############################################
# Destroy B Access ?!!
#rm -f ./TAG/${JSOURCERR}/TAG_publishkey.B.gpg
echo "$FINALSOURCE" > "./TAG/${JSOURCERR}/TAG_amount"
echo "${NANODATE}" > "./TAG/${JSOURCERR}/TAG_nanodate" # TODO: ACTIVATE NANODATE CONTROL / NODES MEDIANTIME
echo "${IPFSNODEID}" > "./TAG/${JSOURCERR}/TAG_writerid"
if [[ "$JSOURCE" == "$JDEST" ]]; then
# PUT "READY" IN TAG_passenger (can now be loaded with a file), now actual IPFSNODEID is creator
echo "READY" > "./TAG/${JSOURCERR}/TAG_passenger"
echo "${IPFSNODEID}" > "./TAG/${JSOURCERR}/TAG_nodeid"
echo "__SUB:tag_OP.sh: ${IPFSNODEID} NODE created REC TAG. ${JSOURCERR}. READY to LOAD_passenger now!!"
fi
SOURCENODEID=$(cat "./TAG/${JSOURCERR}/TAG_nodeid")
I=$(su $YOU -c "ipfs add -qr ./TAG/${JSOURCERR} | tail -n 1")
# RECORD TAG_chain And HASH again
echo "$I" > "./TAG/${JSOURCERR}/TAG_chain"
I=$(su $YOU -c "ipfs add -qr ./TAG/${JSOURCERR} | tail -n 1")
# IPNS $JSOURCERR PUBLISH
J=$(su $YOU -c "ipfs name publish -k ${JSOURCERR} --quieter /ipfs/${I}")
# CLEAN IPFS keystore
if [[ "${SOURCENODEID}" != "${IPFSNODEID}" ]]; then
rm -f "/home/$YOU/.ipfs/keystore/$JSOURCERR"
fi
echo "__SUB:tag_OP.sh: G1Tag $JSOURCERR value ($FINALSOURCE) :: su $YOU -c \"ipfs cat /ipns/$JSOURCE/TAG_amount\""
if [[ "$JSOURCE" != "$JDEST" ]]; then
#############################################
############## DESTINATION OPERATION
NN=$(cat ./TAG/${JDESTRR}/TAG_n); ((NN++)); echo "$NN" > "./TAG/${JDESTRR}/TAG_n"
#############################################
# Destroy B Access ?!!
#rm -f ./TAG/${JDESTRR}/TAG_publishkey.B.gpg
echo "${FINALDEST}" > "./TAG/${JDESTRR}/TAG_amount"
##############################################################################################################################
if [[ $FINALSOURCE -eq 0 ]]; then
echo "OK. G1Tag ${JSOURCERR} is empty destroying it!!";
rm -f "./TAG/${JDESTRR}/TAG_id";
fi
##############################################################################################################################
echo "${NANODATE}" > "./TAG/${JDESTRR}/TAG_nanodate"
echo "${IPFSNODEID}" > "./TAG/${JDESTRR}/TAG_writerid"
DESTNODEID=$(cat "./TAG/${JDESTRR}/TAG_nodeid")
I=$(su $YOU -c "ipfs add -qr ./TAG/${JDESTRR} | tail -n 1")
# RECORD TAG_chain And HASH again
echo "$I" > "./TAG/${JDESTRR}/TAG_chain"
I=$(su $YOU -c "ipfs add -qr ./TAG/${JDESTRR} | tail -n 1")
# IPNS $JDESTRR PUBLISH
J=$(su $YOU -c "ipfs name publish -k ${JDESTRR} --quieter /ipfs/${I}")
if [[ "${DESTNODEID}" != "${IPFSNODEID}" ]]; then
rm -f "/home/$YOU/.ipfs/keystore/$JDESTRR"
fi
echo "__SUB:tag_OP.sh: G1Tag $JDESTRR value ($FINALDEST) :: su $YOU -c \"ipfs cat /ipns/$JDEST/TAG_amount\""
fi
echo "__SUB:tag_OP.sh: DONE. G1Tag balance updated:
$JSOURCERR (/ipns/$JSOURCE) : $JSOURCEVALUE => $FINALSOURCE
$JDESTRR (/ipns/$JSDEST) : $JDESTVALUE) => $FINALDEST"
else
echo "__SUB:tag_OP.sh: NO OPERATION DONE"
fi
######################################################################################################