admintemplate.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 templateNumGuardians *int
  13. var templateGuardianIndex *int
  14. func init() {
  15. templateNumGuardians = AdminClientGuardianSetTemplateCmd.Flags().Int("num", 1, "Number of devnet guardians in example file")
  16. templateGuardianIndex = AdminClientGuardianSetTemplateCmd.Flags().Int("idx", 0, "Default current guardian set index")
  17. }
  18. var AdminClientGuardianSetTemplateCmd = &cobra.Command{
  19. Use: "guardian-set-update-template",
  20. Short: "Generate an empty guardian set template at specified path (offline)",
  21. Run: runGuardianSetTemplate,
  22. Args: cobra.ExactArgs(1),
  23. }
  24. func runGuardianSetTemplate(cmd *cobra.Command, args []string) {
  25. path := args[0]
  26. // Use deterministic devnet addresses as examples in the template, such that this doubles as a test fixture.
  27. guardians := make([]*nodev1.GuardianSetUpdate_Guardian, *templateNumGuardians)
  28. for i := 0; i < *templateNumGuardians; i++ {
  29. k := devnet.DeterministicEcdsaKeyByIndex(crypto.S256(), uint64(i))
  30. guardians[i] = &nodev1.GuardianSetUpdate_Guardian{
  31. Pubkey: crypto.PubkeyToAddress(k.PublicKey).Hex(),
  32. Name: fmt.Sprintf("Example validator %d", i),
  33. }
  34. }
  35. m := &nodev1.GuardianSetUpdate{
  36. CurrentSetIndex: uint32(*templateGuardianIndex),
  37. // Timestamp is hardcoded to make it reproducible on different devnet nodes.
  38. // In production, a real UNIX timestamp should be used (see node.proto).
  39. Timestamp: 1605744545,
  40. Guardians: guardians,
  41. }
  42. b, err := prototext.MarshalOptions{Multiline: true}.Marshal(m)
  43. if err != nil {
  44. panic(err)
  45. }
  46. err = ioutil.WriteFile(path, b, 0640)
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. }