sms_G1TAG.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #!/bin/bash
  2. ################################################################################
  3. # Author: Fred (support@qo-op.com)
  4. # Version: 0.2020
  5. # License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
  6. ###########################################################################################
  7. # == G1tag ==
  8. # IPFS ZEN WALLETS
  9. # RJ: READ G1Tag
  10. # BJ: WRITE G1Tag
  11. # WRITE ACCESS ONLY for B possessor and NODE G1Tag Creator
  12. # TODO: MAKE http API for G1Tag accessible through local ipfs gateway !!!!
  13. # https://github.com/typicode/lowdb USE json and ipfs DAG??!
  14. #
  15. # TAG 250 => Imprime un G1Tag rempli de 250 UNIT
  16. # TAG 2 100 => Imprime 100 G1Tag BON de 2 UNIT
  17. # PHONE any ./wallets/IDs
  18. # TODO: for multi-channel -> Use HASHLINK + UIDNA + NODE_UIDNA + AMOUNT ...
  19. ###########################################################################################
  20. source ./shell/init.sh
  21. source ./shell/functions.sh
  22. log "__SUB:sms_G1TAG.sh: START ($1=PHONE, $2=AMOUNT, $3=NUMBER)"
  23. PHONE=$1
  24. AMOUNT=$2
  25. if [[ "$AMOUNT" == "" ]]; then AMOUNT=0; fi
  26. NUMBER=$3
  27. # DEFAULT TYPE IS PRINTED G1TAG
  28. # BUT COULD BE USED TO CARRY VALUE INTO IPFS STORED FILES
  29. # FIRST ATTEMPT IS TO COPY YOUTUBE INTO M4A AND LEND THEM G1.
  30. TYPE=$4 # This is the type of passenger: YOUTUBE, NODE (EMPTY = default means sticker printed G1Tag)
  31. # Give ipfs hash of any External ID like YOUTUBEID ;)
  32. PASSENGER=$5
  33. # == G1tag ==
  34. # IPFS G1sms Wallet to TAG creation/relation manager
  35. # TODO Modulate access to TAGs with IPNS publish key cypher
  36. # A/R: (G1TagID)
  37. # B: (GPG Key for HUMAN WRITE access)
  38. # J: (IPNS link to G1tag storage)
  39. # MASTER: Global SWARM Access
  40. ###############################################################################################
  41. # Initialize PHONE, PIN, PUBKEY, UNIT, MEMBER, MEMRIB, EMAIL, etc...
  42. sms_INIT_ACCOUNT "$PHONE" "NOSMS"
  43. if [[ $UNKNOWN == "unknown" ]]; then
  44. sms_ERROR "$PHONE" "Porte-monnaie inconnu. Envoyez N (suivi de votre Pseudo membre) pour le créer."
  45. log "__SUB:sms_G1TAG.sh: Porte-monnaie inconnu"
  46. exit
  47. fi
  48. ###########################################################
  49. # CALCULATE if wallet have enough for VIR+PERCENT
  50. AMOUNT=$2 # BE CAREFULL AMOUNT was changed by 1st sms_INIT_ACCOUNT!!
  51. if [[ "$AMOUNT" == "" ]]; then AMOUNT=0; fi
  52. if [[ ! $PASSENGER && "$NUMBER" != "" ]]; then
  53. # MULTI TAG NUMBER ($NUMBER = How many tags to print)
  54. AMOUNT=$(bc -l <<< "$AMOUNT * $NUMBER")
  55. else
  56. NUMBER=1
  57. fi
  58. VIR=0
  59. accounting=($(make_accounting))
  60. log "__SUB:sms_G1TAG.sh: $(declare -p accounting)"
  61. testmin=${accounting[0]}
  62. VIR=${accounting[1]}
  63. PERCENT=${accounting[2]}
  64. CHARGE=${accounting[3]}
  65. if [[ $testmin -eq 0 ]]; then
  66. sms_SEND "$PHONE" "Le solde de votre Porte-monnaie est insuffisant. Minimum requis = ${CHARGE} + ${PERCENT} (+ ${LIMIT}) G1! Rechargez avec https://cesium.app"
  67. exit
  68. fi
  69. ##### IS G1DAB NODE?
  70. if [[ ! $PASSENGER && ! $G1DAB ]]; then
  71. if [[ -f ./wallets/$PHONE/ipfsid.G1TAGNODE ]]; then
  72. DABnode=$(cat ./wallets/$PHONE/ipfsid.G1TAGNODE)
  73. if [[ "$DABnode" == "$IPFSNODEID" ]]; then
  74. sms_ERROR "$PHONE" "IMPRIMANTE G1Tag sur $DABnode non détectée!! Choisissez un autre G1Dab..."
  75. log "__SUB:sms_G1TAG.sh: IMPRIMANTE G1Tag non détectée!!"
  76. exit
  77. fi
  78. fi
  79. fi
  80. # TODO: SEARCH PROXIMITY DAB LOCATION (GeoPoint + GPS)...
  81. # for node in ./wallets_swarm/.*/*.where; do
  82. # done
  83. ###########################################################
  84. # silkaj NODE CHANGE !!!
  85. #SILKAJNODE=$(./shell/checknodes.sh)
  86. #log "NEW SILKAJ NODE: $SILKAJNODE"
  87. ###########################################
  88. # PAY VIR + PERCENT G1
  89. ML=$(bc -l <<< "scale=2; $VIR + $PERCENT")
  90. 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)
  91. if [[ "$PAY" == "" || "$(echo $PAY | cut -d '|' -f 1)" == "KO" ]]; then
  92. # new=$(./shell/checknodes.sh "BAN")
  93. sms_ERROR "$PHONE" "Il est survenu un problème lors de votre virement: $PAY / Silkaj: $new";
  94. exit
  95. else
  96. cents=$(echo $(bc -l <<< "scale=0; $ML * 100") | cut -d '.' -f 1)
  97. move_g1cents "$PHONE" "$NODE_G1PUBKEY" "$cents"
  98. fi
  99. #############################################
  100. log "__SUB:sms_G1TAG.sh: CREATE $NUMBER x $2 $UNIT G1Tag(s) for $MEMBER ($PHONE) : PAYEMENT $ML G1 to $NODE_G1PUBKEY: $PAY"
  101. c=0
  102. while [[ $c -lt $NUMBER ]]; do
  103. ((c++))
  104. # Nom du G1Tag imprimé du le QRCode RJ
  105. AA=$(./shell/diceware.sh 6 | xargs); # Nom = Diceware de 6 mots
  106. RR=$(echo ${AA} | sed s/\ //g ); # Nom concaténé
  107. PR=$(echo ${AA} | cut -d ' ' -f 1 ); # Prénom du G1Tag
  108. # Create Unique G1tag in all SWARM!
  109. while [[ $(ls ./wallets_swarm/.Qm*/TAG/${RR}) ]]; do
  110. AA=$(./shell/diceware.sh 6 | xargs); RR=$(echo ${AA} | sed s/\ //g ); PR=$(echo ${AA} | cut -d ' ' -f 1 );
  111. done
  112. # Clef de chiffrage PGP imprimée sur le QRCode BJ
  113. BB=$(date +%N | sha256sum | base64 | head -c 32) #YjY4MDc3OTVjNmUxN2JhYWVjYmU3MDcy
  114. # BB=$(openssl rand -base64 32 | base64) #Yk8yMnVtdzZmendJbHNoK2Q0b0liakRpNHNJUk9UQW5Dd042cFdDODlJTT0K
  115. # G1Tag init : Nom et usage des fichiers du G1Tag
  116. # TODO CHECK _nanodate to detect bad nodes/tx behaviour... and banish them)
  117. TAGDATE="./TAG/${RR}/TAG_nanodate" # Nanodate notification (ntpdate in init.sh for NODE sync in case of
  118. TAGNODEID="./TAG/${RR}/TAG_nodeid" # G1sms+ NODE IPFS ID managing that G1Tag
  119. TAGID="./TAG/${RR}/TAG_id" #Nom du TAG
  120. TAGCURRENCY="./TAG/${RR}/TAG_currency" # ZEN (0.01 G1)
  121. TAGAMOUNT="./TAG/${RR}/TAG_amount" # Tag amount. It is!
  122. TAGTYPE="./TAG/${RR}/TAG_type" # G1 Tag type
  123. TAGPASSENGER="./TAG/${RR}/TAG_passenger" # Reference to G1Tag owner/counter put in "./wallets/.$IPFSNODEID/TAG/${J}"
  124. # ... GESTION DE LA CLEF DE PUBLICATION IPNS ".ipfs/keystore/${RR}" ...
  125. TAGPUBLISH_B_GPG="./TAG/${RR}/TAG_publishkey.B.gpg" # ipns publish key B PGP cyphered
  126. TAGPUBLISH_PHONE_CRYPT="./TAG/${RR}/TAG_publishkey.phone.crypt" # natools.py crypt with creator G1sms Wallet PUBKEY
  127. TAGPUBLISH_NODE_CRYPT="./TAG/${RR}/TAG_publishkey.$NODE_UIDNA.crypt" # natools.py crypt with NODE G1sms Wallet PUBKEY
  128. TAGPUBLISH_SWARM_CRYPT="./TAG/${RR}/TAG_publishkey.swarm.crypt" # natools.py crypt with SWARM G1sms Wallet PUBKEY
  129. TAGN="./TAG/${RR}/TAG_n" # Tag modification number (0 first)
  130. TAGIPNS="./TAG/${RR}/TAG_ipns" # /ipns/Qm... link to last G1Tag version
  131. TAGCHAIN="./TAG/${RR}/TAG_chain" # contains IPFS current ipfs hash
  132. # WRITE MULTITAG FILES with AMOUNT
  133. mkdir -p ./TAG/${RR}
  134. log "__SUB:sms_G1TAG.sh: G1Tag: $RR / ${NANODATE}"
  135. log "__SUB:sms_G1TAG.sh: MEMRIB: $MEMRIB"
  136. log "__SUB:sms_G1TAG.sh: A: ${AA}"
  137. log "__SUB:sms_G1TAG.sh: B: ${BB}"
  138. log "__SUB:sms_G1TAG.sh: USER_KEY: $TAGPUBLISH_B_GPG"
  139. log "__SUB:sms_G1TAG.sh: PHONE G1WALLET_KEY: $TAGPUBLISH_PHONE_CRYPT"
  140. log "__SUB:sms_G1TAG.sh: NODE G1WALLET_KEY: $TAGPUBLISH_NODE_CRYPT"
  141. log "__SUB:sms_G1TAG.sh: SWARM G1WALLET_KEY: $TAGPUBLISH_SWARM_CRYPT"
  142. ####################
  143. ####################################################
  144. # IPFS files are created and calculated immutable I
  145. ####################################################
  146. # A or R is Human readable G1tag Name
  147. echo "${AA}" > "$TAGID"
  148. echo "0" > "$TAGN"
  149. echo "${NANODATE}" > "$TAGDATE"
  150. echo "${IPFSNODEID}" > "$TAGNODEID"
  151. echo "${TYPE}" > "$TAGTYPE"
  152. echo "ZEN" > "$TAGCURRENCY"
  153. ZEN=$(bc -l <<< "$2 * 100") # TODO, Test. Unit != G1 may lead to miss calculation BUG??
  154. echo "${ZEN}" > "$TAGAMOUNT"
  155. # Création de la clef ${R} IPNS qui permet de publier des changements sur le TAG!
  156. if [[ ! -f "/home/$YOU/.ipfs/keystore/${RR}" ]]; then
  157. su $YOU -c "ipfs key gen -t rsa -s 2048 ${RR}"
  158. fi
  159. # GIVE QRCODE BJ ACCESS TO G1TAG
  160. # Enregistrement de la clef de publication IPNS du wallet ${R} cryptée par B
  161. echo "${BB}" | gpg -q --output "$TAGPUBLISH_B_GPG" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "/home/$YOU/.ipfs/keystore/${RR}"
  162. # GIVE PHONE CREATOR G1WALLET ACCESS TO ZEN TAG
  163. # Enregistrement de la clef de publication chiffrée par $PUBKEY la clef publique du portefeuille G1 createur.
  164. ./shell/natools.py encrypt -p $PUBKEY -i "/home/$YOU/.ipfs/keystore/${RR}" -o "$TAGPUBLISH_PHONE_CRYPT"
  165. # GIVE NODE_UIDNA ACCESS TO ZEN TAG
  166. ./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "/home/$YOU/.ipfs/keystore/${RR}" -o "$TAGPUBLISH_NODE_CRYPT"
  167. # GIVE SWARM ACCESS TO ZEN TAG
  168. ./shell/natools.py encrypt -p $SWARM_G1PUBKEY -i "/home/$YOU/.ipfs/keystore/${RR}" -o "$TAGPUBLISH_SWARM_CRYPT"
  169. ###########################################################
  170. # TAG IPFS STORAGE & G1Tag IPNS PUBLISHING
  171. ###########################################################
  172. I=$(su $YOU -c "ipfs add -qr ./TAG/${RR} | tail -n 1")
  173. # MEMORIZE FOR CHAINING FUTURE CHANGES ( Block0 TAG version has no file published ;)
  174. echo "${I}" > "$TAGCHAIN"
  175. # Activate chain
  176. I=$(su $YOU -c "ipfs add -qr ./TAG/${RR} | tail -n 1")
  177. # IPFS Hash for Immutable G1tag data
  178. J=$(su $YOU -c "ipfs name publish -k ${RR} --quieter /ipfs/${I}")
  179. echo "${J}" > "$TAGIPNS"
  180. # KEEP G1Tag J MEMORY in NODE ./wallets/.$IPFSNODEID/TAG/ (TODO: OPTIMIZE SWARM SYNC and REPUBLISH)
  181. mkdir -p "./wallets/.$IPFSNODEID/TAG/"
  182. # WRITE PASSENGER to J Link (G1Tag is stick to it: PHONE, NODE, FILE )
  183. # TODO USE SHA256 ID !!!
  184. echo "$PHONE" > "./wallets/.$IPFSNODEID/TAG/${J}"
  185. if [[ $PASSENGER ]]; then
  186. HPASSENGER=$(echo -n $PASSENGER | sha256sum | cut -d ' ' -f 1)
  187. echo "$HPASSENGER" > "./wallets/.$IPFSNODEID/TAG/${J}";
  188. fi
  189. ###########################################################
  190. log "__SUB:sms_G1TAG.sh: TAG: ipfs ls /ipns/${J}"
  191. if [[ ! $PASSENGER ]]; then
  192. # QR CODE LECTURE
  193. qrencode -s 5 -o "./TAG/${RR}/TAG_READ.png" "RJ:${RR}#${J}"
  194. log "__SUB:sms_G1TAG.sh: CREATE READ QRCODE -> RJ: ${RR} # ipfs ls /ipns/${J}"
  195. # QR CODE ECRITURE
  196. qrencode -s 5 -o "./TAG/${RR}/TAG_WRITE.png" "BJ:${BB}#${J}"
  197. log "__SUB:sms_G1TAG.sh: CREATE WRITE QRCODE [] BJ: ${BB} # ipfs ls /ipns/${J}"
  198. # COMPOSE G1TAG PRINT + TAG_READ.png + TAG_WRITE.png
  199. composite -compose Over -resize 133% -gravity West "./TAG/${RR}/TAG_READ.png" "./shell/g1tag.png" "./TAG/${RR}/${RR}.png"
  200. composite -compose Over -gravity SouthEast "./TAG/${RR}/TAG_WRITE.png" "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
  201. # ADD Avatar in the Center
  202. # if [[ $(file "./wallets/$PHONE/$PHONE.avatar.png" | grep 'PNG') ]]; then
  203. # composite -compose Over -resize 150% -gravity Center "./wallets/$PHONE/$PHONE.avatar.png" "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
  204. # else
  205. # composite -compose Over -resize 100% -gravity Center "./shell/G1Anar.png" "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
  206. # fi
  207. convert -pointsize 60 -fill black -gravity Center -draw 'text 0,-100 "'"$2"'"' "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png";
  208. convert -pointsize 40 -fill black -gravity NorthEast -draw 'text 80,0 "'"${PR}"'"' "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
  209. # WRITE G1Tag RR
  210. convert -pointsize 40 -fill black -gravity SouthWest -draw 'text 3,0 "'"${RR}"'"' "./TAG/${RR}/${RR}.png" "./TAG/${RR}/${RR}.png"
  211. # ROTATION (FIXE AVATAR & CHIFFRE)?
  212. # convert "./TAG/${RR}/${RR}.png" -rotate -180 "./TAG/${RR}/${RR}.png"
  213. # QL-700 PRINT PREPARE
  214. if [[ -f "./wallets/$PHONE/ipfsid.G1TAGNODE" ]]; then
  215. destnode=$(cat "./wallets/$PHONE/ipfsid.G1TAGNODE")
  216. if [[ "$destnode" == "" || "$destnode" == "$IPFSNODEID" ]]; then
  217. log "__SUB:sms_G1TAG.sh: Local PRINT ${RR} # ipfs ls /ipns/${J}"
  218. brother_ql_create --model QL-700 "./TAG/${RR}/${RR}.png" --label-size 62 > "./TAG/${RR}/${RR}.bin"
  219. brother_ql_print "./TAG/${RR}/${RR}.bin" /dev/usb/lp0
  220. else
  221. # PRINT ON ANOTHER G1Dab. Send files to G1Tag Printer TODO: Could use DESTNODEPUB instead of NODE_G1PUBKEY ?
  222. log "__SUB:sms_G1TAG.sh: Remote PRINT :: ./wallets/.$destnode/PRINT/${RR}.bin "
  223. mkdir -p "./wallets/.$destnode/PRINT/"
  224. # TODO: USE $DEST_G1PUBKEY is better
  225. ./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "./TAG/${RR}/${RR}.png" -o "./wallets/.$destnode/PRINT/${RR}.bin"
  226. fi
  227. else
  228. #
  229. log "__SUB:sms_G1TAG.sh: Becoming default PRINTER :: $IPFSNODEID"
  230. echo "$IPFSNODEID" > "./wallets/$PHONE/ipfsid.G1TAGNODE"
  231. brother_ql_create --model QL-700 "./TAG/${RR}/${RR}.png" --label-size 62 > "./TAG/${RR}/${RR}.bin"
  232. brother_ql_print "./TAG/${RR}/${RR}.bin" /dev/usb/lp0
  233. destnode="$IPFSNODEID"
  234. fi
  235. fi
  236. done
  237. ####################################################
  238. # PRINT $PHONE WALLET PUBKEY
  239. ####################################################
  240. if [[ "$destnode" == "" || "$destnode" == "$IPFSNODEID" ]]; then
  241. qrencode -s 5 -o "/tmp/G1PubQR.$PHONE.png" "$(cat ./wallets/$PHONE/$PHONE.pub)"
  242. composite -compose Over -gravity West "/tmp/G1PubQR.$PHONE.png" "./shell/g1tag.png" "/tmp/G1PubQR.$PHONE.png"
  243. if [[ $(file "./wallets/$PHONE/$PHONE.avatar.png" | grep 'PNG') ]]; then
  244. composite -compose Over -gravity Center "./wallets/$PHONE/$PHONE.avatar.png" "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
  245. fi
  246. if [[ -f ./wallets/$PHONE/$PHONE.uidrib ]]; then
  247. qrencode -s 6 -o "/tmp/G1MemQR.$PHONE.png" "$(cat ./wallets/$PHONE/$PHONE.uidrib)";
  248. composite -compose Over -gravity SouthEast "/tmp/G1MemQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png" && rm /tmp/G1MemQR.$PHONE.png
  249. fi
  250. convert -pointsize 40 -fill black -gravity NorthEast -draw 'text 100,0 "'"G1 $MEMBER"'"' "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
  251. convert -pointsize 40 -fill black -gravity SouthWest -draw 'text 10,0 "'"$UIDNA"'"' "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
  252. #convert -pointsize 60 -fill black -gravity Center -draw 'text 350,80 "'"$PHONE"'"' "/tmp/G1PubQR.$PHONE.png" "/tmp/G1PubQR.$PHONE.png"
  253. brother_ql_create --model QL-700 "/tmp/G1PubQR.$PHONE.png" --label-size 62 > "/tmp/G1PubQR.$PHONE.bin"
  254. brother_ql_print "/tmp/G1PubQR.$PHONE.bin" /dev/usb/lp0
  255. rm -f /tmp/G1PubQR.*
  256. fi
  257. # Send dest SMS
  258. mess_dest="[G1sms+]
  259. Vos $NUMBER G1Tag(s) de $2 $UNIT sont imprimé(s).
  260. Veuillez les récupérer ici:
  261. $ADRESSE
  262. Merci"
  263. sms_SEND "$PHONE" "$mess_dest"
  264. ipfs_node_wallets_add
  265. log "__SUB:sms_G1TAG.sh: END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  266. exit