Skip to content

Commit

Permalink
change params for verification-link: omitempty (#6)
Browse files Browse the repository at this point in the history
* change params for verification-link: omitempty

* remove unnecessary code

* change selector generation
  • Loading branch information
chabanyknikita authored Sep 3, 2024
1 parent f4a0968 commit 3d27cc8
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 34 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
12 changes: 8 additions & 4 deletions internal/service/handlers/get_proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
eventID = userInputs.EventID
}

proofSelector := CalculateProofSelector(userInputs.Uniqueness)
proofSelector := CalculateProofSelector(userInputs.Uniqueness, userInputs.AgeLowerBound, userInputs.Nationality)
if proofSelector&(1<<timestampUpperBoundBit) != 0 &&
proofSelector&(1<<identityCounterUpperBoundBit) != 0 {
TimestampUpperBound = ProofParameters(r).TimestampUpperBound
Expand Down Expand Up @@ -168,11 +168,15 @@ func CalculateBirthDateHex(ageLowerBound int) string {
return fmt.Sprintf("0x%s", hexBirthDateLoweBound)
}

func CalculateProofSelector(uniqueness bool) int {
func CalculateProofSelector(uniqueness bool, ageLowerBound int, nationality string) int {
var bitLine uint32
bitLine |= 1 << nullifierBit
bitLine |= 1 << citizenshipBit
bitLine |= 1 << birthDateUpperboundBit
if nationality != "" {
bitLine |= 1 << citizenshipBit
}
if ageLowerBound != 0 {
bitLine |= 1 << birthDateUpperboundBit
}
if uniqueness {
bitLine |= 1 << timestampUpperBoundBit
bitLine |= 1 << identityCounterUpperBoundBit
Expand Down
9 changes: 7 additions & 2 deletions internal/service/handlers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ func GetProofParamsById(w http.ResponseWriter, r *http.Request) {
eventID = existingUser.EventId
}

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

proofSelector := CalculateProofSelector(existingUser.Uniqueness, existingUser.AgeLowerBound, existingUser.Nationality)
if proofSelector&(1<<timestampUpperBoundBit) != 0 &&
proofSelector&(1<<identityCounterUpperBoundBit) != 0 {
TimestampUpperBound = ProofParameters(r).TimestampUpperBound
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
30 changes: 21 additions & 9 deletions internal/service/handlers/verification-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,27 @@ 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
}

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 3d27cc8

Please sign in to comment.