chore(user/bkmml):Cleanup comment
[BK-2020-03.git] / user / ots-git-gpg-wrapper.sh
CommitLineData
8c7da0e2
SBS
1#!/usr/bin/env bash
2# Desc: Wrapper for ots-git-gpg-wrapper
3# Note: Required because git's gpg.program option doesn't allow you to set
4# command line options.
5# Usage: git -c gpg.program=<path to ots-git-gpg-wrapper.sh> commit -S
6# Input: file: $HOME/.local/share/ots/jsonrpc_url.txt # first line is JSON RPC
7# url for local bitcoin node containing login credentials. Example:
8# http://user:35z8deadbeefhrdeadbeef5rk3@192.168.86.1:8332/
9# Depends: ots 0.7.0 (see https://github.com/opentimestamps/opentimestamps-client )
10# Version: 0.0.6
11# Ref/Attrib: [1] “How can I test if a particular alias is defined?”. https://unix.stackexchange.com/a/288513
12# [2] “OpenTimestamps Git Integration”. (2016-10-13). https://github.com/opentimestamps/opentimestamps-client/blob/master/doc/git-integration.md
13
14declare -a args calendars
15
16# Specify default calendars
17calendars+=("https://finney.calendar.eternitywall.com");
18calendars+=("https://btc.calendar.catallaxy.com");
19calendars+=("https://alice.btc.calendar.opentimestamps.org");
20calendars+=("https://bob.btc.calendar.opentimestamps.org");
21
22# Check if gpg is alias. See [1].
23if alias gpg 2>/dev/null; then
24 ## Get gpg alias command
25 gpg_cmd="$(type gpg)"; # get raw alias definition
26 gpg_cmd="${gpg_cmd#*=\'}"; # trim chars before and including first apostrophe
27 gpg_cmd="${gpg_cmd%\'*}"; # trim chars after and including last apostrophe
28else
29 gpg_cmd="$(which gpg)";
30fi;
31
32# Assemble args array
33## Specify bitcoin node jsonrpc url if available
34path_jsonrpc_url="$HOME/.local/share/ots/jsonrpc_url.txt";
35if [[ -f "$path_jsonrpc_url" ]]; then
36 jsonrpc_url="$(head -n1 "$path_jsonrpc_url")";
37 args+=("--bitcoin-node");
38 args+=("$jsonrpc_url");
39fi;
40
41## Pick random calendar
42### Get calendars array size
43cal_size="${#calendars[@]}";
44cal_rnd_idx="$(shuf -i 1-"$cal_size" -n 1)";
45cal_rnd_idx="$((cal_rnd_idx - 1))"; # bash array is zero-indexed
46url_cal_random="${calendars[$cal_rnd_idx]}";
47args+=("--no-default-whitelist");
48args+=("--whitelist");
49args+=("$url_cal_random");
50
51## Specify '--gpg-program' option
52args+=("--gpg-program");
53args+=("$gpg_cmd");
54
55## Passthrough positional parameters
56args+=("--"); # mark end of ots-git-wrapper options
57for param in "$@"; do
58 args+=("$param");
59done;
60
61# Run command with args
62# pseudocode: ots-git-gpg-wrapper $jsonrpc_option $calendar_option --gpg-program $gpg_cmd -- "$@"
63ots-git-gpg-wrapper "${args[@]}";