Skip to content

Commit

Permalink
code refractor and add verify uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
chabanyknikita committed Aug 26, 2024
1 parent 10f18b8 commit 0633265
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 48 deletions.
5 changes: 2 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ callback:
url: "http://localhost:8000"

proof_parameters:
event_id: "111186066134341633902189494613533900917417361106374681011849132651019822199"
# selector_unique: "236065"
# selector_not_unique: "236061"
event_id: "event_id"
timestamp_lower_bound: "10000000000"
expiration_date_lower_bound: "313031303130"
expiration_date_upper_bound: "333031303130"
birth_date_upper_bound: "323031303130"

verifier:
verification_key_path: "./proof_keys/passport.json"
allowed_age: 18
allowed_identity_timestamp: 1715698750

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/rarimo/verificator-svc

go 1.22

toolchain go1.22.6
go 1.22.0

require (
github.com/Masterminds/squirrel v1.5.4
Expand Down
2 changes: 1 addition & 1 deletion internal/config/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CallbackConfiger interface {
}

type CallbackConfig struct {
Url string `fig:"url,required"`
URL string `fig:"url,required"`
}

type Callback struct {
Expand Down
3 changes: 0 additions & 3 deletions internal/config/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

zk "github.com/rarimo/zkverifier-kit"
"github.com/rarimo/zkverifier-kit/root"
"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/kv"
)
Expand All @@ -15,8 +14,6 @@ const (

type Verifiers struct {
Passport *zk.Verifier
Poll *zk.Verifier
PollRoot *root.ProposalSMTVerifier
}

func (c *config) Verifiers() Verifiers {
Expand Down
2 changes: 1 addition & 1 deletion internal/data/pg/verify_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (q *VerifyUsersQ) Get() (*data.VerifyUsers, error) {
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,
"user_id_hash": VerifyUsers.UserIDHash,
"age_lower_bound": VerifyUsers.AgeLowerBound,
"nationality": VerifyUsers.Nationality,
"uniqueness": VerifyUsers.Uniqueness,
Expand Down
2 changes: 1 addition & 1 deletion internal/data/verify_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type VerifyUsers struct {
UserID string `db:"user_id"`
UserIdHash string `db:"user_id_hash"`
UserIDHash string `db:"user_id_hash"`
AgeLowerBound int `db:"age_lower_bound"`
Nationality string `db:"nationality"`
CreatedAt time.Time `db:"created_at"`
Expand Down
1 change: 0 additions & 1 deletion internal/service/handlers/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type ctxKey int
const (
logCtxKey ctxKey = iota
verifyUserQCtxKey
verifiersSMTCtxKey
verifiersCtxKey
callbackCtxKey
proofParametersCtxKey
Expand Down
19 changes: 9 additions & 10 deletions internal/service/handlers/get_proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
userInputs, err := requests.NewGetUserInputs(r)
if err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
Log(r).Debug(userInputs.Uniqueness)
return
}

TimestampUpperBound := "0"
var IdentityCounterUpperBound int32 = 0
var IdentityCounterUpperBound int32
proofSelector := CalculateProofSelector(userInputs.Uniqueness)
if proofSelector&(1<<9) != 0 && proofSelector&(1<<11) != 0 {
TimestampUpperBound = ProofParameters(r).TimestampUpperBound
Expand All @@ -52,7 +51,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
}
user := &data.VerifyUsers{
UserID: userInputs.UserId,
UserIdHash: userIdHash,
UserIDHash: userIdHash,
CreatedAt: time.Now().UTC(),
Status: "not_verified",
Nationality: userInputs.Nationality,
Expand All @@ -61,7 +60,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
}

proofParams := ProofParams{
host: Callback(r).Url,
host: Callback(r).URL,
eventID: ProofParameters(r).EventID,
proofSelector: strconv.Itoa(proofSelector),
identityCounterUpperBound: IdentityCounterUpperBound,
Expand All @@ -74,7 +73,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
birthDateUpperBound: ProofParameters(r).BirthDateUpperBound,
}

existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIdHash).Get()
existingUser, err := VerifyUsersQ(r).WhereHashID(user.UserIDHash).Get()
if err != nil {
Log(r).WithError(err).Errorf("failed to query user with userID [%s]", userIdHash)
ape.RenderErr(w, problems.InternalError())
Expand All @@ -87,7 +86,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {

err = VerifyUsersQ(r).Insert(user)
if err != nil {
Log(r).WithError(err).Errorf("failed to insert user with userID [%s]", user.UserIdHash)
Log(r).WithError(err).Errorf("failed to insert user with userID [%s]", user.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -105,9 +104,9 @@ func NewProofParametersResponse(user data.VerifyUsers, params ProofParams) resou
Attributes: resources.ParametersAttributes{
BirthDateLowerBound: params.birthDateLowerBound,
BirthDateUpperBound: params.birthDateUpperBound,
CallbackUrl: fmt.Sprintf("%s/integrations/verificator-svc/public/callback/%s", params.host, user.UserIdHash),
CallbackUrl: fmt.Sprintf("%s/integrations/verificator-svc/public/callback/%s", params.host, user.UserIDHash),
CitizenshipMask: params.citizenshipMask,
EventData: user.UserIdHash,
EventData: user.UserIDHash,
EventId: params.eventID,
ExpirationDateLowerBound: params.expirationDateLowerBound,
ExpirationDateUpperBound: params.expirationDateUpperBound,
Expand All @@ -127,7 +126,7 @@ func StringToPoseidonHash(inputString string) (string, error) {

hash, err := poseidon.HashBytes(inputBytes)
if err != nil {
return "", fmt.Errorf("failde to convert input bytes to hash: %s", err)
return "", fmt.Errorf("failde to convert input bytes to hash: %w", err)

}
return hex.EncodeToString(hash.Bytes()), nil
Expand All @@ -148,7 +147,7 @@ func calculateBirthDateHex(ageLowerBound int) string {
}

func CalculateProofSelector(uniqueness bool) int {
var bitLine uint32 = 0
var bitLine uint32
if uniqueness {
bitLine |= 1 << 9
bitLine |= 1 << 11
Expand Down
44 changes: 19 additions & 25 deletions internal/service/handlers/verification_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
"strconv"
)

const (
selector = "1"
)

func VerificationCallback(w http.ResponseWriter, r *http.Request) {
req, err := requests.GetVerificationCallbackByID(r)
if err != nil {
Expand Down Expand Up @@ -54,7 +50,8 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {

selectorInt, err := strconv.Atoi(proof.PubSignals[zk.Selector])
if err != nil {
fmt.Println("Error during conversion")
Log(r).Error("cannot extract identityUpperBound from public signals")
ape.RenderErr(w, problems.InternalError())
return
}
identityCounterUpperBound, err := strconv.ParseInt(proof.PubSignals[zk.IdentityCounterUpperBound], 10, 64)
Expand All @@ -63,20 +60,17 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.InternalError())
return
}
if verifiedUser.Uniqueness {
if proof.PubSignals[zk.TimestampUpperBound] == ProofParameters(r).TimestampUpperBound {
if selectorInt&1<<9 == 0 {
Log(r).Error("cannot extract timestampUpperBound from public signals")
ape.RenderErr(w, problems.InternalError())
return
}
if proof.PubSignals[zk.IdentityCounterUpperBound] == "0" {
if selectorInt&1<<11 == 0 {
Log(r).Error("cannot extract identityUpperBound from public signals")
ape.RenderErr(w, problems.InternalError())
return
}
}
if verifiedUser.Uniqueness &&
proof.PubSignals[zk.TimestampUpperBound] == ProofParameters(r).TimestampUpperBound {
if selectorInt&(1<<9) == 0 {
Log(r).Error("cannot extract timestampUpperBound from public signals")
ape.RenderErr(w, problems.InternalError())
return
}
if proof.PubSignals[zk.IdentityCounterUpperBound] != "1" || selectorInt&(1<<11) == 0 {
Log(r).Error("cannot extract identityUpperBound from public signals")
ape.RenderErr(w, problems.InternalError())
return
}
}

Expand All @@ -94,7 +88,7 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
verifiedUser.Status = "failed_verification"
err = VerifyUsersQ(r).Update(verifiedUser)
if err != nil {
Log(r).WithError(err).Errorf("failed to update user status for userID [%s]", verifiedUser.UserIdHash)
Log(r).WithError(err).Errorf("failed to update user status for userID [%s]", verifiedUser.UserIDHash)
return
}
Log(r).WithError(err).Error("failed to verify proof")
Expand All @@ -106,7 +100,7 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
verifiedUser.Status = "verified"
err = VerifyUsersQ(r).Update(verifiedUser)
if err != nil {
Log(r).WithError(err).Errorf("failed to update user status for userID [%s]", verifiedUser.UserIdHash)
Log(r).WithError(err).Errorf("failed to update user status for userID [%s]", verifiedUser.UserIDHash)
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -131,12 +125,12 @@ func NewVerificationCallbackResponse(user data.VerifyUsers) resources.StatusResp

func ExtractEventData(proof zkptypes.ZKProof) ([]byte, error) {
getter := zk.PubSignalGetter{Signals: proof.PubSignals, ProofType: zk.GlobalPassport}
userIdHashDecimal, ok := new(big.Int).SetString(getter.Get(zk.EventData), 10)
userIDHashDecimal, ok := new(big.Int).SetString(getter.Get(zk.EventData), 10)
if !ok {
return nil, fmt.Errorf("failed to parse event data")
}
var userIdHash [32]byte
userIdHashDecimal.FillBytes(userIdHash[:])
var userIDHash [32]byte
userIDHashDecimal.FillBytes(userIDHash[:])

return userIdHash[:], nil
return userIDHash[:], nil
}

0 comments on commit 0633265

Please sign in to comment.