chore(unitproc/bkt-replace_apostrophes)
[BK-2020-03.git] / user / ots-git-gpg-wrapper-wait.sh
1 #!/usr/bin/env bash
2 # Desc: Wrapper for ots-git-gpg-wrapper with `--wait` option
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-wait.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
14 declare -a args calendars
15
16 # Specify default calendars
17 calendars+=("https://finney.calendar.eternitywall.com");
18 calendars+=("https://btc.calendar.catallaxy.com");
19 calendars+=("https://alice.btc.calendar.opentimestamps.org");
20 calendars+=("https://bob.btc.calendar.opentimestamps.org");
21
22 # Check if gpg is alias. See [1].
23 if 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
28 else
29 gpg_cmd="$(which gpg)";
30 fi;
31
32 # Assemble args array
33 ## Specify '--wait' option
34 args+=("--wait");
35
36 ## Specify bitcoin node jsonrpc url if available
37 path_jsonrpc_url="$HOME/.local/share/ots/jsonrpc_url.txt";
38 if [[ -f "$path_jsonrpc_url" ]]; then
39 jsonrpc_url="$(head -n1 "$path_jsonrpc_url")";
40 args+=("--bitcoin-node");
41 args+=("$jsonrpc_url");
42 fi;
43
44 ## Pick random calendar
45 ### Get calendars array size
46 cal_size="${#calendars[@]}";
47 cal_rnd_idx="$(shuf -i 1-"$cal_size" -n 1)";
48 cal_rnd_idx="$((cal_rnd_idx - 1))"; # bash array is zero-indexed
49 url_cal_random="${calendars[$cal_rnd_idx]}";
50 args+=("--whitelist");
51 args+=("$url_cal_random");
52 args+=("--no-default-whitelist");
53
54 ## Specify '--gpg-program' option
55 args+=("--gpg-program");
56 args+=("$gpg_cmd");
57
58 ## Passthrough positional parameters
59 args+=("--"); # mark end of ots-git-wrapper options
60 for param in "$@"; do
61 args+=("$param");
62 done;
63
64 # Run command with args
65 # pseudocode: ots-git-gpg-wrapper --wait $jsonrpc_option $calendar_option --gpg-program $gpg_cmd -- "$@"
66 ots-git-gpg-wrapper "${args[@]}";