admintemplate.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package guardiand
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. "github.com/spf13/cobra"
  8. "google.golang.org/protobuf/encoding/prototext"
  9. "github.com/certusone/wormhole/bridge/pkg/devnet"
  10. nodev1 "github.com/certusone/wormhole/bridge/pkg/proto/node/v1"
  11. )
  12. var setUpdateNumGuardians *int
  13. var templateGuardianIndex *int
  14. func init() {
  15. templateGuardianIndex = TemplateCmd.PersistentFlags().Int("idx", 0, "Default current guardian set index")
  16. setUpdateNumGuardians = AdminClientGuardianSetTemplateCmd.Flags().Int("num", 1, "Number of devnet guardians in example file")
  17. TemplateCmd.AddCommand(AdminClientGuardianSetTemplateCmd)
  18. TemplateCmd.AddCommand(AdminClientContractUpgradeTemplateCmd)
  19. }
  20. var TemplateCmd = &cobra.Command{
  21. Use: "template",
  22. Short: "Guardian governance VAA template commands ",
  23. }
  24. var AdminClientGuardianSetTemplateCmd = &cobra.Command{
  25. Use: "guardian-set-update",
  26. Short: "Generate an empty guardian set template at specified path (offline)",
  27. Run: runGuardianSetTemplate,
  28. Args: cobra.ExactArgs(1),
  29. }
  30. var AdminClientContractUpgradeTemplateCmd = &cobra.Command{
  31. Use: "contract-upgrade",
  32. Short: "Generate an empty contract upgrade template at specified path (offline)",
  33. Run: runContractUpgradeTemplate,
  34. Args: cobra.ExactArgs(1),
  35. }
  36. func runGuardianSetTemplate(cmd *cobra.Command, args []string) {
  37. path := args[0]
  38. // Use deterministic devnet addresses as examples in the template, such that this doubles as a test fixture.
  39. guardians := make([]*nodev1.GuardianSetUpdate_Guardian, *setUpdateNumGuardians)
  40. for i := 0; i < *setUpdateNumGuardians; i++ {
  41. k := devnet.DeterministicEcdsaKeyByIndex(crypto.S256(), uint64(i))
  42. guardians[i] = &nodev1.GuardianSetUpdate_Guardian{
  43. Pubkey: crypto.PubkeyToAddress(k.PublicKey).Hex(),
  44. Name: fmt.Sprintf("Example validator %d", i),
  45. }
  46. }
  47. m := &nodev1.InjectGovernanceVAARequest{
  48. CurrentSetIndex: uint32(*templateGuardianIndex),
  49. // Timestamp is hardcoded to make it reproducible on different devnet nodes.
  50. // In production, a real UNIX timestamp should be used (see node.proto).
  51. Timestamp: 1605744545,
  52. Payload: &nodev1.InjectGovernanceVAARequest_GuardianSet{
  53. GuardianSet: &nodev1.GuardianSetUpdate{Guardians: guardians},
  54. },
  55. }
  56. b, err := prototext.MarshalOptions{Multiline: true}.Marshal(m)
  57. if err != nil {
  58. panic(err)
  59. }
  60. err = ioutil.WriteFile(path, b, 0640)
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. }
  65. func runContractUpgradeTemplate(cmd *cobra.Command, args []string) {
  66. path := args[0]
  67. m := &nodev1.InjectGovernanceVAARequest{
  68. CurrentSetIndex: uint32(*templateGuardianIndex),
  69. // Timestamp is hardcoded to make it reproducible on different devnet nodes.
  70. // In production, a real UNIX timestamp should be used (see node.proto).
  71. Timestamp: 1605744545,
  72. Payload: &nodev1.InjectGovernanceVAARequest_ContractUpgrade{
  73. ContractUpgrade: &nodev1.ContractUpgrade{
  74. ChainId: 1,
  75. NewContract: make([]byte, 32),
  76. },
  77. },
  78. }
  79. b, err := prototext.MarshalOptions{Multiline: true}.Marshal(m)
  80. if err != nil {
  81. panic(err)
  82. }
  83. err = ioutil.WriteFile(path, b, 0640)
  84. if err != nil {
  85. log.Fatal(err)
  86. }
  87. }