#!/usr/bin/env bash set -e # Colors and symbols RED='\033[0;31m' GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' BOLD="\033[1m" SUCCESS="✓" ERROR="✗" BB_PATH=${BB_PATH:-"$HOME/.bb"} # Utility functions print_spinner() { local pid=$1 local delay=0.1 local spinstr='|/-\' while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do local temp=${spinstr#?} printf " [%c] " "$spinstr" local spinstr=$temp${spinstr%"$temp"} sleep $delay printf "\b\b\b\b\b\b" done printf " \b\b\b\b" } warn() { echo -e "${BOLD}${YELLOW}WARN: ${1}${NC}" } get_bb_version_for_noir() { local noir_version=$1 local resolved_version=$noir_version if [ "$noir_version" = "stable" ] || [ "$noir_version" = "nightly" ]; then # Get releases from GitHub API local releases=$(curl --fail -s "https://api.github.com/repos/noir-lang/noir/releases") if [ "$noir_version" = "stable" ]; then resolved_version=$(echo "$releases" | grep -o '"tag_name": "[^"]*"' | grep -v "aztec\|nightly" | head -1 | cut -d'"' -f4) else resolved_version=$(echo "$releases" | grep -o '"tag_name": "nightly[^"]*"' | head -1 | cut -d'"' -f4) fi fi local lookup_url="https://raw.githubusercontent.com/AztecProtocol/aztec-packages/next/barretenberg/bbup/bb-versions.json" # bb-versions.json is a flat "": "" map with one entry per line. local bb_version=$(curl --fail -s "$lookup_url" | grep -F "\"$resolved_version\":" | head -1 | cut -d'"' -f4) echo "$bb_version" } version_gte() { [ "$(printf '%s\n' "$1" "$2" | sort -V | head -n1)" = "$2" ] } install_bb() { local version=$1 local architecture=$(uname -m) local platform="" if ! version_gte "$version" "0.82.0"; then # Unknown when this vulnerability was introduced so we warn on all versions prior to fix. # See: https://github.com/AztecProtocol/aztec-packages/pull/12602 warn "There is a critical soundness issue in Ultrahonk in this version of Barretenberg. It is recommended to update to v0.82.2 or greater." warn "Any solidity verifier contracts must be regenerated using a patched version of Barretenberg." fi # Convert architecture names if [ "$architecture" = "arm64" ] || [ "$architecture" = "aarch64" ]; then if version_gte "$version" "0.77.0"; then architecture="arm64" else architecture="aarch64" fi elif [ "$architecture" = "x86_64" ]; then if version_gte "$version" "0.77.0"; then architecture="amd64" else architecture="x86_64" fi else printf "${RED}${ERROR} Unsupported architecture: ${architecture}${NC}\n" exit 1 fi # Determine platform if [ "$(uname)" = "Darwin" ]; then if version_gte "$version" "0.77.0"; then platform="darwin" else platform="apple-darwin" fi elif [ "$(uname)" = "Linux" ]; then if version_gte "$version" "0.77.0"; then platform="linux" else platform="linux-gnu" fi else printf "${RED}${ERROR} Unsupported platform: $(uname)${NC}\n" exit 1 fi printf "${BLUE}Installing to ${BB_PATH}${NC}\n" # Create temporary directory local temp_dir=$(mktemp -d) local temp_tar="${temp_dir}/temp.tar.gz" # Download and extract — try AztecProtocol/barretenberg first, fall back to aztec-packages local release_tag="v${version}" if ! version_gte "$version" "0.77.0"; then release_tag="aztec-packages-v${version}" fi local artifact="barretenberg-${architecture}-${platform}.tar.gz" local primary_url="https://github.com/AztecProtocol/barretenberg/releases/download/${release_tag}/${artifact}" local fallback_url="https://github.com/AztecProtocol/aztec-packages/releases/download/${release_tag}/${artifact}" if ! curl -sL --fail "$primary_url" -o "$temp_tar"; then printf "${BLUE}Trying fallback URL...${NC}\n" curl -L --fail "$fallback_url" -o "$temp_tar" fi mkdir -p "$BB_PATH" tar xzf "$temp_tar" -C "$BB_PATH" rm -rf "$temp_dir" # Update shell configuration update_shell_config "$BB_PATH" printf "${GREEN}${SUCCESS} Installed barretenberg to ${BB_PATH}${NC}\n" } update_shell_config() { local bb_bin_path=$1 local path_entry="export PATH=\"${bb_bin_path}:\$PATH\"" # Update various shell configs if they exist if [ -f "${HOME}/.bashrc" ]; then echo "$path_entry" >> "${HOME}/.bashrc" fi if [ -f "${HOME}/.zshrc" ]; then echo "$path_entry" >> "${HOME}/.zshrc" fi if [ -f "${HOME}/.config/fish/config.fish" ]; then echo "set -gx PATH ${bb_bin_path} \$PATH" >> "${HOME}/.config/fish/config.fish" fi # Update current session's PATH export PATH="${bb_bin_path}:$PATH" } # Main script main() { local version="" local noir_version="" # Parse arguments while [[ $# -gt 0 ]]; do case $1 in -v|--version) version="$2" shift 2 ;; -nv|--noir-version) noir_version="$2" shift 2 ;; *) printf "${RED}${ERROR} Unknown option: $1${NC}\n" exit 1 ;; esac done # If no version specified, try to get current noir version if [ -z "$version" ] && [ -z "$noir_version" ]; then noir_version="current" fi if [ "$noir_version" = "current" ]; then printf "${BLUE}Querying noir version from nargo${NC}\n" if ! command -v nargo &> /dev/null; then printf "${RED}${ERROR} Could not get noir version from nargo --version. Please specify a version.${NC}\n" exit 1 fi noir_version=$(nargo --version | grep -o 'nargo version = [0-9]\+\.[0-9]\+\.[0-9]\+\(-[a-zA-Z]\+\.[0-9]\+\)\?' | cut -d' ' -f4) printf "${GREEN}${SUCCESS} Resolved noir version ${noir_version} from nargo${NC}\n" fi if [ -n "$noir_version" ]; then printf "${BLUE}Getting compatible barretenberg version for noir version ${noir_version}${NC}\n" if [ "$noir_version" = "stable" ] || [ "$noir_version" = "nightly" ]; then printf "${BLUE}Resolving noir version ${noir_version}...${NC}\n" # Get releases from GitHub API to show the resolved version local releases=$(curl --fail -s "https://api.github.com/repos/noir-lang/noir/releases") local resolved_version="" if [ "$noir_version" = "stable" ]; then resolved_version=$(echo "$releases" | grep -o '"tag_name": "[^"]*"' | grep -v "aztec\|nightly" | head -1 | cut -d'"' -f4) else resolved_version=$(echo "$releases" | grep -o '"tag_name": "nightly[^"]*"' | head -1 | cut -d'"' -f4) fi printf "${GREEN}${SUCCESS} Resolved noir version ${noir_version} to ${resolved_version}${NC}\n" fi version=$(get_bb_version_for_noir "$noir_version") if [ ! -n "$version" ]; then printf "${RED}${ERROR} No version specified and couldn't determine version from noir${NC}\n" exit 1 fi printf "${GREEN}${SUCCESS} Resolved to barretenberg version ${version}${NC}\n" fi if [ -z "$version" ]; then printf "${RED}${ERROR} No version specified and couldn't determine version from noir${NC}\n" exit 1 fi install_bb "$version" } main "$@"