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