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