#!/bin/bash set -euo pipefail usage="Usage: $(basename "$0") <.so file> [-h] [-n network] -- Verify that the deployed on-chain bytecode matches the local object file where: -h show this help text -n set the network (mainnet, testnet, devnet. defaults to \$NETWORK if set)" network=$NETWORK while getopts ':hn:' option; do case "$option" in h) echo "$usage" exit ;; n) network=$OPTARG ;; :) printf "missing argument for -%s\n" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; \?) printf "illegal option: -%s\n" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; esac done shift $((OPTIND - 1)) case "$network" in mainnet) moniker="m";; testnet) moniker="d";; devnet) moniker="l";; *) printf "Network not set. Specify with -n\n" >&2 echo "$usage" >&2 exit 1 ;; esac [ $# -ne 2 ] && { echo "$usage" >&2; exit 1; } obj_file=$1 sol_addr=$2 # Grab account content as JSON solana account $sol_addr -u $moniker --output-file /tmp/account.json --output json-compact >/dev/null # decode the base64 account data to binary cat /tmp/account.json | jq '.account.data[0]' | sed s/\"//g | base64 -d > /tmp/account.dump # The first 37 bytes are irrelevant, the actual ELF object code starts after, # so we drop these bytes. Presumably those bytes correspond to an encoded rust # enum constructor? # Set the block size to 37 bytes and skip the first block. dd bs=37 skip=1 if=/tmp/account.dump of=/tmp/bytecode.dump 2>/dev/null hash1=`sha256sum /tmp/bytecode.dump | cut -f1 -d' '` hash2=`sha256sum $obj_file | cut -f1 -d' '` echo "Deployed bytecode hash (on $network):" echo $hash1 echo "$obj_file hash:" echo $hash2 if [ "$hash1" == "$hash2" ]; then printf "\033[0;32mSuccessfully verified\033[0m\n"; exit 0; else printf "\033[0;31mFailed to verify\033[0m\n"; exit 1; fi