From 458dcc6dee501efca45af27909d6762fffab2d05 Mon Sep 17 00:00:00 2001 From: roman_harazha Date: Tue, 20 Feb 2024 18:37:26 +0200 Subject: [PATCH] add endpoint to return document nullifier from hash --- .../components/schemas/DocumentNullifier.yaml | 13 +++++ .../schemas/DocumentNullifierKey.yaml | 11 ++++ ...rovider-service@v1@document-nullifier.yaml | 34 +++++++++++ .../api/handlers/get_document_nullifier.go | 57 +++++++++++++++++++ .../api/requests/get_document_nullifier.go | 29 ++++++++++ internal/service/router.go | 1 + resources/model_document_nullifier.go | 31 ++++++++++ .../model_document_nullifier_attributes.go | 9 +++ resources/model_resource_type.go | 1 + 9 files changed, 186 insertions(+) create mode 100644 docs/spec/components/schemas/DocumentNullifier.yaml create mode 100644 docs/spec/components/schemas/DocumentNullifierKey.yaml create mode 100644 docs/spec/paths/intergrations@identity-provider-service@v1@document-nullifier.yaml create mode 100644 internal/service/api/handlers/get_document_nullifier.go create mode 100644 internal/service/api/requests/get_document_nullifier.go create mode 100644 resources/model_document_nullifier.go create mode 100644 resources/model_document_nullifier_attributes.go diff --git a/docs/spec/components/schemas/DocumentNullifier.yaml b/docs/spec/components/schemas/DocumentNullifier.yaml new file mode 100644 index 0000000..ed5590a --- /dev/null +++ b/docs/spec/components/schemas/DocumentNullifier.yaml @@ -0,0 +1,13 @@ +allOf: + - $ref: '#/components/schemas/DocumentNullifierKey' + - type: object + required: + - attributes + properties: + attributes: + type: object + required: + - document_nullifier_hash + properties: + document_nullifier_hash: + type: string \ No newline at end of file diff --git a/docs/spec/components/schemas/DocumentNullifierKey.yaml b/docs/spec/components/schemas/DocumentNullifierKey.yaml new file mode 100644 index 0000000..4e3ac46 --- /dev/null +++ b/docs/spec/components/schemas/DocumentNullifierKey.yaml @@ -0,0 +1,11 @@ +type: object +required: + - id + - type +properties: + id: + type: string + type: + type: string + enum: + - nullifiers diff --git a/docs/spec/paths/intergrations@identity-provider-service@v1@document-nullifier.yaml b/docs/spec/paths/intergrations@identity-provider-service@v1@document-nullifier.yaml new file mode 100644 index 0000000..fb8c327 --- /dev/null +++ b/docs/spec/paths/intergrations@identity-provider-service@v1@document-nullifier.yaml @@ -0,0 +1,34 @@ +get: + tags: + - Document Nullifier + summary: Document Nullifier retrieving + operationId: document-nullifier + parameters: + - in: query + name: dg2_hash + required: true + schema: + type: string + responses: + '200': + description: Success + content: + application/json: + schema: + type: object + properties: + data: + type: object + $ref: '#/components/schemas/DocumentNullifier' + '500': + description: Internal Error + content: + application/json: + schema: + $ref: '#/components/schemas/Errors' + '400': + description: Bad Request Error + content: + application/json: + schema: + $ref: '#/components/schemas/Errors' \ No newline at end of file diff --git a/internal/service/api/handlers/get_document_nullifier.go b/internal/service/api/handlers/get_document_nullifier.go new file mode 100644 index 0000000..1a7357e --- /dev/null +++ b/internal/service/api/handlers/get_document_nullifier.go @@ -0,0 +1,57 @@ +package handlers + +import ( + "encoding/hex" + "github.com/iden3/go-iden3-crypto/poseidon" + "github.com/rarimo/passport-identity-provider/internal/service/api/requests" + "github.com/rarimo/passport-identity-provider/resources" + "gitlab.com/distributed_lab/ape" + "gitlab.com/distributed_lab/ape/problems" + "math/big" + "net/http" +) + +func GetDocumentNullifier(w http.ResponseWriter, r *http.Request) { + req, err := requests.NewGetDocumentNullifierRequest(r) + if err != nil { + ape.RenderErr(w, problems.BadRequest(err)...) + return + } + + dg2HashBytes, err := hex.DecodeString(req.DG2Hash) + if err != nil { + ape.RenderErr(w, problems.BadRequest(err)...) + return + } + + nullifierHashInput := make([]*big.Int, 0) + if len(dg2HashBytes) >= 32 { + // break data in a half + nullifierHashInput = append(nullifierHashInput, new(big.Int).SetBytes(dg2HashBytes[:len(dg2HashBytes)/2])) + nullifierHashInput = append(nullifierHashInput, new(big.Int).SetBytes(dg2HashBytes[len(dg2HashBytes)/2:])) + } else { + nullifierHashInput = append(nullifierHashInput, new(big.Int).SetBytes(dg2HashBytes)) + } + + nullifierHashInput = append(nullifierHashInput, VerifierConfig(r).Blinder) + + nullifierHash, err := poseidon.Hash(nullifierHashInput) + if err != nil { + Log(r).WithError(err).Error("failed to hash via Poseidon") + ape.RenderErr(w, problems.InternalError()) + return + } + + response := resources.DocumentNullifierResponse{ + Data: resources.DocumentNullifier{ + Key: resources.Key{ + Type: resources.NULLIFIERS, + }, + Attributes: resources.DocumentNullifierAttributes{ + DocumentNullifierHash: nullifierHash.String(), + }, + }, + } + + ape.Render(w, response) +} diff --git a/internal/service/api/requests/get_document_nullifier.go b/internal/service/api/requests/get_document_nullifier.go new file mode 100644 index 0000000..e3af9c2 --- /dev/null +++ b/internal/service/api/requests/get_document_nullifier.go @@ -0,0 +1,29 @@ +package requests + +import ( + validation "github.com/go-ozzo/ozzo-validation/v4" + "gitlab.com/distributed_lab/logan/v3/errors" + "gitlab.com/distributed_lab/urlval" + "net/http" +) + +type GetDocumentNullifierRequest struct { + DG2Hash string `url:"dg2_hash"` +} + +func NewGetDocumentNullifierRequest(r *http.Request) (GetDocumentNullifierRequest, error) { + var req GetDocumentNullifierRequest + + err := urlval.Decode(r.URL.Query(), &req) + if err != nil { + return GetDocumentNullifierRequest{}, errors.Wrap(err, "failed to decode url") + } + + return req, validateGetDocumentNullifierRequest(req) +} + +func validateGetDocumentNullifierRequest(r GetDocumentNullifierRequest) error { + return validation.Errors{ + "/dg2_hash": validation.Validate(r.DG2Hash, validation.Required), + }.Filter() +} diff --git a/internal/service/router.go b/internal/service/router.go index 7c583e9..27eef65 100644 --- a/internal/service/router.go +++ b/internal/service/router.go @@ -44,6 +44,7 @@ func (s *service) router() chi.Router { r.Route("/v1", func(r chi.Router) { r.Post("/create-identity", handlers.CreateIdentity) r.Get("/gist-data", handlers.GetGistData) + r.Get("/document-nullifier", handlers.GetDocumentNullifier) }) }) diff --git a/resources/model_document_nullifier.go b/resources/model_document_nullifier.go new file mode 100644 index 0000000..fd1b45a --- /dev/null +++ b/resources/model_document_nullifier.go @@ -0,0 +1,31 @@ +/* + * GENERATED. Do not modify. Your changes might be overwritten! + */ + +package resources + +type DocumentNullifier struct { + Key + Attributes DocumentNullifierAttributes `json:"attributes"` +} +type DocumentNullifierResponse struct { + Data DocumentNullifier `json:"data"` + Included Included `json:"included"` +} + +type DocumentNullifierListResponse struct { + Data []DocumentNullifier `json:"data"` + Included Included `json:"included"` + Links *Links `json:"links"` +} + +// MustDocumentNullifier - returns DocumentNullifier from include collection. +// if entry with specified key does not exist - returns nil +// if entry with specified key exists but type or ID mismatches - panics +func (c *Included) MustDocumentNullifier(key Key) *DocumentNullifier { + var documentNullifier DocumentNullifier + if c.tryFindEntry(key, &documentNullifier) { + return &documentNullifier + } + return nil +} diff --git a/resources/model_document_nullifier_attributes.go b/resources/model_document_nullifier_attributes.go new file mode 100644 index 0000000..04c3d06 --- /dev/null +++ b/resources/model_document_nullifier_attributes.go @@ -0,0 +1,9 @@ +/* + * GENERATED. Do not modify. Your changes might be overwritten! + */ + +package resources + +type DocumentNullifierAttributes struct { + DocumentNullifierHash string `json:"document_nullifier_hash"` +} diff --git a/resources/model_resource_type.go b/resources/model_resource_type.go index 6f24fdc..3e531e1 100644 --- a/resources/model_resource_type.go +++ b/resources/model_resource_type.go @@ -9,5 +9,6 @@ type ResourceType string // List of ResourceType const ( CLAIMS ResourceType = "claims" + NULLIFIERS ResourceType = "nullifiers" GIST_DATAS ResourceType = "gist_datas" )