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