Skip to content

Commit

Permalink
fix: use user id keccak256 hash in event data
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrynenko committed Dec 10, 2024
1 parent be09127 commit f644fcf
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 41 deletions.
10 changes: 2 additions & 8 deletions internal/service/handlers/get_proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,9 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
IdentityCounterUpperBound = 1
}

userIdHash, err := helpers.StringToPoseidonHash(userInputs.UserId)
if err != nil {
Log(r).WithError(err).Errorf("failed to convert user with userID [%s] to poseidon hash", userInputs.UserId)
ape.RenderErr(w, problems.InternalError())
return
}
user := &data.VerifyUsers{
UserID: userInputs.UserId,
UserIDHash: userIdHash,
UserIDHash: helpers.BytesToKeccak256Hash(common.HexToAddress(userInputs.UserId).Bytes()),
CreatedAt: time.Now().UTC(),
Status: "not_verified",
Nationality: userInputs.Nationality,
Expand All @@ -77,7 +71,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
BirthDateUpperBound: helpers.CalculateBirthDateHex(userInputs.AgeLowerBound),
CallbackUrl: fmt.Sprintf("%s/integrations/verificator-svc/public/callback/%s", Callback(r).URL, user.UserIDHash),
CitizenshipMask: helpers.Utf8ToHex(userInputs.Nationality),
EventData: helpers.GetEventData(common.HexToAddress(user.UserID).Bytes()),
EventData: user.UserIDHash,
EventId: eventID,
ExpirationDateLowerBound: expirationLowerBound,
ExpirationDateUpperBound: helpers.DefaultDateHex,
Expand Down
14 changes: 1 addition & 13 deletions internal/service/handlers/helpers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/iden3/go-iden3-crypto/poseidon"
zk "github.com/rarimo/zkverifier-kit"
"github.com/status-im/keycard-go/hexutils"
)
Expand Down Expand Up @@ -59,18 +58,7 @@ func PubSignalsToSha256(pubSignals []string) ([]byte, error) {
return messageHash, nil
}

func StringToPoseidonHash(inputString string) (string, error) {
inputBytes := []byte(inputString)

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

}
return fmt.Sprintf("0x%s", hex.EncodeToString(hash.Bytes())), nil
}

func GetEventData(input []byte) string {
func BytesToKeccak256Hash(input []byte) string {
hashInt := new(big.Int).SetBytes(crypto.Keccak256(common.LeftPadBytes(input, 32)))
mask, _ := new(big.Int).SetString("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
result := new(big.Int).And(hashInt, mask)
Expand Down
3 changes: 1 addition & 2 deletions internal/service/handlers/proof_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"strconv"

"github.com/ethereum/go-ethereum/common"
"github.com/rarimo/verificator-svc/internal/service/handlers/helpers"
"github.com/rarimo/verificator-svc/internal/service/requests"
"github.com/rarimo/verificator-svc/internal/service/responses"
Expand Down Expand Up @@ -67,7 +66,7 @@ func GetProofParamsById(w http.ResponseWriter, r *http.Request) {
BirthDateLowerBound: helpers.DefaultDateHex,
BirthDateUpperBound: birthDateUpperBound,
CitizenshipMask: helpers.Utf8ToHex(existingUser.Nationality),
EventData: helpers.GetEventData(common.HexToAddress(existingUser.UserID).Bytes()),
EventData: existingUser.UserIDHash,
EventId: eventID,
ExpirationDateLowerBound: existingUser.ExpirationLowerBound,
ExpirationDateUpperBound: helpers.DefaultDateHex,
Expand Down
3 changes: 1 addition & 2 deletions internal/service/handlers/proof_params_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"strconv"

"github.com/ethereum/go-ethereum/common"
"github.com/rarimo/verificator-svc/internal/service/handlers/helpers"
"github.com/rarimo/verificator-svc/internal/service/requests"
"github.com/rarimo/verificator-svc/internal/service/responses"
Expand Down Expand Up @@ -67,7 +66,7 @@ func GetProofParamsLightById(w http.ResponseWriter, r *http.Request) {
BirthDateLowerBound: helpers.DefaultDateHex,
BirthDateUpperBound: birthDateUpperBound,
CitizenshipMask: helpers.Utf8ToHex(existingUser.Nationality),
EventData: helpers.GetEventData(common.HexToAddress(existingUser.UserID).Bytes()),
EventData: existingUser.UserIDHash,
EventId: eventID,
ExpirationDateLowerBound: existingUser.ExpirationLowerBound,
ExpirationDateUpperBound: helpers.DefaultDateHex,
Expand Down
10 changes: 2 additions & 8 deletions internal/service/handlers/verification_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/rarimo/verificator-svc/internal/data"
"github.com/rarimo/verificator-svc/internal/service/handlers/helpers"
"github.com/rarimo/verificator-svc/internal/service/requests"
Expand All @@ -25,16 +26,9 @@ func VerificationLink(w http.ResponseWriter, r *http.Request) {
return
}

userIdHash, err := helpers.StringToPoseidonHash(req.Data.ID)
if err != nil {
Log(r).WithError(err).Errorf("failed to convert user with userID [%s] to poseidon hash", req.Data.ID)
ape.RenderErr(w, problems.InternalError())
return
}

user := &data.VerifyUsers{
UserID: req.Data.ID,
UserIDHash: userIdHash,
UserIDHash: helpers.BytesToKeccak256Hash(common.HexToAddress(req.Data.ID).Bytes()),
CreatedAt: time.Now().UTC(),
Status: "not_verified",
Proof: []byte{},
Expand Down
10 changes: 2 additions & 8 deletions internal/service/handlers/verification_link_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/rarimo/verificator-svc/internal/data"
"github.com/rarimo/verificator-svc/internal/service/handlers/helpers"
"github.com/rarimo/verificator-svc/internal/service/requests"
Expand All @@ -25,16 +26,9 @@ func VerificationLinkLight(w http.ResponseWriter, r *http.Request) {
return
}

userIdHash, err := helpers.StringToPoseidonHash(req.Data.ID)
if err != nil {
Log(r).WithError(err).Errorf("failed to convert user with userID [%s] to poseidon hash", req.Data.ID)
ape.RenderErr(w, problems.InternalError())
return
}

user := &data.VerifyUsers{
UserID: req.Data.ID,
UserIDHash: userIdHash,
UserIDHash: helpers.BytesToKeccak256Hash(common.HexToAddress(req.Data.ID).Bytes()),
CreatedAt: time.Now().UTC(),
Status: "not_verified",
Proof: []byte{},
Expand Down

0 comments on commit f644fcf

Please sign in to comment.