solana: fix network handling in verify script (#1340)

This commit is contained in:
Csongor Kiss 2022-07-07 07:21:33 +01:00 committed by GitHub
parent ce87cb6a6e
commit 9b4ac93c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 22 deletions

View File

@ -2,28 +2,33 @@
set -euo pipefail
usage="Usage:
$(basename "$0") <.so file> <account address> [-h] [-n network] -- Verify that the deployed on-chain bytecode matches the local object file
function usage() {
cat<<-EOF >&2
Usage:
$(basename "$0") [-h] [-n network] <.so file> <account address> -- 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)"
where:
-h show this help text
-n set the network (mainnet, testnet, devnet. defaults to \$NETWORK if set)
EOF
exit 1
}
network=$NETWORK
network=""
if [[ -n $NETWORK ]]; then
network=$NETWORK
fi
while getopts ':hn:' option; do
case "$option" in
h) echo "$usage"
exit
h) usage
;;
n) network=$OPTARG
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
usage
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
usage
;;
esac
done
@ -34,19 +39,21 @@ case "$network" in
testnet) moniker="d";;
devnet) moniker="l";;
*) printf "Network not set. Specify with -n\n" >&2
echo "$usage" >&2
exit 1
usage
;;
esac
[ $# -ne 2 ] && { echo "$usage" >&2; exit 1; }
[ $# -ne 2 ] && usage
obj_file=$1
sol_addr=$2
account_json=$(mktemp)
account_dump=$(mktemp)
# Grab account content as JSON
solana account $sol_addr -u $moniker --output-file /tmp/account.json --output json-compact >/dev/null
solana account "$sol_addr" -u $moniker --output-file "$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
jq '.account.data[0]' "$account_json" | sed s/\"//g | base64 -d > "$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
@ -54,18 +61,18 @@ cat /tmp/account.json | jq '.account.data[0]' | sed s/\"//g | base64 -d > /tmp/a
# 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' '`
hash1=$(sha256sum /tmp/bytecode.dump | cut -f1 -d' ')
hash2=$(sha256sum "$obj_file" | cut -f1 -d' ')
echo "Deployed bytecode hash (on $network):"
echo $hash1
echo "$hash1"
echo "$obj_file hash:"
echo $hash2
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";
printf "\033[0;31mFailed to verify\033[0m\n" >&2;
exit 1;
fi