-
Notifications
You must be signed in to change notification settings - Fork 0
/
trusteeExport.go
94 lines (78 loc) · 1.62 KB
/
trusteeExport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bigw-voting/commands"
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
)
// ExportTrusteeVote creates a new trustee vote to be sent to a trustee
func ExportTrusteeVote() {
// Input name
var name string
fmt.Print("Enter name (this must match the votepack): ")
_, err := fmt.Scanln(&name)
if err != nil {
panic(err)
}
// Input IRV Votes
irv := make(map[string]int)
for _, cand := range votepack.Candidates {
var v string
fmt.Print(cand + ": ")
_, err = fmt.Scanln(&v)
if err != nil {
panic(err)
}
irv[cand], err = strconv.Atoi(v)
if err != nil {
panic(err)
}
}
vote := &commands.TrusteeVote{Name: name, Votes: irv}
// Validate votes so that IRV works
for k, v := range irv {
if v > len(irv) {
panic("votes are invalid")
}
for l, w := range irv {
if v == w && k != l {
panic("votes are not transferrable")
}
}
}
fmt.Println("\nValidated votes")
// Marshal vote into JSON
marshaled, err := json.Marshal(vote)
if err != nil {
panic(err)
}
var filename string
fmt.Print("File to output to: ")
_, err = fmt.Scanln(&filename)
if err != nil {
panic(err)
}
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
// Obfuscate votes. This is not encryption, it simply prevents people from reading
// the plaintext.
archiver := gzip.NewWriter(f)
layer1Writer := base64.NewEncoder(base64.StdEncoding, archiver)
layer0 := bytes.NewBuffer(marshaled)
_, err = io.Copy(layer1Writer, layer0)
if err != nil {
panic(err)
}
layer1Writer.Close()
archiver.Close()
f.Close()
os.Exit(0)
}