|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) 2015-2020 The Zcash developers |
| 4 | +# Copyright (c) 2020 The PIVX developers |
| 5 | +# Distributed under the MIT software license, see the accompanying |
| 6 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 7 | + |
| 8 | +export LC_ALL=C.UTF-8 |
| 9 | + |
| 10 | +set -eu |
| 11 | + |
| 12 | +if [ -n "${1:-}" ]; then |
| 13 | + PARAMS_DIR="$1" |
| 14 | +else |
| 15 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 16 | + PARAMS_DIR="$HOME/Library/Application Support/PIVXParams" |
| 17 | + else |
| 18 | + PARAMS_DIR="$HOME/.pivx-params" |
| 19 | + fi |
| 20 | +fi |
| 21 | + |
| 22 | +SAPLING_SPEND_NAME='sapling-spend.params' |
| 23 | +SAPLING_OUTPUT_NAME='sapling-output.params' |
| 24 | + |
| 25 | +SHA256CMD="$(command -v sha256sum || echo shasum)" |
| 26 | +SHA256ARGS="$(command -v sha256sum >/dev/null || echo '-a 256')" |
| 27 | + |
| 28 | +pushd () { |
| 29 | + command pushd "$@" > /dev/null |
| 30 | +} |
| 31 | + |
| 32 | +popd () { |
| 33 | + command popd > /dev/null |
| 34 | +} |
| 35 | + |
| 36 | +function install_params { |
| 37 | + local filename="$1" |
| 38 | + local output="$2" |
| 39 | + local expectedhash="$3" |
| 40 | + |
| 41 | + # if the params don't exist in the current directory, assume we're running from release tarballs |
| 42 | + if ! [ -f "$filename" ] |
| 43 | + then |
| 44 | + filename="share/$filename" |
| 45 | + fi |
| 46 | + |
| 47 | + if ! [ -f "$output" ] |
| 48 | + then |
| 49 | + "$SHA256CMD" $SHA256ARGS -c <<EOF |
| 50 | +$expectedhash $filename |
| 51 | +EOF |
| 52 | + |
| 53 | + # Check the exit code of the shasum command: |
| 54 | + CHECKSUM_RESULT=$? |
| 55 | + if [ $CHECKSUM_RESULT -eq 0 ]; then |
| 56 | + cp -v "$filename" "$output" |
| 57 | + else |
| 58 | + echo "Failed to verify parameter checksums!" >&2 |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +# Use flock to prevent parallel execution. |
| 65 | +function lock() { |
| 66 | + local lockfile=/tmp/install_params.lock |
| 67 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 68 | + if shlock -f ${lockfile} -p $$; then |
| 69 | + return 0 |
| 70 | + else |
| 71 | + return 1 |
| 72 | + fi |
| 73 | + else |
| 74 | + # create lock file |
| 75 | + eval "exec 200>$lockfile" |
| 76 | + # acquire the lock |
| 77 | + flock -n 200 \ |
| 78 | + && return 0 \ |
| 79 | + || return 1 |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +function exit_locked_error { |
| 84 | + echo "Only one instance of install-params.sh can be run at a time." >&2 |
| 85 | + exit 1 |
| 86 | +} |
| 87 | + |
| 88 | +function main() { |
| 89 | + |
| 90 | + lock install-params.sh \ |
| 91 | + || exit_locked_error |
| 92 | + |
| 93 | + cat <<EOF |
| 94 | +PIVX - install-params.sh |
| 95 | +
|
| 96 | +This script will install the PIVX zkSNARK parameters and verify their |
| 97 | +integrity with sha256sum. |
| 98 | +
|
| 99 | +If they already exist locally, it will exit now and do nothing else. |
| 100 | +EOF |
| 101 | + |
| 102 | + # Now create PARAMS_DIR and insert a README if necessary: |
| 103 | + if ! [ -d "$PARAMS_DIR" ] |
| 104 | + then |
| 105 | + mkdir -p "$PARAMS_DIR" |
| 106 | + README_PATH="$PARAMS_DIR/README" |
| 107 | + cat >> "$README_PATH" <<EOF |
| 108 | +This directory stores common PIVX zkSNARK parameters. Note that it is |
| 109 | +distinct from the daemon's -datadir argument because the parameters are |
| 110 | +large and may be shared across multiple distinct -datadir's such as when |
| 111 | +setting up test networks. |
| 112 | +EOF |
| 113 | + |
| 114 | + # This may be the first time the user's run this script, so give |
| 115 | + # them some info: |
| 116 | + cat <<EOF |
| 117 | +If the files are already present and have the correct |
| 118 | +sha256sum, nothing else is done. |
| 119 | +
|
| 120 | +Creating params directory. For details about this directory, see: |
| 121 | +$README_PATH |
| 122 | +
|
| 123 | +EOF |
| 124 | + fi |
| 125 | + |
| 126 | + if [ -d ".git" ] || [ -f autogen.sh ] |
| 127 | + then |
| 128 | + echo "Installing from source repo or dist tarball" |
| 129 | + pushd params |
| 130 | + fi |
| 131 | + |
| 132 | + # Sapling parameters: |
| 133 | + install_params "$SAPLING_SPEND_NAME" "$PARAMS_DIR/$SAPLING_SPEND_NAME" "8e48ffd23abb3a5fd9c5589204f32d9c31285a04b78096ba40a79b75677efc13" |
| 134 | + install_params "$SAPLING_OUTPUT_NAME" "$PARAMS_DIR/$SAPLING_OUTPUT_NAME" "2f0ebbcbb9bb0bcffe95a397e7eba89c29eb4dde6191c339db88570e3f3fb0e4" |
| 135 | + |
| 136 | + popd |
| 137 | +} |
| 138 | + |
| 139 | +main |
| 140 | +rm -f /tmp/install_params.lock |
| 141 | +exit 0 |
0 commit comments