From 456f6a027f1041ca0244dc0340c86aa89e4a24e8 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 17 Jan 2021 16:45:11 -0500 Subject: [PATCH] Store local voter in data.json This way, we don't generate a private key on every program invocation. --- cmd/tallyard/main.go | 15 +++++++++------ matrix/data.go | 12 +++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cmd/tallyard/main.go b/cmd/tallyard/main.go index 37e2517..9b9bfbc 100644 --- a/cmd/tallyard/main.go +++ b/cmd/tallyard/main.go @@ -49,7 +49,10 @@ func main() { panic(err) } - localVoter := election.NewLocalVoter(client.UserID) + if data.LocalVoter == nil { + data.LocalVoter = election.NewLocalVoter(client.UserID) + data.Save() + } elections := election.NewElectionsMap() @@ -69,7 +72,7 @@ func main() { }) syncer.OnEventType(election.EvalMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) - election.OnEvalMessage(source, evt, elections, localVoter) + election.OnEvalMessage(source, evt, elections, data.LocalVoter) }) syncer.OnEventType(election.SumMessage, func(source mautrix.EventSource, evt *event.Event) { DebugCB(source, evt) @@ -92,17 +95,17 @@ func main() { } }() - el, ballot := ui.TUI(client, elections, localVoter) + el, ballot := ui.TUI(client, elections, data.LocalVoter) - localVoter.Poly = math.NewRandomPoly(uint(len(el.Voters)-1), 1024, ballot) + data.LocalVoter.Poly = math.NewRandomPoly(uint(len(el.Voters)-1), 1024, ballot) // TODO we may not have all voters' info - err = localVoter.SendEvals(client, el) + err = data.LocalVoter.SendEvals(client, el) if err != nil { panic(err) } - err = localVoter.SendSum(client, el) + err = data.LocalVoter.SendSum(client, el) if err != nil { panic(err) } diff --git a/matrix/data.go b/matrix/data.go index 6e396ce..6fac0ed 100644 --- a/matrix/data.go +++ b/matrix/data.go @@ -10,14 +10,16 @@ import ( "github.com/kyoh86/xdg" "maunium.net/go/mautrix" "maunium.net/go/mautrix/id" + "tallyard.xyz/election" ) type Data struct { - Homeserver string `json:"homeserver"` - Username string `json:"username"` - AccessToken string `json:"access_token"` - DeviceID id.DeviceID `json:"device_id"` - UserID id.UserID `json:"user_id"` + Homeserver string `json:"homeserver"` + Username string `json:"username"` + AccessToken string `json:"access_token"` + DeviceID id.DeviceID `json:"device_id"` + UserID id.UserID `json:"user_id"` + LocalVoter *election.LocalVoter `json:"local_voter,omitempty"` } var dataFname = xdg.DataHome() + "/tallyard/data.json" -- 2.38.4