From 1d7eeabe2b73d423ab8cd22aed4533b304a0725c Mon Sep 17 00:00:00 2001 From: David Florness Date: Thu, 25 Jun 2020 10:51:36 -0600 Subject: [PATCH] Find election config based on election key --- config.go | 70 +++++++++++++++++++++++++++++++++++++++++--------- config_test.go | 5 +++- go.mod | 1 + go.sum | 1 + main.go | 4 +-- 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/config.go b/config.go index 3f09f4a..91c1b6a 100644 --- a/config.go +++ b/config.go @@ -7,21 +7,30 @@ import ( "path" "github.com/kyoh86/xdg" + "github.com/libp2p/go-libp2p-core/crypto" + "github.com/mr-tron/base58/base58" ) // Used to decode an election in the JSON elections file, which will be a list // of these. Maps the election key to the election metadata type RawElections map[string]struct{ // 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 } -// sees if we've participated in this election before and attempt to retrieve -// the data we stored for it -func resumeElectionMaybe(electionKey string) *Election { +type ElectionConfig struct{ + PrivKey crypto.PrivKey + PubKey crypto.PubKey +} + +type ElectionConfigs map[string]*ElectionConfig + +// sees if we've joined this election before and attempt to retrieve the data we +// stored for it +func getElectionConfigMaybe(electionKey string) *ElectionConfig { root := path.Join(xdg.ConfigHome(), "tallyard") file, err := os.OpenFile(path.Join(root, "elections.json"), os.O_RDONLY|os.O_CREATE, 0600) if err != nil { @@ -29,17 +38,54 @@ func resumeElectionMaybe(electionKey string) *Election { } defer file.Close() - // TOOD: use - decodeElections(file) + rawElections, err := decodeElections(file) + if err != nil { + panic(err) + } - return nil + electionConfigs, err := unmarshalRawElections(rawElections) + if err != nil { + panic(err) + } + + if cfg, hasElectionKey := electionConfigs[electionKey]; hasElectionKey { + return cfg + } else { + return nil + } } -func decodeElections(reader io.Reader) (rawElections RawElections) { +func decodeElections(reader io.Reader) (rawElections RawElections, err error) { decoder := json.NewDecoder(reader) - err := decoder.Decode(&rawElections) + err = decoder.Decode(&rawElections) if err != nil { - panic(err) + return nil, err + } + return rawElections, nil +} + +func unmarshalRawElections(rawElections RawElections) (electionConfigs ElectionConfigs, err error) { + for electionKey, info := range rawElections { + privKeyBytes, err := base58.Decode(info.PrivKey) + if err != nil { + return nil, err + } + privKey, err := crypto.UnmarshalPrivateKey(privKeyBytes) + if err != nil { + return nil, err + } + pubKeyBytes, err := base58.Decode(info.PubKey) + if err != nil { + return nil, err + } + pubKey, err := crypto.UnmarshalPublicKey(pubKeyBytes) + if err != nil { + return nil, err + } + electionConfigs[electionKey] = &ElectionConfig{ + PrivKey: privKey, + PubKey: pubKey, + } } - return rawElections + return electionConfigs, nil } diff --git a/config_test.go b/config_test.go index 6637c6d..7f8420b 100644 --- a/config_test.go +++ b/config_test.go @@ -37,7 +37,10 @@ func TestRawElectionDecoding(t *testing.T) { rawElectionsRef[ek1].PrivKey, rawElectionsRef[ek1].PubKey, ) - rawElections := decodeElections(strings.NewReader(jsonStream)) + rawElections, err := decodeElections(strings.NewReader(jsonStream)) + if err != nil { + panic(err) + } if len(rawElections) != 2 { t.FailNow() diff --git a/go.mod b/go.mod index f5f400a..6094e7d 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/kyoh86/xdg v1.2.0 github.com/libp2p/go-libp2p v0.6.1 github.com/libp2p/go-libp2p-core v0.5.0 + github.com/libp2p/go-libp2p-crypto v0.1.0 github.com/libp2p/go-libp2p-discovery v0.2.0 github.com/libp2p/go-libp2p-kad-dht v0.5.0 github.com/mr-tron/base58 v1.2.0 diff --git a/go.sum b/go.sum index 63ccffc..9b230b7 100644 --- a/go.sum +++ b/go.sum @@ -185,6 +185,7 @@ github.com/libp2p/go-libp2p-core v0.3.1/go.mod h1:thvWy0hvaSBhnVBaW37BvzgVV68OUh github.com/libp2p/go-libp2p-core v0.4.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= github.com/libp2p/go-libp2p-core v0.5.0 h1:FBQ1fpq2Fo/ClyjojVJ5AKXlKhvNc/B6U0O+7AN1ffE= github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= +github.com/libp2p/go-libp2p-crypto v0.1.0 h1:k9MFy+o2zGDNGsaoZl0MA3iZ75qXxr9OOoAZF+sD5OQ= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= github.com/libp2p/go-libp2p-discovery v0.2.0/go.mod h1:s4VGaxYMbw4+4+tsoQTqh7wfxg97AEdo4GYBt6BadWg= diff --git a/main.go b/main.go index 70db376..c7559e1 100644 --- a/main.go +++ b/main.go @@ -46,8 +46,8 @@ func bootstrap(election *Election, me *Me, merkleRoot []byte) { panic(err) } - logger.Info("host:", me.ID()) - logger.Info(me.Addrs()) + logger.Info("host: ", me.ID()) + logger.Info("addrs: ", me.Addrs()) var wg sync.WaitGroup for _, peerAddr := range dht.DefaultBootstrapPeers { -- 2.38.4