Skip to content

Commit

Permalink
change params for verification-link: omitempty
Browse files Browse the repository at this point in the history
  • Loading branch information
chabanyknikita committed Sep 3, 2024
1 parent f4a0968 commit a2d68c5
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 29 deletions.
4 changes: 0 additions & 4 deletions docs/spec/components/schemas/User.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ allOf:
- attributes
properties:
attributes:
required:
- age_lower_bound
- uniqueness
- nationality
properties:
age_lower_bound:
type: integer
Expand Down
6 changes: 3 additions & 3 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ CREATE TABLE verify_users
(
user_id TEXT PRIMARY KEY NOT NULL,
user_id_hash TEXT NOT NULL,
age_lower_bound INT NOT NULL,
nationality TEXT NOT NULL,
age_lower_bound INT,
nationality TEXT,
created_at TIMESTAMP NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc'),
uniqueness BOOLEAN NOT NULL,
event_id TEXT NOT NULL,
event_id TEXT,
status TEXT NOT NULL,
proof JSON NOT NULL
);
Expand Down
1 change: 0 additions & 1 deletion internal/config/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (c *config) Verifiers() Verifiers {
zk.WithVerificationKeyFile(cfg.VerificationKeyPath),
zk.WithPassportRootVerifier(c.passport.ProvideVerifier()),
zk.WithIdentitiesCreationTimestampLimit(cfg.AllowedIdentityTimestamp),
zk.WithEventID(c.ProofParametersConfig().EventID),
)
if err != nil {
panic(fmt.Errorf("failed to initialize passport verifier: %w", err))
Expand Down
2 changes: 1 addition & 1 deletion internal/service/handlers/get_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewProofByUserIDResponse(user data.VerifyUsers) resources.GetProofRequest {
Type: resources.GET_PROOF,
},
Attributes: resources.GetProofAttributes{
Proof: &proof,
Proof: proof,
},
},
}
Expand Down
7 changes: 6 additions & 1 deletion internal/service/handlers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func GetProofParamsById(w http.ResponseWriter, r *http.Request) {
eventID = existingUser.EventId
}

birthDateUpperBound := CalculateBirthDateHex(existingUser.AgeLowerBound)
if existingUser.AgeLowerBound == 0 {
birthDateUpperBound = "0x303030303030"
}

proofSelector := CalculateProofSelector(existingUser.Uniqueness)
if proofSelector&(1<<timestampUpperBoundBit) != 0 &&
proofSelector&(1<<identityCounterUpperBoundBit) != 0 {
Expand All @@ -62,7 +67,7 @@ func GetProofParamsById(w http.ResponseWriter, r *http.Request) {
citizenshipMask: Utf8ToHex(existingUser.Nationality),
timestampLowerBound: "0",
birthDateLowerBound: "0x303030303030",
birthDateUpperBound: CalculateBirthDateHex(existingUser.AgeLowerBound),
birthDateUpperBound: birthDateUpperBound,
expirationDateUpperBound: "52983525027888",
expirationDateLowerBound: "52983525027888",
}
Expand Down
32 changes: 23 additions & 9 deletions internal/service/handlers/verification-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@ func VerificationLink(w http.ResponseWriter, r *http.Request) {
}

user := &data.VerifyUsers{
UserID: req.Data.ID,
UserIDHash: userIdHash,
CreatedAt: time.Now().UTC(),
Status: "not_verified",
Nationality: req.Data.Attributes.Nationality,
EventId: req.Data.Attributes.EventId,
AgeLowerBound: int(req.Data.Attributes.AgeLowerBound),
Uniqueness: req.Data.Attributes.Uniqueness,
Proof: []byte{},
UserID: req.Data.ID,
UserIDHash: userIdHash,
CreatedAt: time.Now().UTC(),
Status: "not_verified",
Proof: []byte{},
}

if req.Data.Attributes.Nationality != nil && *req.Data.Attributes.Nationality != "" {
user.Nationality = *req.Data.Attributes.Nationality
}

if req.Data.Attributes.EventId != nil && *req.Data.Attributes.EventId != "" {
user.EventId = *req.Data.Attributes.EventId
}

if req.Data.Attributes.AgeLowerBound != nil {
user.AgeLowerBound = int(*req.Data.Attributes.AgeLowerBound)
}

if req.Data.Attributes.Uniqueness != nil {
user.Uniqueness = *req.Data.Attributes.Uniqueness
} else {
user.Uniqueness = false
}

existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIDHash).Get()
Expand Down
8 changes: 7 additions & 1 deletion internal/service/handlers/verification_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
return
}

eventID := ProofParameters(r).EventID
if verifiedUser.EventId != "" {
eventID = verifiedUser.EventId
}

selectorInt, err := strconv.Atoi(getter.Get(zk.Selector))
if err != nil {
Log(r).WithError(err).Errorf("cannot extract selector from public signals")
Expand Down Expand Up @@ -97,8 +102,9 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
zk.WithProofSelectorValue(getter.Get(zk.Selector)),
zk.WithIdentitiesCounter(identityCounterUpperBound),
zk.WithAgeAbove(verifiedUser.AgeLowerBound),
zk.WithEventID(eventID),
}
err = Verifiers(r).Passport.VerifyProof(*proof, verifyOpts...)
err = Verifiers(r).Passport.VerifyProof(proof, verifyOpts...)
if err != nil {
var vErr validation.Errors
if errors.As(err, &vErr) {
Expand Down
6 changes: 3 additions & 3 deletions internal/service/requests/verification-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func VerificationLink(r *http.Request) (req resources.UserRequest, err error) {
return req, val.Errors{
"data/id": val.Validate(req.Data.ID, val.Required),
"data/type": val.Validate(req.Data.Type, val.Required, val.In(resources.USER)),
"data/attributes/age_lower_bound": val.Validate(attr.AgeLowerBound, val.Required),
"data/attributes/nationality": val.Validate(attr.Nationality, val.Required),
"data/attributes/event_id": val.Validate(attr.Nationality, val.Required),
"data/attributes/age_lower_bound": val.Validate(attr.AgeLowerBound),
"data/attributes/nationality": val.Validate(attr.Nationality),
"data/attributes/event_id": val.Validate(attr.EventId),
}.Filter()
}
2 changes: 1 addition & 1 deletion resources/model_get_proof_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import "github.com/iden3/go-rapidsnark/types"

type GetProofAttributes struct {
// Query ZK passport verification proof.
Proof *types.ZKProof `json:"proof"`
Proof types.ZKProof `json:"proof"`
}
2 changes: 1 addition & 1 deletion resources/model_proof_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import "github.com/iden3/go-rapidsnark/types"

type ProofAttributes struct {
// Query ZK passport verification proof.
Proof *types.ZKProof `json:"proof"`
Proof types.ZKProof `json:"proof"`
}
8 changes: 4 additions & 4 deletions resources/model_user_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package resources

type UserAttributes struct {
AgeLowerBound int32 `json:"age_lower_bound"`
EventId string `json:"event_id,omitempty"`
Nationality string `json:"nationality"`
Uniqueness bool `json:"uniqueness"`
AgeLowerBound *int32 `json:"age_lower_bound,omitempty"`
EventId *string `json:"event_id,omitempty"`
Nationality *string `json:"nationality,omitempty"`
Uniqueness *bool `json:"uniqueness,omitempty"`
}

0 comments on commit a2d68c5

Please sign in to comment.