2020-08-08 03:26:32 +02:00
#!/bin/bash -e
2020-07-12 03:06:41 +02:00
2020-07-12 03:36:24 +02:00
MY_PATH = " `dirname \" $0 \"` " # relative
MY_PATH = " `( cd \" $MY_PATH \" && pwd )` " # absolutized and normalized
[ [ -n $MY_PATH /.env ] ] && source $MY_PATH /.env || ( echo " Please create $MY_PATH /.env as $MY_PATH /.env.example " && exit 1)
2020-08-08 03:26:32 +02:00
function DEBUG( )
{
[ " $_DEBUG " = = "on" ] && $@
}
2020-07-23 20:28:09 +02:00
chanDB( ) {
2020-08-07 19:03:13 +02:00
if [ [ " $1 " = ~ '@' ] ] ; then
chan = $( echo " $1 " | awk -F '@' '{ print $1 }' )
tool = $( echo " $1 " | awk -F '@' '{ print $2 }' )
else
chan = " $1 "
fi
2020-08-08 03:26:32 +02:00
[ [ ! $chan ] ] && echo "Please select user." && return 2
2020-08-07 19:03:13 +02:00
if [ [ " $chan " = ~ '#' ] ] ; then
homedir = " $HOME /.bog/ids/ $tool / $chan "
else
homedir = " $HOME /.bog/ids/ $tool /@ $chan "
fi
2020-08-08 03:26:32 +02:00
DEBUG echo " _ ${ FUNCNAME [0] } _: $chan "
DEBUG echo " _ ${ FUNCNAME [0] } _: $tool "
DEBUG echo " _ ${ FUNCNAME [0] } _: $homedir "
2020-07-23 20:28:09 +02:00
[ [ -f $homedir /db ] ] && source $homedir /db
}
# Send a message
2020-07-12 03:06:41 +02:00
sendmsg( ) {
2020-08-07 19:03:13 +02:00
chanDB " $1 "
2020-08-08 03:26:32 +02:00
[ [ ! " ${ chan : : 1 } " = ~ ^( #|@)$ ]] && chan="@$chan"
2020-07-22 21:12:27 +02:00
local msg = " ${ @ : 2 } "
2020-07-12 03:06:41 +02:00
2020-08-08 03:26:32 +02:00
[ [ ! $chan || ! $msg ] ] && echo "Please select user and message." && return 2
DEBUG echo " _ ${ FUNCNAME [0] } _: $chan "
DEBUG echo " _ ${ FUNCNAME [0] } _: $tool "
2020-07-12 03:06:41 +02:00
2020-08-08 03:26:32 +02:00
if [ [ $tool = = "axiomchat" ] ] ; then
curl -s -H " X-Auth-Token: $AC_API_TOKEN " \
-H " X-User-Id: $AC_API_ID " \
-H "Content-type:application/json" \
$AC_API_URL /api/v1/chat.postMessage \
-d " { \"channel\": \" $chan \", \"text\": \" $msg \" } " > /dev/null 2>& 1
else
echo -e " $msg "
fi
2020-07-18 02:59:31 +02:00
}
2020-07-23 02:16:31 +02:00
getPubkey( ) {
url = " $node /wot/members "
2020-08-08 03:26:32 +02:00
# [[ -z $username || $username == "bot" ]] && username=$(echo $1 | tr -d '@')
2020-07-23 02:16:31 +02:00
# old way: local pubkey=$(curl -m 5 -s $url | grep -wiB 1 $username | grep "pubkey" | awk -F '"' '{ print $4 }')
2020-08-08 03:26:32 +02:00
[ [ ! $usernameK ] ] && usernameK = " $1 "
[ [ ${ usernameK : : 1 } = = '@' ] ] && usernameK = ${ usernameK #? }
if [ [ " $usernameK " = ~ '@' ] ] ; then
chanDB " $usernameK "
else
local pubkey = $( curl -m 10 -s $url | jq -r --arg username " $usernameK " '.results[] | select(.uid==$username) | .pubkey' )
fi
# DEBUG echo "_${FUNCNAME[0]}_: $pubkey"
# DEBUG echo "_${FUNCNAME[0]}_: $usernameK"
2020-07-23 02:16:31 +02:00
[ [ $pubkey ] ] && echo " $pubkey " || echo "none"
2020-08-08 03:26:32 +02:00
exit 0
2020-07-23 02:16:31 +02:00
}
getPseudo( ) {
url = " $node /wot/members "
2020-09-04 01:47:58 +02:00
args = $( echo $@ | tr ' ' '\n' | grep -vEw 'biz|bog' | tr '\n' ' ' )
2020-07-23 02:16:31 +02:00
[ $( echo $args | wc -w) -ge 2 ] && pubkey = $( echo $args | awk '{ print $2 }' ) || pubkey = $1
# pubkey="$1"
pubkey = $( echo $pubkey | tr -d '@' )
local pseudo = $( curl -m 10 -s $url | jq -r --arg pubkey " $pubkey " '.results[] | select(.pubkey==$pubkey) | .uid' )
[ [ $pseudo ] ] && echo " Le pseudo correspondant à la clé publique $pubkey est: @ $pseudo " || echo " La clé publique $pubkey n'a pas de pseudo associé. "
}
2020-07-23 20:28:09 +02:00
getPassword( ) {
2020-08-07 19:03:13 +02:00
chanDB " $1 "
2020-07-18 02:59:31 +02:00
if [ [ -d $homedir ] ] ; then
2020-07-23 20:28:09 +02:00
sendmsg $chan " Voici les identifiants de connexion de votre ḠCompte: \n \
2020-08-08 03:26:32 +02:00
Login: *$salt *\n Mot de passe: *$pepper *\n Clé publique: *$pubkey * \n \
2020-07-22 21:12:27 +02:00
Calcul du solde de votre compte en cours ..."
2020-07-23 20:28:09 +02:00
else
echo "Vous n'avez pas de compte chez nous."
fi
}
info_account( ) {
2020-08-07 19:03:13 +02:00
chanDB " $1 "
2020-07-23 20:28:09 +02:00
if [ [ -d $homedir ] ] ; then
2020-08-08 03:26:32 +02:00
sendmsg $chan " Récupération de votre solde en cours sur * $pubkey * ... "
2020-07-22 21:12:27 +02:00
local solde = $( $silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2-)
2020-08-08 03:26:32 +02:00
echo " Votre solde est de * $solde * "
2020-07-18 02:59:31 +02:00
else
echo "Vous n'avez pas de compte chez nous."
fi
}
init_account( ) {
2020-08-07 19:03:13 +02:00
chanDB " $1 "
2020-07-18 02:59:31 +02:00
if [ [ -f $homedir /secret.dunikey ] ] ; then
pubkey = $( cat $homedir /secret.dunikey| grep "pub: " | awk '{ print $2 }' )
2020-08-08 03:26:32 +02:00
echo " Vous disposez déjà d'un portefeuille Ḡ1: * $pubkey * "
return 2
2020-07-22 21:12:27 +02:00
else
sendmsg $dest "Création de votre portefeuille..."
2020-07-18 02:59:31 +02:00
fi
# LOGIN (=SALT)
2020-07-22 21:12:27 +02:00
local salt = " $( $MY_PATH /../dunikey/diceware.sh 6) "
2020-07-18 02:59:31 +02:00
# PASS (=PEPPER)
2020-07-22 21:12:27 +02:00
local pepper = " $( $MY_PATH /../dunikey/diceware.sh 4) "
2020-07-18 02:59:31 +02:00
# The pubkey for this salt/papper peer
2020-07-22 21:12:27 +02:00
local pubkey = $( python3 $MY_PATH /../dunikey/key_create_dunikey.py " $salt " " $pepper " )
2020-07-18 02:59:31 +02:00
2020-08-08 03:26:32 +02:00
[ [ ! -f /tmp/secret.dunikey ] ] && echo "Arf, problem happens making your secret.dunikey" && return 2
2020-07-18 02:59:31 +02:00
[ [ ! -d $homedir ] ] && mkdir -p $homedir
[ [ -f /tmp/secret.dunikey ] ] && rm -f $homedir /secret.dunikey && mv /tmp/secret.dunikey $homedir /secret.dunikey
echo -e " salt=\" $salt \"\npepper=\" $pepper \"\npubkey=\" $pubkey \" " > $homedir /db
2020-07-22 21:12:27 +02:00
# info_account $chan
2020-08-08 03:26:32 +02:00
echo -e " Votre ḠCompte a bien été créé.\nPubkey: * $pubkey * "
2020-07-18 02:59:31 +02:00
}
rm_account( ) {
2020-08-07 19:03:13 +02:00
chanDB " $1 "
2020-07-18 02:59:31 +02:00
if [ [ -d $homedir ] ] ; then
2020-08-08 03:26:32 +02:00
local solde = $( $silkaj balance $pubkey | grep "Total Quantitative" | awk -F= '{ print $2 }' | cut -c2- | awk '{ print $1 }' )
[ [ " $solde " != "0.0" ] ] && echo " Votre solde n'est pas nulle: $solde Ḡ1. Veuillez vider votre compte avant de pouvoir le supprimer. " && return 2
2020-07-18 02:59:31 +02:00
echo "Suppression des données de votre portefeuille de votre ḠBanque."
2020-08-08 03:26:32 +02:00
echo -e " Login: * $salt *\nMot de passe: * $pepper *\nClé publique: * $pubkey * "
2020-07-18 02:59:31 +02:00
rm -r $homedir
echo "Suppression terminé, nous ne disposons plus d'aucune donnée vous concernant."
else
echo "Vous n'avez pas de compte chez nous."
fi
2020-07-12 03:06:41 +02:00
}
2020-07-12 03:36:24 +02:00
2020-07-22 21:12:27 +02:00
tx( ) {
local dest = " $2 "
2020-08-08 03:26:32 +02:00
local dest1 = " $dest "
2020-07-22 21:12:27 +02:00
local amount = $( echo " $3 " | tr ',' '.' )
local comment = " ${ @ : 4 } "
2020-08-08 03:26:32 +02:00
! [ [ $dest && $amount ] ] && echo "Des arguments sont manquants. Ex: pay Do99s6wQR2JLfhirPdpAERSjNbmjjECzGxHNJMiNKT3p 10 Merci pour la tarte" && return 2
2020-07-22 21:12:27 +02:00
#This shoudl works, but accents are ok and should not... [[ ! "$comment" =~ [^a-zA-Z0-9\ ] ]] && echo OK
2020-08-08 03:26:32 +02:00
[ [ " $comment " = ~ [ ^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-?:'(' ')' .+\ ] ] ] && echo "Votre commentaire contient des charactères non autorisés." && return 2
2020-07-22 21:12:27 +02:00
[ [ -z " $comment " ] ] && local comment = "Transaction effectuee avec BoG."
2020-08-08 03:26:32 +02:00
[ [ $amount = ~ '-' ] ] && echo " La somme de votre transaction ne peut être négative: * $amount * Ḡ1 " && return 2
2020-07-23 02:16:31 +02:00
[ [ $( echo $dest | wc -c) -le 43 ] ] && local dest = $( getPubkey $dest )
2020-08-08 03:26:32 +02:00
[ [ $dest = = "none" ] ] && echo "Ce pseudo n'a pas de clé Ḡ1 associé." && return 2
2020-07-22 21:12:27 +02:00
2020-08-08 03:26:32 +02:00
toolDest = $( echo $dest1 | awk -F '@' '{ print $2 }' )
chanDB " $1 "
sender = " $chan @ $tool "
2020-08-07 19:03:13 +02:00
local result = $( $silkaj --auth-file --file $homedir /secret.dunikey tx --amount $amount --output $dest --comment " $comment " -y)
2020-08-08 03:26:32 +02:00
resultCode = $?
[ [ -n $result ] ] && echo " \`\`\` $result \`\`\` " || ( echo "Une erreur est survenu, votre transaction n'a pas été effecutée." && return 2)
[ [ ${ dest1 #? } = ~ '@' && $result && $resultCode = = '0' && $toolDest != "telegram" ] ] && sendmsg $dest1 " Vous avez reçus un paiement de * $amount Ḡ1* de la part de * $sender *\nCommentaire: * $comment * " || true
2020-07-22 21:12:27 +02:00
}
2020-07-23 02:16:31 +02:00
history_account( ) {
2020-08-07 19:03:13 +02:00
chanDB " $1 "
2020-08-08 03:26:32 +02:00
sendmsg $chan " Récupération de l'historique de votre compte * $pubkey * ... "
2020-07-22 21:12:27 +02:00
local result = $( $silkaj history $pubkey )
echo " \`\`\` $result \`\`\` "
}
2020-08-07 00:31:39 +02:00
#[[ $(type $1 | grep "est une fonction") ]] && $@ || echo "Veuillez préciser votre commande"