#!/bin/bash source $AK_LIBDIR/_ak_log source $AK_LIBDIR/_ak_script source $AK_LIBDIR/_ak_gpg _ak_zblock_show(){ # _ak_zblock_show # ak-zblock [IPFS CID] # # Returns a JSON array representing the chain retrieved. # Logs messages to $LOGSFILE. verify=1 if [ ! -z "$1" ] && [ "$1" == "-h" ] || [ "$1" == "--help" ] then usage exit elif [ ! -z "$1" ] then echo $1 | grep -e 'Qm.\{44\}' >/dev/null if [ $? -ne 0 ] then logit "ERROR" "Argument provided ($1) was not an IPFS CIDv0 string" exit 1 fi entrance="$1" else 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 # 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(){ # # 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. # # # # GPG is mentioned as a signing algorithm for encryption, decryption and signing. # # Let's say we have a file named as `example` # # # # We can extend this with calling the encoder and further send the transaction # # # # #FINGERPRINT="CHANGE THIS TO YOUR DEFAULT FINGERPRINT" # # We acquire the GPG fingerprint by email address # # The following example finds kaos@kaos.kaos' GPG fingerprint like this # # FINGERPRINT="$(gpg2 --homedir $AK_GPGHOME --list-keys | grep kaos@kaos.kaos -1 | head -n1 | awk '{print $1}')" # # # Below, the usage information # PROGRAM="$(basename $0)" # source $AK_LIBDIR/_ak_log # source $AK_LIBDIR/_ak_ipfs # source $AK_LIBDIR/_ak_gpg # # usage(){ # echo "Usage:" # echo "$PROGRAM -b block_file | -h block_hash | dunno" # echo "Creates and publishes a ZBLOCK based on a block and a previous" # echo "zblock." # echo "" # echo "Either -b or -h is needed. If both, -h is used." # echo "" # echo "-b block_file Points to a block file that we want to fix." # echo "-h block_hash If we don't have the block as a file, we can" # echo " use it's hash to retrieve it and edit it." # echo "-p previous_hash We refering to a previous hash for a ZBLOCK." # echo "" # echo "#TODO:" # echo "-t timestamp Unix UTC timestamp in seconds." # echo "-a \"module/action\" Change the action tag. Format: object/verb." # echo " In sense, \"Add news -> news/add\"." # echo "-d data In case you want to change the data, you 'll" # echo " be changing potentially the key of the block" # echo " and the data signature, otherwise your block" # echo " will be unverified." # echo "" # } # # main(){ # # logit "INFO" "We are doing" $BLOCK_TO_ADD "with content" $PREVIOUS # # 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: # cat > block < zblock << EOF # { # "block":"$BLOCK", # "block_signature":"$BLOCK_SIGNATURE" # } # EOF # ZBL="zblock" # # and we add it on IPFS # ZBLOCK=$(_ak_ipfs_add $ZBL) # echo $ZBLOCK # } # # if [ ! -z $2 ]; # then # PWD="$(pwd)" # # # We ask which BLOCK is goind to be edited # BLOCK_TO_ADD="$1" # # We ask for a ZBLOCK or GENESIS to put in the BLOCK # PREVIOUS="$2" # # _ak_ipfs_cat "$BLOCK_TO_ADD" # if [ $? == 0 ]; # then # echo "Nice! We found the block" # _ak_ipfs_get "$BLOCK_TO_ADD" # # sed -i.bak -e 's/^.*previous.*$/\"previous\":\"'$PREVIOUS'\"/' $BLOCK_TO_ADD # cat $BLOCK_TO_ADD | jq -M # # exit 2 # else # echo "Too bad, it seems the block is not there" # exit 1 # fi # # # cat $PWD/zblock | jq -M # # Optional or extending with # # python send_as_ak_tx $ZBLOCK # # or for "offline" use # echo $ZBLOCK > $ZLATEST # _ak_ipfs_name_publish --key=zchain $ZBLOCK > /dev/null 2>&1 # _ak_ipfs_files_mkdir /zarchive > /dev/null 2>&1 # _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) > /dev/null 2>&1 # _ak_ipfs_files_rm /zlatest > /dev/null 2>&1 # _ak_ipfs_files_cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 # else # usage # exit 0 # fi # # } _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) # Check if there are enough arguments if [ $# -lt 1 ] then logit "ERROR" "Not enough arguments provided" _ak_help exit 1 fi if [ -n "$1" ] then TEST="$(echo -n "$1" | grep -v '^Qm[A-Za-z0-9]\{44\}$')" if [ -n "$TEST" ] then echo not ok exit 1 fi fi arg="$(mktemp)" _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) printf '
\n' >> zblock-$1.html printf ' \n' "$(( 1$nse - $nss ))" >> zblock-$1.html else printf '%s seconds' "$(( $nse - $(echo -n $nss|sed 's/^0*//') ))" >> zblock-$1.html fi printf '' >> zblock-$1.html printf '' >> zblock-$1.html rm $arg } _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" ] then # if [ "$(sha512sum $AK_ZBLOCKDIR/$1 | awk '{ print $1 }')" == "$(_ak_zblock_show $1 | sha512sum | awk '{ print $1 }')" ] if [ ! -f $FZBLOCKSDIR/$1 ] then logit "INFO" "No cache found. Caching..." _ak_zblock_show "$1" > $FZBLOCKSDIR/$1 fi if [ ! -f $FZBLOCKSDIR/$1 ] then logit "INFO" "Caching failed..." exit 1 fi cat $FZBLOCKSDIR/$1 else _ak_zblock_show "$1" > $FZBLOCKSDIR/$1 if [ $? -ne "0" ] then logit "ERROR" "_ak_zblock_show failed..." exit 2 fi logit "ERROR" "Could not find zblock $1..." exit 1 fi exit 0 else logit "ERROR" "No arguments..." exit 1 fi } _ak_zblock_pack(){ if [ ! -z $2 ]; then MESSAGE="$2" ACTION="$1" if [ -f "$MESSAGE" ]; then # We check firstly if the encapsulated value of the "ipfs" key has already # appeared in the zchain. TO_CHECK="$(cat $MESSAGE | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g')" ak-enter | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g' | sort | uniq > tempisalreadythere while IFS="" read -r p || [ -n "$p" ] do if [ "$p" == "$TO_CHECK" ] then logit "ERROR" "Value $TO_CHECK already mentioned on the zchain" exit 1 fi done < tempisalreadythere rm tempisalreadythere logit "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" # and we add it on IPFS ZBLOCK=$(_ak_ipfs_add $ZBL) echo $ZBLOCK else logit "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 echo $ZBLOCK > $AK_ZLATEST _ak_ipfs_name_publish --key=zchain $ZBLOCK > /dev/null 2>&1 if [ $? -ne 0 ] then logit "ERROR" "Failed publishing ZBLOCK: $ZBLOCK" exit 1 fi _ak_ipfs_files_ls /zarchive > /dev/null 2>&1 if [ $? -ne 0 ] then logit "WARNING" "/zarchive does not exist" _ak_ipfs_files_mkdir /zarchive > /dev/null 2>&1 if [ $? -ne 0 ] then logit "ERROR" "Could not create /zarchive directory. Aborting." 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 if [ $? -ne 0 ] then logit "ERROR" "Could not back up previous /zlatest" exit 1 fi _ak_ipfs_files_rm /zlatest > /dev/null 2>&1 if [ $? -ne 0 ] then logit "ERROR" "Could not remove previous /zlatest" exit 1 fi _ak_ipfs_files_cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 if [ $? -ne 0 ] then logit "ERROR" "Could not copy $ZBLOCK to /zlatest" exit 1 fi ak-config --publish if [ $? -ne 0 ] then logit "ERROR" "Could not publish new configuration" exit 1 fi else logit "ERROR" "Not enough arguments" exit 1 fi } _ak_zblock_announce(){ if [ ! -z "$1" ] && [ -n "$1" ] then curl \ --connect-timeout 3 \ --header 'Content-Type: application/json' \ --data-raw '{"zblock":"'$1'"}' \ "http://127.0.0.1:8610/v0/announce/zblock" \ 2>/dev/null | jq -M -c > /dev/null if [ $? -ne 0 ] then logit "ERROR" "zblock $zblock announcement failed" else logit "INFO" "zblock $zblock announced" fi else curl \ --connect-timeout 3 \ --header 'Content-Type: application/json' \ --data-raw '{"zblock":"'$(ak-get-zlatest)'"}' \ "http://127.0.0.1:8610/v0/announce/zblock" \ 2>/dev/null | jq -M -c > /dev/null if [ $? -ne 0 ] then logit "ERROR" "zblock $zblock announcement failed" else logit "INFO" "zblock $zblock announced" fi fi }