From 465d5db06e56dc21072a8ff01258f6815d340b00 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Wed, 27 Mar 2024 09:34:01 +0200 Subject: new lib _ak_ipfs --- bin/ak-articles | 7 +- bin/ak-categories | 7 +- bin/ak-comments | 7 +- bin/ak-config | 7 +- bin/ak-data-expand | 21 ++-- bin/ak-enter | 13 +- bin/ak-files | 5 +- bin/ak-folders | 3 +- bin/ak-follow | 3 +- bin/ak-get-zlatest | 3 +- bin/ak-ipfs-daemon | 7 ++ bin/ak-ipfs-starter | 7 +- bin/ak-mempool | 9 +- bin/ak-mixtapes | 5 +- bin/ak-news | 29 ++--- bin/ak-node-info | 5 +- bin/ak-profile | 9 +- bin/ak-reference | 7 +- bin/ak-repositories | 24 ++-- bin/ak-sm-files | 5 +- bin/ak-todos | 7 +- bin/ak-transactions | 5 +- bin/ak-unfollow | 6 +- bin/ak-zblock-announce | 14 +++ bin/ak-zblock-gen-html | 13 +- bin/ak-zblock-manipulator | 29 ++--- bin/ak-zblock-pack | 26 ++-- bin/ak-zchain-calculate-size | 3 +- bin/ak-zchain-chk | 7 +- bin/ak-zchain-rebase | 11 +- bin/ak-zchain-reset | 12 +- lib/_ak_ipfs | 283 +++++++++++++++++++++++++++++++++++++++++++ 32 files changed, 467 insertions(+), 132 deletions(-) create mode 100755 bin/ak-ipfs-daemon create mode 100755 bin/ak-zblock-announce create mode 100755 lib/_ak_ipfs diff --git a/bin/ak-articles b/bin/ak-articles index 9a9f462..ba45309 100755 --- a/bin/ak-articles +++ b/bin/ak-articles @@ -5,6 +5,7 @@ descriptionString="Articles module for Arching Kaos" source $AK_LIBDIR/_ak_logit source $AK_LIBDIR/_ak_script_description +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg ZARTICLESDIR="$AK_WORKDIR/articles" @@ -35,7 +36,7 @@ _ak_modules_articles_create(){ logit "INFO" "Renaming..." TITLE="$(head -n 1 $ARTICLES_FILE)" TO_FILE=$ARTICLES_FILE-$(echo $TITLE | tr '[:upper:]' '[:lower:]' | sed -e 's/ /\_/g' ) - IPFS_FILE=$(ak-ipfs-add $ARTICLES_FILE) + IPFS_FILE=$(_ak_ipfs_add $ARTICLES_FILE) mv $ARTICLES_FILE $ZARTICLESDIR/$TO_FILE sed -e 's,Qm.*,'"$IPFS_FILE"',g' $ZARTICLESDIR/README _ak_modules_articles_add $ZARTICLESDIR/$TO_FILE @@ -90,10 +91,10 @@ _ak_modules_articles_add(){ echo "Adding articles from " $FILE DATETIME=$(echo $FILE | cut -d - -f 1 | awk '{print $1}') TITLE=$(head -n 1 $FILE) - FILE_IPFS_HASH=$(ak-ipfs-add $FILE) + FILE_IPFS_HASH=$(_ak_ipfs_add $FILE) FILE_SIGN_FILE=$FILE".asc" _ak_gpg_sign_detached $FILE_SIGN_FILE $FILE - FILE_SIGNATURE=$(ak-ipfs-add $FILE_SIGN_FILE) + FILE_SIGNATURE=$(_ak_ipfs_add $FILE_SIGN_FILE) cat > data < data < data else echo "File $FILE doesn't exist"; diff --git a/bin/ak-config b/bin/ak-config index 8e2df7b..1fe4d8d 100755 --- a/bin/ak-config +++ b/bin/ak-config @@ -3,6 +3,7 @@ PROGRAM="$(basename $0)" descriptionString="Arching Kaos Configuration Tool" source $AK_LIBDIR/_ak_script_description +source $AK_LIBDIR/_ak_ipfs _ak_config_title(){ description @@ -25,7 +26,7 @@ _ak_config_show(){ # We will be using our public key also to put it in the block later KEY="tmp-gpg.pub" gpg2 --homedir $AK_GPGHOME --armour --output $KEY --export $AK_FINGERPRINT - GPG_PUB_KEY=$(ak-ipfs-add $KEY) + GPG_PUB_KEY=$(_ak_ipfs_add $KEY) rm $KEY echo ' @@ -45,7 +46,7 @@ _ak_config_publish(){ then ipfs key gen ak-config fi - ak-ipfs-name-publish --key=ak-config /ipfs/$(ak-ipfs-add tmpfile) + _ak_ipfs_name_publish --key=ak-config /ipfs/$(_ak_ipfs_add tmpfile) if [ "$?" != 0 ] then echo -e "\033[0;34mError on publishing\033[0;0m\nYour information:\n" @@ -56,7 +57,7 @@ _ak_config_publish(){ } _ak_config_published(){ - ak-ipfs-cat $(ak-node-info ipfs) | jq -c + _ak_ipfs_cat $(ak-node-info ipfs) | jq -c } if [ ! -z $1 ]; then diff --git a/bin/ak-data-expand b/bin/ak-data-expand index 0a82e63..e5e699f 100755 --- a/bin/ak-data-expand +++ b/bin/ak-data-expand @@ -1,6 +1,7 @@ #!/bin/bash PROGRAM="$(basename $0)" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg if [ ! -d $AK_WORKDIR/ipfs ] @@ -16,37 +17,37 @@ then logit "ERROR" "Invalid hash format for $1" exit 1 fi - ak-ipfs-cat $1 > /dev/null + _ak_ipfs_cat $1 > /dev/null if [ "$?" -ne 0 ] then logit "ERROR" "Error while reading $1" exit 1 fi - ak-ipfs-cat $1 | jq -M > /dev/null + _ak_ipfs_cat $1 | jq -M > /dev/null if [ "$?" -ne 0 ] then logit "ERROR" "Error while parsing JSON for $1" exit 1 fi - ak-ipfs-cat $1 | jq | grep ipfs > /dev/null + _ak_ipfs_cat $1 | jq | grep ipfs > /dev/null if [ "$?" -ne 0 ] then logit "ERROR" "Error while extracting data from JSON for $1" exit 1 fi - DATA="$(ak-ipfs-cat $1 | jq | grep ipfs | sed -e 's/"ipfs": "//g; s/[",]//g; s/ //g')" + DATA="$(_ak_ipfs_cat $1 | jq | grep ipfs | sed -e 's/"ipfs": "//g; s/[",]//g; s/ //g')" if [ "$?" -ne 0 ] then logit "ERROR" "Error while extracting data from JSON for $1" exit 1 fi - ak-ipfs-cat $1 | jq | grep detach > /dev/null + _ak_ipfs_cat $1 | jq | grep detach > /dev/null if [ "$?" -ne 0 ] then logit "ERROR" "Error while extracting data from JSON for $1" exit 1 fi - DETACH="$(ak-ipfs-cat $1 | jq | grep detach | sed -e 's/"detach": "//g; s/[",]//g; s/ //g')" + DETACH="$(_ak_ipfs_cat $1 | jq | grep detach | sed -e 's/"detach": "//g; s/[",]//g; s/ //g')" if [ "$?" -ne 0 ] then logit "ERROR" "Error while extracting data from JSON for $1" @@ -59,7 +60,7 @@ then exit 1 fi gpg="$2" - ak-ipfs-get $gpg > /dev/null 2>&1 + _ak_ipfs_get $gpg > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not get GPG key: $gpg" @@ -71,7 +72,7 @@ then logit "ERROR" "Could not import GPG key: $gpg" exit 1 fi - ak-ipfs-get $DETACH > /dev/null 2>&1 + _ak_ipfs_get $DETACH > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Error while getting signature: $DETACH for data: $DATA" @@ -79,7 +80,7 @@ then fi mv $DETACH $DATA.asc logit "INFO" "Block signature downloaded" - ak-ipfs-get $DATA > /dev/null 2>&1 + _ak_ipfs_get $DATA > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Error while getting data: $DATA" @@ -94,7 +95,7 @@ then fi mv $DATA $AK_WORKDIR/ipfs logit "INFO" "Block signature verified" - echo -n '"data":"'$1'","'$1'":'$(ak-ipfs-cat $1|jq -M -c)',' + echo -n '"data":"'$1'","'$1'":'$(_ak_ipfs_cat $1|jq -M -c)',' exit 0 else echo "Usage: $PROGRAM HASH GPG" diff --git a/bin/ak-enter b/bin/ak-enter index 01a93f9..9d9efc5 100755 --- a/bin/ak-enter +++ b/bin/ak-enter @@ -21,6 +21,7 @@ PROGRAM="$(basename $0)" descriptionString="Crawl an arching kaos chain" source $AK_LIBDIR/_ak_logit source $AK_LIBDIR/_ak_script_description +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg _ak_enter_title(){ @@ -143,7 +144,7 @@ do isIPFSv0 "$zblock" # We check if any $zblock at all - ak-ipfs-cat $zblock | jq -c -M > $AK_ZBLOCKDIR/$zblock + _ak_ipfs_cat $zblock | jq -c -M > $AK_ZBLOCKDIR/$zblock if [ "$?" -ne 0 ] then logit "ERROR" "ZBLOCK $zblock READ failed" @@ -188,7 +189,7 @@ do # Same as above applies to BLOCK and DATA subparts of each ZBLOCK # BLOCKS echo -n '"block":"'$block'",' - ak-ipfs-cat $block | jq -c -M > $AK_BLOCKDIR/$block + _ak_ipfs_cat $block | jq -c -M > $AK_BLOCKDIR/$block cat $AK_BLOCKDIR/$block | jq -M > /dev/null 2>&1 if [ "$?" -ne 0 ] then @@ -231,7 +232,7 @@ do if [ $verify == 1 ] then - ak-ipfs-get $gpg > /dev/null 2>&1 + _ak_ipfs_get $gpg > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not get GPG key: $gpg ." @@ -243,7 +244,7 @@ do logit "ERROR" "Could not import GPG key: $gpg ." exit 1 fi - ak-ipfs-get $block_signature > /dev/null 2>&1 + _ak_ipfs_get $block_signature > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Error while getting $block_signature for $block" @@ -252,7 +253,7 @@ do mv $block_signature $block.asc logit "INFO" "Block signature downloaded" - ak-ipfs-get $block > /dev/null 2>&1 + _ak_ipfs_get $block > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not get $block block" @@ -280,7 +281,7 @@ do # DATA # Only print to stdout - # ak-ipfs-cat $data + # _ak_ipfs_cat $data touch $AK_DATADIR/$data # Now, since we sourced the BLOCK to our terminal, we can search diff --git a/bin/ak-files b/bin/ak-files index c4d6f2c..7cdedcd 100755 --- a/bin/ak-files +++ b/bin/ak-files @@ -4,6 +4,7 @@ ZFILESDIR="$AK_WORKDIR/files" TEMP="/tmp/aktmp" PROGRAM="$(basename $0)" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg if [ ! -d $ZFILESDIR ]; then @@ -55,7 +56,7 @@ main(){ FILE="$TEMPASSIN/$1" logit "INFO" "Adding $FILE to IPFS..." - FILE_IPFS_HASH=$(ak-ipfs-add $FILE) + FILE_IPFS_HASH=$(_ak_ipfs_add $FILE) if [ $? == 0 ]; then logit "INFO" "Added $FILE to IPFS" else @@ -71,7 +72,7 @@ main(){ fi logit "INFO" "Adding signature to IPFS" - SIGNATURE=$(ak-ipfs-add $TEMPASSIN/$SIGN_FILE) + SIGNATURE=$(_ak_ipfs_add $TEMPASSIN/$SIGN_FILE) if [ $? == 0 ]; then logit "INFO" "Added" else diff --git a/bin/ak-folders b/bin/ak-folders index b5f2601..2651571 100755 --- a/bin/ak-folders +++ b/bin/ak-folders @@ -5,6 +5,7 @@ ZFOLDERSDIR="$AK_WORKDIR/folders" TEMP="/tmp/aktmp" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs if [ ! -d $ZFOLDERSDIR ]; then mkdir $ZFOLDERSDIR @@ -45,7 +46,7 @@ _ak_modules_folders_main(){ FOLDER="$1" logit "INFO" "Adding $FOLDER to IPFS..." - FOLDER_IPFS_HASH=$(ak-ipfs-add $FOLDER) + FOLDER_IPFS_HASH=$(_ak_ipfs_add $FOLDER) if [ $? == 0 ]; then logit "INFO" "done" else diff --git a/bin/ak-follow b/bin/ak-follow index e0e7336..dea706f 100755 --- a/bin/ak-follow +++ b/bin/ak-follow @@ -2,6 +2,7 @@ FOLLOWING="$HOME/.arching-kaos/following" PROGRAM="$(basename $0)" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs if [ ! -z $1 ] then @@ -12,7 +13,7 @@ then exit 1 fi echo $1 >> $FOLLOWING - IPFS=$(ak-ipfs-add $FOLLOWING) + IPFS=$(_ak_ipfs_add $FOLLOWING) if [ $? != 0 ] then logit "ERROR" "Addition failed" diff --git a/bin/ak-get-zlatest b/bin/ak-get-zlatest index 36de493..64085fd 100755 --- a/bin/ak-get-zlatest +++ b/bin/ak-get-zlatest @@ -1,2 +1,3 @@ #!/bin/bash -ak-ipfs-files-stat /zlatest | head -n 1| tr -d '\n' +source $AK_LIBDIR/_ak_ipfs +_ak_ipfs_files_stat /zlatest | head -n 1| tr -d '\n' diff --git a/bin/ak-ipfs-daemon b/bin/ak-ipfs-daemon new file mode 100755 index 0000000..ad0565d --- /dev/null +++ b/bin/ak-ipfs-daemon @@ -0,0 +1,7 @@ +#!/bin/bash + +PROGRAM="$(basename $0)" + + +source $AK_LIBDIR/_ak_ipfs +_ak_ipfs daemon diff --git a/bin/ak-ipfs-starter b/bin/ak-ipfs-starter index b067259..3685736 100755 --- a/bin/ak-ipfs-starter +++ b/bin/ak-ipfs-starter @@ -1,8 +1,11 @@ #!/bin/bash PROGRAM="$(basename $0)" -screen -dmS ipfs-daemon ipfs daemon --routing=dht --migrate + +source $AK_LIBDIR/_ak_logit + +screen -dmS akipfsd ak-ipfs-daemon if [ "$?" != "0" ] then - ak-logthis "$PROGRAM" "ERROR" "Failed to start IPFS daemon" + logit "ERROR" "Failed to start IPFS daemon" exit 1 fi diff --git a/bin/ak-mempool b/bin/ak-mempool index 1baf4c8..51f59a7 100755 --- a/bin/ak-mempool +++ b/bin/ak-mempool @@ -5,13 +5,16 @@ # They also are a part of our swarm. # So should we ask for our peers? -ak-ipfs-swarm-peers > tmp_peers +source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs + +_ak_ipfs_swarm_peers > tmp_peers while IFS="" read -r p || [ -n "$p" ] do peer="$(echo "$p" | sed -e 's/^.*\///')" - echo "Peer $peer found! Examining..." - ak-ipfs-cat /ipns/$peer/zlatest + logit "INFO" "Peer $peer found! Examining..." + _ak_ipfs_cat /ipns/$peer/zlatest done < tmp_peers rm tmp_peers diff --git a/bin/ak-mixtapes b/bin/ak-mixtapes index 242211d..6b70bf5 100755 --- a/bin/ak-mixtapes +++ b/bin/ak-mixtapes @@ -5,6 +5,7 @@ PROGRAM="$(basename $0)" descriptionString="AK mixtape block creator" source $AK_LIBDIR/_ak_logit source $AK_LIBDIR/_ak_script_description +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg _ak_modules_mixtapes_usage(){ @@ -33,12 +34,12 @@ _ak_modules_mixtapes_specs(){ _ak_modules_mixtapes_main(){ echo $MIXTAPE_FILE "by" $MIXTAPE_ARTIST "named as" $MIXTAPE_TITLE - MIXTAPE_IPFS_HASH=$(ak-ipfs-add $MIXTAPE_FILE) + MIXTAPE_IPFS_HASH=$(_ak_ipfs_add $MIXTAPE_FILE) MIXTAPE_SIGN_FILE=$MIXTAPE_FILE".asc" _ak_gpg_sign_detached $MIXTAPE_SIGN_FILE $MIXTAPE_FILE - MIXTAPE_SIGNATURE=$(ak-ipfs-add $MIXTAPE_SIGN_FILE) + MIXTAPE_SIGNATURE=$(_ak_ipfs_add $MIXTAPE_SIGN_FILE) cat > data < data < data < data < data < data ak-zblock-pack "repos/add" $PWD/data echo "Done" @@ -116,11 +116,11 @@ add(){ } index(){ - ak-ipfs-key-list-full | grep -e '\.git' + _ak_ipfs_key_list_full | grep -e '\.git' } set-as-profile(){ - IPFS=$(ak-ipfs-add $REPOSTORE) + IPFS=$(_ak_ipfs_add $REPOSTORE) if [ $? == 0 ] then profile set repositories $IPFS diff --git a/bin/ak-sm-files b/bin/ak-sm-files index 9548b3b..56e80d1 100755 --- a/bin/ak-sm-files +++ b/bin/ak-sm-files @@ -6,6 +6,7 @@ CRD=$(cat .pwd) PROGRAM="$(basename $0)" #set -xe source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg if [ ! -d $ZFILESDIR ]; then @@ -61,7 +62,7 @@ main(){ fi logit "INFO" "Adding $FILENAME to IPFS..." - FILE_IPFS_HASH=$(ak-ipfs-add $FILENAME) + FILE_IPFS_HASH=$(_ak_ipfs_add $FILENAME) if [ $? == 0 ]; then logit "INFO" "Added $FILENAME to IPFS" else @@ -86,7 +87,7 @@ main(){ fi logit "INFO" "Adding signature to IPFS" - SIGNATURE=$(ak-ipfs-add $SIGN_FILE) + SIGNATURE=$(_ak_ipfs_add $SIGN_FILE) if [ $? == 0 ]; then logit "INFO" "Added" else diff --git a/bin/ak-todos b/bin/ak-todos index b5ce838..aa3b95f 100755 --- a/bin/ak-todos +++ b/bin/ak-todos @@ -1,6 +1,7 @@ #!/bin/bash PROGRAM="$(basename $0)" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg ZTODOSDIR="$AK_WORKDIR/todos" @@ -26,7 +27,7 @@ _ak_modules_todos_create(){ logit "INFO" "Renaming..." TITLE="$(head -n 1 $TODOS_FILE)" TO_FILE=$TODOS_FILE-$(echo $TITLE | tr '[:upper:]' '[:lower:]' | sed -e 's/ /\_/g' ) - IPFS_FILE=$(ak-ipfs-add $TODOS_FILE) + IPFS_FILE=$(_ak_ipfs_add $TODOS_FILE) mv $TODOS_FILE $ZTODOSDIR/$TO_FILE sed -e 's,Qm.*,'"$IPFS_FILE"',g' $ZTODOSDIR/README _ak_modules_todos_add $ZTODOSDIR/$TO_FILE @@ -82,10 +83,10 @@ _ak_modules_todos_add(){ logit "INFO" "Adding todos from $FILE" DATETIME=$(echo $FILE | cut -d - -f 1 | awk '{print $1}') TITLE=$(head -n 1 $FILE) - FILE_IPFS_HASH=$(ak-ipfs-add $FILE) + FILE_IPFS_HASH=$(_ak_ipfs_add $FILE) FILE_SIGN_FILE=$FILE".asc" _ak_gpg_sign_detached $FILE_SIGN_FILE $FILE - FILE_SIGNATURE=$(ak-ipfs-add $FILE_SIGN_FILE) + FILE_SIGNATURE=$(_ak_ipfs_add $FILE_SIGN_FILE) cat > data < data </dev/null | jq -M -c > /dev/null +if [ "$?" == "0" ] +then + logit "INFO" "zblock $zblock announced" +else + logit "ERROR" "zblock $zblock announcement failed" +fi + diff --git a/bin/ak-zblock-gen-html b/bin/ak-zblock-gen-html index 0ad01ad..289823b 100755 --- a/bin/ak-zblock-gen-html +++ b/bin/ak-zblock-gen-html @@ -1,5 +1,6 @@ #!/bin/bash PROGRAM="$(basename $0)" +source $AK_LIBDIR/_ak_ipfs time_started=$(date -u +%s.%N) ss=$(echo $time_started | cut -d '.' -f 1) @@ -101,7 +102,7 @@ then echo " " echo "
" echo " $zblock" - echo "
$(ak-ipfs-cat $zblock | jq)
" + echo "
$(_ak_ipfs_cat $zblock | jq)
" echo "
" echo " " echo " " @@ -112,7 +113,7 @@ then echo " " echo "
" echo " $block" - echo "
$(ak-ipfs-cat $block | jq)
" + echo "
$(_ak_ipfs_cat $block | jq)
" echo "
" echo " " echo " " @@ -123,7 +124,7 @@ then echo " " echo "
" echo " $block_signature" - echo "
$(ak-ipfs-cat $block_signature)
" + echo "
$(_ak_ipfs_cat $block_signature)
" echo "
" echo " " echo " " @@ -158,7 +159,7 @@ then echo " " echo "
" echo " $gpg" - echo "
$(ak-ipfs-cat $gpg)
" + echo "
$(_ak_ipfs_cat $gpg)
" echo "
" echo " " echo " " @@ -188,7 +189,7 @@ then echo " " echo "
" echo " $detach" - echo "
$(ak-ipfs-cat $detach)
" + echo "
$(_ak_ipfs_cat $detach)
" echo "
" echo " " echo " " @@ -207,7 +208,7 @@ then echo " " echo "
" echo " $(echo $datablock | jq -r '.detach')" - echo "
$(ak-ipfs-cat $(echo $datablock | jq -r '.detach'))
" + echo "
$(_ak_ipfs_cat $(echo $datablock | jq -r '.detach'))
" echo "
" echo " " echo " " diff --git a/bin/ak-zblock-manipulator b/bin/ak-zblock-manipulator index cf67a6e..427ba09 100755 --- a/bin/ak-zblock-manipulator +++ b/bin/ak-zblock-manipulator @@ -17,6 +17,7 @@ # Below, the usage information PROGRAM="$(basename $0)" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_gpg usage(){ @@ -47,22 +48,22 @@ main(){ logit "INFO" "We are doing" $BLOCK_TO_ADD "with content" $PREVIOUS # We add it to IPFS - MESSAGE_HASH=$(ak-ipfs-add $MESSAGE) + MESSAGE_HASH=$(_ak_ipfs_add $MESSAGE) # We create a detached and armor signature of it MESSAGE_SIGN_FILE=$MESSAGE".asc" _ak_gpg_sign_detached $MESSAGE_SIGN_FILE $MESSAGE # We add the signature to IPFS - MESSAGE_SIGNATURE=$(ak-ipfs-add $MESSAGE_SIGN_FILE) + MESSAGE_SIGNATURE=$(_ak_ipfs_add $MESSAGE_SIGN_FILE) # We will be using our public key also to put it in the block later KEY="gpg.pub" _ak_gpg_key_self_export $KEY - GPG_PUB_KEY=$(ak-ipfs-add $KEY) + GPG_PUB_KEY=$(_ak_ipfs_add $KEY) # Acquire last block of information, to chain this one with previous posted - PREVIOUS=$(ak-ipfs-files-stat /zlatest | head -n 1) + PREVIOUS=$(_ak_ipfs_files_stat /zlatest | head -n 1) # We create a block of json like this: cat > block < zblock << EOF @@ -97,7 +98,7 @@ makeZBlock(){ EOF ZBL="zblock" # and we add it on IPFS - ZBLOCK=$(ak-ipfs-add $ZBL) + ZBLOCK=$(_ak_ipfs_add $ZBL) echo $ZBLOCK } @@ -110,11 +111,11 @@ then # We ask for a ZBLOCK or GENESIS to put in the BLOCK PREVIOUS="$2" - ak-ipfs-cat "$BLOCK_TO_ADD" + _ak_ipfs_cat "$BLOCK_TO_ADD" if [ "$?" == 0 ]; then echo "Nice! We found the block" - ak-ipfs-get "$BLOCK_TO_ADD" + _ak_ipfs_get "$BLOCK_TO_ADD" sed -i.bak -e 's/^.*previous.*$/\"previous\":\"'$PREVIOUS'\"/' $BLOCK_TO_ADD cat $BLOCK_TO_ADD | jq -M @@ -130,11 +131,11 @@ then # python send_as_ak_tx $ZBLOCK # or for "offline" use echo $ZBLOCK > $ZLATEST - ak-ipfs-name-publish --key=zchain $ZBLOCK > /dev/null 2>&1 - ak-ipfs-files-mkdir /zarchive > /dev/null 2>&1 - ak-ipfs-files-cp /zlatest /zarchive/$(date -u +%s)-$(ak-ipfs-files-stat /zlatest | head -n 1) > /dev/null 2>&1 - ak-ipfs-files-rm /zlatest > /dev/null 2>&1 - ak-ipfs-files-cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 + _ak_ipfs_name_publish --key=zchain $ZBLOCK > /dev/null 2>&1 + _ak_ipfs_files_mkdir /zarchive > /dev/null 2>&1 + _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) > /dev/null 2>&1 + _ak_ipfs_files_rm /zlatest > /dev/null 2>&1 + _ak_ipfs_files_cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 else usage exit 0 diff --git a/bin/ak-zblock-pack b/bin/ak-zblock-pack index 4f635bf..f5b6505 100755 --- a/bin/ak-zblock-pack +++ b/bin/ak-zblock-pack @@ -54,22 +54,22 @@ main(){ logit "INFO" "We are doing $ACTION with content $MESSAGE" # We add it to IPFS - MESSAGE_HASH=$(ak-ipfs-add $MESSAGE) + MESSAGE_HASH=$(_ak_ipfs_add $MESSAGE) # We create a detached and armor signature of it MESSAGE_SIGN_FILE=$MESSAGE".asc" _ak_gpg_sign_detached $MESSAGE_SIGN_FILE $MESSAGE # We add the signature to IPFS - MESSAGE_SIGNATURE=$(ak-ipfs-add $MESSAGE_SIGN_FILE) + MESSAGE_SIGNATURE=$(_ak_ipfs_add $MESSAGE_SIGN_FILE) # We will be using our public key also to put it in the block later KEY="gpg.pub" _ak_gpg_key_self_export $KEY - GPG_PUB_KEY=$(ak-ipfs-add $KEY) + GPG_PUB_KEY=$(_ak_ipfs_add $KEY) # Acquire last block of information, to chain this one with previous posted - PREVIOUS=$(ak-ipfs-files-stat /zlatest | head -n 1) + PREVIOUS=$(_ak_ipfs_files_stat /zlatest | head -n 1) # We create a block of json like this: printf '{"timestamp":"%s","action":"%s","data":"%s","detach":"%s","gpg":"%s","previous":"%s"}' $(date -u +%s) $ACTION $MESSAGE_HASH $MESSAGE_SIGNATURE $GPG_PUB_KEY $PREVIOUS > block @@ -79,16 +79,16 @@ main(){ _ak_gpg_sign_detached $BLOCK_SIG $BLOCK # We now add the signature to IPFS - BLOCK_SIGNATURE=$(ak-ipfs-add $BLOCK_SIG) + BLOCK_SIGNATURE=$(_ak_ipfs_add $BLOCK_SIG) # We also add the block! - BLOCK=$(ak-ipfs-add $BLOCK) + BLOCK=$(_ak_ipfs_add $BLOCK) # So we now do the think almost again printf '{"block":"%s","block_signature":"%s"}' $BLOCK $BLOCK_SIGNATURE > zblock ZBL="zblock" # and we add it on IPFS - ZBLOCK=$(ak-ipfs-add $ZBL) + ZBLOCK=$(_ak_ipfs_add $ZBL) echo $ZBLOCK } @@ -108,36 +108,36 @@ then # python send_as_ak_tx $ZBLOCK # or for "offline" use echo $ZBLOCK > $AK_ZLATEST - ak-ipfs-name-publish --key=zchain $ZBLOCK > /dev/null 2>&1 + _ak_ipfs_name_publish --key=zchain $ZBLOCK > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Failed publishing ZBLOCK: $ZBLOCK" exit 1 fi - ak-ipfs-files-ls /zarchive > /dev/null 2>&1 + _ak_ipfs_files_ls /zarchive > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "WARNING" "/zarchive does not exist" - ak-ipfs-files-mkdir /zarchive > /dev/null 2>&1 + _ak_ipfs_files_mkdir /zarchive > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not create /zarchive directory. Aborting." exit 1 fi fi - ak-ipfs-files-cp /zlatest /zarchive/$(date -u +%s)-$(ak-ipfs-files-stat /zlatest | head -n 1) > /dev/null 2>&1 + _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not back up previous /zlatest" exit 1 fi - ak-ipfs-files-rm /zlatest > /dev/null 2>&1 + _ak_ipfs_files_rm /zlatest > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not remove previous /zlatest" exit 1 fi - ak-ipfs-files-cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 + _ak_ipfs_files_cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 if [ "$?" -ne 0 ] then logit "ERROR" "Could not copy $ZBLOCK to /zlatest" diff --git a/bin/ak-zchain-calculate-size b/bin/ak-zchain-calculate-size index fe99187..51c4202 100755 --- a/bin/ak-zchain-calculate-size +++ b/bin/ak-zchain-calculate-size @@ -1,5 +1,6 @@ #!/bin/bash PROGRAM="$(basename $0)" +source $AK_LIBDIR/_ak_ipfs temp="$(mktemp -d)" @@ -16,7 +17,7 @@ sum=0 ; while IFS="" read -r p || [ -n "$p" ] do if [ "$p" != "" ] then - ak-ipfs-get $p + _ak_ipfs_get $p num="$(du -bs --apparent-size $p | cut -d $'\t' -f 1)" else num=0 diff --git a/bin/ak-zchain-chk b/bin/ak-zchain-chk index 02d8252..4e509ac 100755 --- a/bin/ak-zchain-chk +++ b/bin/ak-zchain-chk @@ -1,6 +1,7 @@ #!/bin/bash PROGRAM="$(basename $0)" source $AK_LIBDIR/_ak_logit +source $AK_LIBDIR/_ak_ipfs fix="0" usage(){ @@ -78,7 +79,7 @@ do # We concatenate the zblock's contents, pipe them through filter # ak-json2bash and output them to tmp-file - ak-ipfs-cat $zblock | ak-json2bash > tmp-zblock + _ak_ipfs_cat $zblock | ak-json2bash > tmp-zblock logit "INFO" "ZBLOCK $zblock READ" # Supposingly you are on a safe environment and you only have @@ -90,7 +91,7 @@ do # Same as above applies to BLOCK and DATA subparts of each ZBLOCK # BLOCKS - ak-ipfs-cat $block | ak-json2bash > tmp-block + _ak_ipfs_cat $block | ak-json2bash > tmp-block source tmp-block logit "INFO" "BLOCK $block SOURCED" touch $AK_BLOCKDIR/$block @@ -121,7 +122,7 @@ do for value in ${blocks_found[@]} do echo $value - ak-ipfs-cat $value | jq -M + _ak_ipfs_cat $value | jq -M done fi exit 0 diff --git a/bin/ak-zchain-rebase b/bin/ak-zchain-rebase index a798fbe..f0687eb 100755 --- a/bin/ak-zchain-rebase +++ b/bin/ak-zchain-rebase @@ -1,5 +1,6 @@ #!/bin/bash PROGRAM=$(basename $0) +source $AK_LIBDIR/_ak_ipfs usage(){ echo "$PROGRAM - Zchain rebase" echo "" @@ -21,24 +22,24 @@ rebase (){ if [ $? != 0 ]; then exit 1; fi echo "Make sure /zarchive folder exists within IPFS FS" - ak-ipfs-files-mkdir /zarchive + _ak_ipfs_files_mkdir /zarchive if [ $? != 0 ]; then echo "Folder already there"; fi echo "Archive the previous ZLATEST" - ak-ipfs-files-cp /zlatest /zarchive/$(date -u +%s)-$(ak-ipfs-files-stat /zlatest | head -n 1) + _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) if [ $? != 0 ]; then exit 1; fi echo "Removing previous /zlatest entry" - ak-ipfs-files-rm /zlatest + _ak_ipfs_files_rm /zlatest if [ $? != 0 ]; then exit 1; fi echo "Copying rebased ZLATEST" CZLATEST="$(cat $AK_ZLATEST)" - ak-ipfs-files-cp /ipfs/$CZLATEST /zlatest + _ak_ipfs_files_cp /ipfs/$CZLATEST /zlatest if [ $? != 0 ]; then exit 1; fi echo "Publishing new (rebased) ZLATEST" - ak-ipfs-name-publish --key=zchain /ipfs/$(cat $AK_ZLATEST) + _ak_ipfs_name_publish --key=zchain /ipfs/$(cat $AK_ZLATEST) if [ $? != 0 ]; then exit 1; fi ak-config publish diff --git a/bin/ak-zchain-reset b/bin/ak-zchain-reset index f19e1b0..b415c44 100755 --- a/bin/ak-zchain-reset +++ b/bin/ak-zchain-reset @@ -1,5 +1,7 @@ #!/bin/bash PROGRAM=$(basename $0) +source $AK_LIBDIR/_ak_ipfs + usage(){ echo "$PROGRAM - Zchain reset" echo "" @@ -19,24 +21,24 @@ reset (){ if [ $? != 0 ]; then exit 1; fi echo "Make sure /zarchive folder exists within IPFS FS" - ak-ipfs-files-mkdir /zarchive + _ak_ipfs_files_mkdir /zarchive if [ $? != 0 ]; then echo "Folder already there"; fi echo "Archive the previous ZLATEST" - ak-ipfs-files-cp /zlatest /zarchive/$(date -u +%s)-$(ak-ipfs-files-stat /zlatest | head -n 1) + _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) if [ $? != 0 ]; then exit 1; fi echo "Removing previous /zlatest entry" - ak-ipfs-files-rm /zlatest + _ak_ipfs_files_rm /zlatest if [ $? != 0 ]; then exit 1; fi echo "Copying reset ZLATEST" CZLATEST="$(cat $ZLATEST)" - ak-ipfs-files-cp /ipfs/$CZLATEST /zlatest + _ak_ipfs_files_cp /ipfs/$CZLATEST /zlatest if [ $? != 0 ]; then exit 1; fi echo "Publishing new (reset) ZLATEST" - ak-ipfs-name-publish --key=zchain /ipfs/$(cat $ZLATEST) + _ak_ipfs_name_publish --key=zchain /ipfs/$(cat $ZLATEST) if [ $? != 0 ]; then exit 1; fi ak-config publish diff --git a/lib/_ak_ipfs b/lib/_ak_ipfs new file mode 100755 index 0000000..4e7f846 --- /dev/null +++ b/lib/_ak_ipfs @@ -0,0 +1,283 @@ +#!/bin/bash +source $AK_LIBDIR/_ak_logit +IPFS_REPO="$AK_WORKDIR/ipfsrepo" + +_ak_ipfs(){ + export IPFS_PATH=$IPFS_REPO; ipfs $* +} + +_ak_ipfs_get_peers(){ + _ak_ipfs swarm peers 1> /dev/null 2>&1 + if [ $? == 0 ] + then + _ak_ipfs swarm peers > $AK_WORKDIR/peers.ipfs + fi +} + +_ak_ipfs_scanner(){ + peersIPFSfile="$AK_WORKDIR/peers.ipfs" + ak_peersIPFSfile="$AK_WORKDIR/ipfs.peers.akn" + if [ ! -f $peersIPFSfile ] + then + _ak_ipfs_get_peers + if [ $? -ne 0 ] + then + exit 1 + fi + fi + counter=0 + + printf '[' > walk.aknet + cat $peersIPFSfile \ + | cut -d '/' -f 2,3,7 \ + | sort \ + | uniq \ + | while read -r line || [ -n "$line" ] + do + protocol="`printf '%s' "$line" | cut -d '/' -f 1`" + ip="`printf '%s' "$line" | cut -d '/' -f 2`" + ipfsId="`printf '%s' "$line" | cut -d '/' -f 3`" + if [ "$protocol" == "ip6" ] + then + url="http://[$ip]:8610/v0/node_info" + else + url="http://$ip:8610/v0/node_info" + fi + node_info="`curl \ + -A 'akd/0.1.0; https://github.com/arching-kaos' \ + --connect-timeout 3 \ + "$url" 2>/dev/null`" + if [ $? -eq 0 ] + then + if [ "$counter" -ne "0" ] + then + printf ',' >> walk.aknet + fi + printf '{"ipfsPublicKey":"%s","ip":"%s","node_info":%s}' \ + "$ipfsId" "$ip" "$node_info" >> walk.aknet + counter="`expr $counter + 1`" + fi + done + printf ']' >> walk.aknet + + mv walk.aknet $ak_peersIPFSfile +} + +_ak_ipfs_add(){ + # Receives a file + # Returns a hash + _ak_ipfs add -Qr "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to add $1" + exit 1 + fi +} + +_ak_ipfs_block_stat(){ + _ak_ipfs block stat "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to retrieve stat of block $1" + exit 1 + fi +} + +_ak_ipfs_cat(){ + _ak_ipfs --timeout=10s cat $1 + if [ "$?" -ne "0" ] + then + logit "ERROR" "Failed to cat $1" + exit 1 + fi + +} + +_ak_ipfs_files_cp(){ + _ak_ipfs files cp "$1" "$2" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to copy $1 to $2" + exit 1 + fi +} + +_ak_ipfs_files_ls(){ + _ak_ipfs files ls "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to list $1" + exit 1 + fi +} + +_ak_ipfs_files_mkdir(){ + _ak_ipfs files mkdir "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to mkdir $1" + exit 1 + fi +} + +_ak_ipfs_files_mv(){ + _ak_ipfs files mv "$1" "$2" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to move $1 to $2" + exit 1 + fi +} + +_ak_ipfs_files_rm(){ + _ak_ipfs files rm "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to remove $1" + exit 1 + fi +} + +_ak_ipfs_files_stat(){ + _ak_ipfs files stat "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to get file's stats $1" + exit 1 + fi +} + +_ak_ipfs_get(){ + _ak_ipfs --timeout=10s get "$1" > /dev/null 2>&1 + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to get $1" + exit 1 + fi +} + +_ak_ipfs_key_gen(){ + _ak_ipfs key gen "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to generate key $1" + exit 1 + fi +} + +_ak_ipfs_key_list(){ + _ak_ipfs key list + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to get key list" + exit 1 + fi +} + +_ak_ipfs_key_list_full(){ + _ak_ipfs key list -l + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to get key list" + exit 1 + fi +} + +_ak_ipfs_name_publish(){ + _ak_ipfs name publish "$1" "$2" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to get $1" + exit 1 + fi + +} + +_ak_ipfs_name_resolve(){ + _ak_ipfs name resolve "$1" + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to resolve $1" + exit 1 + fi + +} + +_ak_ipfs_swarm_peers(){ + _ak_ipfs swarm peers + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to get list of peers" + exit 1 + fi +} + +_ak_ipfs_starter(){ + _ak_ipfs daemon --routing=dht --migrate + if [ $? -ne 0 ] + then + logit "ERROR" "Failed to start IPFS daemon" + exit 1 + fi +} + +_ak_ipns_resolve(){ + if [ ! -z $1 ] + then + rsld=$(ak-ipfs-name-resolve $1) + if [ "$?" != 0 ] + then + logit "ERROR" "Failed to resolve $1" + exit 1 + fi + echo -n $rsld | sed -e 's/\/ipfs\///' + logit "INFO" "Resolved $1 to $rsld" + else + exit 69 + fi +} + +_ak_ipfs_check(){ + + _ak_ipfs_files_ls /zarchive > /dev/null + if [ $? != 0 ] + then + logit "ERROR" "/zarchive is missing" + else + logit "INFO" "/zarchive OK" + fi + + _ak_ipfs_files_ls /zlatest > /dev/null + if [ $? != 0 ] + then + logit "ERROR" "/zlatest is missing" + else + logit "INFO" "/zlatest is OK" + fi + + _ak_ipfs_key_list | grep zchain > /dev/null + if [ $? != 0 ]; then + logit "WARNING" "zchain key is missing" + _ak_ipfs_key_gen zchain > $ZCHAIN + if [ $? != 0 ]; then + logit "ERROR" "zchain fails to create" + else + logit "INFO" "zchain created" + fi + else + logit "INFO" "zchain is there" + fi + + _ak_ipfs_key_list | grep ak-config > /dev/null + if [ $? != 0 ]; then + logit "WARNING" "ak-config key is missing" + _ak_ipfs_key_gen ak-config + if [ $? != 0 ]; then + logit "ERROR" "ak-config fails to create" + else + logit "INFO" "ak-config created" + fi + else + logit "INFO" "ak-config is there" + fi +} -- cgit v1.2.3