diff options
Diffstat (limited to 'lib/_ak_zblock')
-rwxr-xr-x | lib/_ak_zblock | 149 |
1 files changed, 111 insertions, 38 deletions
diff --git a/lib/_ak_zblock b/lib/_ak_zblock index 667bf80..c5e58d9 100755 --- a/lib/_ak_zblock +++ b/lib/_ak_zblock @@ -18,11 +18,14 @@ ### 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 @@ -51,13 +54,33 @@ function _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 ] @@ -85,37 +108,82 @@ function _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 @@ -126,7 +194,6 @@ function _ak_data_expand(){ # ak-zblock [IPFS CID] # # Returns a JSON array representing the chain retrieved. -# Logs messages to $LOGSFILE. function _ak_zblock_show(){ verify=1 if [ ! -z $1 ] && [ -n "$1" ] @@ -453,7 +520,7 @@ function _ak_zblock_show(){ # } function _ak_zblock_gen_html(){ - time_started=$(date -u +%s.%N) + 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 @@ -476,11 +543,11 @@ function _ak_zblock_gen_html(){ _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 @@ -592,7 +659,7 @@ function _ak_zblock_repack(){ 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 @@ -615,6 +682,8 @@ function _ak_zblock_repack(){ # 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 ] @@ -633,7 +702,7 @@ function _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" @@ -691,11 +760,11 @@ function _ak_zblock_pack(){ _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 @@ -716,6 +785,8 @@ function _ak_zblock_pack(){ # 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 ] @@ -734,7 +805,7 @@ function _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" @@ -794,3 +865,5 @@ function _ak_zblock_announce(){ fi fi } + +_ak_log_debug "_ak_zblock loaded $(caller)" |