Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: QR code event #1

Merged
merged 9 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Zero Block Global Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 11 additions & 32 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,24 @@ event_types:
short_description: Short description
no_auto_open: true
auto_claim: true
- name: meetup_participation
title: Prove your participation by scanning QR code
reward: 5
frequency: unlimited
description: Prove your participation by scanning QR code
short_description: Short description
auto_claim: true
qr_code_value: "qr_code_base64_string"

levels:
levels:
- lvl: 1
threshold: 0
referrals: 5
withdrawal_allowed: false
- lvl: 2
threshold: 5
referrals: 5
withdrawal_allowed: true
- lvl: 3
threshold: 6
referrals: 5
withdrawal_allowed: true

countries:
verification_key: "37bc75afc97f8bdcd21cda85ae7b2885b5f1205ae3d79942e56457230f1636a037cc7ebfe42998d66a3dd3446b9d29366271b4f2bd8e0d307db1d320b38fc02f"
countries:
- code: "UKR"
reserve_limit: 100000
reserve_allowed: true
withdrawal_allowed: true
- code: "USA"
reserve_limit: 0
reserve_allowed: false
withdrawal_allowed: false
- code: "default"
reserve_limit: 100
reserve_allowed: true
withdrawal_allowed: true
referrals: 1

auth:
addr: http://rarime-auth

broadcaster:
addr: broadcaster
sender_account: "rarimo15hcd6tv7pe8hk2re7hu0zg0aphqdm2dtjrs0ds"

verifier:
verification_key_path: "./verification_key.json"
allowed_age: 18
Expand All @@ -95,5 +74,5 @@ root_verifier:
contract: registration_contract_address
request_timeout: 10s

