Skip to content

Commit

Permalink
add nationality_enable flag: only check pub-signal in selector for na…
Browse files Browse the repository at this point in the history
…tionality (#14)

* add nationality_enable flag: only check pub-signal in selector for nationality

* refractor code: only write nationality for user when in db nationality is empty
  • Loading branch information
chabanyknikita authored Oct 4, 2024
1 parent fcd753f commit 4f07c00
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ verifier:
event_id: "event_id"

signature_verification:
pub_key: "public_key"
pub_key: "04e29323ad356ab524fa5dbe3e490244e741b4d445ac7d2ee5f321556b3fda616bb9d2f2216fc27e099ab3019103cca872679e130629b2b90ea16cedb2b2136371"

poseidonsmt_root_verifier:
rpc: rpc_url
Expand Down
4 changes: 4 additions & 0 deletions docs/spec/components/schemas/User.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ allOf:
type: string
example: "UKR"
description: "User nationality"
nationality_check:
type: boolean
example: true
description: "You can use this instead of 'nationality' params, it will check nationality bit in selector"
event_id:
type: string
example: "2234556494903931186902189494613533900917417361106374681011849132651019822199"
Expand Down
4 changes: 4 additions & 0 deletions internal/assets/migrations/004_nationalityEnable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- +migrate Up
ALTER TABLE verify_users ADD COLUMN nationality_enable BOOLEAN NOT NULL DEFAULT FALSE;
-- +migrate Down
ALTER TABLE verify_users DROP COLUMN nationality_enable;
21 changes: 11 additions & 10 deletions internal/data/pg/verify_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ func (q *VerifyUsersQ) Insert(VerifyUsers *data.VerifyUsers) error {
}

stmt := sq.Insert(verifyUsersTableName).SetMap(map[string]interface{}{
"user_id": VerifyUsers.UserID,
"user_id_hash": VerifyUsers.UserIDHash,
"age_lower_bound": VerifyUsers.AgeLowerBound,
"nationality": VerifyUsers.Nationality,
"uniqueness": VerifyUsers.Uniqueness,
"event_id": VerifyUsers.EventId,
"status": VerifyUsers.Status,
"proof": proofJSON,
"sex": VerifyUsers.Sex,
"sex_enable": VerifyUsers.SexEnable,
"user_id": VerifyUsers.UserID,
"user_id_hash": VerifyUsers.UserIDHash,
"age_lower_bound": VerifyUsers.AgeLowerBound,
"nationality": VerifyUsers.Nationality,
"uniqueness": VerifyUsers.Uniqueness,
"event_id": VerifyUsers.EventId,
"status": VerifyUsers.Status,
"proof": proofJSON,
"sex": VerifyUsers.Sex,
"sex_enable": VerifyUsers.SexEnable,
"nationality_enable": VerifyUsers.NationalityEnable,
})

if err = q.db.Exec(stmt); err != nil {
Expand Down
23 changes: 12 additions & 11 deletions internal/data/verify_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
)

type VerifyUsers struct {
UserID string `db:"user_id"`
UserIDHash string `db:"user_id_hash"`
AgeLowerBound int `db:"age_lower_bound"`
Nationality string `db:"nationality"`
CreatedAt time.Time `db:"created_at"`
Uniqueness bool `db:"uniqueness"`
EventId string `db:"event_id"`
Status string `db:"status"`
Proof []byte `db:"proof"`
Sex string `db:"sex"`
SexEnable bool `db:"sex_enable"`
UserID string `db:"user_id"`
UserIDHash string `db:"user_id_hash"`
AgeLowerBound int `db:"age_lower_bound"`
Nationality string `db:"nationality"`
CreatedAt time.Time `db:"created_at"`
Uniqueness bool `db:"uniqueness"`
EventId string `db:"event_id"`
Status string `db:"status"`
Proof []byte `db:"proof"`
Sex string `db:"sex"`
SexEnable bool `db:"sex_enable"`
NationalityEnable bool `db:"nationality_enable"`
}

type VerifyUsersQ interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/get_proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
IdentityCounterUpperBound int32
TimestampUpperBound = "0"
eventID = Verifiers(r).EventID
proofSelector = helpers.CalculateProofSelector(userInputs.Uniqueness, userInputs.AgeLowerBound, userInputs.Nationality, true)
proofSelector = helpers.CalculateProofSelector(userInputs.Uniqueness, userInputs.AgeLowerBound, userInputs.Nationality, true, true)
)

