From c0dbfc8dc7cf7af278de1d233d87ffbe481ba704 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Thu, 4 Jul 2024 18:36:54 +0300 Subject: Refactoring + bug fixes - Recursive sourcing was introduced and fixed - Almost finished removing old style commands 'ak-zchain-chk' in favor of the new design (eg 'ak zchain --check') - A couple of changes on how we really do concatenation of an IPFS link - Interface function for publishing to 'ak-config' key - Interface function for publishing to IPNS keys --- lib/_ak_config | 15 +---- lib/_ak_ipfs | 74 +++++++++++++++------ lib/_ak_log | 23 ++++--- lib/_ak_zblock | 10 +-- lib/_ak_zchain | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 276 insertions(+), 52 deletions(-) (limited to 'lib') diff --git a/lib/_ak_config b/lib/_ak_config index c362178..0159453 100755 --- a/lib/_ak_config +++ b/lib/_ak_config @@ -1,13 +1,6 @@ #!/bin/bash - -source $AK_LIBDIR/_ak_gpg -source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_node -_ak_config_get_ipns_key(){ - _ak_ipfs_key_list_full | grep 'ak-config' | cut -d ' ' -f 1 -} - _ak_config_show(){ # We will be using our public key also to put it in the block later KEY="tmp-gpg.pub" @@ -28,12 +21,8 @@ _ak_config_show(){ _ak_config_publish(){ _ak_config_show | jq -c -M > tmpfile - _ak_ipfs key list | grep ak-config 2> /dev/null 1>&2 - if [ $? -ne 0 ] - then - _ak_ipfs_key_gen ak-config - fi - _ak_ipfs_name_publish --key=ak-config /ipfs/$(_ak_ipfs_add tmpfile) + ipfs_hash="$(_ak_ipfs_add tmpfile)" + _ak_ipfs_config_publish $ipfs_hash if [ $? != 0 ] then echo -e "\033[0;34mError on publishing\033[0;0m\nYour information:\n" diff --git a/lib/_ak_ipfs b/lib/_ak_ipfs index 3437083..5c47f91 100755 --- a/lib/_ak_ipfs +++ b/lib/_ak_ipfs @@ -265,7 +265,7 @@ _ak_ipfs_name_publish(){ _ak_log_error "No argument given" exit 1 fi - _ak_ipfs name publish "$1" "$2" + _ak_ipfs name publish --key="$1" "$2" if [ $? -ne 0 ] then _ak_log_error "Failed to get $1" @@ -273,6 +273,16 @@ _ak_ipfs_name_publish(){ fi } +_ak_ipfs_config_publish(){ + if [ -z $1 ] || [ ! -n "$1" ] + then + _ak_log_error "No argument given" + exit 1 + fi + _ak_ipfs_check_config_key + _ak_ipfs_name_publish "ak-config" $1 +} + _ak_ipfs_name_resolve(){ if [ -z $1 ] || [ ! -n "$1" ] then @@ -321,21 +331,24 @@ _ak_ipns_resolve(){ fi } -_ak_ipfs_check(){ - _ak_ipfs_files_ls /zarchive > /dev/null - if [ $? -ne 0 ] - then - _ak_log_error "/zarchive is missing" - else - _ak_log_info "/zarchive OK" - fi - _ak_ipfs_files_ls /zlatest > /dev/null +_ak_ipfs_check_config_key(){ + _ak_ipfs_key_list | grep ak-config > /dev/null if [ $? -ne 0 ] then - _ak_log_error "/zlatest is missing" + _ak_log_warning "ak-config key is missing" + _ak_ipfs_key_gen ak-config + if [ $? -ne 0 ] + then + _ak_log_error "ak-config fails to create" + else + _ak_log_info "ak-config created" + fi else - _ak_log_info "/zlatest is OK" + _ak_log_info "ak-config is there" fi +} + +_ak_ipfs_check_zchain_key(){ _ak_ipfs_key_list | grep zchain > /dev/null if [ $? -ne 0 ] then @@ -350,22 +363,35 @@ _ak_ipfs_check(){ else _ak_log_info "zchain is there" fi - _ak_ipfs_key_list | grep ak-config > /dev/null +} + +_ak_ipfs_check_zarchive_dir(){ + _ak_ipfs_files_ls /zarchive > /dev/null if [ $? -ne 0 ] then - _ak_log_warning "ak-config key is missing" - _ak_ipfs_key_gen ak-config - if [ $? -ne 0 ] - then - _ak_log_error "ak-config fails to create" - else - _ak_log_info "ak-config created" - fi + _ak_log_error "/zarchive is missing" else - _ak_log_info "ak-config is there" + _ak_log_info "/zarchive OK" fi } +_ak_ipfs_check_zlatest_file(){ + _ak_ipfs_files_ls /zlatest > /dev/null + if [ $? -ne 0 ] + then + _ak_log_error "/zlatest is missing" + else + _ak_log_info "/zlatest is OK" + fi +} + +_ak_ipfs_check(){ + _ak_ipfs_check_zarchive_dir + _ak_ipfs_check_zlatest_file + _ak_ipfs_check_config_key + _ak_ipfs_check_zchain_key +} + _ak_ipfs_init(){ if [ ! -d $AK_IPFS_REPO ] then @@ -429,3 +455,7 @@ _ak_ipfs_swarm_install() { _ak_log_error "Visit https://arching-kaos.net/files/swarm.key and copy it to your ipfs folder" fi } + +_ak_ipfs_get_config_ipns_key(){ + _ak_ipfs_key_list_full | grep 'ak-config' | cut -d ' ' -f 1 +} diff --git a/lib/_ak_log b/lib/_ak_log index c598ab8..df144bd 100755 --- a/lib/_ak_log +++ b/lib/_ak_log @@ -1,17 +1,24 @@ #!/bin/bash -_ak_log_follow(){ - tail -f $AK_LOGSFILE | while read -r p || [ -n "$p" ] - do - timestamp="$(echo "$p" | awk '{print $1}')" - program="$(echo "$p" | awk '{print $2}')" - messagetype="$(echo "$p" | awk '{print $3}')" - message="$(echo "$p" | cut -d ' ' -f4-)" +_ak_log_print_log_line(){ + if [ -n "$1" ] + then + timestamp="$(echo "$*" | awk '{print $1}')" + program="$(echo "$*" | awk '{print $2}')" + messagetype="$(echo "$*" | awk '{print $3}')" + message="$(echo "$*" | cut -d ' ' -f4-)" printf '%s \033[1;32m%s\033[0;00m \033[1;31m%s\033[0;00m %s\n' \ "$(date --date=@$timestamp +%Y%m%d_%H%M%S)" \ "$program" \ "$messagetype" \ "$message" + fi +} + +_ak_log_follow(){ + tail -f $AK_LOGSFILE | while read -r p || [ -n "$p" ] + do + _ak_log_print_log_line "$p" done } @@ -60,7 +67,7 @@ _ak_log_message(){ echo "$TS" "<$1>" "[$2]" "$msg" >> $AK_LOGSFILE if [ ! -z $AK_DEBUG ] && [ -n "$AK_DEBUG" ] && [ "$AK_DEBUG" == "yes" ] then - echo "$TS" "<$1>" "[$2]" "$msg" >&2 + _ak_log_print_log_line "$TS <$1> [$2] $msg" >&2 fi else echo "$TS" "<$1>" "[ERROR]" "No message" >> $AK_LOGSFILE diff --git a/lib/_ak_zblock b/lib/_ak_zblock index 6afacfc..48aa7b5 100755 --- a/lib/_ak_zblock +++ b/lib/_ak_zblock @@ -3,19 +3,16 @@ source $AK_LIBDIR/_ak_log source $AK_LIBDIR/_ak_script source $AK_LIBDIR/_ak_gpg source $AK_LIBDIR/_ak_html +source $AK_LIBDIR/_ak_ipfs -_ak_zblock_show(){ # _ak_zblock_show # ak-zblock [IPFS CID] # # Returns a JSON array representing the chain retrieved. # Logs messages to $LOGSFILE. +_ak_zblock_show(){ verify=1 - if [ ! -z "$1" ] && [ "$1" == "-h" ] || [ "$1" == "--help" ] - then - usage - exit - elif [ ! -z "$1" ] + if [ ! -z $1 ] && [ -n "$1" ] then echo $1 | grep -e 'Qm.\{44\}' >/dev/null if [ $? -ne 0 ] @@ -28,7 +25,6 @@ _ak_zblock_show(){ entrance="$(cat $AK_ZLATEST)" fi zblock=$entrance - # ak-enter -l 1 "$entrance" > "$AK_CACHEDIR/fzblocks/$entrance" # Check if $zblock exists as variable if [ ! -v $zblock ] then diff --git a/lib/_ak_zchain b/lib/_ak_zchain index 462a509..3776e49 100755 --- a/lib/_ak_zchain +++ b/lib/_ak_zchain @@ -1,7 +1,7 @@ #!/bin/bash -source $AK_LIBDIR/_ak_ipfs source $AK_LIBDIR/_ak_log -source $AK_LIBDIR/_ak_config +source $AK_LIBDIR/_ak_ipfs +source $AK_LIBDIR/_ak_zblock _ak_zchain_reset(){ echo "Reseting ZLATEST to ZGENESIS" @@ -230,3 +230,205 @@ _ak_zchain_crawl_remote_ipns(){ _ak_zchain_crawl -n $1 #ak enter -n $1 } + +# _ak_zchain_announce(){} +# PROGRAM="$(basename $0)" +# printf '[%s]\n' "$PROGRAM" +# printf "TEST\t/v0/announce/zchain\n" +# # curl http://127.0.0.1:8610/v0/announce/zchain +# printf "\t01:\tendpoint with valid data" +# curl \ +# --connect-timeout 3 \ +# -POST http://localhost:8610/v0/announce/zchain \ +# --header 'Content-Type: application/json' \ +# --data-raw '{"zchain":"k51qzi5uqu5dgapvk7bhxmchuqya9immqdpbz0f1r91ckpdqzub63afn3d5apr"}' 2>/dev/null | jq -M -c > /dev/null +# if [ $? -eq 0 ] +# then +# printf "\t\t\033[0;32mPASSED\033[0;0m" +# else +# printf "\t\033[0;31mFAILED\033[0;0m" +# fi +# printf "\n" +# +# printf "\t02:\tendpoint with invalid data" +# curl \ +# --connect-timeout 3 \ +# -POST http://localhost:8610/v0/announce/zchain \ +# --header 'Content-Type: application/json' \ +# --data-raw '{"zchain":"k51qzi5uqu5dgapvk7bhxmchuqya9immqdpbz0f1r91ckpdqzub63afn3d5aar"}' 2>/dev/null | jq -M -c > /dev/null +# if [ $? -eq 0 ] +# then +# printf "\t\t\033[0;32mPASSED\033[0;0m" +# else +# printf "\t\033[0;31mFAILED\033[0;0m" +# fi +# printf "\n" +# +# printf "\t03:\tendpoint no data" +# curl \ +# --connect-timeout 3 \ +# -POST http://localhost:8610/v0/announce/zchain \ +# --header 'Content-Type: application/json' \ +# --data-raw '{"zchain":""}' 2>/dev/null | jq -M -c > /dev/null +# if [ $? -eq 0 ] +# then +# printf "\t\t\t\033[0;32mPASSED\033[0;0m" +# else +# printf "\t\t\t\033[0;31mFAILED\033[0;0m" +# fi +# printf "\n" +# } + +# _ak_zchain_chk(){ +# ## +# ## -h, --help Prints this help message +# ## +# fullprogrampath="$(realpath $0)" +# PROGRAM=$(basename $0) +# descriptionString="Quick description" +# source $AK_LIBDIR/_ak_log +# source $AK_LIBDIR/_ak_script +# source $AK_LIBDIR/_ak_ipfs +# +# fix="0" +# usage(){ +# echo "zchain-chk - Check and fix zchain" +# echo "---------------------------------" +# echo "Usage:" +# echo " --help, -h Print this help and exit" +# echo " --chain , -n Crawl specified chain" +# echo " --fix #TODO Fix your chain" +# echo "" +# echo "Note that combined flags don't work for now" +# echo "Running with no flags crawls your chain" +# } +# +# if [ ! -z "$1" ] && [ "$1" == "-h" ] || [ "$1" == "--help" ] +# then +# usage +# exit +# elif [ ! -z "$1" ] && [ "$1" == "-f" ] || [ "$1" == "--fix" ] +# then +# fix="1" +# entrance="$(cat $AK_ZLATEST)" +# elif [ ! -z "$1" ] && [ "$1" == "-n" ] || [ "$1" == "--chain" ] +# then +# entrance="$(_ak_ipns_resolve $2)" +# elif [ ! -z "$1" ] +# then +# entrance="$1" +# else +# # By default we ak-enter from the latest block +# # We can alter this by changing this value +# entrance="$(cat $AK_ZLATEST)" +# fi +# +# # We assign the IPFS CIDv0 of an empty file as this is used +# # as our GENESIS block, hence the "seed" that the tree grows +# # from. +# seed="$(cat $AK_ZGENESIS)" +# +# # We assume that we found the entrance inside a block, hence +# # ZBLOCK is labeled as previous +# zblock="$entrance" +# declare -A blocks_found +# +# # Enter temp folder +# TEMPASSIN="$(_ak_make_temp_directory)" +# cd $TEMPASSIN +# counter=0 +# +# # The loop +# # We break the loop from inside the loop +# while true +# do +# if [ $counter == 0 ] +# then +# echo 'Start checking' +# fi +# counter=$(expr $counter + 1) +# +# # Check if $zblock exists as variable +# if [ ! -v $zblock ] +# then +# # Check if it is not our seed cause if it is we skip this part +# if [ "$zblock" != "$seed" ] +# then +# # Reset timestamp since it's introduced later +# timestamp='' +# # Announce to stdout which ZBLOCK is being read at the moment +# _ak_log_info "Examining $zblock" +# +# # We create files named after each ZBLOCK IPFS CID for later +# # reference. Files are empty. +# touch $AK_ZBLOCKDIR/$zblock +# _ak_log_info "Created reference" +# +# # 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_log_info "ZBLOCK $zblock READ" +# +# # Supposingly you are on a safe environment and you only have +# # access to your chain, I would consider mild secure to source +# # the files into your bash. +# # File an issue/pull request if you think it can be done better!! +# source tmp-zblock +# _ak_log_info "ZBLOCK SOURCED" +# +# # Same as above applies to BLOCK and DATA subparts of each ZBLOCK +# # BLOCKS +# _ak_ipfs_cat $block | ak-json2bash > tmp-block +# source tmp-block +# _ak_log_info "BLOCK $block SOURCED" +# touch $AK_BLOCKDIR/$block +# _ak_log_info "BLOCK REFERENCED" +# module="$(echo $action | sed -e 's/\// /g' | awk '{ print $1 }')" +# _ak_log_info "DATA is $module module." +# command="$(echo $action | sed -e 's/\// /g' | awk '{ print $2 }')" +# _ak_log_info "COMMAND is $command" +# if [ ! -v $timestamp ] +# then +# echo "$timestamp : $zblock -> $block -> $previous" +# blocks_found[$counter]="$block" +# fi +# touch $AK_DATADIR/$data +# +# # Now, since we sourced the BLOCK to our terminal, we can search +# # for $previous variable. In case we don't find one, we exit with +# # code 0 +# if [ -v $previous ] +# then +# _ak_log_error "Block $block has no previous zblock" +# echo "Chain with no genesis" +# if [ "$fix" == "1" ] +# then +# echo "LOL" +# else +# echo "Blocks found and need repacking..." +# for value in ${blocks_found[@]} +# do +# echo $value +# _ak_ipfs_cat $value | jq -M +# done +# fi +# exit 0 +# # Otherwise, we inform of the sequence +# else +# zblock=$previous +# fi +# # Now check if it is equal to the seed +# # which apparently means we reached the seed. +# elif [ "$zblock" == "$seed" ] +# then +# echo "Chain is OK with GENESIS block = $seed" +# _ak_log_info "Counter $counter" +# exit 0 +# fi +# # And finally, if nothing is there exit with error +# else +# _ak_log_error "Check not passed... No previous IPFS CID" +# exit 1 +# fi +# done +# } -- cgit v1.2.3