Skip to content

Commit

Permalink
Adding admin and voter utils
Browse files Browse the repository at this point in the history
  • Loading branch information
phayes committed Sep 28, 2017
1 parent 903bf21 commit 6297489
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 6 deletions.
53 changes: 53 additions & 0 deletions utils/cryptoballot/admin_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"io/ioutil"
"log"

"github.com/cryptoballot/cryptoballot/cryptoballot"
"github.com/urfave/cli"
)

func actionAdminCreate(c *cli.Context) error {
filename := c.Args().First()

if filename == "" {
log.Fatal("Please specify an election file to PUT to the ballotclerk server")
}

if PrivateKey == nil {
log.Fatal("Please specify a private key pem file with --key (eg: `--key=path/to/mykey.pem`)")
}

content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}

election, err := cryptoballot.NewElection(content)
if err != nil {
log.Fatal(err)
}

// Sign election if needed
if !election.HasSignature() {
election.Signature, err = PrivateKey.SignString(election.String())
if err != nil {
log.Fatal(err)
}
}

// Verify the election was signed correctly
err = election.VerifySignature()
if err != nil {
log.Fatal(err)
}

// PUT the election to the Election Clerk server
err = BallotClerkClient.PutElection(election, PrivateKey)
if err != nil {
log.Fatal(err)
}

return nil
}
1 change: 1 addition & 0 deletions utils/cryptoballot/admin_tally.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
41 changes: 35 additions & 6 deletions utils/cryptoballot/cryptoballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/cryptoballot/cryptoballot/clients/ballotbox"
"github.com/cryptoballot/cryptoballot/clients/ballotclerk"
"github.com/cryptoballot/cryptoballot/cryptoballot"
"github.com/phayes/decryptpem"
"github.com/urfave/cli"
)

Expand All @@ -19,6 +21,12 @@ var BallotClerkClient *ballotclerk.Client
// BallotBoxClient is used to connect to ballotbox server
var BallotBoxClient *ballotbox.Client

// PrivateKey for all operations that require a private key
var PrivateKey cryptoballot.PrivateKey

// PublicKey derived from PrivateKey
var PublicKey cryptoballot.PublicKey

func main() {
app := cli.NewApp()
app.Name = "cryptoballot"
Expand All @@ -33,6 +41,10 @@ func main() {
Name: "ballotbox",
Value: "http://localhost:8002",
},
cli.StringFlag{
Name: "key",
Value: "",
},
}

// Commands
Expand All @@ -42,12 +54,9 @@ func main() {
Usage: "perform election administrative operations",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create a new election",
Action: func(c *cli.Context) error {
fmt.Println("create: ", c.Args().First())
return nil
},
Name: "create",
Usage: "create a new election",
Action: actionAdminCreate,
ArgsUsage: "[electionfile]",
},
{
Expand Down Expand Up @@ -104,6 +113,26 @@ func main() {
// Connect to A4D Extract
BallotBoxClient = ballotbox.NewClient(c.String("ballotbox"))

// Privat Key
if c.String("key") != "" {

// Decrypt it as needed
pem, err := decryptpem.DecryptFileWithPrompt(c.String("key"))
if err != nil {
log.Fatal(err)
}

PrivateKey, err = cryptoballot.NewPrivateKeyFromBlock(pem)
if err != nil {
log.Fatal(err)
}

PublicKey, err = PrivateKey.PublicKey()
if err != nil {
log.Fatal(err)
}
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions utils/cryptoballot/voter_verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
75 changes: 75 additions & 0 deletions utils/cryptoballot/voter_vote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"io/ioutil"
"log"

"github.com/cryptoballot/cryptoballot/cryptoballot"
"github.com/urfave/cli"
)

func actionVoterVote(c *cli.Context) error {
filename := c.Args().First()

if filename == "" {
log.Fatal("Please specify an balliot file to PUT to the ballotbox server")
}

if PrivateKey == nil {
log.Fatal("Please specify a private key pem file with --key (eg: `--key=path/to/mykey.pem`)")
}

content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}

ballot, err := cryptoballot.NewBallot(content)
if err != nil {
log.Fatal(err)
}

// Get public key from ballotclerk server
clerkPublicKey, err := BallotClerkClient.GetPublicKey()
if err != nil {
log.Fatal(err)
}

// Blind the ballot
blindBallot, unblinder, err := ballot.Blind(clerkPublicKey)
if err != nil {
log.Fatal(err)
}

// Create a signature request
signatureRequest := &cryptoballot.SignatureRequest{
ElectionID: ballot.ElectionID,
RequestID: PublicKey.GetSHA256(),
PublicKey: PublicKey.Bytes(),
BlindBallot: blindBallot,
}
signatureRequest.Signature, err = PrivateKey.SignString(signatureRequest.String())
if err != nil {
log.Fatal(err)
}

// Do the signature request
fulfilled, err := BallotClerkClient.PostSignatureRequest(signatureRequest, PrivateKey)
if err != nil {
log.Fatal(err)
}

// Unblind the ballot using the FulfilledSignatureRequest
err = ballot.Unblind(clerkPublicKey, fulfilled.BallotSignature, unblinder)
if err != nil {
log.Fatal(err)
}

// PUT the ballot
err = BallotBoxClient.PutBallot(ballot)
if err != nil {
log.Fatal(err)
}

return nil
}

0 comments on commit 6297489

Please sign in to comment.