diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/_ak_zblock | 192 |
1 files changed, 189 insertions, 3 deletions
diff --git a/lib/_ak_zblock b/lib/_ak_zblock index cb6af36..324d3aa 100755 --- a/lib/_ak_zblock +++ b/lib/_ak_zblock @@ -26,10 +26,196 @@ _ak_zblock_show(){ else entrance="$(cat $AK_ZLATEST)" fi - ak-enter -l 1 "$entrance" > "$AK_CACHEDIR/fzblocks/$entrance" + # ak-enter -l 1 "$entrance" > "$AK_CACHEDIR/fzblocks/$entrance" + # 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 logs which ZBLOCK is being read at the moment + logit "INFO" "Examining $zblock" + + _ak_ipfs_cid_v0_check "$zblock" + + # We check if any $zblock at all + _ak_ipfs_cat $zblock | jq -c -M > $AK_ZBLOCKDIR/$zblock + if [ "$?" -ne 0 ] + then + logit "ERROR" "ZBLOCK $zblock READ failed" + exit 1 + fi + logit "INFO" "ZBLOCK $zblock READ" + echo -n '{' +# echo -n '"id":"'$counter'",' + echo -n '"zblock":"'$zblock'",' + + # If it's JSON formated + cat $AK_ZBLOCKDIR/$zblock | jq -M > /dev/null 2>&1 + if [ $? -ne 0 ] + then + logit "ERROR" "ZBLOCK $zblock is not JSON" + cat /dev/null > $AK_ZBLOCKDIR/$zblock > /dev/null 2>&1 + exit 1 + fi + logit "INFO" "ZBLOCK $zblock is JSON" + + # Be sure that there are the expected values + # We need 'block' and 'block_signature' inside a 'zblock' + # Exit if any is missing + block="$(cat $AK_ZBLOCKDIR/$zblock | jq -M -r .block)" + if [ "$block" == "null" ] + then + logit "ERROR" "ZBLOCK $zblock has no block" + exit 1 + fi + _ak_ipfs_cid_v0_check "$block" + logit "INFO" "ZBLOCK $zblock has block" + + block_signature="$(cat $AK_ZBLOCKDIR/$zblock | jq -M -r .block_signature)" + if [ "$block_signature" == "null" ] + then + logit "ERROR" "ZBLOCK $zblock doesn't contain a block_signature" + exit 1 + fi + _ak_ipfs_cid_v0_check "$block_signature" + logit "INFO" "ZBLOCK $zblock contains a block_signature" + + # 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 + cat $AK_BLOCKDIR/$block | jq -M > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "BLOCK $block READ failed" + exit 1 + fi + + grep -e 'timestamp' -e 'gpg' -e 'data' -e 'action' -e 'detach' -e 'previous' $AK_BLOCKDIR/$block > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "BLOCK $block is NOT a valid block" + exit 1 + fi + logit "INFO" "BLOCK $block is a block" + + action="$(cat $AK_BLOCKDIR/$block | jq -M -r .action)" + module="$(echo $action | sed -e 's/\// /g' | awk '{ print $1 }')" + logit "INFO" "DATA is $module module." + + command="$(echo $action | sed -e 's/\// /g' | awk '{ print $2 }')" + logit "INFO" "COMMAND is $command" + + timestamp="$(cat $AK_BLOCKDIR/$block | jq -M -r .timestamp)" + if [ "$timestamp" != "null" ] + then + echo -n '"timestamp":"'$timestamp'",' + fi + + echo -n '"block_signature":"'$block_signature'",' + + detach="$(cat $AK_BLOCKDIR/$block | jq -M -r .detach)" + echo -n '"detach":"'$detach'",' + + echo -n '"module":"'$module'",' + + echo -n '"action":"'$command'",' + + gpg="$(cat $AK_BLOCKDIR/$block | jq -M -r .gpg)" + echo -n '"gpg":"'$gpg'",' + + if [ $verify == 1 ] + then + _ak_ipfs_get $gpg > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "Could not get GPG key: $gpg ." + exit 1 + fi + _ak_gpg_key_import_from_file $gpg > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "Could not import GPG key: $gpg ." + exit 1 + fi + _ak_ipfs_get $block_signature > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "Error while getting $block_signature for $block" + exit 1 + fi + mv $block_signature $block.asc + logit "INFO" "Block signature downloaded" + + _ak_ipfs_get $block > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "Could not get $block block" + exit 1 + fi + logit "INFO" "Downloaded block $block." + + _ak_gpg_verify_signature $block.asc $block > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "ERROR" "Could not verify $block with GPG key $gpg." + exit 1 + fi + logit "GPG" "$gpg signature of $block is verified." + fi + + data="$(cat $AK_BLOCKDIR/$block | jq -M -r .data)" + _ak_ipfs_cid_v0_check "$data" + ak-data-expand $data $gpg + if [ "$?" -ne 0 ] + then + logit "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 + + # 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. + previous="$(cat $AK_BLOCKDIR/$block | jq -M -r .previous)" + if [ -v $previous ] + then + logit "WARNING" "Block $block has no previous zblock, appending pseudo genesis to exit with 2." + echo -n '"previous":"'$seed'"},{"genesis":"genesis"}]' + logit "INFO" "Reached pseudo-genesis, counted $counter zblocks." + exit 2 + + # Otherwise, we inform of the sequence + else + #echo "$zblock after $previous" + logit "INFO" "Found a previous block for $zblock: $previous" + 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" ] + then + echo -n '{"genesis":"genesis"}]' + logit "INFO" "Reached $seed, counted $counter zblocks." + exit 0 + fi + # And finally, if nothing is there exit with error + else + echo "Check not passed... No previous IPFS CID" + exit 1 + fi } + # _ak_zblock_manipulator(){ -# #!/bin/bash # # This file describe the structure of the ArchingKaos messages in their basis. # # # # As previously thought, we exchange one IPFS hash through whatever means we can. @@ -197,7 +383,7 @@ _ak_zblock_gen_html(){ fi arg="$(mktemp)" - ak-enter -l 1 "$1" | jq '.[]' > $arg + _ak_zblock_show "$1" | jq > $arg ( \ # Create HTML document |