if userInputs.EventID != "" {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/handlers/helpers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ func ExtractEventData(getter zk.PubSignalGetter) (string, error) {
return fmt.Sprintf("0x%s", hex.EncodeToString(userIDHash[:])), nil
}

func CalculateProofSelector(uniqueness bool, ageLowerBound int, nationality string, sexEnable bool) int {
func CalculateProofSelector(uniqueness bool, ageLowerBound int, nationality string, sexEnable bool, nationalityEnable bool) int {
var bitLine uint32
bitLine |= 1 << NullifierBit
if nationality != "" {
if nationality != "" || nationalityEnable {
bitLine |= 1 << CitizenshipBit
}
if sexEnable {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetProofParamsById(w http.ResponseWriter, r *http.Request) {
TimestampUpperBound = "0"
eventID = Verifiers(r).EventID
birthDateUpperBound = helpers.CalculateBirthDateHex(existingUser.AgeLowerBound)
proofSelector = helpers.CalculateProofSelector(existingUser.Uniqueness, existingUser.AgeLowerBound, existingUser.Nationality, existingUser.SexEnable)
proofSelector = helpers.CalculateProofSelector(existingUser.Uniqueness, existingUser.AgeLowerBound, existingUser.Nationality, existingUser.SexEnable, existingUser.NationalityEnable)
callbackURL = fmt.Sprintf("%s/integrations/verificator-svc/public/callback/%s", Callback(r).URL, userIDHash)
)

Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/proof_params_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetProofParamsLightById(w http.ResponseWriter, r *http.Request) {
TimestampUpperBound = "0"
eventID = Verifiers(r).EventID
birthDateUpperBound = helpers.CalculateBirthDateHex(existingUser.AgeLowerBound)
proofSelector = helpers.CalculateProofSelector(existingUser.Uniqueness, existingUser.AgeLowerBound, existingUser.Nationality, existingUser.SexEnable)
proofSelector = helpers.CalculateProofSelector(existingUser.Uniqueness, existingUser.AgeLowerBound, existingUser.Nationality, existingUser.SexEnable, existingUser.NationalityEnable)
callbackURL = fmt.Sprintf("%s/integrations/verificator-svc/light/public/callback-sign/%s", Callback(r).URL, userIDHash)
)

Expand Down
9 changes: 9 additions & 0 deletions internal/service/handlers/verification_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
return
}

userNationality, err := helpers.DecimalToHexToUtf8(getter.Get(zk.Citizenship))
if err != nil {
Log(r).WithError(err).Errorf("failed to convert decimal(nationality) to utf8")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

userIDHash, err := helpers.ExtractEventData(getter)
if err != nil {
Log(r).WithError(err).Errorf("failed to extract user hash from event data")
Expand Down Expand Up @@ -88,6 +95,8 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
}
if verifiedUser.Nationality != "" {
verifyOpts = append(verifyOpts, zk.WithCitizenships(verifiedUser.Nationality))
} else {
verifiedUser.Nationality = userNationality
}

err = Verifiers(r).Passport.VerifyProof(proof, verifyOpts...)
Expand Down
4 changes: 4 additions & 0 deletions internal/service/handlers/verification_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func VerificationLink(w http.ResponseWriter, r *http.Request) {
user.SexEnable = *req.Data.Attributes.Sex
}

if req.Data.Attributes.NationalityCheck != nil {
user.NationalityEnable = *req.Data.Attributes.NationalityCheck
}

existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIDHash).Get()
if err != nil {
Log(r).WithError(err).Errorf("failed to query user with userID [%s]", userIdHash)
Expand Down
2 changes: 2 additions & 0 deletions resources/model_user_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type UserAttributes struct {
EventId *string `json:"event_id,omitempty"`
// User nationality
Nationality *string `json:"nationality,omitempty"`
// You can use this instead of 'nationality' params, it will check nationality bit in selector
NationalityCheck *bool `json:"nationality_check,omitempty"`
// Enable verification of sex param
Sex *bool `json:"sex,omitempty"`
// Parameters for checking user uniqueness
Expand Down

0 comments on commit 4f07c00

Please sign in to comment.