verify 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. set -euo pipefail
  3. usage="Usage:
  4. $(basename "$0") [-h] [-n network] [-c chain] [-w .wasm file] [-i code id || -a contract address] -- Verify that the deployed on-chain bytecode matches the local object file
  5. where:
  6. -h show this help text
  7. -n set the network (mainnet, testnet, devnet. defaults to \$NETWORK if set)
  8. -c set the chain (terra, terra2, injective, xpla)
  9. -w set the .wasm file to verify against
  10. -i set the code id of the stored wasm contract on chain
  11. -a set the on chain address of the contract to verify"
  12. chain=""
  13. network=""
  14. wasm=""
  15. code_id=""
  16. contract_addr=""
  17. if [[ ! -z "${NETWORK+x}" ]]; then
  18. network=$NETWORK
  19. fi
  20. while getopts ':hn:c:i:w:a:' option; do
  21. case "$option" in
  22. h) echo "$usage"
  23. exit
  24. ;;
  25. n) network=$OPTARG
  26. ;;
  27. c) chain=$OPTARG
  28. ;;
  29. w) wasm=$OPTARG
  30. ;;
  31. i) code_id=$OPTARG
  32. ;;
  33. a) contract_addr=$OPTARG
  34. ;;
  35. :) printf "missing argument for -%s\n" "$OPTARG" >&2
  36. echo "$usage" >&2
  37. exit 1
  38. ;;
  39. \?) printf "illegal option: -%s\n" "$OPTARG" >&2
  40. echo "$usage" >&2
  41. exit 1
  42. ;;
  43. esac
  44. done
  45. shift $((OPTIND - 1))
  46. if [[ -z $code_id && -z $contract_addr ]]; then
  47. printf "Need one of the -i and -a parameters to be specified.\n" >&2
  48. echo "$usage" >&2
  49. exit 1
  50. fi
  51. if [[ ! -z $code_id && ! -z $contract_addr ]]; then
  52. printf "Both of the -i and -a parameters cannot be specified.\n" >&2
  53. echo "$usage" >&2
  54. exit 1
  55. fi
  56. if [[ -z $chain || -z $network || -z $wasm ]]; then
  57. printf "The -c, -n, and -w parameters are required.\n" >&2
  58. echo "$usage" >&2
  59. exit 1
  60. fi
  61. url=""
  62. jq_cmd=""
  63. code_id_url=""
  64. code_id_jq=""
  65. # Fill in the above values based on network and chain
  66. case "$network" in
  67. mainnet)
  68. case "$chain" in
  69. terra) url="https://columbus-lcd.terra.dev/terra/wasm/v1beta1/codes/"
  70. jq_cmd="jq '.code_info.code_hash' -r | base64 -d | hexdump -v -e '/1 \"%02x\" '"
  71. code_id_url="https://columbus-lcd.terra.dev/terra/wasm/v1beta1/contracts/"
  72. code_id_jq="jq '.contract_info.code_id' -r "
  73. ;;
  74. terra2) url="https://phoenix-lcd.terra.dev/wasm/code/"
  75. jq_cmd="jq '.result.data_hash' -r "
  76. code_id_url="https://phoenix-lcd.terra.dev/wasm/contract/"
  77. code_id_jq="jq '.result.code_id' -r "
  78. ;;
  79. xpla) url="https://dimension-lcd.xpla.dev/cosmwasm/wasm/v1/code/"
  80. jq_cmd="jq '.code_info.data_hash' -r "
  81. code_id_url="https://dimension-lcd.xpla.dev/cosmwasm/wasm/v1/contract/"
  82. code_id_jq="jq '.contract_info.code_id' -r"
  83. ;;
  84. injective)
  85. url="https://k8s.mainnet.lcd.injective.network/cosmwasm/wasm/v1/code/"
  86. jq_cmd="jq '.code_info.data_hash' -r | cut -d 'x' -f2"
  87. code_id_url="https://k8s.mainnet.lcd.injective.network/cosmwasm/wasm/v1/contract/"
  88. code_id_jq="jq '.contract_info.code_id' -r"
  89. ;;
  90. esac ;;
  91. testnet)
  92. case "$chain" in
  93. terra2) url="https://pisco-lcd.terra.dev/wasm/code/"
  94. jq_cmd="jq '.result.data_hash' -r "
  95. code_id_url="https://pisco-lcd.terra.dev/wasm/contract/"
  96. code_id_jq="jq '.result.code_id' -r "
  97. ;;
  98. injective) url="https://k8s.testnet.exchange.grpc-web.injective.network/api/explorer/v1/wasm/codes/"
  99. jq_cmd="jq '.checksum.hash' -r | cut -d 'x' -f2"
  100. code_id_url="https://k8s.testnet.lcd.injective.network/cosmwasm/wasm/v1/contract/"
  101. code_id_jq="jq '.contract_info.code_id' -r"
  102. ;;
  103. xpla) url="https://cube-lcd.xpla.dev/cosmwasm/wasm/v1/code/"
  104. jq_cmd="jq '.code_info.data_hash' -r "
  105. code_id_url="https://cube-lcd.xpla.dev/cosmwasm/wasm/v1/contract/"
  106. code_id_jq="jq '.contract_info.code_id' -r "
  107. ;;
  108. esac ;;
  109. devnet)
  110. case "$chain" in
  111. terra) url="http://localhost:1317/terra/wasm/v1beta1/codes/"
  112. jq_cmd="jq '.code_info.code_hash' -r | base64 -d | hexdump -v -e '/1 \"%02x\" '"
  113. ;;
  114. terra2) url="http://localhost:1318/terra/wasm/v1beta1/codes/"
  115. jq_cmd="jq '.code_info.code_hash' -r | base64 -d | hexdump -v -e '/1 \"%02x\" '"
  116. ;;
  117. esac ;;
  118. *) printf "Network not set. Specify with -n\n" >&2
  119. echo "$usage" >&2
  120. exit 1
  121. ;;
  122. esac
  123. if [[ -z $url ]]; then
  124. printf "The combination of $network and $chain is not supported.\n" >&2
  125. exit 1
  126. fi
  127. if [[ ! -z $contract_addr && -z $code_id_url ]]; then
  128. printf "There is no contract to code_id conversion for the combination of $network and $chain.\n" >&2
  129. exit 1
  130. fi
  131. if [[ ! -z $contract_addr ]]; then
  132. code_id=`curl "$code_id_url$contract_addr" --silent | eval $code_id_jq`
  133. printf "Found code_id $code_id\n"
  134. fi
  135. hash1=`curl "$url$code_id" --silent | eval $jq_cmd | tr '[:upper:]' '[:lower:]'`
  136. hash2=`sha256sum $wasm | cut -f1 -d' ' | tr '[:upper:]' '[:lower:]'`
  137. echo "Deployed bytecode hash (on $network):"
  138. echo $hash1
  139. echo "$wasm hash:"
  140. echo $hash2
  141. if [[ "$hash1" == "$hash2" ]]; then
  142. printf "\033[0;32mSuccessfully verified\033[0m\n";
  143. exit 0;
  144. else
  145. printf "\033[0;31mFailed to verify\033[0m\n";
  146. exit 1;
  147. fi
  148. # Test Cases:
  149. # Terra:
  150. # Mainnet: wormhole code id = 557, tokenBridge code id = 6097
  151. # Mainnet: wormhole contract address = terra1dq03ugtd40zu9hcgdzrsq6z2z4hwhc9tqk2uy5
  152. # Mainnet: tokenBridge contract Address = terra10nmmwe8r3g99a9newtqa7a75xfgs2e8z87r2sf
  153. # Terra2:
  154. # Mainnet: wormhole code id = 146, tokenBridge code id = 151
  155. # Mainnet: wormhole contract address = terra12mrnzvhx3rpej6843uge2yyfppfyd3u9c3uq223q8sl48huz9juqffcnhp
  156. # Mainnet: tokenBridge contract address = terra153366q50k7t8nn7gec00hg66crnhkdggpgdtaxltaq6xrutkkz3s992fw9
  157. # Testnet: wormhole code id = 890, tokenBridge code id = 4773
  158. # Testnet: wormhole contract address = terra19nv3xr5lrmmr7egvrk2kqgw4kcn43xrtd5g0mpgwwvhetusk4k7s66jyv0
  159. # Testnet: tokenBridge contract address = terra1c02vds4uhgtrmcw7ldlg75zumdqxr8hwf7npseuf2h58jzhpgjxsgmwkvk
  160. # Injective:
  161. # Testnet: wormhole code id = 126, tokenBridge code id = 124
  162. # Testnet: wormhole contract address = inj1xx3aupmgv3ce537c0yce8zzd3sz567syuyedpg
  163. # Testnet: tokenBridge contract address = inj1q0e70vhrv063eah90mu97sazhywmeegp7myvnh
  164. # Xpla:
  165. # Mainnet: wormhole code id = 10, tokenBridge code id = 13
  166. # Mainnet: wormhole contract address = xpla1jn8qmdda5m6f6fqu9qv46rt7ajhklg40ukpqchkejcvy8x7w26cqxamv3w
  167. # Mainnet: tokenBridge contract address = xpla137w0wfch2dfmz7jl2ap8pcmswasj8kg06ay4dtjzw7tzkn77ufxqfw7acv
  168. # Testnet: wormhole code id = 53, tokenBridge code id = 135
  169. # Testnet: wormhole contract address = xpla1upkjn4mthr0047kahvn0llqx4qpqfn75lnph4jpxfn8walmm8mqsanyy35
  170. # Testnet: tokenBridge contract address = xpla1kek6zgdaxcsu35nqfsyvs2t9vs87dqkkq6hjdgczacysjn67vt8sern93x