You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.3 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "strings"
  9. "time"
  10. kzgceremony "github.com/arnaucube/eth-kzg-ceremony-alt"
  11. "github.com/arnaucube/eth-kzg-ceremony-alt/client"
  12. "github.com/fatih/color"
  13. flag "github.com/spf13/pflag"
  14. )
  15. func main() {
  16. fmt.Println("eth-kzg-ceremony-alt")
  17. fmt.Printf("====================\n\n")
  18. redB := color.New(color.FgRed, color.Bold)
  19. cyan := color.New(color.FgCyan)
  20. cyanB := color.New(color.FgCyan, color.Bold)
  21. green := color.New(color.FgHiGreen)
  22. greenB := color.New(color.FgHiGreen, color.Bold)
  23. var sequencerURL string
  24. var randomness string
  25. var sleepTime uint64
  26. flag.StringVarP(&sequencerURL, "url", "u",
  27. "https://kzg-ceremony-sequencer-dev.fly.dev", "sequencer url")
  28. flag.StringVarP(&randomness, "rand", "r",
  29. "", fmt.Sprintf("randomness, needs to be bigger than %d", kzgceremony.MinRandomnessLen))
  30. flag.Uint64VarP(&sleepTime, "sleeptime", "s",
  31. 10, "time (seconds) sleeping before trying again to be the next contributor")
  32. flag.CommandLine.SortFlags = false
  33. flag.Parse()
  34. c := client.NewClient(sequencerURL)
  35. // get status
  36. msgStatus, err := c.GetCurrentStatus()
  37. if err != nil {
  38. printErrAndExit(err)
  39. }
  40. fmt.Println(msgStatus)
  41. if randomness == "" {
  42. _, _ =
  43. cyanB.Println("To contribute to the ceremony, please set your randomness. Use -h to show the available flags.")
  44. os.Exit(0)
  45. }
  46. if len([]byte(randomness)) < kzgceremony.MinRandomnessLen {
  47. _, _ = redB.Printf("Randomness must be longer than %d, current length: %d\n",
  48. kzgceremony.MinRandomnessLen, len([]byte(randomness)))
  49. os.Exit(1)
  50. }
  51. // Auth
  52. msgReqLink, err := c.GetRequestLink()
  53. if err != nil {
  54. printErrAndExit(err)
  55. }
  56. _, _ = green.Printf("Please go to\n%s\n and authenticate with Github.\n", msgReqLink.GithubAuthURL)
  57. fmt.Println("(currently only Github auth is supported)")
  58. _, _ = greenB.Printf("Paste here the RawData from the auth answer:\n")
  59. s, err := readInput()
  60. if err != nil {
  61. printErrAndExit(err)
  62. }
  63. var authMsg client.MsgAuthCallback
  64. if err = json.Unmarshal([]byte(s), &authMsg); err != nil {
  65. printErrAndExit(err)
  66. }
  67. fmt.Print("Parsed auth msg: ")
  68. _, _ = cyan.Printf("%#v\n", authMsg)
  69. // TODO this will be only triggered by a flag
  70. // msg, err := c.PostAbortContribution(authMsg.SessionID)
  71. // if err != nil {
  72. // red.Println(err)
  73. // os.Exit(1)
  74. // }
  75. // fmt.Println("ABORT", string(msg))
  76. // os.Exit(0)
  77. // Get on queue
  78. var prevBatchContribution *kzgceremony.BatchContribution
  79. for {
  80. fmt.Printf("%s sending try_contribute\n", time.Now().Format("2006-01-02 15:04:05"))
  81. var retry bool
  82. prevBatchContribution, retry, err = c.PostTryContribute(authMsg.SessionID)
  83. if err != nil {
  84. _, _ = cyan.Println(err)
  85. }
  86. if !retry {
  87. break
  88. }
  89. fmt.Printf("%s try_contribute unsuccessful, going to sleep %d seconds\n",
  90. time.Now().Format("2006-01-02 15:04:05"), sleepTime)
  91. time.Sleep(time.Duration(sleepTime) * time.Second)
  92. }
  93. // get latest state
  94. // currentState, err := c.GetCurrentState()
  95. // if err != nil {
  96. // red.Println(err)
  97. // os.Exit(1)
  98. // }
  99. fmt.Println("starting to compute new contribution")
  100. newBatchContribution, err := prevBatchContribution.Contribute([]byte(randomness))
  101. if err != nil {
  102. printErrAndExit(err)
  103. }
  104. // store contribution
  105. fmt.Println("storing contribution.json")
  106. b, err := json.Marshal(newBatchContribution)
  107. if err != nil {
  108. printErrAndExit(err)
  109. }
  110. err = ioutil.WriteFile("contribution.json", b, 0600)
  111. if err != nil {
  112. printErrAndExit(err)
  113. }
  114. // send contribution
  115. fmt.Println("sending contribution")
  116. receipt, err := c.PostContribute(authMsg.SessionID, newBatchContribution)
  117. if err != nil {
  118. printErrAndExit(err)
  119. }
  120. fmt.Println("Receipt:")
  121. _, _ = green.Println(receipt)
  122. // store receipt
  123. fmt.Println("storing contribution_receipt.json")
  124. b, err = json.Marshal(receipt)
  125. if err != nil {
  126. printErrAndExit(err)
  127. }
  128. err = ioutil.WriteFile("contribution_receipt.json", b, 0600)
  129. if err != nil {
  130. printErrAndExit(err)
  131. }
  132. }
  133. func printErrAndExit(err error) {
  134. red := color.New(color.FgRed)
  135. _, _ = red.Println(err)
  136. os.Exit(1)
  137. }
  138. func readInput() (string, error) {
  139. reader := bufio.NewReader(os.Stdin)
  140. input, err := reader.ReadString('\n')
  141. if err != nil {
  142. return "", err
  143. }
  144. // remove the delimeter from the string
  145. input = strings.TrimSuffix(input, "\n")
  146. return input, nil
  147. }