verify 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. set -euo pipefail
  3. function usage() {
  4. cat <<EOF >&2
  5. Usage:
  6. $(basename "$0") [-h] [-n network] [-r rpc] [-c chain] <.json file> <contract address> -- Verify that the deployed on-chain bytecode matches the local build artifact
  7. where:
  8. -h show this help text
  9. -n set the network (mainnet, testnet, devnet)
  10. -r rpc url
  11. -c set the chain name (required)"
  12. The -n and -r flags are mutually exclusive.
  13. EOF
  14. exit 1
  15. }
  16. chain=""
  17. network=""
  18. rpc=""
  19. while getopts ':hn:r:c:' option; do
  20. case "$option" in
  21. h) usage
  22. ;;
  23. c) chain=$OPTARG
  24. ;;
  25. n) network=$OPTARG
  26. ;;
  27. r) rpc=$OPTARG
  28. ;;
  29. :) printf "missing argument for -%s\n" "$OPTARG" >&2
  30. usage
  31. ;;
  32. \?) printf "illegal option: -%s\n" "$OPTARG" >&2
  33. usage
  34. ;;
  35. esac
  36. done
  37. shift $((OPTIND - 1))
  38. [ $# -ne 2 ] && usage
  39. [[ -z $chain ]] && { echo "Missing chain flag (-c)"; usage; }
  40. json_file=$1
  41. contract_addr=$2
  42. # network and rpc flags are mutually exlusive
  43. [[ -n $network && -n $rpc ]] && usage
  44. # if network flag is set, we query the rpc from the cli tool.
  45. if [[ -n $network ]]; then
  46. if ! command -v worm &> /dev/null
  47. then
  48. echo "worm binary could not be found. See installation instructions in clients/js/README.md"
  49. exit 1
  50. fi
  51. rpc=$(worm rpc "$network" "$chain")
  52. fi
  53. if [[ -z $rpc ]]; then
  54. echo "rpc endpoint or network name required."
  55. usage
  56. fi
  57. # We'll write the bytecodes to temporary files
  58. deployed=$(mktemp)
  59. local=$(mktemp)
  60. cat "$json_file" | jq -r .deployedBytecode > "$local"
  61. ret=0
  62. # Grab bytecode from the JSON RPC using the eth_getCode method.
  63. curl "$rpc" \
  64. -X POST \
  65. -H "Content-Type: application/json" \
  66. --data "{\"method\":\"eth_getCode\",\"params\":[\"$contract_addr\",\"latest\"],\"id\":1,\"jsonrpc\":\"2.0\"}" --silent | jq -r .result > "$deployed" || ret=$?
  67. if [ $ret -gt 0 ]; then
  68. printf "\033[0;31mFailed to query eth RPC '%s' while verifying %s on %s\033[0m\n" "$rpc" "$contract_addr" "$chain"
  69. exit 1
  70. fi
  71. # hash, then see if they match up
  72. hash1=$(sha256sum "$deployed" | cut -f1 -d' ')
  73. hash2=$(sha256sum "$local" | cut -f1 -d' ')
  74. if [ "$hash1" == "$hash2" ]; then
  75. printf "\033[0;32mDeployed bytecode of %s on %s matches %s\033[0m\n" "$contract_addr" "$chain" "$json_file";
  76. exit 0;
  77. else
  78. printf "\033[0;31mDeployed bytecode of %s on %s doesn't match %s\033[0m\n" "$contract_addr" "$chain" "$json_file";
  79. echo "deployed hash:"
  80. echo "$hash1"
  81. echo "$json_file hash:"
  82. echo "$hash2"
  83. exit 1;
  84. fi