withdrawal:
point_price_urmo: 1000000
sig_verifier:
verification_key: "37bc75afc97f8bdcd21cda85ae7b2885b5f1205ae3d79942e56457230f1636a037cc7ebfe42998d66a3dd3446b9d29366271b4f2bd8e0d307db1d320b38fc02f"
2 changes: 1 addition & 1 deletion docs/spec/components/schemas/CreateBalanceKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ properties:
pattern: '^0x[0-9a-fA-F]{64}$'
type:
type: string
enum: [ create_balance, update_balance ]
enum: [ create_balance ]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
allOf:
- $ref: '#/components/schemas/PassportEventStateKey'
- $ref: '#/components/schemas/EventClaimingStateKey'
- type: object
required:
- attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ properties:
pattern: '^0x[0-9a-fA-F]{64}$'
type:
type: string
enum: [ passport_event_state ]
enum: [ event_claiming_state ]
4 changes: 4 additions & 0 deletions docs/spec/components/schemas/EventStaticMeta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ properties:
- not_started
- expired
- disabled
qr_code_value:
type: string
description: Base64-encoded QR code. Must match the code provided in event type.
example: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR4AWP4//8/AyUYw"
16 changes: 16 additions & 0 deletions docs/spec/components/schemas/FulfillQREvent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
allOf:
- $ref: '#/components/schemas/FulfillQREventKey'
- type: object
x-go-is-request: true
required:
- attributes
properties:
attributes:
required:
- qr_code
type: object
properties:
qr_code:
type: string
description: Base64-encoded QR code
example: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR4AWP4//8/AyUYw"
12 changes: 12 additions & 0 deletions docs/spec/components/schemas/FulfillQREventKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type: object
required:
- id
- type
properties:
id:
type: string
description: Event ID
example: "059c81dd-2a54-44a8-8142-c15ad8f88949"
type:
type: string
enum: [ fulfill_qr_event ]
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ post:
- data
properties:
data:
$ref: '#/components/schemas/PassportEventState'
$ref: '#/components/schemas/EventClaimingState'
400:
$ref: '#/components/responses/invalidParameter'
401:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ post:
- data
properties:
data:
$ref: '#/components/schemas/PassportEventState'
$ref: '#/components/schemas/EventClaimingState'
400:
$ref: '#/components/responses/invalidParameter'
401:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
patch:
tags:
- Events
summary: Fulfill QR code event
description: Fulfill QR code event
operationId: fulfillQREvent
parameters:
- in: path
name: 'id'
required: true
schema:
type: string
example: "059c81dd-2a54-44a8-8142-c15ad8f88949"
- in: header
name: Signature
description: Signature of the request
required: true
schema:
type: string
pattern: '^[a-f0-9]{64}$'
requestBody:
required: true
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/FulfillQREvent'
responses:
200:
description: Success
content:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
$ref: '#/components/schemas/EventClaimingState'
400:
$ref: '#/components/responses/invalidParameter'
401:
$ref: '#/components/responses/invalidAuth'
403:
description: This event type was disabled and cannot be fulfilled
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Errors'
404:
$ref: '#/components/responses/notFound'
500:
$ref: '#/components/responses/internalError'
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ require (
github.com/go-co-op/gocron/v2 v2.2.2
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/google/jsonapi v1.0.0
github.com/google/uuid v1.6.0
github.com/iden3/go-rapidsnark/types v0.0.3
github.com/labstack/gommon v0.4.0
github.com/rarimo/decentralized-auth-svc v0.0.0-20240522134350-2694eafa9509
github.com/rarimo/saver-grpc-lib v1.0.0
github.com/rarimo/zkverifier-kit v1.0.0
github.com/rubenv/sql-migrate v1.6.1
github.com/stretchr/testify v1.9.0
gitlab.com/distributed_lab/ape v1.7.1
gitlab.com/distributed_lab/figure/v3 v3.1.4
gitlab.com/distributed_lab/kit v1.11.3
Expand Down Expand Up @@ -80,7 +83,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
Expand All @@ -104,6 +106,7 @@ require (
github.com/lib/pq v1.10.9 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
Expand Down Expand Up @@ -133,7 +136,6 @@ require (
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand All @@ -144,6 +146,8 @@ require (
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
gitlab.com/distributed_lab/figure v2.1.2+incompatible // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,7 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
github.com/labstack/echo/v4 v4.10.0/go.mod h1:S/T/5fy/GigaXnHTkh0ZGe4LpkkQysvRjFMSUTkDRNQ=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
Expand Down Expand Up @@ -2282,10 +2283,12 @@ github.com/urfave/cli/v2 v2.10.2/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhA
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
Expand Down
24 changes: 12 additions & 12 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ AS $$ BEGIN NEW.updated_at = EXTRACT('EPOCH' FROM NOW()); RETURN NEW; END; $$;

CREATE TABLE IF NOT EXISTS balances
(
nullifier TEXT PRIMARY KEY,
amount bigint NOT NULL default 0,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
updated_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
referred_by text UNIQUE,
level INT NOT NULL,
anonymous_id TEXT UNIQUE,
is_verified BOOLEAN NOT NULL default FALSE,
is_passport_proven BOOLEAN NOT NULL default FALSE
nullifier TEXT PRIMARY KEY,
amount bigint NOT NULL default 0,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
updated_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
referred_by text,
level INT NOT NULL,
anonymous_id TEXT UNIQUE,
is_verified BOOLEAN NOT NULL default FALSE,
is_passport_proven BOOLEAN NOT NULL default FALSE
);

CREATE INDEX IF NOT EXISTS balances_page_index ON balances (amount, updated_at) WHERE referred_by IS NOT NULL;
Expand All @@ -27,8 +27,8 @@ EXECUTE FUNCTION trigger_set_updated_at();

CREATE TABLE IF NOT EXISTS referrals
(
id text PRIMARY KEY,
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
id text PRIMARY KEY,
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
usage_left INTEGER NOT NULL DEFAULT 1
);

Expand All @@ -40,7 +40,7 @@ CREATE TYPE event_status AS ENUM ('open', 'fulfilled', 'claimed');
CREATE TABLE IF NOT EXISTS events
(
id uuid PRIMARY KEY NOT NULL default gen_random_uuid(),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
nullifier TEXT NOT NULL REFERENCES balances (nullifier),
type text NOT NULL,
status event_status NOT NULL,
created_at integer NOT NULL default EXTRACT('EPOCH' FROM NOW()),
Expand Down
7 changes: 3 additions & 4 deletions internal/config/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
)

type Level struct {
Level int `fig:"lvl,required"`
Threshold int `fig:"threshold,required"`
Referrals int `fig:"referrals,required"`
WithdrawalAllowed bool `fig:"withdrawal_allowed"`
Level int `fig:"lvl,required"`
Threshold int `fig:"threshold,required"`
Referrals int `fig:"referrals,required"`
}

type Levels map[int]Level
Expand Down
8 changes: 5 additions & 3 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config interface {

Levels() Levels
Verifier() *zk.Verifier
SigVerifier() []byte
}

type config struct {
Expand All @@ -32,9 +33,10 @@ type config struct {
identity.VerifierProvider
evtypes.EventTypeser

levels comfig.Once
verifier comfig.Once
getter kv.Getter
levels comfig.Once
verifier comfig.Once
sigVerifier comfig.Once
getter kv.Getter
}

func New(getter kv.Getter) Config {
Expand Down
31 changes: 31 additions & 0 deletions internal/config/sig_verifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import (
"encoding/hex"
"fmt"

"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/kv"
)

func (c *config) SigVerifier() []byte {
return c.sigVerifier.Do(func() interface{} {
var cfg struct {
VerificationKey string `fig:"verification_key,required"`
}

err := figure.Out(&cfg).
From(kv.MustGetStringMap(c.getter, "sig_verifier")).
Please()
if err != nil {
panic(fmt.Errorf("failed to figure out sig_verifier: %w", err))
}

key, err := hex.DecodeString(cfg.VerificationKey)
if err != nil {
panic(fmt.Errorf("verification_key is not a hex: %w", err))
}

return key
}).([]byte)
}
Loading
Loading