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