Last active
August 30, 2022 09:04
-
-
Save simbafs/be22b65d57bec3eb17b44c5d98d929d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author: simbafs | |
# date: 2022-08-30 | |
# MIT License | |
IPFS="${IPFS:-$(which ipfs)}" | |
KEY="${KEY:-q8s}" | |
# this line will be changed by the script | |
PKEY="${PKEY:-"k51qzi5uqu5dg9y6e55vjfb583rdoshtko6qizgk7nde9ybm9gyor5ujpfb9m2"}" | |
DIR="${DIR:-.}" | |
# cehck executable | |
if [[ -f "$IPFS" && !(-x "$IPFS") ]]; then | |
echo "IPFS is not installed" | |
exit 1 | |
fi | |
# check ipfs daemon | |
$IPFS swarm peers > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo "ipfs daemon is not running" | |
exit 1 | |
fi | |
case $1 in | |
server) | |
cd $DIR | |
CID=$($IPFS add -rwq . | tail -n2 | head -n1) # get the second last line | |
PK=$($IPFS name publish --key=$KEY $CID | cut -d' ' -f3 | sed -e 's/://') # get public key and remove ':' | |
if [[ $? -ne 0 ]]; then | |
echo "Failed to publish $CID" | |
exit 1 | |
fi | |
echo $PK | |
;; | |
client) | |
$IPFS get /ipns/$PKEY 2>&1 > /dev/null | |
if [[ $? -ne 0 ]]; then | |
echo "Failed to get $PKEY" | |
exit 1 | |
fi | |
;; | |
setPK) | |
if [[ -z $2 ]]; then | |
echo "Usage: $0 setPK <public key>" | |
exit 1 | |
fi | |
sed -ie "s/^PKEY=.*/PKEY=\"\${PKEY:-\"$2\"}\"/" $0 | |
chmod 755 $0 | |
rm $0e | |
;; | |
help|*) | |
echo "Usage: $0 {server|client|setPK}" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment