Skip to content

Commit

Permalink
add endpoint to return document nullifier from hash
Browse files Browse the repository at this point in the history
  • Loading branch information
freigeistig committed Feb 20, 2024
1 parent 9195aef commit 458dcc6
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/spec/components/schemas/DocumentNullifier.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions docs/spec/components/schemas/DocumentNullifierKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: object
required:
- id
- type
properties:
id:
type: string
type:
type: string
enum:
- nullifiers
Original file line number Diff line number Diff line change
@@ -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'
57 changes: 57 additions & 0 deletions internal/service/api/handlers/get_document_nullifier.go
Original file line number Diff line number Diff line change
@@ -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)
}
29 changes: 29 additions & 0 deletions internal/service/api/requests/get_document_nullifier.go
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down
31 changes: 31 additions & 0 deletions resources/model_document_nullifier.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions resources/model_document_nullifier_attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* GENERATED. Do not modify. Your changes might be overwritten!
*/

package resources

type DocumentNullifierAttributes struct {
DocumentNullifierHash string `json:"document_nullifier_hash"`
}
1 change: 1 addition & 0 deletions resources/model_resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ type ResourceType string
// List of ResourceType
const (
CLAIMS ResourceType = "claims"
NULLIFIERS ResourceType = "nullifiers"
GIST_DATAS ResourceType = "gist_datas"
)

0 comments on commit 458dcc6

Please sign in to comment.