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.

194 lines
5.0 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. var (
  16. red = color.New(color.FgRed)
  17. redB = color.New(color.FgRed, color.Bold)
  18. cyan = color.New(color.FgCyan)
  19. cyanB = color.New(color.FgCyan, color.Bold)
  20. green = color.New(color.FgHiGreen)
  21. greenB = color.New(color.FgHiGreen, color.Bold)
  22. )
  23. func main() {
  24. fmt.Println("eth-kzg-ceremony-alt")
  25. fmt.Printf("====================\n")
  26. fmt.Printf(" https://github.com/arnaucube/eth-kzg-ceremony-alt\n\n")
  27. var sequencerURL string
  28. var randomness string
  29. var sleepTime uint64
  30. flag.StringVarP(&sequencerURL, "url", "u",
  31. "https://seq.ceremony.ethereum.org", "sequencer url")
  32. flag.StringVarP(&randomness, "rand", "r",
  33. "", fmt.Sprintf("randomness, needs to be bigger than %d bytes", kzgceremony.MinRandomnessLen))
  34. flag.Uint64VarP(&sleepTime, "sleeptime", "s",
  35. 30, "time (seconds) sleeping before trying again to be the next contributor")
  36. flag.CommandLine.SortFlags = false
  37. flag.Parse()
  38. c := client.NewClient(sequencerURL)
  39. // get status
  40. msgStatus, err := c.GetCurrentStatus()
  41. if err != nil {
  42. printErrAndExit(err)
  43. }
  44. fmt.Println(msgStatus)
  45. if randomness == "" {
  46. _, _ =
  47. cyanB.Println("To contribute to the ceremony, please set your randomness. Use -h to show the available flags.")
  48. os.Exit(0)
  49. }
  50. if len([]byte(randomness)) < kzgceremony.MinRandomnessLen {
  51. _, _ = redB.Printf("Randomness must be longer than %d, current length: %d\n",
  52. kzgceremony.MinRandomnessLen, len([]byte(randomness)))
  53. os.Exit(1)
  54. }
  55. // Auth
  56. fmt.Println("Github Authorization:")
  57. authMsg := authGH(c)
  58. // TODO this will be only triggered by a flag
  59. // msg, err := c.PostAbortContribution(authMsg.SessionID)
  60. // if err != nil {
  61. // red.Println(err)
  62. // os.Exit(1)
  63. // }
  64. // fmt.Println("ABORT", string(msg))
  65. // os.Exit(0)
  66. // Get on queue
  67. var prevBatchContribution *kzgceremony.BatchContribution
  68. for {
  69. fmt.Printf("%s sending try_contribute\n", time.Now().Format("2006-01-02 15:04:05"))
  70. var status client.Status
  71. prevBatchContribution, status, err = c.PostTryContribute(authMsg.SessionID)
  72. if err != nil {
  73. _, _ = cyan.Println(err)
  74. }
  75. if status == client.StatusProceed {
  76. break
  77. }
  78. if status == client.StatusReauth {
  79. fmt.Println("SessionID has expired, authenticate again with Github:")
  80. authMsg = authGH(c)
  81. }
  82. msgStatus, err := c.GetCurrentStatus()
  83. if err != nil {
  84. printErrAndExit(err)
  85. }
  86. fmt.Printf("%s try_contribute unsuccessful, lobby size %d, num contrib %d,"+
  87. "\n going to sleep %d seconds\n",
  88. time.Now().Format("2006-01-02 15:04:05"),
  89. msgStatus.LobbySize, msgStatus.NumContributions,
  90. 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. t0 := time.Now()
  101. newBatchContribution, err := prevBatchContribution.Contribute([]byte(randomness))
  102. if err != nil {
  103. fmt.Println("error on prevBatchContribution.Contribute")
  104. printErrAndExit(err)
  105. }
  106. fmt.Println("Contribution computed in", time.Since(t0))
  107. // store contribution
  108. fmt.Println("storing contribution.json")
  109. b, err := json.Marshal(newBatchContribution)
  110. if err != nil {
  111. // print error but do not exit
  112. _, _ = red.Println(err)
  113. }
  114. err = ioutil.WriteFile("contribution.json", b, 0600)
  115. if err != nil {
  116. // print error but do not exit
  117. _, _ = red.Println(err)
  118. }
  119. // send contribution
  120. fmt.Println("sending contribution")
  121. receipt, err := c.PostContribute(authMsg.SessionID, newBatchContribution)
  122. if err != nil {
  123. printErrAndExit(err)
  124. }
  125. fmt.Println("Receipt:")
  126. _, _ = green.Println(receipt)
  127. // store receipt
  128. fmt.Println("storing contribution_receipt.json")
  129. b, err = json.Marshal(receipt)
  130. if err != nil {
  131. printErrAndExit(err)
  132. }
  133. err = ioutil.WriteFile("contribution_receipt.json", b, 0600)
  134. if err != nil {
  135. printErrAndExit(err)
  136. }
  137. }
  138. func authGH(c *client.Client) client.MsgAuthCallback {
  139. msgReqLink, err := c.GetRequestLink()
  140. if err != nil {
  141. printErrAndExit(err)
  142. }
  143. _, _ = green.Printf("Please go to\n%s\n and authenticate with Github.\n", msgReqLink.GithubAuthURL)
  144. fmt.Println("(currently only Github auth is supported)")
  145. _, _ = greenB.Printf("Paste here the RawData from the auth answer:\n")
  146. s, err := readInput()
  147. if err != nil {
  148. printErrAndExit(err)
  149. }
  150. var authMsg client.MsgAuthCallback
  151. if err = json.Unmarshal([]byte(s), &authMsg); err != nil {
  152. printErrAndExit(err)
  153. }
  154. fmt.Print("Parsed auth msg: ")
  155. _, _ = cyan.Printf("%#v\n", authMsg)
  156. return authMsg
  157. }
  158. func printErrAndExit(err error) {
  159. _, _ = red.Println(err)
  160. os.Exit(1)
  161. }
  162. func readInput() (string, error) {
  163. reader := bufio.NewReader(os.Stdin)
  164. input, err := reader.ReadString('\n')
  165. if err != nil {
  166. return "", err
  167. }
  168. // remove the delimeter from the string
  169. input = strings.TrimSuffix(input, "\n")
  170. return input, nil
  171. }