|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +export LC_ALL=C |
| 4 | +set -eu |
| 5 | + |
| 6 | +uname_S=$(uname -s 2>/dev/null || echo not) |
| 7 | + |
| 8 | +if [ "$uname_S" = "Darwin" ]; then |
| 9 | + PARAMS_DIR="$HOME/Library/Application Support/ZcashParams" |
| 10 | +else |
| 11 | + PARAMS_DIR="$HOME/.zcash-params" |
| 12 | +fi |
| 13 | + |
| 14 | +# Commented out because these are unused; see below. |
| 15 | +#SPROUT_PKEY_NAME='sprout-proving.key' |
| 16 | +#SPROUT_VKEY_NAME='sprout-verifying.key' |
| 17 | +SAPLING_SPEND_NAME='sapling-spend.params' |
| 18 | +SAPLING_OUTPUT_NAME='sapling-output.params' |
| 19 | +SAPLING_SPROUT_GROTH16_NAME='sprout-groth16.params' |
| 20 | +DOWNLOAD_URL="https://download.z.cash/downloads" |
| 21 | +IPFS_HASH="/ipfs/QmXRHVGLQBiKwvNq7c2vPxAKz1zRVmMYbmt7G5TQss7tY7" |
| 22 | + |
| 23 | +SHA256CMD="$(command -v sha256sum || echo shasum)" |
| 24 | +SHA256ARGS="$(command -v sha256sum >/dev/null || echo '-a 256')" |
| 25 | + |
| 26 | +WGETCMD="$(command -v wget || echo '')" |
| 27 | +IPFSCMD="$(command -v ipfs || echo '')" |
| 28 | +CURLCMD="$(command -v curl || echo '')" |
| 29 | + |
| 30 | +# fetch methods can be disabled with ZC_DISABLE_SOMETHING=1 |
| 31 | +ZC_DISABLE_WGET="${ZC_DISABLE_WGET:-}" |
| 32 | +ZC_DISABLE_IPFS="${ZC_DISABLE_IPFS:-}" |
| 33 | +ZC_DISABLE_CURL="${ZC_DISABLE_CURL:-}" |
| 34 | + |
| 35 | +LOCKFILE=/tmp/fetch_params.lock |
| 36 | + |
| 37 | +fetch_wget() { |
| 38 | + if [ -z "$WGETCMD" ] || ! [ -z "$ZC_DISABLE_WGET" ]; then |
| 39 | + return 1 |
| 40 | + fi |
| 41 | + |
| 42 | + cat <<EOF |
| 43 | +
|
| 44 | +Retrieving (wget): $DOWNLOAD_URL/$1 |
| 45 | +EOF |
| 46 | + |
| 47 | + wget \ |
| 48 | + --progress=dot:giga \ |
| 49 | + --output-document="$2" \ |
| 50 | + --continue \ |
| 51 | + --retry-connrefused --waitretry=3 --timeout=30 \ |
| 52 | + "$DOWNLOAD_URL/$1" |
| 53 | +} |
| 54 | + |
| 55 | +fetch_ipfs() { |
| 56 | + if [ -z "$IPFSCMD" ] || ! [ -z "$ZC_DISABLE_IPFS" ]; then |
| 57 | + return 1 |
| 58 | + fi |
| 59 | + |
| 60 | + cat <<EOF |
| 61 | +
|
| 62 | +Retrieving (ipfs): $IPFS_HASH/$1 |
| 63 | +EOF |
| 64 | + |
| 65 | + ipfs get --output "$2" "$IPFS_HASH/$1" |
| 66 | +} |
| 67 | + |
| 68 | +fetch_curl() { |
| 69 | + if [ -z "$CURLCMD" ] || ! [ -z "$ZC_DISABLE_CURL" ]; then |
| 70 | + return 1 |
| 71 | + fi |
| 72 | + |
| 73 | + cat <<EOF |
| 74 | +
|
| 75 | +Retrieving (curl): $DOWNLOAD_URL/$1 |
| 76 | +EOF |
| 77 | + |
| 78 | + curl \ |
| 79 | + --output "$2" \ |
| 80 | + -# -L -C - \ |
| 81 | + "$DOWNLOAD_URL/$1" |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +fetch_failure() { |
| 86 | + cat >&2 <<EOF |
| 87 | +
|
| 88 | +Failed to fetch the Zcash zkSNARK parameters! |
| 89 | +Try installing one of the following programs and make sure you're online: |
| 90 | +
|
| 91 | + * ipfs |
| 92 | + * wget |
| 93 | + * curl |
| 94 | +
|
| 95 | +EOF |
| 96 | + exit 1 |
| 97 | +} |
| 98 | + |
| 99 | +fetch_params() { |
| 100 | + # We only set these variables inside this function, |
| 101 | + # and unset them at the end of the function. |
| 102 | + filename="$1" |
| 103 | + output="$2" |
| 104 | + dlname="${output}.dl" |
| 105 | + expectedhash="$3" |
| 106 | + |
| 107 | + if ! [ -f "$output" ] |
| 108 | + then |
| 109 | + for i in 1 2 |
| 110 | + do |
| 111 | + for method in wget ipfs curl failure; do |
| 112 | + if "fetch_$method" "${filename}.part.${i}" "${dlname}.part.${i}"; then |
| 113 | + echo "Download of part ${i} successful!" |
| 114 | + break |
| 115 | + fi |
| 116 | + done |
| 117 | + done |
| 118 | + |
| 119 | + for i in 1 2 |
| 120 | + do |
| 121 | + if ! [ -f "${dlname}.part.${i}" ] |
| 122 | + then |
| 123 | + fetch_failure |
| 124 | + fi |
| 125 | + done |
| 126 | + |
| 127 | + cat "${dlname}.part.1" "${dlname}.part.2" > "${dlname}" |
| 128 | + rm "${dlname}.part.1" "${dlname}.part.2" |
| 129 | + |
| 130 | + "$SHA256CMD" $SHA256ARGS -c <<EOF |
| 131 | +$expectedhash $dlname |
| 132 | +EOF |
| 133 | + |
| 134 | + # Check the exit code of the shasum command: |
| 135 | + CHECKSUM_RESULT=$? |
| 136 | + if [ $CHECKSUM_RESULT -eq 0 ]; then |
| 137 | + mv -v "$dlname" "$output" |
| 138 | + else |
| 139 | + echo "Failed to verify parameter checksums!" >&2 |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + fi |
| 143 | + |
| 144 | + unset -v filename |
| 145 | + unset -v output |
| 146 | + unset -v dlname |
| 147 | + unset -v expectedhash |
| 148 | +} |
| 149 | + |
| 150 | +# Use flock to prevent parallel execution. |
| 151 | +lock() { |
| 152 | + if [ "$uname_S" = "Darwin" ]; then |
| 153 | + if shlock -f ${LOCKFILE} -p $$; then |
| 154 | + return 0 |
| 155 | + else |
| 156 | + return 1 |
| 157 | + fi |
| 158 | + else |
| 159 | + # create lock file |
| 160 | + eval "exec 9>$LOCKFILE" |
| 161 | + # acquire the lock |
| 162 | + flock -n 9 \ |
| 163 | + && return 0 \ |
| 164 | + || return 1 |
| 165 | + fi |
| 166 | +} |
| 167 | + |
| 168 | +exit_locked_error() { |
| 169 | + echo "Only one instance of fetch-params.sh can be run at a time." >&2 |
| 170 | + exit 1 |
| 171 | +} |
| 172 | + |
| 173 | +main() { |
| 174 | + |
| 175 | + lock fetch-params.sh \ |
| 176 | + || exit_locked_error |
| 177 | + |
| 178 | + cat <<EOF |
| 179 | +Zcash - fetch-params.sh |
| 180 | +
|
| 181 | +This script will fetch the Zcash zkSNARK parameters and verify their |
| 182 | +integrity with sha256sum. |
| 183 | +
|
| 184 | +If they already exist locally, it will exit now and do nothing else. |
| 185 | +EOF |
| 186 | + |
| 187 | + # Now create PARAMS_DIR and insert a README if necessary: |
| 188 | + if ! [ -d "$PARAMS_DIR" ] |
| 189 | + then |
| 190 | + mkdir -p "$PARAMS_DIR" |
| 191 | + README_PATH="$PARAMS_DIR/README" |
| 192 | + cat >> "$README_PATH" <<EOF |
| 193 | +This directory stores common Zcash zkSNARK parameters. Note that it is |
| 194 | +distinct from the daemon's -datadir argument because the parameters are |
| 195 | +large and may be shared across multiple distinct -datadir's such as when |
| 196 | +setting up test networks. |
| 197 | +EOF |
| 198 | + |
| 199 | + # This may be the first time the user's run this script, so give |
| 200 | + # them some info, especially about bandwidth usage: |
| 201 | + cat <<EOF |
| 202 | +The complete parameters are currently just under 1.7GB in size, so plan |
| 203 | +accordingly for your bandwidth constraints. If the Sprout parameters are |
| 204 | +already present the additional Sapling parameters required are just under |
| 205 | +800MB in size. If the files are already present and have the correct |
| 206 | +sha256sum, no networking is used. |
| 207 | +
|
| 208 | +Creating params directory. For details about this directory, see: |
| 209 | +$README_PATH |
| 210 | +
|
| 211 | +EOF |
| 212 | + fi |
| 213 | + |
| 214 | + cd "$PARAMS_DIR" |
| 215 | + |
| 216 | + # Sprout parameters: |
| 217 | + # Commented out because they are unneeded, but we will eventually update |
| 218 | + # this to delete the parameters if possible. |
| 219 | + #fetch_params "$SPROUT_PKEY_NAME" "$PARAMS_DIR/$SPROUT_PKEY_NAME" "8bc20a7f013b2b58970cddd2e7ea028975c88ae7ceb9259a5344a16bc2c0eef7" |
| 220 | + #fetch_params "$SPROUT_VKEY_NAME" "$PARAMS_DIR/$SPROUT_VKEY_NAME" "4bd498dae0aacfd8e98dc306338d017d9c08dd0918ead18172bd0aec2fc5df82" |
| 221 | + |
| 222 | + # Sapling parameters: |
| 223 | + fetch_params "$SAPLING_SPEND_NAME" "$PARAMS_DIR/$SAPLING_SPEND_NAME" "8e48ffd23abb3a5fd9c5589204f32d9c31285a04b78096ba40a79b75677efc13" |
| 224 | + fetch_params "$SAPLING_OUTPUT_NAME" "$PARAMS_DIR/$SAPLING_OUTPUT_NAME" "2f0ebbcbb9bb0bcffe95a397e7eba89c29eb4dde6191c339db88570e3f3fb0e4" |
| 225 | + fetch_params "$SAPLING_SPROUT_GROTH16_NAME" "$PARAMS_DIR/$SAPLING_SPROUT_GROTH16_NAME" "b685d700c60328498fbde589c8c7c484c722b788b265b72af448a5bf0ee55b50" |
| 226 | +} |
| 227 | + |
| 228 | +if [ "x${1:-}" = 'x--testnet' ] |
| 229 | +then |
| 230 | + echo "NOTE: testnet now uses the mainnet parameters, so the --testnet argument" |
| 231 | + echo "is no longer needed (ignored)" |
| 232 | + echo "" |
| 233 | +fi |
| 234 | + |
| 235 | +main |
| 236 | +rm -f $LOCKFILE |
| 237 | +exit 0 |
0 commit comments