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