functions.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #!/bin/bash
  2. # License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
  3. # G1SMS COMMUN FUNCTIONS
  4. # TODO: REWRITE AND MAKE FULL IPFS get wallet value through IPNS links
  5. # ipfs cat /ipns/QmUrUdfz5hWJ6354D3N6DT64tbs5CLEndSYYiqrj9Wa4Ax/+33647683646/+33647683646.uidna
  6. ###################################################################################################################################
  7. ###################################################################################################################################
  8. MY_PATH="`dirname \"$0\"`" # relative
  9. MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
  10. ## Récupère les données du profile
  11. [[ -f $MY_PATH/.profile ]] && source $MY_PATH/.profile
  12. function sms_SEND () {
  13. # sms_SEND ($1=phone, $2=message)
  14. local dest="$1"
  15. local mess="$2"
  16. if [[ ${#dest} -eq 10 || ${#dest} -eq 12 ]]; then
  17. if [[ -d ./wallets/$dest ]]; then
  18. # TEST IF $dest IS ON CURRENT NODE
  19. gammu-smsd-inject TEXT "$dest" -text "$mess" 1>&2
  20. else
  21. # Send SMS through $dest NODE (TODO: send it like PRINT, TASK, ...)
  22. DESTNODEID=$(cat ./wallets_swarm/$dest/MASTERPHONE.ipfsid)
  23. log "$dest is managed by NODE: $DESTNODEID)"
  24. mkdir "./wallets/.$DESTNODEID/SMS"
  25. echo $mess > "./wallets/.$DESTNODEID/SMS/$dest.sms2send"
  26. I=$(ipfs_node_wallets_add)
  27. fi
  28. else
  29. log ">>>>>>>>>>SMS ERROR BAD DESTINATION $dest"
  30. fi
  31. log ">>>>>>>>>> SENDING SMS $mess TO $dest ($DESTNODEID)"
  32. }
  33. #################################################################################################################################
  34. function sms_ERROR () {
  35. # sms_ERROR ($1=phone, $2=message)
  36. local dest="$1"
  37. local mess="[ERREUR]
  38. $2"
  39. if [[ ${#dest} -eq 10 || ${#dest} -eq 12 ]]; then
  40. gammu-smsd-inject TEXT "$dest" -text "$mess" 1>&2
  41. else
  42. log ">>>>>>>>>>SMS ERROR BAD DESTINATION $dest"
  43. fi
  44. log ">>>>>>>>>> SENDING SMS $mess TO $dest"
  45. # SEND ERROR COPY TO ADMINPHONE
  46. if [[ "$CPERROR" == "YES" ]]; then sms_SEND "$ADMINPHONE" "ADMIN! ERROR ($dest): $mess"; fi
  47. }
  48. #################################################################################################################################
  49. function log_history () {
  50. log "__SUB:log_history: history ($1, $2)"
  51. PHONE="$1"
  52. HISTFILE="./history.$IPFSNODEID.log.csv"
  53. echo "$(date +%Y%m%d), $(date +%H:%M:%S), $1, $2" >> "$HISTFILE"
  54. }
  55. #################################################################################################################################
  56. function security () {
  57. if [[ "$G1SMS" == "YES" ]]; then # ONLY OF gammu is really sending SMS on that NODE
  58. # No Reply to MySelf (or enjoy SMS LOOP of the death) Nobody neither
  59. if [[ "$PHONE" == "$MASTERPHONE" || "$PHONE" == "" ]]; then log "__SUB:security: SMS REJECTED !! $PHONE $TEXT"; exit; fi
  60. # ADAPT TO YOUR PHONE COMPANY / ADVERT ANNOYANCE
  61. if [[ "$PHONE" == "Orange Info" ]]; then log "__SUB:security: SMS REJECTED !! $PHONE ADVERT $TEXT"; exit; fi
  62. if [[ ${#PHONE} -ne 12 && ${#PHONE} -ne 46 ]]; then log "!! SMS REJECTED !! $PHONE BAD $TEXT"; exit; fi
  63. fi
  64. # ANTI SPAM SMS (1 mn entre chaque commande)
  65. find /tmp -cmin +1 -type f -name "sms_received_*" -exec rm -f '{}' \;
  66. # Do not consider SPAM, Delivered notification
  67. if [[ "$CMD" != "DELIVERED" && "$CMD" != "PENDING" ]]; then
  68. if [[ -f "/tmp/sms_received_$PHONE" ]]; then
  69. # Send response SMS
  70. if [[ ! -f "/tmp/sms_SPAM_$PHONE" ]]; then
  71. sms_ERROR "$PHONE" "Je ne le dirai pas 2 fois... Laissez moi au moins 1 minute entre chacune de vos commandes SMS! Autrement je n'y répondrai pas. Merci!"
  72. echo $(date) > "/tmp/sms_SPAM_$PHONE"
  73. fi
  74. log "__SUB:security: ################################"
  75. log "__SUB:security: $PHONE COMMAND REJECTED: $TEXT"
  76. exit
  77. fi
  78. echo "$TEXT" > "/tmp/sms_received_$PHONE"
  79. # Remove SPAM flag older than one day
  80. find /tmp -ctime +1 -type f -name "sms_SPAM_*" -exec rm -f '{}' \;
  81. else
  82. # THIS IS AN AKNOWLEGEMENT
  83. log "__SUB:security: ################################"
  84. log "__SUB:security: $PHONE COMMAND REJECTED: $TEXT"
  85. exit
  86. fi
  87. # Remove SPAM flag older than one day
  88. find /tmp -ctime +1 -type f -name "sms_SPAM_*" -exec rm -f '{}' \;
  89. }
  90. #################################################################################################################################
  91. function ipfs_swarm_wallets_refresh (){
  92. # Refresh CURRENT NODE wallets STATUS
  93. I=$(ipfs_node_wallets_add)
  94. # REFRESH SWARM MEMORY
  95. mkdir -p "./wallets_swarm"
  96. chown -R $YOU "./wallets_swarm"
  97. log "__SUB:ipfs_swarm_wallets_refresh: REFRESHING SWARM $PHONE shared memory..."
  98. if [[ $PHONE ]]; then rm -Rf ./wallets_swarm/$PHONE; fi
  99. # GET IPNS published wallets from ALL SWARM NODES / TODO: IF NODES MISSING, check ./wallets_swarm/.QmNODES...
  100. #log "__SUB:ipfs_swarm_wallets_refresh: IPFS: ipfs get --output=./wallets_swarm/ /ipns/$IPFSNODEID"
  101. su $YOU -c "ipfs get --output=./wallets_swarm/ /ipns/$IPFSNODEID"
  102. count=1
  103. # Search for All peers Nodes. TODO: To be changed when Swarm is too bug or not expendanding
  104. #for id in ./wallets/.Qm*/;
  105. for id in $(su $YOU -c "ipfs swarm peers" | awk -F '/' '{print $7}');
  106. do
  107. count=$((count+1))
  108. id=$(echo $id | cut -d '.' -f 3 | cut -d '/' -f 1)
  109. log "__SUB:ipfs_swarm_wallets_refresh: IPFS: ipfs get --output=./wallets_swarm/ /ipns/$id"
  110. rm -Rf ./wallets_swarm/.$id
  111. ./shell/timeout.sh -t 20 su $YOU -c "ipfs get --output=./wallets_swarm/ /ipns/$id"
  112. done
  113. log "__SUB:ipfs_swarm_wallets_refresh: ./wallets_swarm/ RENEW from $count peers .........OK!!!"
  114. ############################################################################"
  115. # TREAT move_g1cents Tasks in ./wallets/.$IPFSNODEID
  116. # IF NODE find in ./wallets_swarm/.$IPFSNODEID/TASK directory && Have it's G1 Wallet already (for natools crypto)
  117. if [[ "$1" != "SIMPLE" && -d ./wallets_swarm/.$IPFSNODEID/TASK && -f "./wallets/.$IPFSNODEID/$IPFSNODEID.authfile.GPGPASS.gpg" ]]; then
  118. ############################################################################"
  119. for task in $(ls ./wallets_swarm/.$IPFSNODEID/TASK/*.move_g1cents.*); # ./wallets/.$IPFSNODEID/TASK/$NANODATE.move_g1cents.$DESTPHONE.NODEPUB.crypt OR "done.$NANODATE" FILES
  120. do
  121. FTASK=$( echo $task | cut -d '/' -f 5 ) # "$NANODATE.move_g1cents.$DESTPHONE.NODEPUB.crypt"
  122. TNANO=$( echo $FTASK | cut -d '.' -f 1) # $NANODATE
  123. TTYPE=$( echo $FTASK | cut -d '.' -f 2) # move_g1cents
  124. TDEST=$( echo $FTASK | cut -d '.' -f 3) # $DESTPHONE
  125. log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) FOUND TASK $TNANO:$TTYPE:$TDEST "
  126. # MAKE LOCAL .$IPFSNODEID directory
  127. mkdir -p ./wallets/.$IPFSNODEID/TASK/
  128. # TODO: CHECK BETTER NOT DONE YET and $NANODATE > $TNANO (=> detect NODES writing in the future!!)
  129. if [[ ! -f "./wallets/.$IPFSNODEID/TASK/done.$TNANO" ]]; then # NOT DONE YET: NEW TASK!
  130. tdiff=$(bc -l <<< "$NANODATE - $TNANO")
  131. if [[ $tdiff -gt 0 ]]; then
  132. # GET BACK MY NODE G1 Wallet authfile from my LOCAL ./wallets
  133. echo "${GPGPASS}" | gpg -q -d --output "./NODEG1Wallet.authfile" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "./wallets/.$IPFSNODEID/$IPFSNODEID.authfile.GPGPASS.gpg"
  134. # DECRYPT and CAT move_g1cents VALUE
  135. ./shell/natools.py decrypt -k "./NODEG1Wallet.authfile" -i "$task" -o "/tmp/move_g1cents.$TDEST"
  136. ZENVAL=$(cat "/tmp/move_g1cents.$TDEST")
  137. log "__SUB:ipfs_swarm_wallets_refresh: tdiff=$tdiff .$IPFSNODEID G1 = ./NODEG1Wallet.authfile :decrypt: Found $ZENVAL Zen to ADD"
  138. rm -f "./NODEG1Wallet.authfile"
  139. if [[ $ZENVAL -gt 0 ]]; then
  140. curvalue=$(cat ./wallets/$TDEST/$TDEST.g1cents)
  141. newvalue=$(bc -l <<< "$curvalue + $ZENVAL")
  142. echo $newvalue > ./wallets/$TDEST/$TDEST.g1cents
  143. echo "OK.$tdiff" > ./wallets/.$IPFSNODEID/TASK/done.$TNANO
  144. log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) DONE! OPERATION: ($curvalue + $ZENVAL) = $newvalue Zen <=> ./wallets/$TDEST/$TDEST.g1cents:: OK"
  145. # REFRESH ./wallets and PUBLISH
  146. I=$(ipfs_node_wallets_add)
  147. else
  148. echo "KO.$tdiff" > ./wallets/.$IPFSNODEID/TASK/done.$TNANO
  149. log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) ERROR! BAD: ($curvalue + $ZENVAL) = $newvalue Zen <=> ./wallets/$TDEST/$TDEST.g1cents :: KO"
  150. I=$(ipfs_node_wallets_add)
  151. sms_SEND "$ADMINPHONE" "ADMIN! TASK ERROR: .$IPFSNODEID($FTASK) ERROR! BAD: ($curvalue + $ZENVAL) = $newvalue Zen <=> ./wallets/$TDEST/$TDEST.g1cents :: KO"
  152. fi
  153. else
  154. # TODO: Bad NODE in the Future task !!! Make better BAD Node detection = Swarm Banish?
  155. echo "KO.$tdiff" > ./wallets/.$IPFSNODEID/TASK/done.$TNANO
  156. log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) ERROR! DATE PROBLEM: $NANODATE < $TNANO :: KO"
  157. I=$(ipfs_node_wallets_add)
  158. sms_SEND "$ADMINPHONE" "ADMIN! .$IPFSNODEID($FTASK) ERROR! DATE PROBLEM: $NANODATE < $TNANO :: KO"
  159. fi
  160. fi
  161. done
  162. log "__SUB:ipfs_swarm_wallets_refresh: REMOVE OLD TASK MARKED AS DONE"
  163. for scan in ./wallets_swarm/.Qm*/TASK/done.*; do
  164. lscan=$(echo $scan | sed s/_swarm//g )
  165. lid=$(echo $scan | cut -d '/' -f 3 | cut -d '.' -f 2 )
  166. lnano=$(echo $scan | cut -d '/' -f 5 | cut -d '.' -f 2 )
  167. if [[ "$lid" != "$IPFSNODEID" ]]; then
  168. log "__SUB:ipfs_swarm_wallets_refresh: CLEANING done OLD TASK ${lscan} SENT to $lid ($lnano.bin)"
  169. rm -f ./wallets/.$lid/TASK/$lnano.*
  170. I=$(ipfs_node_wallets_add)
  171. fi
  172. done
  173. else
  174. log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID :: NO TASK ! "
  175. ############################################################################"
  176. fi
  177. }
  178. #################################################################################################################################
  179. function ipfs_node_wallets_add (){
  180. ##########################
  181. ##########################
  182. IWALLETS=$(su $YOU -c "ipfs add -rHq './wallets' | tail -n 1")
  183. # CHAIN STATE FOR $IPFSNODEID
  184. echo $IWALLETS > ./wallets/.$IPFSNODEID.wallets.chain
  185. log "__SUB:ipfs_node_wallets_add: NEW './wallets' HASH ipfs ls $IWALLETS"
  186. # PUBLISH IPNS NODE WALLETS
  187. NODEWALLETS=$(su $YOU -c "ipfs name publish --quieter /ipfs/$IWALLETS")
  188. log "__SUB:ipfs_node_wallets_add: G1smsWallet = ipfs cat /ipns/$NODEWALLETS/$PHONE/$PHONE.g1cents Zen"
  189. echo $IWALLETS
  190. }
  191. #################################################################################################################################
  192. function sms_uid2key (){
  193. RIBFILE="./wallets/$PHONE/$PHONE.uidrib"
  194. NAMEFILE="./wallets/$PHONE/$PHONE.uidname"
  195. UIDNAFILE="./wallets/$PHONE/$PHONE.uidna"
  196. # New related wallet asked
  197. ASKWALLET="$1"
  198. #For micro payement
  199. PHONE="$2"
  200. PIN="$3"
  201. if -f "$UIDNAFILE"; then UIDNA=$(cat "$UIDNAFILE"); else UIDNA=""; fi
  202. log "__SUB:sms_uid2key: $UIDNA Try to associate $ASKWALLET with $PHONE... SEARCHING..."
  203. if [[ $UIDNA && "$ASKWALLET" == "$UIDNA" ]]; then
  204. log "__SUB:sms_uid2key: "$ASKWALLET" == "$UIDNA" !!"
  205. sms_SEND "$PHONE" "[G1sms+] $UIDNA est le nom de votre porte monnaie libre pour $PHONE. Il ne peut également être compte maître!"
  206. fi
  207. # Test if member files exists
  208. if [ -f "$NAMEFILE" ]; then
  209. UIDNAME=$(cat "$NAMEFILE")
  210. log "__SUB:sms_uid2key: ALREADY $UIDNAME related Wallet for $PHONE Wallet change to $ASKWALLET !!"
  211. if [["$UIDNAME" != "$ASKWALLET" ]]; then
  212. sms_SEND "$ADMINPHONE" "ADMIN! INFO: $PHONE Wallet associé à $UIDNAME change en $ASKWALLET !!"
  213. sms_SEND "$PHONE" "[G1sms+] Votre portefeuille maitre va passer de $UIDNAME à $ASKWALLET!"
  214. fi
  215. fi
  216. # Search fo duplicate
  217. DUP=$(grep -Rwl "$ASKWALLET" ./wallets/*/*.uidname | cut -f 3 -d '/')
  218. if [[ "$DUP" != "" && "$DUP" != "$PHONE" ]]; then
  219. sms_SEND "$ADMINPHONE" "ADMIN! INFO: $PHONE prend le même $ASKWALLET, que $DUP"
  220. sms_SEND "$DUP" "Vous devez savoir que $ASKWALLET est associé à un autre téléphone que le votre également!"
  221. fi
  222. ###########################################################
  223. # FIND ASKWALLET into wallets, wallest_swarm OR duniter (silkaj)
  224. DESTMEM="$VIRDEST"
  225. # SEARCH IN WALLETS
  226. LOCAL=$(grep -Rwl "$ASKWALLET" ./wallets/*/*.uidna | cut -f 3 -d '/')
  227. if [[ "$LOCAL" != "" ]]; then
  228. # LOCAL G1sms account
  229. DESTRIB=$(cat ./wallets/$LOCAL/$LOCAL.pub)
  230. DESTPHONE=$LOCAL
  231. log "__SUB:sms_uid2key: FOUND LOCAL ($MASTERPHONE) G1sms+ wallet: $DESTRIB ($LOCAL)"
  232. else
  233. # SEARCH IN WALLETS SWARM
  234. INSWARM=$(grep -Rwl "$ASKWALLET" ./wallets_swarm/*/*.uidna | cut -f 3 -d '/')
  235. if [[ "$INSWARM" != "" ]]; then
  236. REMOTENODE=$(cat ./wallets_swarm/$INSWARM/MASTERPHONE.sms)
  237. DESTRIB=$(cat ./wallets_swarm/$INSWARM/$INSWARM.pub)
  238. DESTPHONE=$INSWARM
  239. log "__SUB:sms_uid2key: FOUND SWARM ($REMOTENODE) G1sms+ wallet: $DESTRIB ($INSWARM)"
  240. else
  241. # SEARCH WITH SILKAJ
  242. log "__SUB:sms_uid2key: GETTING FROM SILKAJ MEMBER ID"
  243. DESTRIB=$(./silkaj/silkaj id "$ASKWALLET" | grep -w "$ASKWALLET" | awk '{print $2}')
  244. log "__SUB:sms_uid2key: OUT OFF SWARM MEMBER wallet: $DESTRIB"
  245. # GET CESIUM+ geoPoint DATA (TODO: cron for GCHANGE SMS Alert)
  246. curl -s ${CESIUM}/user/profile/${DESTRIB} | jq '._source.geoPoint' > "./wallets/$PHONE/$PHONE.uidgeo.json"
  247. log "__SUB:sms_uid2key: GET CESIUM+ geoPoint and AVATAR : $(cat ./wallets/$PHONE/$PHONE.uidgeo.json)"
  248. curl -s ${CESIUM}/user/profile/${DESTRIB} | jq '._source.avatar._content' | sed 's/\"//g' | base64 -d > "./wallets/$PHONE/$PHONE.avatar.png"
  249. fi
  250. fi
  251. if [ "$DESTRIB" != "" ]; then
  252. echo "$DESTRIB" > "$RIBFILE"
  253. echo "$ASKWALLET" > "$NAMEFILE"
  254. log "__SUB:sms_uid2key: NEW EXTERNAL RELATED $DESTRIB / FOR MEMBER OR WALLET = $ASKWALLET - INIT 0.1 TX"
  255. INIT=$(./silkaj/silkaj transaction --auth-scrypt -salt="$PHONE" -password="$PIN" --amount="0.1" --output=$DESTRIB --comment="[G1sms+] Porte Monnaie $(cat $UIDNAFILE) -> $(cat $NAMEFILE)" -y)
  256. log "__SUB:sms_uid2key: INIT = $INIT | Create Connection with related Wallet."
  257. log_history "$PHONE" "MEMBER, $ASKWALLET"
  258. sms_SEND "$ADMINPHONE" "ADMIN! LOG: $PHONE Nouveau compte lié $UIDNA -- $ASKWALLET ($INIT)"
  259. else
  260. sms_SEND "$ADMINPHONE" "ADMIN! LOG: $PHONE / $ASKWALLET ABSENT"
  261. fi
  262. log "__SUB:sms_uid2key: $MEMBER($DESTRIB)"
  263. echo "$DESTRIB"
  264. }
  265. #################################################################################################################################
  266. function sms_INIT_ACCOUNT () {
  267. log "__SUB:sms_INIT_ACCOUNT: ($1=phone, $2=NOSMS)"
  268. PHONE="$1"
  269. UNKNOWN=0
  270. # TODO Optimize GLOBAL/local variables use, use more function array output ?!
  271. # Initiate PHONE settings files and values
  272. IPFSNODEIDFILE="./wallets/$PHONE/MASTERPHONE.ipfsid" # Contains G1sms+ NODE IPFS id
  273. SMSNODE="./wallets/$PHONE/MASTERPHONE.sms" # Contains G1sms+ SMS phone number
  274. PINFILE="./wallets/$PHONE/$PHONE.pin" # Contains phone wallet diceware password
  275. GPGPINFILE="./wallets/$PHONE/$PHONE.pin.gpg" # Contains g1smsnode cypher phone wallet diceware password
  276. AUTHCRYPTFILE="./wallets/$PHONE/$PHONE.authfile.crypt" # Crypt with $NODE_G1PUBKEY
  277. PUBKEYFILE="./wallets/$PHONE/$PHONE.pub" # Contains phone wallet public key (RIB)
  278. UNITFILE="./wallets/$PHONE/$PHONE.unit" # Contains phone wallet prefered unit (LOVE,G1,DU)
  279. RIBFILE="./wallets/$PHONE/$PHONE.uidrib" # Contains phone wallet related UID Pubkey
  280. NAMEFILE="./wallets/$PHONE/$PHONE.uidname" # Contains the member UID related to wallet
  281. MAILFILE="./wallets/$PHONE/$PHONE.email" # Contains the member EMAIL (Send BILLET)
  282. GPGMAILFILE="./wallets/$PHONE/$PHONE.email.gpg" # Contains the member EMAIL (Send BILLET)
  283. UIDNAFILE="./wallets/$PHONE/$PHONE.uidna" # Contains the G1sms+ UID Name for recursive VIR
  284. GEOFILE="./wallets/$PHONE/$PHONE.uidgeo.json" # Contains the member GeoPoint from member Cesium+ related to wallet
  285. RECFILE="./wallets/$PHONE/VIREMENTS" # File showing recurrent payements are left to be done with this G1sms Wallet
  286. GPGRECFILE="./wallets/$PHONE/VIREMENTS.gpg" # Cypher file with GPGPASS (Node .Identity.PrivKey)
  287. G1COUNTFILE="./wallets/$PHONE/$PHONE.g1cents" # Last sms_COUNT.sh call value
  288. CHAINFILE="./wallets/$PHONE/$PHONE.chain" # Contains wallet last IPFS hash before ZEN value change
  289. DABUIDF="./wallets/$PHONE/uidna.G1TAGNODE" # Contains G1Dab Name
  290. # GET CURRENT NODE UIDNA (default DABUID)
  291. NODEUIDNA=$(cat "./wallets/.$IPFSNODEID/$IPFSNODEID.uidna")
  292. [[ $NODEUIDNA == "" ]] && NODEUIDNA=$(cat "./wallets/.$IPFSNODEID/_uidna")
  293. ######################################################################
  294. # (NO PIN) = FIRST ACCOUNT: Create wallet
  295. if [ ! -f "$PINFILE" ]; then
  296. # NOSMS mode?
  297. if [[ $2 == "NOSMS" ]]; then UNKNOWN="unknown"; return; fi
  298. #######################
  299. # Create Account Files
  300. #######################
  301. mkdir -p "./wallets/$PHONE/"
  302. PIN=$(./shell/diceware.sh | xargs)
  303. PUBKEY=$(./silkaj/silkaj generate_auth_file --auth-scrypt -salt="$PHONE" -password="$PIN")
  304. # BACKUP authfile available to authfile or PIN owner (DOUBLON AVEC PIN) TEST
  305. log "__SUB:sms_INIT_ACCOUNT: ./shell/natools.py encrypt -p $PUBKEY -i ""./authfile"" -o ""$AUTHCRYPTFILE"""
  306. ./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "./authfile" -o "$AUTHCRYPTFILE"
  307. rm -f ./authfile
  308. log "__SUB:sms_INIT_ACCOUNT: !! G1 Wallet CREATED: $PHONE $PIN : $PUBKEY"
  309. #######################
  310. # GIVE NAME TO WALLET
  311. #######################
  312. # 4 derniers chiffres du numéro de téléphone
  313. TAIL=${PHONE:8:4}
  314. # No UIDNA yet. Create new
  315. UIDNA=$(./shell/diceware.sh 1 | xargs)
  316. # Check if already existing among all swarm wallets
  317. while [[ $(grep -Rwl $UIDNA$TAIL ./wallets_swarm/*/*.uidna) ]]; do UIDNA=$(./shell/diceware.sh 1 | xargs); done
  318. echo "$UIDNA$TAIL" > "$UIDNAFILE"
  319. #######################
  320. # ACTIVATE G1SMS WALLET
  321. #######################
  322. # log "$NODE_G1AUTHFILE $PUBKEY THIRD_PARTY_MANAGER:$NODE_G1PUBKEY"
  323. TX_IN=$(./silkaj/silkaj transaction --auth-file -file="$NODE_G1AUTHFILE" --amount=3.24 --output=$PUBKEY --comment="[G1sms+] $UIDNA$TAIL 3RD:$NODE_G1PUBKEY " -y)
  324. log "__SUB:sms_INIT_ACCOUNT: G1 Wallet TX IN: $TX_IN"
  325. sleep 2
  326. TX_OUT=$(./silkaj/silkaj transaction --auth-scrypt -salt="$PHONE" -password="$PIN" --amount=0.1 --output=$NODE_G1PUBKEY --comment="[G1sms+] $UIDNA$TAIL 3RD:$NODE_G1PUBKEY:ACK" -y)
  327. log "__SUB:sms_INIT_ACCOUNT: G1 Wallet TX OUT: $TX_OUT"
  328. ################
  329. # GPG cypher PIN (ONLY CREATOR NODE CAN ACCESS IT !)
  330. ################
  331. echo "$PIN" > "$PINFILE"
  332. echo "${GPGPASS}" | gpg -q --output "$GPGPINFILE" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "$PINFILE"
  333. PIN=$(cat "$PINFILE" | xargs)
  334. [[ "$PIN" != "" ]] && echo "" > "$PINFILE" || log "SYSTEM ERROR :: PIN EMPTY !!! GPG???"
  335. echo "114" > "$G1COUNTFILE"
  336. echo "$PUBKEY" > "$PUBKEYFILE"
  337. echo "$COIN" > "$UNITFILE"
  338. echo "$MASTERPHONE" > "$SMSNODE"
  339. log_history "$PHONE" "NEW, $PUBKEY"
  340. # ADD WALLET TO IPFS
  341. I=$(ipfs_node_wallets_add)
  342. fi
  343. #################################################
  344. # PUBLISH G1sms+ WALLET PROPERTIES MAIN PROCESS
  345. #################################################
  346. ######################################################################
  347. if [[ ! -f "$GPGPINFILE" ]]
  348. then
  349. # ------------8<------------------
  350. # WALLET MIGRATION -> CRYPT PIN (For Old G1sms clear PIN WALLETs...)
  351. # ADD NEW PARAMS TO WALLET
  352. #######################
  353. # 4 derniers chiffres du numéro de téléphone
  354. echo "$MASTERPHONE" > "$SMSNODE"
  355. TAIL=${PHONE:8:4}
  356. # No UIDNA yet. Create new
  357. UIDNA=$(./shell/diceware.sh 1 | xargs)
  358. # Check if already existing among all swarm wallets
  359. while [[ $(grep -Rwl $UIDNA$TAIL ./wallets_swarm/*/*.uidna) ]]; do UIDNA=$(./shell/diceware.sh 1 | xargs); done
  360. echo "$UIDNA$TAIL" > "$UIDNAFILE"
  361. PIN=$(cat "$PINFILE" | xargs)
  362. log "__SUB:sms_INIT_ACCOUNT: Old G1sms wallet ;) SECURITY HOLE... Chiffrage PGP du PIN !!!"
  363. echo "${GPGPASS}" | gpg -q --output "$GPGPINFILE" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "$PINFILE";
  364. ./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "$PINFILE" -o "$PINFILE.$NODE_UIDNA.crypt"
  365. else
  366. # ------------>8------------------
  367. log "__SUB:sms_INIT_ACCOUNT: Déchiffrage PGP PIN..."
  368. # GPG decypher PIN
  369. # TODO make decypher less stress on filesystem, use /tmp and ramdisk
  370. echo "${GPGPASS}" | gpg -d -q --output "$PINFILE" --yes --pinentry-mode loopback --passphrase-fd 0 "$GPGPINFILE"
  371. PIN=$(cat "$PINFILE" | xargs)
  372. [[ "$PIN" == "" && -f "$PINFILE.$NODE_UIDNA.crypt" ]] && ./shell/natools.py decrypt -k "$NODE_G1AUTHFILE" -i "$PINFILE.$NODE_UIDNA.crypt" -o "$PINFILE" && PIN=$(cat "$PINFILE");
  373. [[ "$PIN" != "" ]] && echo "" > "$PINFILE" || log "SYSTEM ERROR :: PIN EMPTY !!! GPG???"
  374. fi
  375. # CYPHER EMAIL FILE
  376. if [[ ! -f "$GPGMAILFILE" ]]; then
  377. # ------------8<------------------
  378. log "__SUB:sms_INIT_ACCOUNT: NO EMAIL .gpg file, CREATING it"
  379. if [ -f "$MAILFILE" ]; then
  380. MAIL=$(cat "$MAILFILE");
  381. echo "${GPGPASS}" | gpg -q --output "$GPGMAILFILE" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "$MAILFILE";
  382. else
  383. MAIL="";
  384. fi
  385. # ------------>8------------------
  386. else
  387. log "__SUB:sms_INIT_ACCOUNT: DECODING EMAIL .gpg... "
  388. echo "${GPGPASS}" | gpg -d -q --output "$MAILFILE" --yes --pinentry-mode loopback --passphrase-fd 0 "$GPGMAILFILE"
  389. MAIL=$(cat "$MAILFILE");
  390. [[ $MAIL == "" && -f "$MAILFILE.node.crypt" ]] && ./shell/natools.py decrypt -k "$NODE_G1AUTHFILE" -i "$MAILFILE.node.crypt" -o "$MAILFILE" && MAIL=$(cat "$MAILFILE");
  391. # EMPTY CLEAR EMAIL FILE
  392. [[ $MAIL != "" ]] && echo "" > "$MAILFILE" || log "SYSTEM ERROR :: MAIL EMPTY !!! GPG???"
  393. fi
  394. # ------------8<------------------
  395. # REMOVE OLD HISTORY FILES
  396. rm -f ./wallets/$PHONE/$PHONE.hist
  397. # ------------>8------------------
  398. # PUBLISH GLOBAL VARS FROM ACCOUNT FILES
  399. echo $IPFSNODEID > $IPFSNODEIDFILE
  400. PUBKEY=$(cat "$PUBKEYFILE" | xargs)
  401. UNIT=$(cat "$UNITFILE" | xargs)
  402. if [ -f "$G1COUNTFILE" ]; then AMOUNTG1=$(bc <<< "scale=2; $(cat $G1COUNTFILE) / 100"); fi
  403. if [ -f "$NAMEFILE" ]; then MEMBER=$(cat "$NAMEFILE"); else MEMBER=""; fi
  404. if [ -f "$RIBFILE" ]; then MEMRIB=$(cat "$RIBFILE"); else MEMRIB=""; fi
  405. if [ -f "$UIDNAFILE" ]; then UIDNA=$(cat "$UIDNAFILE"); else UIDNA=""; fi
  406. if [ -f "$DABUIDF" ]; then DABID=$(cat "$DABUIDF"); else DABID="$NODEUIDNA"; fi
  407. AMOUNTDU=$(bc <<< "scale=2; $AMOUNTG1 / $DUFACTOR")
  408. AMOUNTLOVE=$(bc <<< "$AMOUNTG1 * 100 / $DUFACTOR")
  409. case "$UNIT" in
  410. G1)
  411. AMOUNT=$AMOUNTG1
  412. ;;
  413. DU)
  414. AMOUNT=$AMOUNTDU
  415. ;;
  416. *)
  417. AMOUNT=$AMOUNTLOVE
  418. ;;
  419. esac
  420. log "__SUB:sms_INIT_ACCOUNT: $AMOUNTG1 = $AMOUNT $UNIT"
  421. # LOG # TODO REMOVE PIN LOG!!
  422. log "#####################"
  423. log "PHONE: $PHONE"
  424. log "PIN: $PIN"
  425. log "PUBKEY: $PUBKEY"
  426. log "G1: $AMOUNTG1"
  427. log "AMOUNT: $AMOUNT $UNIT"
  428. log "UIDNA: $UIDNA"
  429. log "MEMBER: $MEMBER"
  430. log "MEMRIB: $MEMRIB"
  431. log "MAIL: $MAIL"
  432. log "DABID: $DABID"
  433. log "#####################"
  434. log "__SUB:sms_INIT_ACCOUNT: END"
  435. }
  436. #################################################################################################################################
  437. function check_account (){
  438. log "__SUB:check_account: SILKAJ...... max 30 sec....."
  439. AMOUNTG1=$(./shell/timeout.sh -t 30 ./silkaj/silkaj amount "$PUBKEY")
  440. AMOUNTDU=$(bc <<< "scale=2; $AMOUNTG1 / $DUFACTOR")
  441. AMOUNTLOVE=$(bc <<< "$AMOUNTG1 * 100 / $DUFACTOR")
  442. case "$UNIT" in
  443. G1)
  444. AMOUNT=$AMOUNTG1
  445. ;;
  446. DU)
  447. AMOUNT=$AMOUNTDU
  448. ;;
  449. *)
  450. AMOUNT=$AMOUNTLOVE
  451. ;;
  452. esac
  453. G1cents=$(echo "$AMOUNTG1*100" | bc -l | awk '{print int($0)}')
  454. log "__SUB:check_account: $PHONE: $AMOUNTG1 = $AMOUNT $UNIT ($G1cents) - $PUBKEY"
  455. if [[ "$G1cents" != "" ]]; then
  456. NN=$(cat "./wallets/$PHONE/$PHONE.n")
  457. ((NN++))
  458. echo "$NN" > "./wallets/$PHONE/$PHONE.n"
  459. echo $G1cents > "./wallets/$PHONE/$PHONE.g1cents"
  460. # REFRESH NODE IPFS wallets & PUBLISH
  461. I=$(ipfs_node_wallets_add)
  462. # Register modification
  463. echo "$I" > "./wallets/$PHONE/$PHONE.chain"
  464. else
  465. # BAN actual Duniter Node in case of silkaj timeout.
  466. export NEWDUNITER=$(./shell/checknodes.sh 'BAN')
  467. log "__SUB:check_account: !!! SILKAJ IS TOO SLOW !!! => NEW DUNITER SERVER: $NEWDUNITER"
  468. fi
  469. declare -a aaa
  470. aaa=( "$AMOUNTG1" "$AMOUNT" "$UNIT" )
  471. log "__SUB:check_account: $(declare -p aaa)"
  472. echo ${aaa[@]}
  473. }
  474. #################################################################################################################################
  475. function make_accounting (){
  476. # CONVERT AMOUNT UNIT to VIR G1
  477. case "$UNIT" in
  478. G1)
  479. VIR=$AMOUNT;
  480. ;;
  481. DU)
  482. VIR=$(bc -l <<< "scale=2; $AMOUNT * $DUFACTOR")
  483. ;;
  484. LOVE)
  485. VIR=$(bc -l <<< "scale=2; $AMOUNT * $DUFACTOR / 100")
  486. ;;
  487. *)
  488. VIR=$(bc -l <<< "scale=2; $AMOUNT * $DUFACTOR / 100")
  489. ;;
  490. esac
  491. ###########################################################
  492. # GET G1sms wallet AMOUNTG1 and CHECK for right balance
  493. log "__SUB:make_accounting: LOCAL $VIR amount for $PHONE : $PUBKEY"
  494. # Try to ask silkaj. If timeout, use SWARM last known $PHONE.g1cents value
  495. AMOUNTG1=$(./shell/timeout.sh -t 30 ./silkaj/silkaj amount "$PUBKEY")
  496. if [[ "$AMOUNTG1" == "" ]]; then AMOUNTG1=0; fi
  497. # TAX AND MINIMUM WALLET ACCOUNT CALCULATION
  498. PERCENT=0$(bc -l <<< "scale=2; $VIR / $SWARMCOMM")
  499. MIN=$(bc -l <<< "$AMOUNTG1 - $PERCENT - $LIMIT")
  500. CHARGE=$(bc -l <<< "scale=2; $VIR + $PERCENT + $LIMIT")
  501. TESTMIN=$( echo "${VIR} < ${MIN}" | bc -l )
  502. log "__SUB:make_accounting: TEST $AMOUNT $UNIT :: $VIR + $PERCENT + $LIMIT = $CHARGE < $AMOUNTG1 ? $TESTMIN"
  503. declare -a myarray
  504. myarray=( "$TESTMIN" "$VIR" "$PERCENT" "$CHARGE")
  505. log "__SUB:make_accounting: $(declare -p myarray)"
  506. echo ${myarray[@]}
  507. }
  508. #################################################################################################################################
  509. function move_g1cents (){
  510. # MODIFY $PHONE.g1cents value according to current silkaj
  511. # TODO MAKE ALL SILKAJ TX CALL HERE. OR REMOVE???
  512. # USE TASK COMMUNICATION BETWEEN NODES.
  513. phonesrc=$1
  514. pubkeydest=$2
  515. zenvalue=$3
  516. log "__SUB:move_g1cents: $phonesrc ($zenvalue) -> $pubkeydest :: $G1COUNTFILE ::"
  517. ### CHANGE INPUT VALUE ####
  518. curvalue=$(cat ./wallets/$phonesrc/$phonesrc.g1cents | cut -d '.' -f 1)
  519. newvalue=$(bc -l <<< "$curvalue - $zenvalue")
  520. echo $newvalue > ./wallets/$phonesrc/$phonesrc.g1cents
  521. log "__SUB:move_g1cents: DEBIT: OK APPLY $curvalue - $zenvalue = $newvalue TO ./wallets/$phonesrc/$phonesrc.g1cents"
  522. ### CHANGE OUTPUT VALUE ####
  523. # TEST IF $dest is a wallets_ G1sms PUBKEY?
  524. winnerphone=$(grep -Rwl "$pubkeydest" ./wallets_swarm/*/*.pub | cut -d '.' -f 2 | cut -d '/' -f 3)
  525. if [[ $winnerphone ]]; then
  526. # GET INFORMATON ON MANAGER NODE
  527. NODEID=$(cat ./wallets_swarm/$winnerphone/MASTERPHONE.ipfsid)
  528. if [[ "$NODEID" == "$IPFSNODEID" ]]; then
  529. # ACTUAL NODE IS WALLET MANAGING NODE ;)
  530. curvalue=$(cat ./wallets/$winnerphone/$winnerphone.g1cents | cut -d '.' -f 1)
  531. newvalue=$(bc -l <<< "$curvalue + $zenvalue")
  532. echo $newvalue > ./wallets/$winnerphone/$winnerphone.g1cents
  533. log "__SUB:move_g1cents: CREDIT: OK APPLY $curvalue + $zenvalue = $newvalue TO ./wallets/$winnerphone/$winnerphone.g1cents"
  534. else
  535. # MUST ASK MANAGER NODE TO CORRECT g1cents FOR $winnerphone WALLET
  536. # WRITE TASK TO .$NODEID CHANNEL
  537. mkdir -p ./wallets/.$NODEID/TASK/
  538. # USE natools with NODEPUB crypt
  539. echo "$zenvalue" > "/tmp/move_g1cents.$winnerphone"
  540. # GET NODE G1sms Wallet PUBKEY
  541. NODEPUB=$(cat ./wallets_swarm/.$NODEID/$NODEID.pub)
  542. ./shell/natools.py encrypt -p $NODEPUB -i "/tmp/move_g1cents.$winnerphone" -o "./wallets/.$NODEID/TASK/$NANODATE.move_g1cents.$winnerphone.NODEPUB.crypt"
  543. log "__SUB:move_g1cents: CREDIT: WRITE TASK TO ./wallets/.$NODEID/$NANODATE.move_g1cents.$winnerphone.NODEPUB.crypt "
  544. rm -f /tmp/move_g1cents.$winnerphone
  545. fi
  546. else
  547. log "__SUB:move_g1cents: ERROR: NO $pubkeydest WALLET FOUND"
  548. fi
  549. # UPDATE MY new ./wallet STATUS to SWARM
  550. I=$(ipfs_node_wallets_add)
  551. }
  552. #################################################################################################################################
  553. # Contact database management
  554. function add_contact(){
  555. userDB="gammu"
  556. pwdDB=$(cat /etc/gammu-smsdrc | grep "password =" | awk '{ print $3 }' || exit 1)
  557. local sql="mysql -N -u$userDB -p$pwdDB gammu -e"
  558. local PHONE=$1
  559. local MEMBERUID=$2
  560. [[ -z $MEMBERUID ]] && MEMBERUID=$PHONE
  561. [[ -z $($sql "SELECT * FROM pbk_groups WHERE ID='1'") ]] && $sql "INSERT INTO pbk_groups (ID, Name, id_user, is_public) VALUES ('1', '$(grep "ADRESSE" $MY_PATH/../.profile | awk -F= '{ print $2 }' | tr -d '"')', '1', 'false')"
  562. if [[ -z $($sql "SELECT * FROM pbk WHERE Number=$PHONE") ]]; then
  563. $sql "INSERT INTO pbk (GroupID, Name, Number, id_user, is_public) VALUES ('1', '$MEMBERUID', '$PHONE', '1', 'false')" || exit 1
  564. local id_user=$($sql "SELECT ID FROM pbk WHERE Number=$PHONE" || exit 1)
  565. $sql "INSERT INTO user_group (id_group, id_pbk, id_pbk_groups, id_user) VALUES ('$id_user', '$id_user', '1', '1')" || exit 1
  566. else
  567. log "${red}Contact already exist$c_"
  568. fi
  569. }
  570. function rm_contact(){
  571. userDB="gammu"
  572. pwdDB=$(cat /etc/gammu-smsdrc | grep "password =" | awk '{ print $3 }' || exit 1)
  573. local sql="mysql -N -u$userDB -p$pwdDB gammu -e"
  574. local PHONE=$1
  575. local MEMBERUID=$2
  576. [[ -z $MEMBERUID ]] && MEMBERUID=$PHONE
  577. if [[ ! -z $($sql "SELECT * FROM pbk WHERE Number=$PHONE") ]]; then
  578. local id_user=$($sql "SELECT ID FROM pbk WHERE Number=$PHONE" || exit 1)
  579. $sql "DELETE FROM pbk WHERE Number=$PHONE" || exit 1
  580. $sql "DELETE FROM user_group WHERE id_group='$id_user'" || exit 1
  581. else
  582. log "${red}Contact doesn't exist$c_"
  583. fi
  584. }