Skip to content

Instantly share code, notes, and snippets.

@gharriso
Created May 30, 2019 01:22
Show Gist options
  • Save gharriso/33636b8ce9549a1ef246a7c20c8fd30a to your computer and use it in GitHub Desktop.
Save gharriso/33636b8ce9549a1ef246a7c20c8fd30a to your computer and use it in GitHub Desktop.
Shell script to sign, upload and prove files on provendb.com
#
# provenFiles.sh: Store files into a ProvenDB database using `mongofiles`
# optionally sign using a private key
# submit a Blockchain Proof once the files are loaded
#
# Usage: provenFiles.sh -u ProvenDBUri -k keyFile fileSpecification mongoFilesArgs
#
# Author: guy.a.harrison@gmail.com
# License: AGPL
#
DATEFORMAT="+%Y-%m-%dT%H:%M:%S.000%z"
usage() {
if [ $# -gt 0 ];then
echo "`date $DATEFORMAT`" $*
fi
echo "Usage:"
echo "${O} -u ProvenDBUri [-k privateKeyFile ] -f fileName"
exit 0
}
# Check arguments
while getopts ":k:u:f:h" opt;do
case ${opt} in
u ) URI=${OPTARG}
DB=`echo $URI|cut -f1 -d'?'|cut -d'/' -f4` #NB: this relies on provendb URI being in normal format
;;
k ) KEYFILE=${OPTARG}
;;
f ) FILENAME=${OPTARG}
;;
h ) usage;
;;
esac
done
shift $((OPTIND -1))
[ "$FILENAME" = "" ] && usage "Error: -f option must be specified"
[ "$URI" = "" ] && usage "Error: -u option must be specified"
[ -e "$FILENAME" ] || usage "File $FILENAME does not exist"
if [ "$KEYFILE" != "" ];then
[ -e "$KEYFILE" ] || usage "keyfile $KEYFILE does not exist"
fi
if [ "$KEYFILE" != "" ];then
# sign the file
echo "`date "$DATEFORMAT"` : Signing $FILENAME with $KEYFILE"
openssl dgst -sha256 -sign $KEYFILE -out $FILENAME.sign $FILENAME
if [ $? -ne 0 ];then
echo "Error signing file. Is $KEYFILE really a private key file?"
exit 1
fi
fi
echo "`date "$DATEFORMAT"` : Uploading files to ProvenDB"
mongofiles --uri $URI --db $DB put $FILENAME|| echo "Error loading $FILENAME"
if [ "$KEYFILE" != "" ];then
mongofiles --uri $URI --db $DB put ${FILENAME}.sign|| echo "Error loading ${FILENAME}.sign"
fi
echo "`date "$DATEFORMAT"` : Submitting blockchain proof"
cat >/tmp/provenfiles.$$ <<EOF
var currentVersion=db.runCommand({getVersion:1}).version;
var proof=db.runCommand({submitProof:currentVersion});
print("Blockchain proof",proof.proofId,"submitted");
EOF
mongo --quiet $URI /tmp/provenfiles.$$||echo "Error submitting blockchain proof"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment