#!/bin/bash
# Desc: Utility for backing up and retrieving ots files
# Usage: bkotslu -I [dir]
-# Version: 0.1.0
+# Version: 0.1.3
# Depends: OpenTimestamps 0.7.0 (see https://opentimestamps.org )
# GNU Coreutils 8.32
# NOTE: This script does not verify OTS files; it assumes the contents of OTS files fed to it are valid.
# Output: stdout sha256 file hash (lowercase)
local output;
vbm "DEBUG:Starting get_ots_filehash() on:$1";
-
+
+ re='[0-9a-f]{64}';
if output="$( "$(which ots)" info "$1" | \
grep -E "^File sha256 hash: " | \
head -n1 | \
sed -E -e 's/(^File sha256 hash: )([0-9a-f]+$)/\2/g'; )" && \
- [[ -n "$output" ]]; then
+ [[ -n "$output" ]] && \
+ [[ "$output" =~ $re ]]; then
vbm "STATUS:Read file digest (${output}) via ots from:$1";
printf "%s" "$output";
return 0;
else
- yell "ERROR:Encountered problem getting file hash via ots from:$1";
- return 1;
+ die "ERROR:Encountered problem getting file hash via ots from:$1";
fi;
}; # Gets hash of file from ots file
get_ots_oldestblock() {
# BK-2020-03: yell()
local output;
vbm "DEBUG:Starting get_ots_oldestblock() on:$1";
-
+
+ re='[0-9]+';
if output="$( "$(which ots)" info "$1" | \
grep -E "verify BitcoinBlockHeaderAttestation\([0-9]+\)" | \
sort | head -n1 | \
sed -E -e 's/(^ verify BitcoinBlockHeaderAttestation)\(([0-9]+)(\))/\2/g'; )" && \
- [[ -n "$output" ]]; then
+ [[ -n "$output" ]] && \
+ [[ "$output" =~ $re ]]; then
vbm "STATUS:Retrieved Bitcoin block (${output}) via ots from:$1";
printf "%s" "$output";
return 0;
else
- yell "ERROR:Encountered problem getting Bitcoin block number via ots from:$1";
- return 1;
+ die "ERROR:Encountered problem getting Bitcoin block number via ots from:$1";
fi;
}; # Gets oldest Bitcoin block from ots file
store_ots_file() {
if ! { blk_provid="$block"; blk_stored="$(get_ots_oldestblock "$pout"; )"; }; then
yell "ERROR:Could not read block numbers from OTS files: $(declare -p fhash block pout )";
fi;
+ re='[0-9]+';
+ if [[ ! "$blk_stored" =~ $re ]] || [[ ! "$blk_provid" =~ $re ]]; then
+ die "FATAL:Invalid block number(s):$(declare -p blk_stored blk_provid)";
+ fi;
# Copy provided OTS if matching OTS found stored but provided is older
if [[ "$blk_provid" -lt "$blk_stored" ]]; then
digest="$1";
mapfile -t otsStorePaths < <(find "$pathOtsStore" -type f -name "${digest}*.otsu"; );
if [[ "${#otsStorePaths[@]}" -le 0 ]]; then
- yell "ERROR:No OTS file in OTS storage dir found. $(declare -p pathOtsStore digest otsStorePaths)";
+ yell "NOTICE:No OTS file for digest ${digest} found in ${pathOtsStore}.";
return 1;
fi;
i_oldest=0;
+ re='[0-9]+';
blockNumOldest="$( get_block_num_from_stored_ots_path "${otsStorePaths[0]}" )";
+ if ! [[ "$blockNumOldest" =~ $re ]]; then die "FATAL:Invalid block number:${blockNumOldest}"; fi;
for ((i=0; i<"${#otsStorePaths[@]}"; i++ )); do
blockNum="$( get_block_num_from_stored_ots_path "${otsStorePaths[$i]}" )";
+ if ! [[ "$blockNum" =~ $re ]]; then die "FATAL:Invalid block number:${blockNum}"; fi;
if [[ $blockNum -lt $blockNumOldest ]]; then
blockNumOldest=$blockNum;
i_oldest=$i;
## Check for OTS file accompanying provided file
if [[ -f "${pathFileIn}.ots" ]]; then
vbm "STATUS:An OTS file is next to provided file ${pathFileIn}.";
- blk_provid="$(get_ots_oldestblock "${pathFileIn}.ots"; )";
+ blk_provid="$(must get_ots_oldestblock "${pathFileIn}.ots"; )";
vbm "STATUS:The provided file's OTS file has block number ${blk_provid}";
+ re='[0-9]+';
+ if [[ ! "$blk_stored" =~ $re ]] || [[ ! "$blk_provid" =~ $re ]]; then
+ die "FATAL:Invalid block number(s):$(declare -p blk_stored blk_provid)";
+ fi;
if [[ "$blk_stored" -lt "$blk_provid" ]]; then
vbm "STATUS:An older timestamp in OTS store found. Replacing ${pathFileIn}.ots (block ${blk_provid}) with ${path_stored_ots} (block ${blk_stored}).";
if [[ ! -f "${pathFileIn}.ots.baku" ]]; then