You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.5 KiB

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