chore(unitproc/bkt-replace_apostrophes)minor syntax
[BK-2020-03.git] / user / ots-git-gpg-wrapper.sh
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
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 bitcoin node jsonrpc url if available
34 path_jsonrpc_url="$HOME/.local/share/ots/jsonrpc_url.txt";
35 if [[ -f "$path_jsonrpc_url" ]]; then
36 jsonrpc_url="$(head -n1 "$path_jsonrpc_url")";
37 args+=("--bitcoin-node");
38 args+=("$jsonrpc_url");
39 fi;
40
41 ## Pick random calendar
42 ### Get calendars array size
43 cal_size="${#calendars[@]}";
44 cal_rnd_idx="$(shuf -i 1-"$cal_size" -n 1)";
45 cal_rnd_idx="$((cal_rnd_idx - 1))"; # bash array is zero-indexed
46 url_cal_random="${calendars[$cal_rnd_idx]}";
47 args+=("--no-default-whitelist");
48 args+=("--whitelist");
49 args+=("$url_cal_random");
50
51 ## Specify '--gpg-program' option
52 args+=("--gpg-program");
53 args+=("$gpg_cmd");
54
55 ## Passthrough positional parameters
56 args+=("--"); # mark end of ots-git-wrapper options
57 for param in "$@"; do
58 args+=("$param");
59 done;
60
61 # Run command with args
62 # pseudocode: ots-git-gpg-wrapper $jsonrpc_option $calendar_option --gpg-program $gpg_cmd -- "$@"
63 ots-git-gpg-wrapper "${args[@]}";