From 22f4c1287850b79638e290df67c397b5f6fb6e10 Mon Sep 17 00:00:00 2001 From: David Florness Date: Thu, 6 Aug 2020 16:12:13 -0600 Subject: [PATCH] Include candidates and rendezvous nonce in election info --- config.go | 38 ++++++++++++++++++++++++++++---------- election.go | 21 ++++++++++++++------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/config.go b/config.go index 5a68989..953ae51 100644 --- a/config.go +++ b/config.go @@ -23,19 +23,23 @@ type ElectionInfos map[string]*ElectionInfo // an entry of an election's cached info type ElectionInfo struct{ - PrivKey crypto.PrivKey - PubKey crypto.PubKey + Candidates []Candidate + PrivKey crypto.PrivKey + PubKey crypto.PubKey + RendezvousNonce Nonce } // used for JSON {en,de}coding type RawElectionInfos map[string]RawElectionInfo type RawElectionInfo struct{ + Candidates []string // Our private key in base58. TODO: encrypt - PrivKey string + PrivKey string // Our public key in base58. Kept in addition to the private key to // check against when the private key is later encrypted - PubKey string + PubKey string + RendezvousNonce string } // Saves an election's info into a file that can later be easily retrieved, even @@ -57,8 +61,10 @@ func (e *Election) saveInfo() error { defer file.Close() localVoter := e.localVoter infos[e.electionKey] = &ElectionInfo{ - PrivKey: localVoter.Peerstore().PrivKey(localVoter.ID()), - PubKey: localVoter.Peerstore().PubKey(localVoter.ID()), + e.Candidates, + localVoter.Peerstore().PrivKey(localVoter.ID()), + localVoter.Peerstore().PubKey(localVoter.ID()), + e.rendezvousNonce, } raw, err := marshalElectionInfos(infos) if err != nil { @@ -128,6 +134,10 @@ func decodeElectionInfos(reader io.Reader) (rawElections RawElectionInfos, err e func marshalElectionInfos(electionConfigs ElectionInfos) (rawElections RawElectionInfos, err error) { rawElections = make(RawElectionInfos) for electionKey, electionConfig := range electionConfigs { + candidates := make([]string, 0, len(electionConfig.Candidates)) + for _, c := range electionConfig.Candidates { + candidates = append(candidates, string(c)) + } privKeyBytes, err := crypto.MarshalPrivateKey(electionConfig.PrivKey) if err != nil { return nil, err @@ -139,8 +149,10 @@ func marshalElectionInfos(electionConfigs ElectionInfos) (rawElections RawElecti } pubKeyStr := base58.Encode(pubKeyBytes) rawElections[electionKey] = RawElectionInfo{ - PrivKey: privKeyStr, - PubKey: pubKeyStr, + candidates, + privKeyStr, + pubKeyStr, + string(electionConfig.RendezvousNonce), } } return rawElections, nil @@ -149,6 +161,10 @@ func marshalElectionInfos(electionConfigs ElectionInfos) (rawElections RawElecti func unmarshalRawElectionInfos(rawElections RawElectionInfos) (electionConfigs ElectionInfos, err error) { electionConfigs = make(ElectionInfos) for electionKey, info := range rawElections { + candidates := make([]Candidate, 0, len(info.Candidates)) + for _, s := range info.Candidates { + candidates = append(candidates, Candidate(s)) + } privKeyBytes, err := base58.Decode(info.PrivKey) if err != nil { return nil, err @@ -166,8 +182,10 @@ func unmarshalRawElectionInfos(rawElections RawElectionInfos) (electionConfigs E return nil, err } electionConfigs[electionKey] = &ElectionInfo{ - PrivKey: privKey, - PubKey: pubKey, + candidates, + privKey, + pubKey, + Nonce(info.RendezvousNonce), } } return electionConfigs, nil diff --git a/election.go b/election.go index 64b9ba0..4d2d8b1 100644 --- a/election.go +++ b/election.go @@ -56,7 +56,11 @@ func NewElectionWithCandidates(candidates []Candidate) *Election { } func NewElectionWithElectionKey(electionKey string) *Election { - var localVoter *LocalVoter + var ( + candidates []Candidate + localVoter *LocalVoter + rendezvousNonce Nonce + ) // try to recover info from elections file to see if we've // joined/created this election before electionInfo, err := getElectionInfo(electionKey) @@ -64,21 +68,24 @@ func NewElectionWithElectionKey(electionKey string) *Election { panic(err) } if electionInfo != nil { // we've joined this election before - // ensure public key from config is same as one derived - // from private key + // ensure public key from config is same as one derived from + // private key + candidates = electionInfo.Candidates if !electionInfo.PrivKey.GetPublic().Equals(electionInfo.PubKey) { // TODO: handle properly panic("loaded public key does not match derived one from private key") } localVoter = NewLocalVoter(libp2p.Identity(electionInfo.PrivKey)) + rendezvousNonce = electionInfo.RendezvousNonce } else { // we've never joined this election before localVoter = NewLocalVoter(libp2p.RandomIdentity) } - e := &Election{ - electionKey: electionKey, - localVoter: localVoter, - remoteVoters: make(map[peer.ID]*Voter), + Candidates: candidates, + electionKey: electionKey, + localVoter: localVoter, + remoteVoters: make(map[peer.ID]*Voter), + rendezvousNonce: rendezvousNonce, } zeroi := strings.IndexByte(electionKey, '0') if zeroi == -1 { -- 2.38.4