guardian-set-init.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env bash
  2. # This script allows devnet initalization with more than one guardian.
  3. # First argument is the number of guardians for the initial guardian set.
  4. set -exuo pipefail
  5. numGuardians=$1
  6. echo "number of guardians to initialize: ${numGuardians}"
  7. addressesJson="./scripts/devnet-consts.json"
  8. # working files for accumulating state
  9. envFile="./scripts/.env.hex" # for generic hex data, for solana, terra, etc
  10. ethFile="./scripts/.env.0x" # for "0x" prefixed data, for ethereum scripts
  11. # copy the eth defaults so we can override just the things we need
  12. cp ./ethereum/.env.test $ethFile
  13. # function for updating or inserting a KEY=value pair in a file.
  14. function upsert_env_file {
  15. file=${1} # file will be created if it does not exist.
  16. key=${2} # line must start with the key.
  17. new_value=${3}
  18. # replace the value if it exists, else, append it to the file
  19. if [[ -f $file ]] && grep -q "^$key=" $file; then
  20. # file has the key, update it:
  21. if [[ "$OSTYPE" == "darwin"* ]]; then
  22. # on macOS's sed, the -i flag needs the '' argument to not create
  23. # backup files
  24. sed -i '' -e "/^$key=/s/=.*/=$new_value/" $file
  25. else
  26. sed -i -e "/^$key=/s/=.*/=$new_value/" $file
  27. fi
  28. else
  29. # file does not have the key, add it:
  30. echo "$key=$new_value" >> $file
  31. fi
  32. }
  33. echo "# This file was auto-generated by $(basename $0). Do not modify by hand!" >> $ethFile
  34. echo "# This file was auto-generated by $(basename $0). Do not modify by hand!" >> $envFile
  35. # assert jq exists before trying to use it
  36. if ! type -p jq; then
  37. echo "ERROR: jq is not installed"! >&2
  38. exit 1
  39. fi
  40. # Rebuild the CLI binary if needed. If the binary is already up to date, this
  41. # command finishes in a fraction of a second.
  42. make build -C ./clients/js
  43. # 1) guardian public keys - used as the inital guardian set when initializing contracts.
  44. echo "generating guardian set addresses"
  45. # create an array of strings containing the ECDSA public keys of the devnet guardians in the guardianset:
  46. # guardiansPublicEth has the leading "0x" that Eth scripts expect.
  47. guardiansPublicEth=$(jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].public]' $addressesJson)
  48. # guardiansPublicHex does not have a leading "0x", just hex strings.
  49. guardiansPublicHex=$(jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].public[2:]]' $addressesJson)
  50. # also make a CSV string of the hex addresses, so the client scripts that need that format don't have to.
  51. guardiansPublicHexCSV=$(echo ${guardiansPublicHex} | jq --raw-output -c '. | join(",")')
  52. # write the lists of addresses to the env files
  53. initSigners="INIT_SIGNERS"
  54. upsert_env_file $ethFile $initSigners $guardiansPublicEth
  55. upsert_env_file $envFile $initSigners $guardiansPublicHex
  56. upsert_env_file $envFile "INIT_SIGNERS_CSV" $guardiansPublicHexCSV
  57. # 2) guardian private keys - used for generating the initial governance VAAs (register token bridge & nft bridge contracts on each chain).
  58. echo "generating guardian set keys"
  59. # create an array of strings containing the private keys of the devnet guardians in the guardianset
  60. guardiansPrivate=$(jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].private]' $addressesJson)
  61. # create a CSV string with the private keys of the guardians in the guardianset, that will be used to create registration VAAs
  62. guardiansPrivateCSV=$( echo ${guardiansPrivate} | jq --raw-output -c '. | join(",")')
  63. # write the lists of keys to the env files
  64. upsert_env_file $ethFile "INIT_SIGNERS_KEYS_JSON" $guardiansPrivate
  65. upsert_env_file $envFile "INIT_SIGNERS_KEYS_CSV" $guardiansPrivateCSV
  66. # 3) fetch and store the contract addresses that we need to make contract registration governance VAAs for:
  67. echo "getting contract addresses for chain registrations from $addressesJson"
  68. # get addresses from the constants file
  69. solTokenBridge=$(jq --raw-output '.chains."1".contracts.tokenBridgeEmitterAddress' $addressesJson)
  70. ethTokenBridge=$(jq --raw-output '.chains."2".contracts.tokenBridgeEmitterAddress' $addressesJson)
  71. terraTokenBridge=$(jq --raw-output '.chains."3".contracts.tokenBridgeEmitterAddress' $addressesJson)
  72. bscTokenBridge=$(jq --raw-output '.chains."4".contracts.tokenBridgeEmitterAddress' $addressesJson)
  73. algoTokenBridge=$(jq --raw-output '.chains."8".contracts.tokenBridgeEmitterAddress' $addressesJson)
  74. nearTokenBridge=$(jq --raw-output '.chains."15".contracts.tokenBridgeEmitterAddress' $addressesJson)
  75. terra2TokenBridge=$(jq --raw-output '.chains."18".contracts.tokenBridgeEmitterAddress' $addressesJson)
  76. wormchainTokenBridge=$(jq --raw-output '.chains."3104".contracts.tokenBridgeEmitterAddress' $addressesJson)
  77. aptosTokenBridge=$(jq --raw-output '.chains."22".contracts.tokenBridgeEmitterAddress' $addressesJson)
  78. solNFTBridge=$(jq --raw-output '.chains."1".contracts.nftBridgeEmitterAddress' $addressesJson)
  79. ethNFTBridge=$(jq --raw-output '.chains."2".contracts.nftBridgeEmitterAddress' $addressesJson)
  80. terraNFTBridge=$(jq --raw-output '.chains."3".contracts.nftBridgeEmitterAddress' $addressesJson)
  81. nearNFTBridge=$(jq --raw-output '.chains."15".contracts.nftBridgeEmitterAddress' $addressesJson)
  82. aptosNFTBridge=$(jq --raw-output '.chains."22".contracts.nftBridgeEmitterAddress' $addressesJson)
  83. # 4) create token bridge registration VAAs
  84. # invoke CLI commands to create registration VAAs
  85. solTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c solana -a ${solTokenBridge} -g ${guardiansPrivateCSV})
  86. ethTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c ethereum -a ${ethTokenBridge} -g ${guardiansPrivateCSV} )
  87. terraTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c terra -a ${terraTokenBridge} -g ${guardiansPrivateCSV})
  88. bscTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c bsc -a ${bscTokenBridge} -g ${guardiansPrivateCSV})
  89. algoTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c algorand -a ${algoTokenBridge} -g ${guardiansPrivateCSV})
  90. nearTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c near -a ${nearTokenBridge} -g ${guardiansPrivateCSV})
  91. terra2TokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c terra2 -a ${terra2TokenBridge} -g ${guardiansPrivateCSV})
  92. wormchainTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c wormchain -a ${wormchainTokenBridge} -g ${guardiansPrivateCSV})
  93. aptosTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c aptos -a ${aptosTokenBridge} -g ${guardiansPrivateCSV})
  94. # 5) create nft bridge registration VAAs
  95. echo "generating contract registration VAAs for nft bridges"
  96. solNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c solana -a ${solNFTBridge} -g ${guardiansPrivateCSV})
  97. ethNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c ethereum -a ${ethNFTBridge} -g ${guardiansPrivateCSV})
  98. terraNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c terra -a ${terraNFTBridge} -g ${guardiansPrivateCSV})
  99. nearNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c near -a ${nearNFTBridge} -g ${guardiansPrivateCSV})
  100. aptosNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c aptos -a ${aptosNFTBridge} -g ${guardiansPrivateCSV})
  101. # 6) write the registration VAAs to env files
  102. echo "writing VAAs to .env files"
  103. # define the keys that will hold the chain registration governance VAAs
  104. solTokenBridge="REGISTER_SOL_TOKEN_BRIDGE_VAA"
  105. ethTokenBridge="REGISTER_ETH_TOKEN_BRIDGE_VAA"
  106. terraTokenBridge="REGISTER_TERRA_TOKEN_BRIDGE_VAA"
  107. bscTokenBridge="REGISTER_BSC_TOKEN_BRIDGE_VAA"
  108. algoTokenBridge="REGISTER_ALGO_TOKEN_BRIDGE_VAA"
  109. terra2TokenBridge="REGISTER_TERRA2_TOKEN_BRIDGE_VAA"
  110. nearTokenBridge="REGISTER_NEAR_TOKEN_BRIDGE_VAA"
  111. wormchainTokenBridge="REGISTER_WORMCHAIN_TOKEN_BRIDGE_VAA"
  112. aptosTokenBridge="REGISTER_APTOS_TOKEN_BRIDGE_VAA"
  113. solNFTBridge="REGISTER_SOL_NFT_BRIDGE_VAA"
  114. ethNFTBridge="REGISTER_ETH_NFT_BRIDGE_VAA"
  115. terraNFTBridge="REGISTER_TERRA_NFT_BRIDGE_VAA"
  116. nearNFTBridge="REGISTER_NEAR_NFT_BRIDGE_VAA"
  117. aptosNFTBridge="REGISTER_APTOS_NFT_BRIDGE_VAA"
  118. # solana token bridge
  119. upsert_env_file $ethFile $solTokenBridge $solTokenBridgeVAA
  120. upsert_env_file $envFile $solTokenBridge $solTokenBridgeVAA
  121. # solana nft bridge
  122. upsert_env_file $ethFile $solNFTBridge $solNFTBridgeVAA
  123. upsert_env_file $envFile $solNFTBridge $solNFTBridgeVAA
  124. # ethereum token bridge
  125. upsert_env_file $ethFile $ethTokenBridge $ethTokenBridgeVAA
  126. upsert_env_file $envFile $ethTokenBridge $ethTokenBridgeVAA
  127. # ethereum nft bridge
  128. upsert_env_file $ethFile $ethNFTBridge $ethNFTBridgeVAA
  129. upsert_env_file $envFile $ethNFTBridge $ethNFTBridgeVAA
  130. # terra token bridge
  131. upsert_env_file $ethFile $terraTokenBridge $terraTokenBridgeVAA
  132. upsert_env_file $envFile $terraTokenBridge $terraTokenBridgeVAA
  133. # terra nft bridge
  134. upsert_env_file $ethFile $terraNFTBridge $terraNFTBridgeVAA
  135. upsert_env_file $envFile $terraNFTBridge $terraNFTBridgeVAA
  136. # bsc token bridge
  137. upsert_env_file $ethFile $bscTokenBridge $bscTokenBridgeVAA
  138. upsert_env_file $envFile $bscTokenBridge $bscTokenBridgeVAA
  139. # algo token bridge
  140. upsert_env_file $ethFile $algoTokenBridge $algoTokenBridgeVAA
  141. upsert_env_file $envFile $algoTokenBridge $algoTokenBridgeVAA
  142. # terra2 token bridge
  143. upsert_env_file $ethFile $terra2TokenBridge $terra2TokenBridgeVAA
  144. upsert_env_file $envFile $terra2TokenBridge $terra2TokenBridgeVAA
  145. # aptos token bridge
  146. upsert_env_file $ethFile $aptosTokenBridge $aptosTokenBridgeVAA
  147. upsert_env_file $envFile $aptosTokenBridge $aptosTokenBridgeVAA
  148. # aptos nft bridge
  149. upsert_env_file $ethFile $aptosNFTBridge $aptosNFTBridgeVAA
  150. upsert_env_file $envFile $aptosNFTBridge $aptosNFTBridgeVAA
  151. # near token bridge
  152. upsert_env_file $ethFile $nearTokenBridge $nearTokenBridgeVAA
  153. upsert_env_file $envFile $nearTokenBridge $nearTokenBridgeVAA
  154. # near nft bridge
  155. upsert_env_file $ethFile $nearNFTBridge $nearNFTBridgeVAA
  156. upsert_env_file $envFile $nearNFTBridge $nearNFTBridgeVAA
  157. # wormchain token bridge
  158. upsert_env_file $ethFile $wormchainTokenBridge $wormchainTokenBridgeVAA
  159. upsert_env_file $envFile $wormchainTokenBridge $wormchainTokenBridgeVAA
  160. # 7) copy the local .env file to the solana & terra dirs, if the script is running on the host machine
  161. # chain dirs will not exist if running in docker for Tilt, only if running locally. check before copying.
  162. # copy ethFile to ethereum
  163. if [[ -d ./ethereum ]]; then
  164. echo "copying $ethFile to /etherum/.env"
  165. cp $ethFile ./ethereum/.env
  166. fi
  167. # copy the hex envFile to each of the non-EVM chains
  168. paths=(
  169. ./algorand/.env
  170. ./near/.env
  171. ./solana/.env
  172. ./terra/tools/.env
  173. ./cosmwasm/deployment/terra2/tools/.env
  174. )
  175. for envDest in "${paths[@]}"; do
  176. dirname=$(dirname $envDest)
  177. if [[ -d "$dirname" ]]; then
  178. echo "copying $envFile to $envDest"
  179. cp $envFile $envDest
  180. fi
  181. done
  182. echo "guardian set init complete!"