From 89d00fcbc75e128cba516009db100daec4b794a9 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Sat, 28 Dec 2024 15:59:04 +0200 Subject: refactoring --- lib/_ak_zblock | 68 ++++++---------------------------------------------------- 1 file changed, 7 insertions(+), 61 deletions(-) (limited to 'lib/_ak_zblock') diff --git a/lib/_ak_zblock b/lib/_ak_zblock index 6edf33c..667bf80 100755 --- a/lib/_ak_zblock +++ b/lib/_ak_zblock @@ -29,7 +29,7 @@ then mkdir $AK_WORKDIR/ipfs fi -_ak_data_expand(){ +function _ak_data_expand(){ if [ ! -z $1 ] && [ ! -z $2 ] && [ -n "$1" ] && [ -n "$2" ] then TEMP="$(_ak_make_temp_directory)" @@ -127,7 +127,7 @@ _ak_data_expand(){ # # Returns a JSON array representing the chain retrieved. # Logs messages to $LOGSFILE. -_ak_zblock_show(){ +function _ak_zblock_show(){ verify=1 if [ ! -z $1 ] && [ -n "$1" ] then @@ -149,9 +149,7 @@ _ak_zblock_show(){ timestamp='' # Announce to logs which ZBLOCK is being read at the moment _ak_log_info "Examining $zblock" - _ak_ipfs_cid_v0_check "$zblock" - # We check if any $zblock at all... # TODO: Eliminate outputing to file since we store all cat/get'ed # ipfs hashes on local fs. @@ -174,7 +172,6 @@ _ak_zblock_show(){ echo -n '{' # echo -n '"id":"'$counter'",' echo -n '"zblock":"'$zblock'",' - # Be sure that there are the expected values # We need 'block' and 'block_signature' inside a 'zblock' # Exit if any is missing @@ -186,7 +183,6 @@ _ak_zblock_show(){ fi _ak_ipfs_cid_v0_check "$block" _ak_log_debug "ZBLOCK $zblock has block $block" - block_signature="$(cat $AK_IPFS_ARTIFACTS/$zblock | jq -M -r .block_signature)" if [ "$block_signature" == "null" ] then @@ -195,12 +191,10 @@ _ak_zblock_show(){ fi _ak_ipfs_cid_v0_check "$block_signature" _ak_log_debug "ZBLOCK $zblock contains a BLOCK_SIGNATURE $block_signature" - # Same as above applies to BLOCK and DATA subparts of each ZBLOCK # BLOCKS echo -n '"block":"'$block'",' echo -n '"block_signature":"'$block_signature'",' - _ak_ipfs_cat $block | jq -c -M > /dev/null 2>&1 cat $AK_IPFS_ARTIFACTS/$block | jq -M > /dev/null 2>&1 if [ $? -ne 0 ] @@ -208,7 +202,6 @@ _ak_zblock_show(){ _ak_log_error "BLOCK $block READ failed" exit 1 fi - grep -e 'timestamp' -e 'gpg' -e 'data' -e 'action' -e 'detach' -e 'previous' $AK_IPFS_ARTIFACTS/$block > /dev/null 2>&1 if [ $? -ne 0 ] then @@ -216,30 +209,22 @@ _ak_zblock_show(){ exit 1 fi _ak_log_debug "BLOCK $block is a block" - action="$(cat $AK_IPFS_ARTIFACTS/$block | jq -M -r .action)" module="$(echo $action | sed -e 's/\// /g' | awk '{ print $1 }')" _ak_log_debug "DATA is $module module." - command="$(echo $action | sed -e 's/\// /g' | awk '{ print $2 }')" _ak_log_debug "COMMAND is $command" - timestamp="$(cat $AK_IPFS_ARTIFACTS/$block | jq -M -r .timestamp)" if [ "$timestamp" != "null" ] then echo -n '"timestamp":"'$timestamp'",' fi - detach="$(cat $AK_IPFS_ARTIFACTS/$block | jq -M -r .detach)" echo -n '"detach":"'$detach'",' - echo -n '"module":"'$module'",' - echo -n '"action":"'$command'",' - gpg="$(cat $AK_IPFS_ARTIFACTS/$block | jq -M -r .gpg)" echo -n '"gpg":"'$gpg'",' - if [ $verify -eq 1 ] then _ak_ipfs_get $gpg > /dev/null 2>&1 @@ -262,7 +247,6 @@ _ak_zblock_show(){ fi ln -s $AK_IPFS_ARTIFACTS/$block_signature $TEMP/$block.asc _ak_log_info "BLOCK_SIGNATURE $block_signature downloaded" - _ak_ipfs_get $block > /dev/null 2>&1 if [ $? -ne 0 ] then @@ -270,7 +254,6 @@ _ak_zblock_show(){ exit 1 fi _ak_log_info "BLOCK $block downloaded" - _ak_gpg_verify_signature $TEMP/$block.asc $AK_IPFS_ARTIFACTS/$block > /dev/null 2>&1 if [ $? -ne 0 ] then @@ -279,7 +262,6 @@ _ak_zblock_show(){ fi _ak_log_info "$gpg signature of $block is verified." fi - data="$(cat $AK_IPFS_ARTIFACTS/$block | jq -M -r .data)" _ak_ipfs_cid_v0_check "$data" _ak_data_expand $data $gpg @@ -288,13 +270,11 @@ _ak_zblock_show(){ _ak_log_error "Failed on data signature verification [data: $data, gpg: $gpg, zblock: $zblock]" exit 1 fi - # DATA # Only print to stdout # _ak_ipfs_cat $data # touch $AK_DATADIR/$data # ^ unreasonable? - # Now, since we sourced the BLOCK to our terminal, we can search # for $previous variable. In case we don't find one, we append one # and we exit. @@ -305,7 +285,6 @@ _ak_zblock_show(){ echo -n '"previous":"'$seed'"},{"genesis":"genesis"}]' _ak_log_info "Reached pseudo-genesis, counted $counter zblocks." exit 2 - # Otherwise, we inform of the sequence else #echo "$zblock after $previous" @@ -313,7 +292,6 @@ _ak_zblock_show(){ echo -n '"previous":"'$previous'"}' zblock=$previous fi - # Now check if it is equal to the seed # which apparently means we reached the seed. elif [ "$zblock" == "$seed" ] @@ -474,7 +452,7 @@ _ak_zblock_show(){ # # } -_ak_zblock_gen_html(){ +function _ak_zblock_gen_html(){ time_started=$(date -u +%s.%N) ss=$(echo $time_started | cut -d '.' -f 1) nss=$(echo $time_started | cut -d '.' -f 2) @@ -485,7 +463,6 @@ _ak_zblock_gen_html(){ _ak_help exit 1 fi - if [ -n "$1" ] then TEST="$(echo -n "$1" | grep -v '^Qm[A-Za-z0-9]\{44\}$')" @@ -495,13 +472,10 @@ _ak_zblock_gen_html(){ exit 1 fi fi - arg="$(_ak_make_temp_file)" _ak_zblock_show "$1" | jq > $arg - _ak_generate_html_header > zblock-$1.html _ak_generate_html_zblock $arg >> zblock-$1.html - time_ended=$(date -u +%s.%N) se=$(echo $time_ended | cut -d '.' -f 1) nse=$(echo $time_ended | cut -d '.' -f 2) @@ -515,17 +489,15 @@ _ak_zblock_gen_html(){ fi printf '' >> zblock-$1.html printf '' >> zblock-$1.html - rm $arg } -_ak_zblock_cache(){ +function _ak_zblock_cache(){ if [ ! -d "$AK_CACHEDIR/fzblocks" ] then mkdir $AK_CACHEDIR/fzblocks fi FZBLOCKSDIR="$AK_CACHEDIR/fzblocks" - if [ ! -z "$1" ] && [ -n "$1" ] then if [ -f "$AK_ZBLOCKDIR/$1" ] && [ "$( (du -b $AK_ZBLOCKDIR/$1 || du -A $AK_ZBLOCKDIR/$1)2>/dev/null | awk '{ print $1 }')" != "0" ] @@ -557,10 +529,9 @@ _ak_zblock_cache(){ _ak_log_error "No arguments..." exit 1 fi - } -_ak_zblock_repack(){ +function _ak_zblock_repack(){ if [ -z $1 ] || [ ! -n "$1" ] then _ak_log_error "No zblock hash provided" @@ -585,9 +556,7 @@ _ak_zblock_repack(){ exit 1 fi zblock_to_repack_at="$2" - MESSAGE="$zblock_to_repack" - ACTION="$(_ak_zblock_show $zblock_to_repack | jq -r '.module,.action' | tr $'\n' '/' | sed -e 's/\/$//g')" if [ ! -n "$ACTION" ] then @@ -600,36 +569,28 @@ _ak_zblock_repack(){ exit 1 fi data="$(_ak_zblock_show $zblock_to_repack | jq -r '.data')" - if [ ! -f $AK_IPFS_ARTIFACTS/$data ] then _ak_log_error "$data file doesn't exist" exit 1 fi _ak_log_info "We are repacking $ACTION with content $MESSAGE at $zblock_to_repack_at" - - MESSAGE_HASH="$data" - # We create a detached and armor signature of it MESSAGE_SIGN_FILE="$data.asc" _ak_gpg_sign_detached $MESSAGE_SIGN_FILE $AK_IPFS_ARTIFACTS/$data _ak_log_debug "Signature made for $data" - # We add the signature to IPFS MESSAGE_SIGNATURE=$(_ak_ipfs_add $MESSAGE_SIGN_FILE) _ak_log_debug "Hash made for $data: $MESSAGE_SIGNATURE" - # We will be using our public key also to put it in the block later KEY="self.pub" _ak_gpg_key_self_export $KEY GPG_PUB_KEY=$(_ak_ipfs_add $KEY) _ak_log_debug "Key: $GPG_PUB_KEY" - # Acquire last block of information, to chain this one with previous posted PREVIOUS="$zblock_to_repack_at" _ak_log_debug "Previous: $PREVIOUS" - # 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 BLOCK="block" @@ -637,15 +598,12 @@ _ak_zblock_repack(){ # We have a block now, so we sign it _ak_gpg_sign_detached $BLOCK_SIG $BLOCK _ak_log_debug "Signature made for block" - # We now add the signature to IPFS BLOCK_SIGNATURE=$(_ak_ipfs_add $BLOCK_SIG) _ak_log_debug "Block signature hash: $BLOCK_SIGNATURE" - # We also add the block! BLOCK=$(_ak_ipfs_add $BLOCK) _ak_log_debug "Block hash: $BLOCK" - # So we now do the think almost again printf '{"block":"%s","block_signature":"%s"}' $BLOCK $BLOCK_SIGNATURE > zblock ZBL="zblock" @@ -653,7 +611,6 @@ _ak_zblock_repack(){ ZBLOCK=$(_ak_ipfs_add $ZBL) echo $ZBLOCK _ak_log_debug "Zblock hash: $ZBLOCK" - # cat $PWD/zblock | jq -M # Optional or extending with # python send_as_ak_tx $ZBLOCK @@ -702,7 +659,7 @@ _ak_zblock_repack(){ fi } -_ak_zblock_pack(){ +function _ak_zblock_pack(){ if [ ! -z $2 ] then MESSAGE="$2" @@ -721,39 +678,30 @@ _ak_zblock_pack(){ fi done < tempisalreadythere rm tempisalreadythere - _ak_log_info "We are doing $ACTION with content $MESSAGE" # We add it to IPFS 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) - # 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) - # Acquire last block of information, to chain this one with previous posted 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 BLOCK="block" BLOCK_SIG=$BLOCK".asc" # We have a block now, so we sign it _ak_gpg_sign_detached $BLOCK_SIG $BLOCK - # We now add the signature to IPFS BLOCK_SIGNATURE=$(_ak_ipfs_add $BLOCK_SIG) - # We also add the 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" @@ -764,7 +712,6 @@ _ak_zblock_pack(){ _ak_log_error "File does not exist. Aborting..." exit 1 fi - # cat $PWD/zblock | jq -M # Optional or extending with # python send_as_ak_tx $ZBLOCK @@ -815,10 +762,9 @@ _ak_zblock_pack(){ _ak_log_error "Not enough arguments" exit 1 fi - } -_ak_zblock_announce(){ +function _ak_zblock_announce(){ if [ ! -z "$1" ] && [ -n "$1" ] then curl \ -- cgit v1.2.3