guardian-set-init.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. # assert jq exists before trying to use it
  34. if ! type -p jq; then
  35. echo "ERROR: jq is not installed"! >&2
  36. exit 1
  37. fi
  38. # Rebuild the CLI binary if needed. If the binary is already up to date, this
  39. # command finishes in a fraction of a second.
  40. make build -C ./clients/js
  41. # 1) guardian public keys - used as the inital guardian set when initializing contracts.
  42. echo "generating guardian set addresses"
  43. # create an array of strings containing the ECDSA public keys of the devnet guardians in the guardianset:
  44. # guardiansPublicEth has the leading "0x" that Eth scripts expect.
  45. guardiansPublicEth=$(jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].public]' $addressesJson)
  46. # guardiansPublicHex does not have a leading "0x", just hex strings.
  47. guardiansPublicHex=$(jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].public[2:]]' $addressesJson)
  48. # also make a CSV string of the hex addresses, so the client scripts that need that format don't have to.
  49. guardiansPublicHexCSV=$(echo ${guardiansPublicHex} | jq --raw-output -c '. | join(",")')
  50. # write the lists of addresses to the env files
  51. initSigners="INIT_SIGNERS"
  52. upsert_env_file $ethFile $initSigners $guardiansPublicEth
  53. upsert_env_file $envFile $initSigners $guardiansPublicHex
  54. upsert_env_file $envFile "INIT_SIGNERS_CSV" $guardiansPublicHexCSV
  55. # 2) guardian private keys - used for generating the initial governance VAAs (register token bridge & nft bridge contracts on each chain).
  56. echo "generating guardian set keys"
  57. # create an array of strings containing the private keys of the devnet guardians in the guardianset
  58. guardiansPrivate=$(jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].private]' $addressesJson)
  59. # create a CSV string with the private keys of the guardians in the guardianset, that will be used to create registration VAAs
  60. guardiansPrivateCSV=$( echo ${guardiansPrivate} | jq --raw-output -c '. | join(",")')
  61. # write the lists of keys to the env files
  62. upsert_env_file $ethFile "INIT_SIGNERS_KEYS_JSON" $guardiansPrivate
  63. upsert_env_file $envFile "INIT_SIGNERS_KEYS_CSV" $guardiansPrivateCSV
  64. # 3) fetch and store the contract addresses that we need to make contract registration governance VAAs for:
  65. echo "getting contract addresses for chain registrations from $addressesJson"
  66. # get addresses from the constants file
  67. solTokenBridge=$(jq --raw-output '.chains."1".contracts.tokenBridgeEmitterAddress' $addressesJson)
  68. ethTokenBridge=$(jq --raw-output '.chains."2".contracts.tokenBridgeEmitterAddress' $addressesJson)
  69. terraTokenBridge=$(jq --raw-output '.chains."3".contracts.tokenBridgeEmitterAddress' $addressesJson)
  70. bscTokenBridge=$(jq --raw-output '.chains."4".contracts.tokenBridgeEmitterAddress' $addressesJson)
  71. algoTokenBridge=$(jq --raw-output '.chains."8".contracts.tokenBridgeEmitterAddress' $addressesJson)
  72. terra2TokenBridge=$(jq --raw-output '.chains."18".contracts.tokenBridgeEmitterAddress' $addressesJson)
  73. solNFTBridge=$(jq --raw-output '.chains."1".contracts.nftBridgeEmitterAddress' $addressesJson)
  74. ethNFTBridge=$(jq --raw-output '.chains."2".contracts.nftBridgeEmitterAddress' $addressesJson)
  75. terraNFTBridge=$(jq --raw-output '.chains."3".contracts.nftBridgeEmitterAddress' $addressesJson)
  76. # 4) create token bridge registration VAAs
  77. # invoke CLI commands to create registration VAAs
  78. solTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c solana -a ${solTokenBridge} -g ${guardiansPrivateCSV})
  79. ethTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c ethereum -a ${ethTokenBridge} -g ${guardiansPrivateCSV} )
  80. terraTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c terra -a ${terraTokenBridge} -g ${guardiansPrivateCSV})
  81. bscTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c bsc -a ${bscTokenBridge} -g ${guardiansPrivateCSV})
  82. algoTokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c algorand -a ${algoTokenBridge} -g ${guardiansPrivateCSV})
  83. terra2TokenBridgeVAA=$(node ./clients/js/build/main.js generate registration -m TokenBridge -c terra2 -a ${terra2TokenBridge} -g ${guardiansPrivateCSV})
  84. # 5) create nft bridge registration VAAs
  85. echo "generating contract registration VAAs for nft bridges"
  86. solNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c solana -a ${solNFTBridge} -g ${guardiansPrivateCSV})
  87. ethNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c ethereum -a ${ethNFTBridge} -g ${guardiansPrivateCSV})
  88. terraNFTBridgeVAA=$(node ./clients/js/build/main.js generate registration -m NFTBridge -c terra -a ${terraNFTBridge} -g ${guardiansPrivateCSV})
  89. # 6) write the registration VAAs to env files
  90. echo "writing VAAs to .env files"
  91. # define the keys that will hold the chain registration governance VAAs
  92. solTokenBridge="REGISTER_SOL_TOKEN_BRIDGE_VAA"
  93. ethTokenBridge="REGISTER_ETH_TOKEN_BRIDGE_VAA"
  94. terraTokenBridge="REGISTER_TERRA_TOKEN_BRIDGE_VAA"
  95. bscTokenBridge="REGISTER_BSC_TOKEN_BRIDGE_VAA"
  96. algoTokenBridge="REGISTER_ALGO_TOKEN_BRIDGE_VAA"
  97. terra2TokenBridge="REGISTER_TERRA2_TOKEN_BRIDGE_VAA"
  98. solNFTBridge="REGISTER_SOL_NFT_BRIDGE_VAA"
  99. ethNFTBridge="REGISTER_ETH_NFT_BRIDGE_VAA"
  100. terraNFTBridge="REGISTER_TERRA_NFT_BRIDGE_VAA"
  101. # solana token bridge
  102. upsert_env_file $ethFile $solTokenBridge $solTokenBridgeVAA
  103. upsert_env_file $envFile $solTokenBridge $solTokenBridgeVAA
  104. # solana nft bridge
  105. upsert_env_file $ethFile $solNFTBridge $solNFTBridgeVAA
  106. upsert_env_file $envFile $solNFTBridge $solNFTBridgeVAA
  107. # ethereum token bridge
  108. upsert_env_file $ethFile $ethTokenBridge $ethTokenBridgeVAA
  109. upsert_env_file $envFile $ethTokenBridge $ethTokenBridgeVAA
  110. # ethereum nft bridge
  111. upsert_env_file $ethFile $ethNFTBridge $ethNFTBridgeVAA
  112. upsert_env_file $envFile $ethNFTBridge $ethNFTBridgeVAA
  113. # terra token bridge
  114. upsert_env_file $ethFile $terraTokenBridge $terraTokenBridgeVAA
  115. upsert_env_file $envFile $terraTokenBridge $terraTokenBridgeVAA
  116. # terra nft bridge
  117. upsert_env_file $ethFile $terraNFTBridge $terraNFTBridgeVAA
  118. upsert_env_file $envFile $terraNFTBridge $terraNFTBridgeVAA
  119. # bsc token bridge
  120. upsert_env_file $ethFile $bscTokenBridge $bscTokenBridgeVAA
  121. upsert_env_file $envFile $bscTokenBridge $bscTokenBridgeVAA
  122. # algo token bridge
  123. upsert_env_file $ethFile $algoTokenBridge $algoTokenBridgeVAA
  124. upsert_env_file $envFile $algoTokenBridge $algoTokenBridgeVAA
  125. # terra2 token bridge
  126. upsert_env_file $ethFile $terra2TokenBridge $terra2TokenBridgeVAA
  127. upsert_env_file $envFile $terra2TokenBridge $terra2TokenBridgeVAA
  128. # 7) copy the local .env file to the solana & terra dirs, if the script is running on the host machine
  129. # chain dirs will not exist if running in docker for Tilt, only if running locally. check before copying.
  130. # copy ethFile to ethereum
  131. if [[ -d ./ethereum ]]; then
  132. echo "copying $ethFile to /etherum/.env"
  133. cp $ethFile ./ethereum/.env
  134. fi
  135. # copy the hex envFile to each of the non-EVM chains
  136. for envDest in ./solana/.env ./terra/tools/.env ./cosmwasm/tools/.env ./algorand/.env; do
  137. dirname=$(dirname $envDest)
  138. if [[ -d "$dirname" ]]; then
  139. echo "copying $envFile to $envDest"
  140. cp $envFile $envDest
  141. fi
  142. done
  143. echo "guardian set init complete!"