Skip to content

Commit

Permalink
Merge pull request #1 from debabky/feature/on-chain-voting
Browse files Browse the repository at this point in the history
Feature/on chain voting
  • Loading branch information
freigeistig authored Mar 24, 2024
2 parents e4043a3 + 4eb8d76 commit e4274b3
Show file tree
Hide file tree
Showing 23 changed files with 939 additions and 995 deletions.
1 change: 0 additions & 1 deletion docs/spec/components/schemas/VotingOption.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ allOf:
format: int64
voting_id:
type: string
format: uuid.UUID
name:
type: string
description:
Expand Down
1 change: 0 additions & 1 deletion docs/spec/components/schemas/VotingOptionKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ required:
properties:
id:
type: string
format: uuid.UUID
type:
type: string
enum:
Expand Down
10 changes: 5 additions & 5 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- +migrate Up

create table votings(
id uuid primary key default gen_random_uuid(),
id text primary key default gen_random_uuid(),
name text not null,
description text not null,
voting_type integer not null,
Expand All @@ -12,25 +12,25 @@ create table votings(
create table voting_options(
id integer not null,
name text not null,
voting_id uuid not null,
voting_id text not null,
description text,
primary key (id, name, voting_id)
);

create table verification_requests(
id uuid not null primary key,
voting_id uuid not null,
voting_id text not null,
nullifier text not null
);

create table registrations(
voting_id uuid not null,
voting_id text not null,
nullifier text not null,
primary key (voting_id, nullifier)
);

create table votes(
voting_id uuid not null,
voting_id text not null,
voting_option text not null,
nullifier text not null,
rank integer,
Expand Down
572 changes: 0 additions & 572 deletions internal/contracts/registration.go

This file was deleted.

566 changes: 566 additions & 0 deletions internal/contracts/voting.go

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions internal/data/registrations.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package data

import (
"github.com/google/uuid"
)

type RegistrationsQ interface {
New() RegistrationsQ
Insert(value Registration) error
Expand All @@ -12,6 +8,6 @@ type RegistrationsQ interface {
}

type Registration struct {
VotingID uuid.UUID `db:"voting_id" structs:"voting_id"`
Nullifier string `db:"nullifier" structs:"nullifier"`
VotingID string `db:"voting_id" structs:"voting_id"`
Nullifier string `db:"nullifier" structs:"nullifier"`
}
2 changes: 1 addition & 1 deletion internal/data/verification_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ type VerificationRequestsQ interface {

type VerificationRequest struct {
ID uuid.UUID `db:"id" structs:"id"`
VotingID uuid.UUID `db:"voting_id" structs:"voting_id"`
VotingID string `db:"voting_id" structs:"voting_id"`
Nullifier string `db:"nullifier" structs:"nullifier"`
}
12 changes: 4 additions & 8 deletions internal/data/votes.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package data

import (
"github.com/google/uuid"
)

type VotesQ interface {
New() VotesQ
Insert(vote Vote) error
Expand All @@ -13,8 +9,8 @@ type VotesQ interface {
}

type Vote struct {
VotingID uuid.UUID `db:"voting_id" structs:"voting_id"`
VotingOption string `db:"voting_option" structs:"voting_option"`
Rank *int64 `db:"rank" structs:"rank"`
Nullifier string `db:"nullifier" structs:"nullifier"`
VotingID string `db:"voting_id" structs:"voting_id"`
VotingOption string `db:"voting_option" structs:"voting_option"`
Rank *int64 `db:"rank" structs:"rank"`
Nullifier string `db:"nullifier" structs:"nullifier"`
}
12 changes: 4 additions & 8 deletions internal/data/voting_options.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package data

import (
"github.com/google/uuid"
)

type VotingOptionsQ interface {
New() VotingOptionsQ
FilterBy(column string, value any) VotingOptionsQ
Select() ([]VotingOption, error)
}

type VotingOption struct {
ID int64 `db:"id" structs:"id"`
Name string `db:"name" structs:"name"`
VotingID uuid.UUID `db:"voting_id" structs:"voting_id"`
Description *string `db:"description" structs:"description"`
ID int64 `db:"id" structs:"id"`
Name string `db:"name" structs:"name"`
VotingID string `db:"voting_id" structs:"voting_id"`
Description *string `db:"description" structs:"description"`
}
3 changes: 1 addition & 2 deletions internal/data/votings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package data

import (
"github.com/google/uuid"
"time"
)

Expand All @@ -13,7 +12,7 @@ type VotingsQ interface {
}

type Voting struct {
ID uuid.UUID `db:"id" structs:"-"`
ID string `db:"id" structs:"-"`
Name string `db:"name" structs:"name"`
Description string `db:"description" structs:"description"`
Type VotingType `db:"voting_type" structs:"voting_type"`
Expand Down
10 changes: 5 additions & 5 deletions internal/service/api/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
tokenClaimsCtxKey
cookiesCtxKey
networkConfigCtxKey
registrationContractCtxKey
votingContractCtxKey
ethClientCtxKey
)

Expand Down Expand Up @@ -86,14 +86,14 @@ func Cookies(r *http.Request) *cookies.Cookies {
return r.Context().Value(cookiesCtxKey).(*cookies.Cookies)
}

func CtxRegistrationContract(contract *contracts.Registration) func(context.Context) context.Context {
func CtxVotingContract(contract *contracts.Voting) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, registrationContractCtxKey, contract)
return context.WithValue(ctx, votingContractCtxKey, contract)
}
}

func RegistrationContract(r *http.Request) *contracts.Registration {
return r.Context().Value(registrationContractCtxKey).(*contracts.Registration)
func VotingContract(r *http.Request) *contracts.Voting {
return r.Context().Value(votingContractCtxKey).(*contracts.Voting)
}

func CtxEthClient(client *ethclient.Client) func(context.Context) context.Context {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/api/handlers/get_auth_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetAuthData(w http.ResponseWriter, r *http.Request) {
}

access, err := JWTIssuer(r).IssueJWT(
verificationRequest.VotingID.String(), verificationRequest.Nullifier, jwt.AccessTokenType,
verificationRequest.VotingID, verificationRequest.Nullifier, jwt.AccessTokenType,
)
if err != nil {
Log(r).WithError(err).Error("failed to issuer JWT token")
Expand All @@ -42,7 +42,7 @@ func GetAuthData(w http.ResponseWriter, r *http.Request) {
}

refresh, err := JWTIssuer(r).IssueJWT(
verificationRequest.VotingID.String(), verificationRequest.Nullifier, jwt.RefreshTokenType,
verificationRequest.VotingID, verificationRequest.Nullifier, jwt.RefreshTokenType,
)
if err != nil {
Log(r).WithError(err).Error("failed to issuer JWT token")
Expand Down
4 changes: 3 additions & 1 deletion internal/service/api/handlers/get_voting.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetVoting(w http.ResponseWriter, r *http.Request) {
response := resources.VotingResponse{
Data: resources.Voting{
Key: resources.Key{
ID: voting.ID.String(),
ID: voting.ID,
Type: resources.VOTINGS,
},
Attributes: resources.VotingAttributes{
Expand Down Expand Up @@ -120,6 +120,8 @@ func GetVoting(w http.ResponseWriter, r *http.Request) {
})
}
response.Data.Attributes.Votes = &votesToAdd

// TODO check on-chain status via nullifier
}

ape.Render(w, response)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/api/handlers/get_votings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func GetVotings(w http.ResponseWriter, r *http.Request) {
for i, voting := range votings {
responseData[i] = resources.Voting{
Key: resources.Key{
ID: voting.ID.String(),
ID: voting.ID,
Type: resources.VOTINGS,
},
Attributes: resources.VotingAttributes{
Expand Down
Loading

0 comments on commit e4274b3

Please sign in to comment.