astroport/zen/ssb_SURVEY_swarmkey.sh

110 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
########################################################################
# Author: Fred (support@qo-op.com)
# Version: 2020.03.24
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
########################################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
ME="${0##*/}"
echo '
########################################################################
# \\///
# qo-op
############# '$MY_PATH/$ME'
########################################################################
# ex: ./'$ME'
# SURVEY received #astroport-swarmkey ipfs_swarm.key.crypt for IPFS
######################################################################## _ _ _ _ _
___ _ _ __
| |_)|_(_ _ _.._._ _ | _
_|_| | __) _>\/\/(_|| | | ||<(/_\/
/
USED ONLY ONCE... Next swarm.key change will accurs in ~/.zen/ipfs-swarm
{
"key": "%ydN2fyzKTKzVZiZJSxiTXbb17GVvu0ZOf7LPY6u3Bc8=.sha256",
"value": {
"previous": "%SM2J1D8RcWzd2/P6I3ATyN4VXHuQEP3sXy7YCtK0djE=.sha256",
"sequence": 18,
"author": "@t/giTDc0EtzdPQGC7iAAzgzVOkFo++XZRvzOOlqgX1c=.ed25519",
"timestamp": 1585194822251,
"hash": "sha256",
"content": {
"type": "post",
"text": "#astroport-swarmkey = [ipfs_swarm.key.crypt](&L9nCidDjJ4c4Jz1LNTtx8Xp0SJW9HPCD9IVsbbAhS/I=.sha256) TO.SSB_${author} TO.G1_${g1author} +++ FROM.SSB_${self_name} FROM.G1_${g1self} FROM.IPFS_${ipfsnodeid}",
"mentions": [
{
"link": "&L9nCidDjJ4c4Jz1LNTtx8Xp0SJW9HPCD9IVsbbAhS/I=.sha256",
"name": "ipfs_swarm.key.crypt",
"size": 144,
"type": "application/octet-stream"
}
]
},
"signature": "DHMTIS17yF450CqFssuP2iwYMMdOd3PzCDTkLYdjprTtvjWZYUEG9vHaXBrGuaFZRhV2gGZ3WSknM7YLevilAQ==.sig.ed25519"
},
"timestamp": 1585194822367
}
'
self=$(sbotc whoami | jq -r .id) || exit 1
g1self=$(echo $self | cut -d '@' -f 2 | cut -d '.' -f 1 | base64 -d | base58)
self_name=$(sbotc query.read '{"query":[{"$filter":{"value":{"author": "'"$self"'", "content":{"type":"about", "about": "'"$self"'"}}}}]}' | jq -r .value?.content?.name | grep -v null | tail -n 1)
ipfsnodeid=$(ipfs id -f='<id>\n')
[[ -f ~/.ipfs/ipfs_swarm.key ]] && echo "SWARM KEY OK" && exit 1
# SEARCH "#astroport-swarmkey" CMD in message text
# Not working without patchwork (TODO: find bug. installation ok!? activate? ssb-server ssb-backlinks node_modules. HELP !! )
# messages=$(sbotc backlinks.read '{"query":[{"$filter":{"dest":"#astroport-swarmkey","value":{"content":{"type":"post"}}}}]}')
# The backlinks.read command does not publish a message, it queries the database for messages. It comes from the ssb-backlinks plugin. This plugin does not come with ssb-server by default (but it does come with Patchwork) so if you are using plain ssb-server and want to use ssb-backlinks you have to install it additionally. But to publish a message you would use the publish method (or private.publish to publish a private message, and that requires the ssb-private plugin, which is included in Patchwork but must be installed separately for ssb-server).
# SCRIPT RUN BY cron_MINUTE.sh check last hour messages
current_ts=$(date -u +%s%N | cut -b1-13)
last_ts=$((current_ts - 3600000)) # timestamp from 1h ago
#messages=$(sbotc messagesByType '{"type":"post","gt":'$last_ts'}')
echo '
_ _
-|-|- _. __|_.__ ._ _ .__|___/ |\/|| \
-|-|-(_|_> |_|(_)|_)(_)| |_ \_| ||_/
|
sbotc messagesByType "post"
'
messages=$(sbotc messagesByType '{"type":"post"}')
while read -r msg
do
attached_file=$(printf %s "$msg" | jq -r .value.content.mentions.link 2>/dev/null)
if [[ $attached_file == "ipfs_swarm.key.crypt" ]]; then
echo '
__ _ _
(_\ //\ |_)|\/| |/|_\_/ _._ .__|_
__)\/\//--\| \| | |\|_ | de(_|\/|_)|_
/ |
to ~/.ipfs/ipfs_swarm.key
'
mytmp=$(mktemp -d "${TMPDIR:-/tmp}/ssb-npm.XXXXXXXXX")
curl -s "http://localhost:8989/blobs/get/$attached_file" > $mytmp/ipfs_swarm.key.crypt
$MY_PATH/tools/natools.py decrypt -k ~/.ssb/secret.dunikey -i $mytmp/ipfs_swarm.key.crypt -o ~/.ipfs/ipfs_swarm.key
echo '
___ _ _ __
| |_)|_(_ _| _. _ ._ _ _ ._
_|_| | __) (_|(_|(/_| | |(_)| | ... restart ...
'
# TODO!!! Add user in sudo without password like "pi" = Run astroport under pi user IDEA.
sudo systemctl restart ipfs
fi
done < <(printf '%s\n' "$messages")