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.
 
 
 
 
 
 

672 lines
30 KiB

#!/bin/bash
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
# G1SMS COMMUN FUNCTIONS
# TODO: REWRITE AND MAKE FULL IPFS get wallet value through IPNS links
# ipfs cat /ipns/QmUrUdfz5hWJ6354D3N6DT64tbs5CLEndSYYiqrj9Wa4Ax/+33647683646/+33647683646.uidna
###################################################################################################################################
###################################################################################################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
## Récupère les données du profile
[[ -f $MY_PATH/.profile ]] && source $MY_PATH/.profile
function sms_SEND () {
# sms_SEND ($1=phone, $2=message)
local dest="$1"
local mess="$2"
if [[ ${#dest} -eq 10 || ${#dest} -eq 12 ]]; then
if [[ -d ./wallets/$dest ]]; then
# TEST IF $dest IS ON CURRENT NODE
gammu-smsd-inject TEXT "$dest" -text "$mess" 1>&2
else
# Send SMS through $dest NODE (TODO: send it like PRINT, TASK, ...)
DESTNODEID=$(cat ./wallets_swarm/$dest/MASTERPHONE.ipfsid)
log "$dest is managed by NODE: $DESTNODEID)"
mkdir "./wallets/.$DESTNODEID/SMS"
echo $mess > "./wallets/.$DESTNODEID/SMS/$dest.sms2send"
I=$(ipfs_node_wallets_add)
fi
else
log ">>>>>>>>>>SMS ERROR BAD DESTINATION $dest"
fi
log ">>>>>>>>>> SENDING SMS $mess TO $dest ($DESTNODEID)"
}
#################################################################################################################################
function sms_ERROR () {
# sms_ERROR ($1=phone, $2=message)
local dest="$1"
local mess="[ERREUR]
$2"
if [[ ${#dest} -eq 10 || ${#dest} -eq 12 ]]; then
gammu-smsd-inject TEXT "$dest" -text "$mess" 1>&2
else
log ">>>>>>>>>>SMS ERROR BAD DESTINATION $dest"
fi
log ">>>>>>>>>> SENDING SMS $mess TO $dest"
# SEND ERROR COPY TO ADMINPHONE
if [[ "$CPERROR" == "YES" ]]; then sms_SEND "$ADMINPHONE" "ADMIN! ERROR ($dest): $mess"; fi
}
#################################################################################################################################
function log_history () {
log "__SUB:log_history: history ($1, $2)"
PHONE="$1"
HISTFILE="./history.$IPFSNODEID.log.csv"
echo "$(date +%Y%m%d), $(date +%H:%M:%S), $1, $2" >> "$HISTFILE"
}
#################################################################################################################################
function security () {
if [[ "$G1SMS" == "YES" ]]; then # ONLY OF gammu is really sending SMS on that NODE
# No Reply to MySelf (or enjoy SMS LOOP of the death) Nobody neither
if [[ "$PHONE" == "$MASTERPHONE" || "$PHONE" == "" ]]; then log "__SUB:security: SMS REJECTED !! $PHONE $TEXT"; exit; fi
# ADAPT TO YOUR PHONE COMPANY / ADVERT ANNOYANCE
if [[ "$PHONE" == "Orange Info" ]]; then log "__SUB:security: SMS REJECTED !! $PHONE ADVERT $TEXT"; exit; fi
if [[ ${#PHONE} -ne 12 && ${#PHONE} -ne 46 ]]; then log "!! SMS REJECTED !! $PHONE BAD $TEXT"; exit; fi
fi
# ANTI SPAM SMS (1 mn entre chaque commande)
find /tmp -cmin +1 -type f -name "sms_received_*" -exec rm -f '{}' \;
# Do not consider SPAM, Delivered notification
if [[ "$CMD" != "DELIVERED" && "$CMD" != "PENDING" ]]; then
if [[ -f "/tmp/sms_received_$PHONE" ]]; then
# Send response SMS
if [[ ! -f "/tmp/sms_SPAM_$PHONE" ]]; then
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!"
echo $(date) > "/tmp/sms_SPAM_$PHONE"
fi
log "__SUB:security: ################################"
log "__SUB:security: $PHONE COMMAND REJECTED: $TEXT"
exit
fi
echo "$TEXT" > "/tmp/sms_received_$PHONE"
# Remove SPAM flag older than one day
find /tmp -ctime +1 -type f -name "sms_SPAM_*" -exec rm -f '{}' \;
else
# THIS IS AN AKNOWLEGEMENT
log "__SUB:security: ################################"
log "__SUB:security: $PHONE COMMAND REJECTED: $TEXT"
exit
fi
# Remove SPAM flag older than one day
find /tmp -ctime +1 -type f -name "sms_SPAM_*" -exec rm -f '{}' \;
}
#################################################################################################################################
function ipfs_swarm_wallets_refresh (){
# Refresh CURRENT NODE wallets STATUS
I=$(ipfs_node_wallets_add)
# REFRESH SWARM MEMORY
mkdir -p "./wallets_swarm"
chown -R $YOU "./wallets_swarm"
log "__SUB:ipfs_swarm_wallets_refresh: REFRESHING SWARM $PHONE shared memory..."
if [[ $PHONE ]]; then rm -Rf ./wallets_swarm/$PHONE; fi
# GET IPNS published wallets from ALL SWARM NODES / TODO: IF NODES MISSING, check ./wallets_swarm/.QmNODES...
#log "__SUB:ipfs_swarm_wallets_refresh: IPFS: ipfs get --output=./wallets_swarm/ /ipns/$IPFSNODEID"
su $YOU -c "ipfs get --output=./wallets_swarm/ /ipns/$IPFSNODEID"
count=1
# Search for All peers Nodes. TODO: To be changed when Swarm is too bug or not expendanding
#for id in ./wallets/.Qm*/;
for id in $(su $YOU -c "ipfs swarm peers" | awk -F '/' '{print $7}');
do
count=$((count+1))
id=$(echo $id | cut -d '.' -f 3 | cut -d '/' -f 1)
log "__SUB:ipfs_swarm_wallets_refresh: IPFS: ipfs get --output=./wallets_swarm/ /ipns/$id"
rm -Rf ./wallets_swarm/.$id
./shell/timeout.sh -t 20 su $YOU -c "ipfs get --output=./wallets_swarm/ /ipns/$id"
done
log "__SUB:ipfs_swarm_wallets_refresh: ./wallets_swarm/ RENEW from $count peers .........OK!!!"
############################################################################"
# TREAT move_g1cents Tasks in ./wallets/.$IPFSNODEID
# IF NODE find in ./wallets_swarm/.$IPFSNODEID/TASK directory && Have it's G1 Wallet already (for natools crypto)
if [[ "$1" != "SIMPLE" && -d ./wallets_swarm/.$IPFSNODEID/TASK && -f "./wallets/.$IPFSNODEID/$IPFSNODEID.authfile.GPGPASS.gpg" ]]; then
############################################################################"
for task in $(ls ./wallets_swarm/.$IPFSNODEID/TASK/*.move_g1cents.*); # ./wallets/.$IPFSNODEID/TASK/$NANODATE.move_g1cents.$DESTPHONE.NODEPUB.crypt OR "done.$NANODATE" FILES
do
FTASK=$( echo $task | cut -d '/' -f 5 ) # "$NANODATE.move_g1cents.$DESTPHONE.NODEPUB.crypt"
TNANO=$( echo $FTASK | cut -d '.' -f 1) # $NANODATE
TTYPE=$( echo $FTASK | cut -d '.' -f 2) # move_g1cents
TDEST=$( echo $FTASK | cut -d '.' -f 3) # $DESTPHONE
log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) FOUND TASK $TNANO:$TTYPE:$TDEST "
# MAKE LOCAL .$IPFSNODEID directory
mkdir -p ./wallets/.$IPFSNODEID/TASK/
# TODO: CHECK BETTER NOT DONE YET and $NANODATE > $TNANO (=> detect NODES writing in the future!!)
if [[ ! -f "./wallets/.$IPFSNODEID/TASK/done.$TNANO" ]]; then # NOT DONE YET: NEW TASK!
tdiff=$(bc -l <<< "$NANODATE - $TNANO")
if [[ $tdiff -gt 0 ]]; then
# GET BACK MY NODE G1 Wallet authfile from my LOCAL ./wallets
echo "${GPGPASS}" | gpg -q -d --output "./NODEG1Wallet.authfile" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "./wallets/.$IPFSNODEID/$IPFSNODEID.authfile.GPGPASS.gpg"
# DECRYPT and CAT move_g1cents VALUE
./shell/natools.py decrypt -k "./NODEG1Wallet.authfile" -i "$task" -o "/tmp/move_g1cents.$TDEST"
ZENVAL=$(cat "/tmp/move_g1cents.$TDEST")
log "__SUB:ipfs_swarm_wallets_refresh: tdiff=$tdiff .$IPFSNODEID G1 = ./NODEG1Wallet.authfile :decrypt: Found $ZENVAL Zen to ADD"
rm -f "./NODEG1Wallet.authfile"
if [[ $ZENVAL -gt 0 ]]; then
curvalue=$(cat ./wallets/$TDEST/$TDEST.g1cents)
newvalue=$(bc -l <<< "$curvalue + $ZENVAL")
echo $newvalue > ./wallets/$TDEST/$TDEST.g1cents
echo "OK.$tdiff" > ./wallets/.$IPFSNODEID/TASK/done.$TNANO
log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) DONE! OPERATION: ($curvalue + $ZENVAL) = $newvalue Zen <=> ./wallets/$TDEST/$TDEST.g1cents:: OK"
# REFRESH ./wallets and PUBLISH
I=$(ipfs_node_wallets_add)
else
echo "KO.$tdiff" > ./wallets/.$IPFSNODEID/TASK/done.$TNANO
log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) ERROR! BAD: ($curvalue + $ZENVAL) = $newvalue Zen <=> ./wallets/$TDEST/$TDEST.g1cents :: KO"
I=$(ipfs_node_wallets_add)
sms_SEND "$ADMINPHONE" "ADMIN! TASK ERROR: .$IPFSNODEID($FTASK) ERROR! BAD: ($curvalue + $ZENVAL) = $newvalue Zen <=> ./wallets/$TDEST/$TDEST.g1cents :: KO"
fi
else
# TODO: Bad NODE in the Future task !!! Make better BAD Node detection = Swarm Banish?
echo "KO.$tdiff" > ./wallets/.$IPFSNODEID/TASK/done.$TNANO
log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID($FTASK) ERROR! DATE PROBLEM: $NANODATE < $TNANO :: KO"
I=$(ipfs_node_wallets_add)
sms_SEND "$ADMINPHONE" "ADMIN! .$IPFSNODEID($FTASK) ERROR! DATE PROBLEM: $NANODATE < $TNANO :: KO"
fi
fi
done
log "__SUB:ipfs_swarm_wallets_refresh: REMOVE OLD TASK MARKED AS DONE"
for scan in ./wallets_swarm/.Qm*/TASK/done.*; do
lscan=$(echo $scan | sed s/_swarm//g )
lid=$(echo $scan | cut -d '/' -f 3 | cut -d '.' -f 2 )
lnano=$(echo $scan | cut -d '/' -f 5 | cut -d '.' -f 2 )
if [[ "$lid" != "$IPFSNODEID" ]]; then
log "__SUB:ipfs_swarm_wallets_refresh: CLEANING done OLD TASK ${lscan} SENT to $lid ($lnano.bin)"
rm -f ./wallets/.$lid/TASK/$lnano.*
I=$(ipfs_node_wallets_add)
fi
done
else
log "__SUB:ipfs_swarm_wallets_refresh: .$IPFSNODEID :: NO TASK ! "
############################################################################"
fi
}
#################################################################################################################################
function ipfs_node_wallets_add (){
##########################
##########################
IWALLETS=$(su $YOU -c "ipfs add -rHq './wallets' | tail -n 1")
# CHAIN STATE FOR $IPFSNODEID
echo $IWALLETS > ./wallets/.$IPFSNODEID.wallets.chain
log "__SUB:ipfs_node_wallets_add: NEW './wallets' HASH ipfs ls $IWALLETS"
# PUBLISH IPNS NODE WALLETS
NODEWALLETS=$(su $YOU -c "ipfs name publish --quieter /ipfs/$IWALLETS")
log "__SUB:ipfs_node_wallets_add: G1smsWallet = ipfs cat /ipns/$NODEWALLETS/$PHONE/$PHONE.g1cents Zen"
echo $IWALLETS
}
#################################################################################################################################
function sms_uid2key (){
RIBFILE="./wallets/$PHONE/$PHONE.uidrib"
NAMEFILE="./wallets/$PHONE/$PHONE.uidname"
UIDNAFILE="./wallets/$PHONE/$PHONE.uidna"
# New related wallet asked
ASKWALLET="$1"
#For micro payement
PHONE="$2"
PIN="$3"
if -f "$UIDNAFILE"; then UIDNA=$(cat "$UIDNAFILE"); else UIDNA=""; fi
log "__SUB:sms_uid2key: $UIDNA Try to associate $ASKWALLET with $PHONE... SEARCHING..."
if [[ $UIDNA && "$ASKWALLET" == "$UIDNA" ]]; then
log "__SUB:sms_uid2key: "$ASKWALLET" == "$UIDNA" !!"
sms_SEND "$PHONE" "[G1sms+] $UIDNA est le nom de votre porte monnaie libre pour $PHONE. Il ne peut également être compte maître!"
fi
# Test if member files exists
if [ -f "$NAMEFILE" ]; then
UIDNAME=$(cat "$NAMEFILE")
log "__SUB:sms_uid2key: ALREADY $UIDNAME related Wallet for $PHONE Wallet change to $ASKWALLET !!"
if [["$UIDNAME" != "$ASKWALLET" ]]; then
sms_SEND "$ADMINPHONE" "ADMIN! INFO: $PHONE Wallet associé à $UIDNAME change en $ASKWALLET !!"
sms_SEND "$PHONE" "[G1sms+] Votre portefeuille maitre va passer de $UIDNAME à $ASKWALLET!"
fi
fi
# Search fo duplicate
DUP=$(grep -Rwl "$ASKWALLET" ./wallets/*/*.uidname | cut -f 3 -d '/')
if [[ "$DUP" != "" && "$DUP" != "$PHONE" ]]; then
sms_SEND "$ADMINPHONE" "ADMIN! INFO: $PHONE prend le même $ASKWALLET, que $DUP"
sms_SEND "$DUP" "Vous devez savoir que $ASKWALLET est associé à un autre téléphone que le votre également!"
fi
###########################################################
# FIND ASKWALLET into wallets, wallest_swarm OR duniter (silkaj)
DESTMEM="$VIRDEST"
# SEARCH IN WALLETS
LOCAL=$(grep -Rwl "$ASKWALLET" ./wallets/*/*.uidna | cut -f 3 -d '/')
if [[ "$LOCAL" != "" ]]; then
# LOCAL G1sms account
DESTRIB=$(cat ./wallets/$LOCAL/$LOCAL.pub)
DESTPHONE=$LOCAL
log "__SUB:sms_uid2key: FOUND LOCAL ($MASTERPHONE) G1sms+ wallet: $DESTRIB ($LOCAL)"
else
# SEARCH IN WALLETS SWARM
INSWARM=$(grep -Rwl "$ASKWALLET" ./wallets_swarm/*/*.uidna | cut -f 3 -d '/')
if [[ "$INSWARM" != "" ]]; then
REMOTENODE=$(cat ./wallets_swarm/$INSWARM/MASTERPHONE.sms)
DESTRIB=$(cat ./wallets_swarm/$INSWARM/$INSWARM.pub)
DESTPHONE=$INSWARM
log "__SUB:sms_uid2key: FOUND SWARM ($REMOTENODE) G1sms+ wallet: $DESTRIB ($INSWARM)"
else
# SEARCH WITH SILKAJ
log "__SUB:sms_uid2key: GETTING FROM SILKAJ MEMBER ID"
DESTRIB=$(./silkaj/silkaj id "$ASKWALLET" | grep -w "$ASKWALLET" | awk '{print $2}')
log "__SUB:sms_uid2key: OUT OFF SWARM MEMBER wallet: $DESTRIB"
# GET CESIUM+ geoPoint DATA (TODO: cron for GCHANGE SMS Alert)
curl -s ${CESIUM}/user/profile/${DESTRIB} | jq '._source.geoPoint' > "./wallets/$PHONE/$PHONE.uidgeo.json"
log "__SUB:sms_uid2key: GET CESIUM+ geoPoint and AVATAR : $(cat ./wallets/$PHONE/$PHONE.uidgeo.json)"
curl -s ${CESIUM}/user/profile/${DESTRIB} | jq '._source.avatar._content' | sed 's/\"//g' | base64 -d > "./wallets/$PHONE/$PHONE.avatar.png"
fi
fi
if [ "$DESTRIB" != "" ]; then
echo "$DESTRIB" > "$RIBFILE"
echo "$ASKWALLET" > "$NAMEFILE"
log "__SUB:sms_uid2key: NEW EXTERNAL RELATED $DESTRIB / FOR MEMBER OR WALLET = $ASKWALLET - INIT 0.1 TX"
INIT=$(./silkaj/silkaj transaction --auth-scrypt -salt="$PHONE" -password="$PIN" --amount="0.1" --output=$DESTRIB --comment="[G1sms+] Porte Monnaie $(cat $UIDNAFILE) -> $(cat $NAMEFILE)" -y)
log "__SUB:sms_uid2key: INIT = $INIT | Create Connection with related Wallet."
log_history "$PHONE" "MEMBER, $ASKWALLET"
sms_SEND "$ADMINPHONE" "ADMIN! LOG: $PHONE Nouveau compte lié $UIDNA -- $ASKWALLET ($INIT)"
else
sms_SEND "$ADMINPHONE" "ADMIN! LOG: $PHONE / $ASKWALLET ABSENT"
fi
log "__SUB:sms_uid2key: $MEMBER($DESTRIB)"
echo "$DESTRIB"
}
#################################################################################################################################
function sms_INIT_ACCOUNT () {
log "__SUB:sms_INIT_ACCOUNT: ($1=phone, $2=NOSMS)"
PHONE="$1"
UNKNOWN=0
# TODO Optimize GLOBAL/local variables use, use more function array output ?!
# Initiate PHONE settings files and values
IPFSNODEIDFILE="./wallets/$PHONE/MASTERPHONE.ipfsid" # Contains G1sms+ NODE IPFS id
SMSNODE="./wallets/$PHONE/MASTERPHONE.sms" # Contains G1sms+ SMS phone number
PINFILE="./wallets/$PHONE/$PHONE.pin" # Contains phone wallet diceware password
GPGPINFILE="./wallets/$PHONE/$PHONE.pin.gpg" # Contains g1smsnode cypher phone wallet diceware password
AUTHCRYPTFILE="./wallets/$PHONE/$PHONE.authfile.crypt" # Crypt with $NODE_G1PUBKEY
PUBKEYFILE="./wallets/$PHONE/$PHONE.pub" # Contains phone wallet public key (RIB)
UNITFILE="./wallets/$PHONE/$PHONE.unit" # Contains phone wallet prefered unit (LOVE,G1,DU)
RIBFILE="./wallets/$PHONE/$PHONE.uidrib" # Contains phone wallet related UID Pubkey
NAMEFILE="./wallets/$PHONE/$PHONE.uidname" # Contains the member UID related to wallet
MAILFILE="./wallets/$PHONE/$PHONE.email" # Contains the member EMAIL (Send BILLET)
GPGMAILFILE="./wallets/$PHONE/$PHONE.email.gpg" # Contains the member EMAIL (Send BILLET)
UIDNAFILE="./wallets/$PHONE/$PHONE.uidna" # Contains the G1sms+ UID Name for recursive VIR
GEOFILE="./wallets/$PHONE/$PHONE.uidgeo.json" # Contains the member GeoPoint from member Cesium+ related to wallet
RECFILE="./wallets/$PHONE/VIREMENTS" # File showing recurrent payements are left to be done with this G1sms Wallet
GPGRECFILE="./wallets/$PHONE/VIREMENTS.gpg" # Cypher file with GPGPASS (Node .Identity.PrivKey)
G1COUNTFILE="./wallets/$PHONE/$PHONE.g1cents" # Last sms_COUNT.sh call value
CHAINFILE="./wallets/$PHONE/$PHONE.chain" # Contains wallet last IPFS hash before ZEN value change
DABUIDF="./wallets/$PHONE/uidna.G1TAGNODE" # Contains G1Dab Name
# GET CURRENT NODE UIDNA (default DABUID)
NODEUIDNA=$(cat "./wallets/.$IPFSNODEID/$IPFSNODEID.uidna")
[[ $NODEUIDNA == "" ]] && NODEUIDNA=$(cat "./wallets/.$IPFSNODEID/_uidna")
######################################################################
# (NO PIN) = FIRST ACCOUNT: Create wallet
if [ ! -f "$PINFILE" ]; then
# NOSMS mode?
if [[ $2 == "NOSMS" ]]; then UNKNOWN="unknown"; return; fi
#######################
# Create Account Files
#######################
mkdir -p "./wallets/$PHONE/"
PIN=$(./shell/diceware.sh | xargs)
PUBKEY=$(./silkaj/silkaj generate_auth_file --auth-scrypt -salt="$PHONE" -password="$PIN")
# BACKUP authfile available to authfile or PIN owner (DOUBLON AVEC PIN) TEST
log "__SUB:sms_INIT_ACCOUNT: ./shell/natools.py encrypt -p $PUBKEY -i ""./authfile"" -o ""$AUTHCRYPTFILE"""
./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "./authfile" -o "$AUTHCRYPTFILE"
rm -f ./authfile
log "__SUB:sms_INIT_ACCOUNT: !! G1 Wallet CREATED: $PHONE $PIN : $PUBKEY"
#######################
# GIVE NAME TO WALLET
#######################
# 4 derniers chiffres du numéro de téléphone
TAIL=${PHONE:8:4}
# No UIDNA yet. Create new
UIDNA=$(./shell/diceware.sh 1 | xargs)
# Check if already existing among all swarm wallets
while [[ $(grep -Rwl $UIDNA$TAIL ./wallets_swarm/*/*.uidna) ]]; do UIDNA=$(./shell/diceware.sh 1 | xargs); done
echo "$UIDNA$TAIL" > "$UIDNAFILE"
#######################
# ACTIVATE G1SMS WALLET
#######################
# log "$NODE_G1AUTHFILE $PUBKEY THIRD_PARTY_MANAGER:$NODE_G1PUBKEY"
TX_IN=$(./silkaj/silkaj transaction --auth-file -file="$NODE_G1AUTHFILE" --amount=3.24 --output=$PUBKEY --comment="[G1sms+] $UIDNA$TAIL 3RD:$NODE_G1PUBKEY " -y)
log "__SUB:sms_INIT_ACCOUNT: G1 Wallet TX IN: $TX_IN"
sleep 2
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)
log "__SUB:sms_INIT_ACCOUNT: G1 Wallet TX OUT: $TX_OUT"
################
# GPG cypher PIN (ONLY CREATOR NODE CAN ACCESS IT !)
################
echo "$PIN" > "$PINFILE"
echo "${GPGPASS}" | gpg -q --output "$GPGPINFILE" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "$PINFILE"
PIN=$(cat "$PINFILE" | xargs)
# [[ "$PIN" != "" ]] && echo "" > "$PINFILE" || log "SYSTEM ERROR :: PIN EMPTY !!! GPG???"
echo "114" > "$G1COUNTFILE"
echo "$PUBKEY" > "$PUBKEYFILE"
echo "$COIN" > "$UNITFILE"
echo "$MASTERPHONE" > "$SMSNODE"
log_history "$PHONE" "NEW, $PUBKEY"
# ADD WALLET TO IPFS
I=$(ipfs_node_wallets_add)
fi
#################################################
# PUBLISH G1sms+ WALLET PROPERTIES MAIN PROCESS
#################################################
######################################################################
if [[ ! -f "$GPGPINFILE" ]]
then
# ------------8<------------------
# WALLET MIGRATION -> CRYPT PIN (For Old G1sms clear PIN WALLETs...)
# ADD NEW PARAMS TO WALLET
#######################
# 4 derniers chiffres du numéro de téléphone
echo "$MASTERPHONE" > "$SMSNODE"
TAIL=${PHONE:8:4}
# No UIDNA yet. Create new
UIDNA=$(./shell/diceware.sh 1 | xargs)
# Check if already existing among all swarm wallets
while [[ $(grep -Rwl $UIDNA$TAIL ./wallets_swarm/*/*.uidna) ]]; do UIDNA=$(./shell/diceware.sh 1 | xargs); done
echo "$UIDNA$TAIL" > "$UIDNAFILE"
PIN=$(cat "$PINFILE" | xargs)
log "__SUB:sms_INIT_ACCOUNT: Old G1sms wallet ;) SECURITY HOLE... Chiffrage PGP du PIN !!!"
echo "${GPGPASS}" | gpg -q --output "$GPGPINFILE" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "$PINFILE";
./shell/natools.py encrypt -p $NODE_G1PUBKEY -i "$PINFILE" -o "$PINFILE.$NODE_UIDNA.crypt"
else
# ------------>8------------------
log "__SUB:sms_INIT_ACCOUNT: Déchiffrage PGP PIN..."
# GPG decypher PIN
# TODO make decypher less stress on filesystem, use /tmp and ramdisk
echo "${GPGPASS}" | gpg -d -q --output "$PINFILE" --yes --pinentry-mode loopback --passphrase-fd 0 "$GPGPINFILE"
PIN=$(cat "$PINFILE" | xargs)
[[ "$PIN" == "" && -f "$PINFILE.$NODE_UIDNA.crypt" ]] && ./shell/natools.py decrypt -k "$NODE_G1AUTHFILE" -i "$PINFILE.$NODE_UIDNA.crypt" -o "$PINFILE" && PIN=$(cat "$PINFILE");
# [[ "$PIN" != "" ]] && echo "" > "$PINFILE" || log "SYSTEM ERROR :: PIN EMPTY !!! GPG???"
fi
# CYPHER EMAIL FILE
if [[ ! -f "$GPGMAILFILE" ]]; then
# ------------8<------------------
log "__SUB:sms_INIT_ACCOUNT: NO EMAIL .gpg file, CREATING it"
if [ -f "$MAILFILE" ]; then
MAIL=$(cat "$MAILFILE");
echo "${GPGPASS}" | gpg -q --output "$GPGMAILFILE" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "$MAILFILE";
else
MAIL="";
fi
# ------------>8------------------
else
log "__SUB:sms_INIT_ACCOUNT: DECODING EMAIL .gpg... "
echo "${GPGPASS}" | gpg -d -q --output "$MAILFILE" --yes --pinentry-mode loopback --passphrase-fd 0 "$GPGMAILFILE"
MAIL=$(cat "$MAILFILE");
[[ $MAIL == "" && -f "$MAILFILE.node.crypt" ]] && ./shell/natools.py decrypt -k "$NODE_G1AUTHFILE" -i "$MAILFILE.node.crypt" -o "$MAILFILE" && MAIL=$(cat "$MAILFILE");
# EMPTY CLEAR EMAIL FILE
[[ $MAIL != "" ]] && echo "" > "$MAILFILE" || log "SYSTEM ERROR :: MAIL EMPTY !!! GPG???"
fi
# ------------8<------------------
# REMOVE OLD HISTORY FILES
rm -f ./wallets/$PHONE/$PHONE.hist
# ------------>8------------------
# PUBLISH GLOBAL VARS FROM ACCOUNT FILES
echo $IPFSNODEID > $IPFSNODEIDFILE
PUBKEY=$(cat "$PUBKEYFILE" | xargs)
UNIT=$(cat "$UNITFILE" | xargs)
if [ -f "$G1COUNTFILE" ]; then AMOUNTG1=$(bc <<< "scale=2; $(cat $G1COUNTFILE) / 100"); fi
if [ -f "$NAMEFILE" ]; then MEMBER=$(cat "$NAMEFILE"); else MEMBER=""; fi
if [ -f "$RIBFILE" ]; then MEMRIB=$(cat "$RIBFILE"); else MEMRIB=""; fi
if [ -f "$UIDNAFILE" ]; then UIDNA=$(cat "$UIDNAFILE"); else UIDNA=""; fi
if [ -f "$DABUIDF" ]; then DABID=$(cat "$DABUIDF"); else DABID="$NODEUIDNA"; fi
AMOUNTDU=$(bc <<< "scale=2; $AMOUNTG1 / $DUFACTOR")
AMOUNTLOVE=$(bc <<< "$AMOUNTG1 * 100 / $DUFACTOR")
case "$UNIT" in
G1)
AMOUNT=$AMOUNTG1
;;
DU)
AMOUNT=$AMOUNTDU
;;
*)
AMOUNT=$AMOUNTLOVE
;;
esac
log "__SUB:sms_INIT_ACCOUNT: $AMOUNTG1 = $AMOUNT $UNIT"
# LOG # TODO REMOVE PIN LOG!!
log "#####################"
log "PHONE: $PHONE"
log "PIN: $PIN"
log "PUBKEY: $PUBKEY"
log "G1: $AMOUNTG1"
log "AMOUNT: $AMOUNT $UNIT"
log "UIDNA: $UIDNA"
log "MEMBER: $MEMBER"
log "MEMRIB: $MEMRIB"
log "MAIL: $MAIL"
log "DABID: $DABID"
log "#####################"
log "__SUB:sms_INIT_ACCOUNT: END"
}
#################################################################################################################################
function check_account (){
log "__SUB:check_account: SILKAJ...... max 30 sec....."
AMOUNTG1=$(./shell/timeout.sh -t 30 ./silkaj/silkaj amount "$PUBKEY")
AMOUNTDU=$(bc <<< "scale=2; $AMOUNTG1 / $DUFACTOR")
AMOUNTLOVE=$(bc <<< "$AMOUNTG1 * 100 / $DUFACTOR")
case "$UNIT" in
G1)
AMOUNT=$AMOUNTG1
;;
DU)
AMOUNT=$AMOUNTDU
;;
*)
AMOUNT=$AMOUNTLOVE
;;
esac
G1cents=$(echo "$AMOUNTG1*100" | bc -l | awk '{print int($0)}')
log "__SUB:check_account: $PHONE: $AMOUNTG1 = $AMOUNT $UNIT ($G1cents) - $PUBKEY"
if [[ "$G1cents" != "" ]]; then
NN=$(cat "./wallets/$PHONE/$PHONE.n")
((NN++))
echo "$NN" > "./wallets/$PHONE/$PHONE.n"
echo $G1cents > "./wallets/$PHONE/$PHONE.g1cents"
# REFRESH NODE IPFS wallets & PUBLISH
I=$(ipfs_node_wallets_add)
# Register modification
echo "$I" > "./wallets/$PHONE/$PHONE.chain"
else
# BAN actual Duniter Node in case of silkaj timeout.
export NEWDUNITER=$(./shell/checknodes.sh 'BAN')
log "__SUB:check_account: !!! SILKAJ IS TOO SLOW !!! => NEW DUNITER SERVER: $NEWDUNITER"
fi
declare -a aaa
aaa=( "$AMOUNTG1" "$AMOUNT" "$UNIT" )
log "__SUB:check_account: $(declare -p aaa)"
echo ${aaa[@]}
}
#################################################################################################################################
function make_accounting (){
# CONVERT AMOUNT UNIT to VIR G1
case "$UNIT" in
G1)
VIR=$AMOUNT;
;;
DU)
VIR=$(bc -l <<< "scale=2; $AMOUNT * $DUFACTOR")
;;
LOVE)
VIR=$(bc -l <<< "scale=2; $AMOUNT * $DUFACTOR / 100")
;;
*)
VIR=$(bc -l <<< "scale=2; $AMOUNT * $DUFACTOR / 100")
;;
esac
###########################################################
# GET G1sms wallet AMOUNTG1 and CHECK for right balance
log "__SUB:make_accounting: LOCAL $VIR amount for $PHONE : $PUBKEY"
# Try to ask silkaj. If timeout, use SWARM last known $PHONE.g1cents value
AMOUNTG1=$(./shell/timeout.sh -t 30 ./silkaj/silkaj amount "$PUBKEY")
if [[ "$AMOUNTG1" == "" ]]; then AMOUNTG1=0; fi
# TAX AND MINIMUM WALLET ACCOUNT CALCULATION
PERCENT=0$(bc -l <<< "scale=2; $VIR / $SWARMCOMM")
MIN=$(bc -l <<< "$AMOUNTG1 - $PERCENT - $LIMIT")
CHARGE=$(bc -l <<< "scale=2; $VIR + $PERCENT + $LIMIT")
TESTMIN=$( echo "${VIR} < ${MIN}" | bc -l )
log "__SUB:make_accounting: TEST $AMOUNT $UNIT :: $VIR + $PERCENT + $LIMIT = $CHARGE < $AMOUNTG1 ? $TESTMIN"
declare -a myarray
myarray=( "$TESTMIN" "$VIR" "$PERCENT" "$CHARGE")
log "__SUB:make_accounting: $(declare -p myarray)"
echo ${myarray[@]}
}
#################################################################################################################################
function move_g1cents (){
# MODIFY $PHONE.g1cents value according to current silkaj
# TODO MAKE ALL SILKAJ TX CALL HERE. OR REMOVE???
# USE TASK COMMUNICATION BETWEEN NODES.
phonesrc=$1
pubkeydest=$2
zenvalue=$3
log "__SUB:move_g1cents: $phonesrc ($zenvalue) -> $pubkeydest :: $G1COUNTFILE ::"
### CHANGE INPUT VALUE ####
curvalue=$(cat ./wallets/$phonesrc/$phonesrc.g1cents | cut -d '.' -f 1)
newvalue=$(bc -l <<< "$curvalue - $zenvalue")
echo $newvalue > ./wallets/$phonesrc/$phonesrc.g1cents
log "__SUB:move_g1cents: DEBIT: OK APPLY $curvalue - $zenvalue = $newvalue TO ./wallets/$phonesrc/$phonesrc.g1cents"
### CHANGE OUTPUT VALUE ####
# TEST IF $dest is a wallets_ G1sms PUBKEY?
winnerphone=$(grep -Rwl "$pubkeydest" ./wallets_swarm/*/*.pub | cut -d '.' -f 2 | cut -d '/' -f 3)
if [[ $winnerphone ]]; then
# GET INFORMATON ON MANAGER NODE
NODEID=$(cat ./wallets_swarm/$winnerphone/MASTERPHONE.ipfsid)
if [[ "$NODEID" == "$IPFSNODEID" ]]; then
# ACTUAL NODE IS WALLET MANAGING NODE ;)
curvalue=$(cat ./wallets/$winnerphone/$winnerphone.g1cents | cut -d '.' -f 1)
newvalue=$(bc -l <<< "$curvalue + $zenvalue")
echo $newvalue > ./wallets/$winnerphone/$winnerphone.g1cents
log "__SUB:move_g1cents: CREDIT: OK APPLY $curvalue + $zenvalue = $newvalue TO ./wallets/$winnerphone/$winnerphone.g1cents"
else
# MUST ASK MANAGER NODE TO CORRECT g1cents FOR $winnerphone WALLET
# WRITE TASK TO .$NODEID CHANNEL
mkdir -p ./wallets/.$NODEID/TASK/
# USE natools with NODEPUB crypt
echo "$zenvalue" > "/tmp/move_g1cents.$winnerphone"
# GET NODE G1sms Wallet PUBKEY
NODEPUB=$(cat ./wallets_swarm/.$NODEID/$NODEID.pub)
./shell/natools.py encrypt -p $NODEPUB -i "/tmp/move_g1cents.$winnerphone" -o "./wallets/.$NODEID/TASK/$NANODATE.move_g1cents.$winnerphone.NODEPUB.crypt"
log "__SUB:move_g1cents: CREDIT: WRITE TASK TO ./wallets/.$NODEID/$NANODATE.move_g1cents.$winnerphone.NODEPUB.crypt "
rm -f /tmp/move_g1cents.$winnerphone
fi
else
log "__SUB:move_g1cents: ERROR: NO $pubkeydest WALLET FOUND"
fi
# UPDATE MY new ./wallet STATUS to SWARM
I=$(ipfs_node_wallets_add)
}
#################################################################################################################################
# Contact database management
function add_contact(){
userDB="gammu"
pwdDB=$(cat /etc/gammu-smsdrc | grep "password =" | awk '{ print $3 }' || exit 1)
local sql="mysql -N -u$userDB -p$pwdDB gammu -e"
local PHONE=$1
local MEMBERUID=$2
[[ -z $MEMBERUID ]] && MEMBERUID=$PHONE
[[ -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')"
if [[ -z $($sql "SELECT * FROM pbk WHERE Number=$PHONE") ]]; then
$sql "INSERT INTO pbk (GroupID, Name, Number, id_user, is_public) VALUES ('1', '$MEMBERUID', '$PHONE', '1', 'false')" || exit 1
local id_user=$($sql "SELECT ID FROM pbk WHERE Number=$PHONE" || exit 1)
$sql "INSERT INTO user_group (id_group, id_pbk, id_pbk_groups, id_user) VALUES ('$id_user', '$id_user', '1', '1')" || exit 1
else
log "${red}Contact already exist$c_"
fi
}
function rm_contact(){
userDB="gammu"
pwdDB=$(cat /etc/gammu-smsdrc | grep "password =" | awk '{ print $3 }' || exit 1)
local sql="mysql -N -u$userDB -p$pwdDB gammu -e"
local PHONE=$1
local MEMBERUID=$2
[[ -z $MEMBERUID ]] && MEMBERUID=$PHONE
if [[ ! -z $($sql "SELECT * FROM pbk WHERE Number=$PHONE") ]]; then
local id_user=$($sql "SELECT ID FROM pbk WHERE Number=$PHONE" || exit 1)
$sql "DELETE FROM pbk WHERE Number=$PHONE" || exit 1
$sql "DELETE FROM user_group WHERE id_group='$id_user'" || exit 1
else
log "${red}Contact doesn't exist$c_"
fi
}