~edwargix/tallyard

1d7eeabe2b73d423ab8cd22aed4533b304a0725c — David Florness 5 years ago 8570935
Find election config based on election key
5 files changed, 66 insertions(+), 15 deletions(-)

M config.go
M config_test.go
M go.mod
M go.sum
M main.go
M config.go => config.go +58 -12
@@ 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
}

M config_test.go => config_test.go +4 -1
@@ 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()

M go.mod => go.mod +1 -0
@@ 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

M go.sum => go.sum +1 -0
@@ 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=

M main.go => main.go +2 -2
@@ 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 {