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.
 
 
 
 
 
 

302 lines
14 KiB

#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.2020
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
###########################################################################################
# == G1tag ==
# IPFS ZEN WALLETS
# RJ: READ G1Tag
# BJ: WRITE G1Tag
# WRITE ACCESS ONLY for B possessor and NODE G1Tag Creator
# TODO: MAKE http API for G1Tag accessible through local ipfs gateway !!!!
# https://github.com/typicode/lowdb USE json and ipfs DAG??!
#
# TAG 250 => Imprime un G1Tag rempli de 250 UNIT
# TAG 2 100 => Imprime 100 G1Tag BON de 2 UNIT
# PHONE any ./wallets/IDs
# TODO: for multi-channel -> Use HASHLINK + UIDNA + NODE_UIDNA + AMOUNT ...
###########################################################################################
source ./shell/init.sh
source ./shell/functions.sh
log "__SUB:sms_G1TAG.sh: START ($1=PHONE, $2=AMOUNT, $3=NUMBER)"
PHONE=$1
AMOUNT=$2
if [[ "$AMOUNT" == "" ]]; then AMOUNT=0; fi
NUMBER=$3
# DEFAULT TYPE IS PRINTED G1TAG
# BUT COULD BE USED TO CARRY VALUE INTO IPFS STORED FILES
# FIRST ATTEMPT IS TO COPY YOUTUBE INTO M4A AND LEND THEM G1.
TYPE=$4 # This is the type of passenger: YOUTUBE, NODE (EMPTY = default means sticker printed G1Tag)
# Give ipfs hash of any External ID like YOUTUBEID ;)
PASSENGER=$5
# == G1tag ==
# IPFS G1sms Wallet to TAG creation/relation manager
# TODO Modulate access to TAGs with IPNS publish key cypher
# A/R: (G1TagID)
# B: (GPG Key for HUMAN WRITE access)
# J: (IPNS link to G1tag storage)
# MASTER: Global SWARM Access
###############################################################################################
# Initialize PHONE, PIN, PUBKEY, UNIT, MEMBER, MEMRIB, EMAIL, etc...
sms_INIT_ACCOUNT "$PHONE" "NOSMS"
if [[ $UNKNOWN == "unknown" ]]; then
sms_ERROR "$PHONE" "Porte-monnaie inconnu. Envoyez N (suivi de votre Pseudo membre) pour le créer."
log "__SUB:sms_G1TAG.sh: Porte-monnaie inconnu"
exit
fi
###########################################################
# CALCULATE if wallet have enough for VIR+PERCENT
AMOUNT=$2 # BE CAREFULL AMOUNT was changed by 1st sms_INIT_ACCOUNT!!
if [[ "$AMOUNT" == "" ]]; then AMOUNT=0; fi
if [[ ! $PASSENGER && "$NUMBER" != "" ]]; then
# MULTI TAG NUMBER ($NUMBER = How many tags to print)
AMOUNT=$(bc -l <<< "$AMOUNT * $NUMBER")
else
NUMBER=1
fi
VIR=0
accounting=($(make_accounting))
log "__SUB:sms_G1TAG.sh: $(declare -p accounting)"
testmin=${accounting[0]}
VIR=${accounting[1]}
PERCENT=${accounting[2]}
CHARGE=${accounting[3]}
if [[ $testmin -eq 0 ]]; then
sms_SEND "$PHONE" "Le solde de votre Porte-monnaie est insuffisant. Minimum requis = ${CHARGE} + ${PERCENT} (+ ${LIMIT}) G1! Rechargez avec https://cesium.app"
exit
fi
##### IS G1DAB NODE?
if [[ ! $PASSENGER && ! $G1DAB ]]; then
if [[ -f ./wallets/$PHONE/ipfsid.G1TAGNODE ]]; then
DABnode=$(cat ./wallets/$PHONE/ipfsid.G1TAGNODE)
if [[ "$DABnode" == "$IPFSNODEID" ]]; then
sms_ERROR "$PHONE" "IMPRIMANTE G1Tag sur $DABnode non détectée!! Choisissez un autre G1Dab..."
log "__SUB:sms_G1TAG.sh: IMPRIMANTE G1Tag non détectée!!"
exit
fi
fi
fi
# TODO: SEARCH PROXIMITY DAB LOCATION (GeoPoint + GPS)...
# for node in ./wallets_swarm/.*/*.where; do
# done
###########################################################
# silkaj NODE CHANGE !!!
#SILKAJNODE=$(./shell/checknodes.sh)
#log "NEW SILKAJ NODE: $SILKAJNODE"
###########################################
# PAY VIR + PERCENT G1
ML=$(bc -l <<< "scale=2; $VIR + $PERCENT")
PAY=$(./shell/timeout.sh -t 29 ./silkaj/silkaj transaction --auth-scrypt -salt="$PHONE" -password="$PIN" --amount="$ML" --output="$NODE_G1PUBKEY" --comment="[G1sms+] ACHAT $3 G1Tag(s) - $2 $UNIT" -y)
if [[ "$PAY" == "" || "$(echo $PAY | cut -d '|' -f 1)" == "KO" ]]; then
# new=$(./shell/checknodes.sh "BAN")
sms_ERROR "$PHONE" "Il est survenu un problème lors de votre virement: $PAY / Silkaj: $new";
exit
else
cents=$(echo $(bc -l <<< "scale=0; $ML * 100") | cut -d '.' -f 1)
move_g1cents "$PHONE" "$NODE_G1PUBKEY" "$cents"
fi
#############################################
log "__SUB:sms_G1TAG.sh: CREATE $NUMBER x $2 $UNIT G1Tag(s) for $MEMBER ($PHONE) : PAYEMENT $ML G1 to $NODE_G1PUBKEY: $PAY"
c=0
while [[ $c -lt $NUMBER ]]; do
((c++))
# Nom du G1Tag imprimé du le QRCode RJ
AA=$(./shell/diceware.sh 6 | xargs); # Nom = Diceware de 6 mots
RR=$(echo ${AA} | sed s/\ //g ); # Nom concaténé
PR=$(echo ${AA} | cut -d ' ' -f 1 ); # Prénom du G1Tag
# Create Unique G1tag in all SWARM!
while [[ $(ls ./wallets_swarm/.Qm*/TAG/${RR}) ]]; do
AA=$(./shell/diceware.sh 6 | xargs); RR=$(echo ${AA} | sed s/\ //g ); PR=$(echo ${AA} | cut -d ' ' -f 1 );
done
# Clef de chiffrage PGP imprimée sur le QRCode BJ
BB=$(date +%N | sha256sum | base64 | head -c 32) #YjY4MDc3OTVjNmUxN2JhYWVjYmU3MDcy
# BB=$(openssl rand -base64 32 | base64) #Yk8yMnVtdzZmendJbHNoK2Q0b0liakRpNHNJUk9UQW5Dd042cFdDODlJTT0K
# G1Tag init : Nom et usage des fichiers du G1Tag
# TODO CHECK _nanodate to detect bad nodes/tx behaviour... and banish them)
TAGDATE="./TAG/${RR}/TAG_nanodate" # Nanodate notification (ntpdate in init.sh for NODE sync in case of
TAGNODEID="./TAG/${RR}/TAG_nodeid" # G1sms+ NODE IPFS ID managing that G1Tag
TAGID="./TAG/${RR}/TAG_id" #Nom du TAG
TAGCURRENCY="./TAG/${RR}/TAG_currency" # ZEN (0.01 G1)
TAGAMOUNT="./TAG/${RR}/TAG_amount" # Tag amount. It is!
TAGTYPE="./TAG/${RR}/TAG_type" # G1 Tag type
TAGPASSENGER="./TAG/${RR}/TAG_passenger" # Reference to G1Tag owner/counter put in "./wallets/.$IPFSNODEID/TAG/${J}"
# ... GESTION DE LA CLEF DE PUBLICATION IPNS ".ipfs/keystore/${RR}" ...
TAGPUBLISH_B_GPG="./TAG/${RR}/TAG_publishkey.B.gpg" # ipns publish key B PGP cyphered
TAGPUBLISH_PHONE_CRYPT="./TAG/${RR}/TAG_publishkey.phone.crypt" # natools.py crypt with creator G1sms Wallet PUBKEY
TAGPUBLISH_NODE_CRYPT="./TAG/${RR}/TAG_publishkey.$NODE_UIDNA.crypt" # natools.py crypt with NODE G1sms Wallet PUBKEY
TAGPUBLISH_SWARM_CRYPT="./TAG/${RR}/TAG_publishkey.swarm.crypt" # natools.py crypt with SWARM G1sms Wallet PUBKEY
TAGN="./TAG/${RR}/TAG_n" # Tag modification number (0 first)
TAGIPNS="./TAG/${RR}/TAG_ipns" # /ipns/Qm... link to last G1Tag version
TAGCHAIN="./TAG/${RR}/TAG_chain" # contains IPFS current ipfs hash
# WRITE MULTITAG FILES with AMOUNT
mkdir -p ./TAG/${RR}
log "__SUB:sms_G1TAG.sh: G1Tag: $RR / ${NANODATE}"
log "__SUB:sms_G1TAG.sh: MEMRIB: $MEMRIB"
log "__SUB:sms_G1TAG.sh: A: ${AA}"
log "__SUB:sms_G1TAG.sh: B: ${BB}"
log "__SUB:sms_G1TAG.sh: USER_KEY: $TAGPUBLISH_B_GPG"
log "__SUB:sms_G1TAG.sh: PHONE G1WALLET_KEY: $TAGPUBLISH_PHONE_CRYPT"
log "__SUB:sms_G1TAG.sh: NODE G1WALLET_KEY: $TAGPUBLISH_NODE_CRYPT"
log "__SUB:sms_G1TAG.sh: SWARM G1WALLET_KEY: $TAGPUBLISH_SWARM_CRYPT"
####################
####################################################
# IPFS files are created and calculated immutable I
####################################################
# A or R is Human readable G1tag Name
echo "${AA}" > "$TAGID"
echo "0" > "$TAGN"
echo "${NANODATE}" > "$TAGDATE"
echo "${IPFSNODEID}" > "$TAGNODEID"
echo "${TYPE}" > "$TAGTYPE"
echo "ZEN" > "$TAGCURRENCY"
ZEN=$(bc -l <<< "$2 * 100") # TODO, Test. Unit != G1 may lead to miss calculation BUG??
echo "${ZEN}" > "$TAGAMOUNT"
# Création de la clef ${R} IPNS qui permet de publier des changements sur le TAG!
if [[ ! -f "/home/$YOU/.ipfs/keystore/${RR}" ]]; then
su $YOU -c "ipfs key gen -t rsa -s 2048 ${RR}"
fi
# GIVE QRCODE BJ ACCESS TO G1TAG
# Enregistrement de la clef de publication IPNS du wallet ${R} cryptée par B
echo "${BB}" | gpg -q --output "$TAGPUBLISH_B_GPG" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "/home/$YOU/.ipfs/keystore/${RR}"
# GIVE PHONE CREATOR G1WALLET ACCESS TO ZEN TAG
# Enregistrement de la clef de publication chiffrée par $PUBKEY la clef publique du portefeuille G1 createur.
./shell/natools.py encrypt -p $PUBKEY -i "/home/$YOU/.ipfs/keystore/${RR}" -o "$TAGPUBLISH_PHONE_CRYPT"
# GIVE NODE_UIDNA ACCESS TO ZEN TAG
./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "/home/$YOU/.ipfs/keystore/${RR}" -o "$TAGPUBLISH_NODE_CRYPT"
# GIVE SWARM ACCESS TO ZEN TAG
./shell/natools.py encrypt -p $SWARM_G1PUBKEY -i "/home/$YOU/.ipfs/keystore/${RR}" -o "$TAGPUBLISH_SWARM_CRYPT"
###########################################################
# TAG IPFS STORAGE & G1Tag IPNS PUBLISHING
###########################################################
I=$(su $YOU -c "ipfs add -qr ./TAG/${RR} | tail -n 1")
# MEMORIZE FOR CHAINING FUTURE CHANGES ( Block0 TAG version has no file published ;)
echo "${I}" > "$TAGCHAIN"
# Activate chain
I=$(su $YOU -c "ipfs add -qr ./TAG/${RR} | tail -n 1")
# IPFS Hash for Immutable G1tag data
J=$(su $YOU -c "ipfs name publish -k ${RR} --quieter /ipfs/${I}")
echo "${J}" > "$TAGIPNS"
# KEEP G1Tag J MEMORY in NODE ./wallets/.$IPFSNODEID/TAG/ (TODO: OPTIMIZE SWARM SYNC and REPUBLISH)
mkdir -p "./wallets/.$IPFSNODEID/TAG/"
# WRITE PASSENGER to J Link (G1Tag is stick to it: PHONE, NODE, FILE )
# TODO USE SHA256 ID !!!
echo "$PHONE" > "./wallets/.$IPFSNODEID/TAG/${J}"
if [[ $PASSENGER ]]; then
HPASSENGER=$(echo -n $PASSENGER | sha256sum | cut -d ' ' -f 1)
echo "$HPASSENGER" > "./wallets/.$IPFSNODEID/TAG/${J}";
fi
###########################################################
log "__SUB:sms_G1TAG.sh: TAG: ipfs ls /ipns/${J}"
if [[ ! $PASSENGER ]]; then
# QR CODE LECTURE
qrencode -s 5 -o "./TAG/${RR}/TAG_READ.png" "RJ:${RR}#${J}"
log "__SUB:sms_G1TAG.sh: CREATE READ QRCODE -> RJ: ${RR} # ipfs ls /ipns/${J}"
# QR CODE ECRITURE
qrencode -s 5 -o "./TAG/${RR}/TAG_WRITE.png" "BJ:${BB}#${J}"
log "__SUB:sms_G1TAG.sh: CREATE WRITE QRCODE [] BJ: ${BB} # ipfs ls /ipns/${J}"
# COMPOSE G1TAG PRINT + TAG_READ.png + TAG_WRITE.png
composite -compose Over -resize 133% -gravity West "./TAG/${RR}/TAG_READ.png" "./shell/g1tag.png" "./TAG/${RR}/${RR}.png"
composite -compose Over -gravity SouthEast "./TAG/${RR}/TAG_WRITE.png" "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
# ADD Avatar in the Center
# if [[ $(file "./wallets/$PHONE/$PHONE.avatar.png" | grep 'PNG') ]]; then
# composite -compose Over -resize 150% -gravity Center "./wallets/$PHONE/$PHONE.avatar.png" "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
# else
# composite -compose Over -resize 100% -gravity Center "./shell/G1Anar.png" "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
# fi
convert -pointsize 60 -fill black -gravity Center -draw 'text 0,-100 "'"$2"'"' "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png";
convert -pointsize 40 -fill black -gravity NorthEast -draw 'text 80,0 "'"${PR}"'"' "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
# WRITE G1Tag RR
convert -pointsize 40 -fill black -gravity SouthWest -draw 'text 3,0 "'"${RR}"'"' "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
# ROTATION (FIXE AVATAR & CHIFFRE)?
# convert "./TAG/${RR}/${RR}.png" -rotate -180 "./TAG/${RR}/${RR}.png"
# QL-700 PRINT PREPARE
if [[ -f "./wallets/$PHONE/ipfsid.G1TAGNODE" ]]; then
destnode=$(cat "./wallets/$PHONE/ipfsid.G1TAGNODE")
if [[ "$destnode" == "" || "$destnode" == "$IPFSNODEID" ]]; then
log "__SUB:sms_G1TAG.sh: Local PRINT ${RR} # ipfs ls /ipns/${J}"
brother_ql_create --model QL-700 "./TAG/${RR}/${RR}.png" --label-size 62 > "./TAG/${RR}/${RR}.bin"
brother_ql_print "./TAG/${RR}/${RR}.bin" /dev/usb/lp0
else
# PRINT ON ANOTHER G1Dab. Send files to G1Tag Printer TODO: Could use DESTNODEPUB instead of NODE_G1PUBKEY ?
log "__SUB:sms_G1TAG.sh: Remote PRINT :: ./wallets/.$destnode/PRINT/${RR}.bin "
mkdir -p "./wallets/.$destnode/PRINT/"
# TODO: USE $DEST_G1PUBKEY is better
./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "./TAG/${RR}/${RR}.png" -o "./wallets/.$destnode/PRINT/${RR}.bin"
fi
else
#
log "__SUB:sms_G1TAG.sh: Becoming default PRINTER :: $IPFSNODEID"
echo "$IPFSNODEID" > "./wallets/$PHONE/ipfsid.G1TAGNODE"
brother_ql_create --model QL-700 "./TAG/${RR}/${RR}.png" --label-size 62 > "./TAG/${RR}/${RR}.bin"
brother_ql_print "./TAG/${RR}/${RR}.bin" /dev/usb/lp0
destnode="$IPFSNODEID"
fi
fi
done
####################################################
# PRINT $PHONE WALLET PUBKEY
####################################################
if [[ "$destnode" == "" || "$destnode" == "$IPFSNODEID" ]]; then
qrencode -s 5 -o "/tmp/G1PubQR.$PHONE.png" "$(cat ./wallets/$PHONE/$PHONE.pub)"
composite -compose Over -gravity West "/tmp/G1PubQR.$PHONE.png" "./shell/g1tag.png" "/tmp/G1PubQR.$PHONE.png"
if [[ $(file "./wallets/$PHONE/$PHONE.avatar.png" | grep 'PNG') ]]; then
composite -compose Over -gravity Center "./wallets/$PHONE/$PHONE.avatar.png" "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
fi
if [[ -f ./wallets/$PHONE/$PHONE.uidrib ]]; then
qrencode -s 6 -o "/tmp/G1MemQR.$PHONE.png" "$(cat ./wallets/$PHONE/$PHONE.uidrib)";
composite -compose Over -gravity SouthEast "/tmp/G1MemQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png" && rm /tmp/G1MemQR.$PHONE.png
fi
convert -pointsize 40 -fill black -gravity NorthEast -draw 'text 100,0 "'"G1 $MEMBER"'"' "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
convert -pointsize 40 -fill black -gravity SouthWest -draw 'text 10,0 "'"$UIDNA"'"' "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
#convert -pointsize 60 -fill black -gravity Center -draw 'text 350,80 "'"$PHONE"'"' "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
brother_ql_create --model QL-700 "/tmp/G1PubQR.$PHONE.png" --label-size 62 > "/tmp/G1PubQR.$PHONE.bin"
brother_ql_print "/tmp/G1PubQR.$PHONE.bin" /dev/usb/lp0
rm -f /tmp/G1PubQR.*
fi
# Send dest SMS
mess_dest="[G1sms+]
Vos $NUMBER G1Tag(s) de $2 $UNIT sont imprimé(s).
Veuillez les récupérer ici:
$ADRESSE
Merci"
sms_SEND "$PHONE" "$mess_dest"
ipfs_node_wallets_add
log "__SUB:sms_G1TAG.sh: END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
exit