go-dvote/cmd/generator/generator.go
cmd/generator/generator.goRaw
1package main
2
3import (
4 "os"
5 "strconv"
6 "time"
7 "fmt"
8 "math/rand"
9 "encoding/json"
10 "encoding/base64"
11// "net/http"
12// "bytes"
13// "io/ioutil"
14 "github.com/vocdoni/dvote-relay/types"
15 "github.com/vocdoni/dvote-relay/data"
16)
17
18func makeBallot() string {
19 var bal types.Ballot
20
21 bal.Type = "ballot0"
22
23 pidBytes := make([]byte, 32)
24 rand.Read(pidBytes)
25 bal.PID = base64.StdEncoding.EncodeToString(pidBytes)
26
27 nullifier := make([]byte, 32)
28 rand.Read(nullifier)
29 bal.Nullifier = nullifier
30
31 vote := make([]byte, 32)
32 rand.Read(vote)
33 bal.Vote = vote
34
35 franchise := make([]byte, 32)
36 rand.Read(franchise)
37 bal.Franchise = franchise
38
39
40 b, err := json.Marshal(bal)
41 if err != nil {
42 fmt.Println(err)
43 return "error"
44 }
45 //todo: add encryption, pow
46 return string(b)
47}
48
49func makeEnvelope(ballot string) string {
50 var env types.Envelope
51
52 env.Type = "envelope0"
53
54 env.Nonce = rand.Uint64()
55
56 kp := make([]byte, 4)
57 rand.Read(kp)
58 env.KeyProof = kp
59
60 env.Ballot = []byte(ballot)
61
62 env.Timestamp = time.Now()
63
64 e, err := json.Marshal(env)
65 if err != nil {
66 fmt.Println(err)
67 return "error"
68 }
69 //todo: add encryption, pow
70 return string(e)
71
72}
73
74func main() {
75 interval := os.Args[1]
76 i, _ := strconv.Atoi(interval)
77 timer := time.NewTicker(time.Millisecond * time.Duration(i))
78 rand.Seed(time.Now().UnixNano())
79 topic := "vocdoni_pubsub_testing"
80 fmt.Println("PubSub Topic:>", topic)
81
82
83 for {
84 select {
85 case <- timer.C:
86 var jsonStr = makeEnvelope(makeBallot())
87 fmt.Println(jsonStr)
88 data.PsPublish(topic, jsonStr)
89 default:
90 continue
91 }
92 }
93}