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