2022-04-01 08:49:54 -07:00
#!/usr/bin/env bash
# This script allows devnet initalization with more than one guardian.
# First argument is the number of guardians for the initial guardian set.
2022-04-08 13:54:04 -07:00
set -exuo pipefail
2022-04-01 08:49:54 -07:00
numGuardians = $1
echo " number of guardians to initialize: ${ numGuardians } "
addressesJson = "./scripts/devnet-consts.json"
2022-04-08 13:54:04 -07:00
# working files for accumulating state
envFile = "./scripts/.env.hex" # for generic hex data, for solana, terra, etc
ethFile = "./scripts/.env.0x" # for "0x" prefixed data, for ethereum scripts
# copy the eth defaults so we can override just the things we need
cp ./ethereum/.env.test $ethFile
# function for updating or inserting a KEY=value pair in a file.
function upsert_env_file {
file = ${ 1 } # file will be created if it does not exist.
key = ${ 2 } # line must start with the key.
new_value = ${ 3 }
# replace the value if it exists, else, append it to the file
if [ [ -f $file ] ] && grep -q " ^ $key = " $file ; then
# file has the key, update it:
2022-07-02 10:51:12 -07:00
if [ [ " $OSTYPE " = = "darwin" * ] ] ; then
# on macOS's sed, the -i flag needs the '' argument to not create
# backup files
sed -i '' -e " /^ $key =/s/=.*/= $new_value / " $file
else
sed -i -e " /^ $key =/s/=.*/= $new_value / " $file
fi
2022-04-08 13:54:04 -07:00
else
# file does not have the key, add it:
echo " $key = $new_value " >> $file
fi
}
# assert jq exists before trying to use it
if ! type -p jq; then
echo "ERROR: jq is not installed" ! >& 2
exit 1
fi
2022-04-01 08:49:54 -07:00
2022-07-02 10:52:28 -07:00
# Rebuild the CLI binary if needed. If the binary is already up to date, this
# command finishes in a fraction of a second.
make build -C ./clients/js
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# 1) guardian public keys - used as the inital guardian set when initializing contracts.
echo "generating guardian set addresses"
# create an array of strings containing the ECDSA public keys of the devnet guardians in the guardianset:
2022-04-01 08:49:54 -07:00
# guardiansPublicEth has the leading "0x" that Eth scripts expect.
guardiansPublicEth = $( jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].public]' $addressesJson )
# guardiansPublicHex does not have a leading "0x", just hex strings.
guardiansPublicHex = $( jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].public[2:]]' $addressesJson )
2022-04-08 13:54:04 -07:00
# also make a CSV string of the hex addresses, so the client scripts that need that format don't have to.
guardiansPublicHexCSV = $( echo ${ guardiansPublicHex } | jq --raw-output -c '. | join(",")' )
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# write the lists of addresses to the env files
initSigners = "INIT_SIGNERS"
upsert_env_file $ethFile $initSigners $guardiansPublicEth
upsert_env_file $envFile $initSigners $guardiansPublicHex
upsert_env_file $envFile "INIT_SIGNERS_CSV" $guardiansPublicHexCSV
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# 2) guardian private keys - used for generating the initial governance VAAs (register token bridge & nft bridge contracts on each chain).
echo "generating guardian set keys"
2022-04-01 08:49:54 -07:00
# create an array of strings containing the private keys of the devnet guardians in the guardianset
guardiansPrivate = $( jq -c --argjson lastIndex $numGuardians '.devnetGuardians[:$lastIndex] | [.[].private]' $addressesJson )
# create a CSV string with the private keys of the guardians in the guardianset, that will be used to create registration VAAs
guardiansPrivateCSV = $( echo ${ guardiansPrivate } | jq --raw-output -c '. | join(",")' )
2022-04-08 13:54:04 -07:00
# write the lists of keys to the env files
upsert_env_file $ethFile "INIT_SIGNERS_KEYS_JSON" $guardiansPrivate
upsert_env_file $envFile "INIT_SIGNERS_KEYS_CSV" $guardiansPrivateCSV
# 3) fetch and store the contract addresses that we need to make contract registration governance VAAs for:
echo " getting contract addresses for chain registrations from $addressesJson "
2022-04-01 08:49:54 -07:00
# get addresses from the constants file
solTokenBridge = $( jq --raw-output '.chains."1".contracts.tokenBridgeEmitterAddress' $addressesJson )
ethTokenBridge = $( jq --raw-output '.chains."2".contracts.tokenBridgeEmitterAddress' $addressesJson )
terraTokenBridge = $( jq --raw-output '.chains."3".contracts.tokenBridgeEmitterAddress' $addressesJson )
bscTokenBridge = $( jq --raw-output '.chains."4".contracts.tokenBridgeEmitterAddress' $addressesJson )
2022-04-29 12:53:48 -07:00
algoTokenBridge = $( jq --raw-output '.chains."8".contracts.tokenBridgeEmitterAddress' $addressesJson )
2022-08-04 08:52:26 -07:00
nearTokenBridge = $( jq --raw-output '.chains."15".contracts.tokenBridgeEmitterAddress' $addressesJson )
2022-06-07 08:36:21 -07:00
terra2TokenBridge = $( jq --raw-output '.chains."18".contracts.tokenBridgeEmitterAddress' $addressesJson )
2022-09-02 01:36:24 -07:00
wormchainTokenBridge = $( jq --raw-output '.chains."3104".contracts.tokenBridgeEmitterAddress' $addressesJson )
2022-04-01 08:49:54 -07:00
solNFTBridge = $( jq --raw-output '.chains."1".contracts.nftBridgeEmitterAddress' $addressesJson )
ethNFTBridge = $( jq --raw-output '.chains."2".contracts.nftBridgeEmitterAddress' $addressesJson )
terraNFTBridge = $( jq --raw-output '.chains."3".contracts.nftBridgeEmitterAddress' $addressesJson )
2022-08-04 08:52:26 -07:00
nearNFTBridge = $( jq --raw-output '.chains."15".contracts.nftBridgeEmitterAddress' $addressesJson )
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# 4) create token bridge registration VAAs
2022-07-02 10:52:28 -07:00
# invoke CLI commands to create registration VAAs
2022-10-06 08:24:43 -07:00
solTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c solana -a ${ solTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
ethTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c ethereum -a ${ ethTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
terraTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c terra -a ${ terraTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
bscTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c bsc -a ${ bscTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
algoTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c algorand -a ${ algoTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
nearTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c near -a ${ nearTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
terra2TokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c terra2 -a ${ terra2TokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
wormchainTokenBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m TokenBridge -c wormholechain -a ${ wormchainTokenBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# 5) create nft bridge registration VAAs
echo "generating contract registration VAAs for nft bridges"
2022-10-06 08:24:43 -07:00
solNFTBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m NFTBridge -c solana -a ${ solNFTBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
ethNFTBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m NFTBridge -c ethereum -a ${ ethNFTBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
terraNFTBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m NFTBridge -c terra -a ${ terraNFTBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
nearNFTBridgeVAA = ` echo $( node ./clients/js/build/main.js generate registration -m NFTBridge -c near -a ${ nearNFTBridge } -g ${ guardiansPrivateCSV } ) | sed 's/secp256k1.*version//' `
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# 6) write the registration VAAs to env files
echo "writing VAAs to .env files"
# define the keys that will hold the chain registration governance VAAs
solTokenBridge = "REGISTER_SOL_TOKEN_BRIDGE_VAA"
ethTokenBridge = "REGISTER_ETH_TOKEN_BRIDGE_VAA"
terraTokenBridge = "REGISTER_TERRA_TOKEN_BRIDGE_VAA"
bscTokenBridge = "REGISTER_BSC_TOKEN_BRIDGE_VAA"
2022-04-29 12:53:48 -07:00
algoTokenBridge = "REGISTER_ALGO_TOKEN_BRIDGE_VAA"
2022-06-07 08:36:21 -07:00
terra2TokenBridge = "REGISTER_TERRA2_TOKEN_BRIDGE_VAA"
2022-08-04 08:52:26 -07:00
nearTokenBridge = "REGISTER_NEAR_TOKEN_BRIDGE_VAA"
2022-09-02 01:36:24 -07:00
wormchainTokenBridge = "REGISTER_WORMCHAIN_TOKEN_BRIDGE_VAA"
2022-04-08 13:54:04 -07:00
solNFTBridge = "REGISTER_SOL_NFT_BRIDGE_VAA"
ethNFTBridge = "REGISTER_ETH_NFT_BRIDGE_VAA"
terraNFTBridge = "REGISTER_TERRA_NFT_BRIDGE_VAA"
2022-08-04 08:52:26 -07:00
nearNFTBridge = "REGISTER_NEAR_NFT_BRIDGE_VAA"
2022-04-08 13:54:04 -07:00
2022-04-01 08:49:54 -07:00
# solana token bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $solTokenBridge $solTokenBridgeVAA
upsert_env_file $envFile $solTokenBridge $solTokenBridgeVAA
2022-04-01 08:49:54 -07:00
# solana nft bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $solNFTBridge $solNFTBridgeVAA
upsert_env_file $envFile $solNFTBridge $solNFTBridgeVAA
2022-04-01 08:49:54 -07:00
# ethereum token bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $ethTokenBridge $ethTokenBridgeVAA
upsert_env_file $envFile $ethTokenBridge $ethTokenBridgeVAA
2022-04-01 08:49:54 -07:00
# ethereum nft bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $ethNFTBridge $ethNFTBridgeVAA
upsert_env_file $envFile $ethNFTBridge $ethNFTBridgeVAA
2022-04-01 08:49:54 -07:00
# terra token bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $terraTokenBridge $terraTokenBridgeVAA
upsert_env_file $envFile $terraTokenBridge $terraTokenBridgeVAA
2022-04-01 08:49:54 -07:00
# terra nft bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $terraNFTBridge $terraNFTBridgeVAA
upsert_env_file $envFile $terraNFTBridge $terraNFTBridgeVAA
2022-04-01 08:49:54 -07:00
# bsc token bridge
2022-04-08 13:54:04 -07:00
upsert_env_file $ethFile $bscTokenBridge $bscTokenBridgeVAA
upsert_env_file $envFile $bscTokenBridge $bscTokenBridgeVAA
2022-04-01 08:49:54 -07:00
2022-04-29 12:53:48 -07:00
# algo token bridge
upsert_env_file $ethFile $algoTokenBridge $algoTokenBridgeVAA
upsert_env_file $envFile $algoTokenBridge $algoTokenBridgeVAA
2022-06-07 08:36:21 -07:00
# terra2 token bridge
upsert_env_file $ethFile $terra2TokenBridge $terra2TokenBridgeVAA
upsert_env_file $envFile $terra2TokenBridge $terra2TokenBridgeVAA
2022-08-04 08:52:26 -07:00
# near token bridge
upsert_env_file $ethFile $nearTokenBridge $nearTokenBridgeVAA
upsert_env_file $envFile $nearTokenBridge $nearTokenBridgeVAA
# near nft bridge
upsert_env_file $ethFile $nearNFTBridge $nearNFTBridgeVAA
upsert_env_file $envFile $nearNFTBridge $nearNFTBridgeVAA
2022-04-01 08:49:54 -07:00
2022-09-02 01:36:24 -07:00
# wormchain token bridge
upsert_env_file $ethFile $wormchainTokenBridge $wormchainTokenBridgeVAA
upsert_env_file $envFile $wormchainTokenBridge $wormchainTokenBridgeVAA
2022-04-08 13:54:04 -07:00
# 7) copy the local .env file to the solana & terra dirs, if the script is running on the host machine
# chain dirs will not exist if running in docker for Tilt, only if running locally. check before copying.
# copy ethFile to ethereum
if [ [ -d ./ethereum ] ] ; then
echo " copying $ethFile to /etherum/.env "
cp $ethFile ./ethereum/.env
fi
2022-04-01 08:49:54 -07:00
2022-04-08 13:54:04 -07:00
# copy the hex envFile to each of the non-EVM chains
2022-08-04 08:52:26 -07:00
for envDest in ./solana/.env ./terra/tools/.env ./cosmwasm/tools/.env ./algorand/.env ./near/.env; do
2022-04-08 13:54:04 -07:00
dirname = $( dirname $envDest )
if [ [ -d " $dirname " ] ] ; then
echo " copying $envFile to $envDest "
cp $envFile $envDest
fi
done
2022-04-01 08:49:54 -07:00
echo "guardian set init complete!"