diff options
Diffstat (limited to 'lib/_ak_zblock')
-rwxr-xr-x | lib/_ak_zblock | 240 |
1 files changed, 138 insertions, 102 deletions
diff --git a/lib/_ak_zblock b/lib/_ak_zblock index 1b278a0..c5e58d9 100755 --- a/lib/_ak_zblock +++ b/lib/_ak_zblock @@ -1,17 +1,38 @@ -#!/bin/bash +#!/usr/bin/env bash +### +### arching-kaos-tools +### Tools to interact and build an Arching Kaos Infochain +### Copyright (C) 2021 - 2025 kaotisk +### +### This program is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### This program is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with this program. If not, see <http://www.gnu.org/licenses/>. +### #set -x -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 +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_script +_ak_lib_load _ak_datetime +_ak_lib_load _ak_gpg +_ak_lib_load _ak_html +_ak_lib_load _ak_ipfs +_ak_lib_load _ak_fs if [ ! -d $AK_WORKDIR/ipfs ] 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)" @@ -33,13 +54,33 @@ _ak_data_expand(){ if [ $? -ne 0 ] then _ak_log_error "_ak_data_expand: No 'ipfs' field in $1" - exit 1 + _ak_log_debug "Looking for akfsmap" + _ak_ipfs_cat $1 | jq | grep akfsmap > /dev/null + if [ $? -ne 0 ] + then + _ak_log_error "_ak_data_expand: No 'akfsmap' field in $1" + exit 1 + fi + is_data_akfs="yes" + else + is_data_akfs="no" fi - DATA="$(_ak_ipfs_cat $1 | jq -r '.ipfs')" #| grep ipfs | sed -e 's/"ipfs": "//g; s/[",]//g; s/ //g')" - if [ $? -ne 0 ] + if [ "${is_data_akfs}" == "yes" ] then - _ak_log_error "Error while extracting data from JSON for $1" - exit 1 + DATA=$(_ak_ipfs_cat $1 | jq -r '.akfsmap') + if [ $? -ne 0 ] + then + _ak_log_error "Error while extracting data (akfsmap) from JSON for $1" + exit 1 + fi + else + DATA="$(_ak_ipfs_cat $1 | jq -r '.ipfs')" #| grep ipfs | sed -e 's/"ipfs": "//g; s/[",]//g; s/ //g')" + if [ $? -ne 0 ] + then + _ak_log_error "Error while extracting data (ipfs) from JSON for $1" + exit 1 + fi + # _ak_log_debug "DATA of $1 is: [$DATA]" fi _ak_ipfs_cat $1 | jq | grep detach > /dev/null if [ $? -ne 0 ] @@ -67,37 +108,82 @@ _ak_data_expand(){ _ak_log_error "Could not import GPG key: $gpg" exit 1 fi - _ak_ipfs_get $DETACH > /dev/null 2>&1 - if [ $? -ne 0 ] - then - _ak_log_error "Error while getting signature: $DETACH for data: $DATA" - exit 1 - fi - if [ ! -f $AK_IPFS_ARTIFACTS/$DETACH ] + _ak_log_debug "GPG key from $gpg was imported!" + if [ "${is_data_akfs}" == "yes" ] then - _ak_log_error "$DETACH was downloaded but not found" - exit 1 + _ak_log_info "DATA is on AKFS" + _ak_fs_net_get_from_map_hash $DETACH + if [ $? -ne 0 ] + then + _ak_log_error "Failed to download $DETACH" + exit 1 + fi + detach_filename="$(_ak_fs_from_map_net_get_original_filename $DETACH)" + _ak_log_info "$DETACH downloaded @ $detach_filename (we are in: $(pwd))" + if [ ! -f $TEMP/$detach_filename ] + then + _ak_log_error "File downloaded from akfs wasn't found: $DETACH $detach_filename" + exit 1 + fi + _ak_log_debug "detach filename is $detach_filename" + mv $TEMP/$detach_filename $TEMP/$DETACH + if [ ! -f $TEMP/$DETACH ] + then + _ak_log_error "$DETACH was downloaded but not found" + exit 1 + fi + mv $TEMP/$DETACH $TEMP/$DATA.asc + else + _ak_ipfs_get $DETACH > /dev/null 2>&1 + if [ $? -ne 0 ] + then + _ak_log_error "Error while getting signature: $DETACH for data: $DATA" + exit 1 + fi + _ak_log_debug "$DETACH was downloaded" + if [ ! -f $AK_IPFS_ARTIFACTS/$DETACH ] + then + _ak_log_error "$DETACH was downloaded but not found" + exit 1 + fi + ln -s $AK_IPFS_ARTIFACTS/$DETACH $TEMP/$DATA.asc fi - ln -s $AK_IPFS_ARTIFACTS/$DETACH $TEMP/$DATA.asc _ak_log_debug "DATA_SIGNATURE $DETACH downloaded" - _ak_ipfs_get $DATA > /dev/null 2>&1 - if [ $? -ne 0 ] + _ak_log_info "Gonna get $DATA..." + if [ "${is_data_akfs}" == "yes" ] then - _ak_log_error "Error while getting data: $DATA" - exit 1 + _ak_fs_net_cat_from_map_hash $DATA > $TEMP/$DATA + if [ $? -ne 0 ] + then + _ak_log_error "Failed to download $DATA" + exit 1 + fi + if [ ! -f $TEMP/$DATA ] + then + _ak_log_error "$DATA was downloaded but not found" + exit 1 + fi + else + _ak_ipfs_get $DATA > /dev/null 2>&1 + if [ $? -ne 0 ] + then + _ak_log_error "Error while getting data: $DATA" + exit 1 + fi + ln -s $AK_IPFS_ARTIFACTS/$DATA $TEMP/$DATA fi _ak_log_debug "DATA $DATA downloaded" - _ak_gpg_verify_signature $TEMP/$DATA.asc $AK_IPFS_ARTIFACTS/$DATA # > /dev/null 2>&1 + _ak_gpg_verify_signature $TEMP/$DATA.asc $TEMP/$DATA # > /dev/null 2>&1 if [ $? -ne 0 ] then - _ak_log_error "Error while verifying signature for $DATA" + _ak_log_error "Error while verifying signature for $DATA [ $TEMP/$DATA ] against $DETACH [ $TEMP/$DATA.asc ]" exit 1 fi # ln -s $AK_IPFS_ARTIFACTS/$DATA $AK_WORKDIR/ipfs - _ak_log_info "DATA_SIGNATURE $DATA_SIGNATURE verified" + _ak_log_info "DATA_SIGNATURE $DETACH verified" echo -n '"data":"'$1'","'$1'":'$(_ak_ipfs_cat $1|jq -M -c)',' cd - # rm -rf $TEMP + rm -rf $TEMP else _ak_log_error "_ak_data_expand HASH GPG" exit 1 @@ -108,9 +194,7 @@ _ak_data_expand(){ # ak-zblock [IPFS CID] # # Returns a JSON array representing the chain retrieved. -# Logs messages to $LOGSFILE. -_ak_zblock_show(){ - caller >&2 +function _ak_zblock_show(){ verify=1 if [ ! -z $1 ] && [ -n "$1" ] then @@ -132,9 +216,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. @@ -157,7 +239,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 @@ -169,7 +250,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 @@ -178,12 +258,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 ] @@ -191,7 +269,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 @@ -199,30 +276,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 @@ -245,7 +314,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 @@ -253,7 +321,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 @@ -262,7 +329,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 @@ -271,13 +337,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. @@ -288,7 +352,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" @@ -296,7 +359,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" ] @@ -457,8 +519,8 @@ _ak_zblock_show(){ # # } -_ak_zblock_gen_html(){ - time_started=$(date -u +%s.%N) +function _ak_zblock_gen_html(){ + time_started=$(_ak_datetime_unix_nanosecs) ss=$(echo $time_started | cut -d '.' -f 1) nss=$(echo $time_started | cut -d '.' -f 2) # Check if there are enough arguments @@ -468,7 +530,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\}$')" @@ -478,18 +539,15 @@ _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) + time_ended=$(_ak_datetime_unix_nanosecs) se=$(echo $time_ended | cut -d '.' -f 1) nse=$(echo $time_ended | cut -d '.' -f 2) printf ' <hr>\n' >> zblock-$1.html - printf ' <footer>Generated by %s on %s in %s.' "$PROGRAM" "$(datehuman)" "$(( $se - $ss ))">> zblock-$1.html + printf ' <footer>Generated by %s on %s in %s.' "$PROGRAM" "$(_ak_datetime_human)" "$(( $se - $ss ))">> zblock-$1.html if [ $nse -lt $nss ] then printf '%s seconds</footer>\n' "$(( 1$nse - $nss ))" >> zblock-$1.html @@ -498,20 +556,18 @@ _ak_zblock_gen_html(){ fi printf '</body>' >> zblock-$1.html printf '</html>' >> 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 | awk '{ print $1 }')" != "0" ] + if [ -f "$AK_ZBLOCKDIR/$1" ] && [ "$( (du -b $AK_ZBLOCKDIR/$1 || du -A $AK_ZBLOCKDIR/$1)2>/dev/null | awk '{ print $1 }')" != "0" ] then # if [ "$(sha512sum $AK_ZBLOCKDIR/$1 | awk '{ print $1 }')" == "$(_ak_zblock_show $1 | sha512sum | awk '{ print $1 }')" ] if [ ! -f $FZBLOCKSDIR/$1 ] @@ -540,10 +596,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" @@ -568,9 +623,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 @@ -583,52 +636,41 @@ _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 + printf '{"timestamp":"%s","action":"%s","data":"%s","detach":"%s","gpg":"%s","previous":"%s"}' $(_ak_datetime_unix) $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 _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" @@ -636,11 +678,12 @@ _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 # or for "offline" use + archivaltime="$(_ak_datetime_unix)" + echo "${archivaltime} $(cat $AK_ZLATEST)" >> $AK_ZLATEST_HISTORY echo $ZBLOCK > $AK_ZLATEST _ak_ipfs_name_publish zchain /ipfs/$ZBLOCK #> /dev/null 2>&1 if [ $? -ne 0 ] @@ -659,7 +702,7 @@ _ak_zblock_repack(){ 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/${archivaltime}-$(_ak_ipfs_files_stat /zlatest | head -n 1) > /dev/null 2>&1 if [ $? -ne 0 ] then _ak_log_error "Could not back up previous /zlatest" @@ -685,7 +728,7 @@ _ak_zblock_repack(){ fi } -_ak_zblock_pack(){ +function _ak_zblock_pack(){ if [ ! -z $2 ] then MESSAGE="$2" @@ -704,39 +747,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) - + PREVIOUS="$(cat $AK_ZLATEST)" # 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 + printf '{"timestamp":"%s","action":"%s","data":"%s","detach":"%s","gpg":"%s","previous":"%s"}' "$(_ak_datetime_unix)" "$ACTION" "$MESSAGE_HASH" "$MESSAGE_SIGNATURE" "$GPG_PUB_KEY" "$PREVIOUS" > block BLOCK="block" - BLOCK_SIG=$BLOCK".asc" + 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" @@ -747,11 +781,12 @@ _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 # or for "offline" use + archivaltime="$(_ak_datetime_unix)" + echo "${archivaltime} $(cat $AK_ZLATEST)" >> $AK_ZLATEST_HISTORY echo $ZBLOCK > $AK_ZLATEST _ak_ipfs_name_publish zchain /ipfs/$ZBLOCK > /dev/null 2>&1 if [ $? -ne 0 ] @@ -770,7 +805,7 @@ _ak_zblock_pack(){ 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/${archivaltime}-$(_ak_ipfs_files_stat /zlatest | head -n 1) > /dev/null 2>&1 if [ $? -ne 0 ] then _ak_log_error "Could not back up previous /zlatest" @@ -798,10 +833,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 \ @@ -831,3 +865,5 @@ _ak_zblock_announce(){ fi fi } + +_ak_log_debug "_ak_zblock loaded $(caller)" |