Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add verifiable presentations #73

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ components:
type: object
required:
- userDID
- verifiablePresentations
properties:
userDID:
type: string
Expand All @@ -495,6 +496,8 @@ components:
x-omitempty: false
items:
$ref: '#/components/schemas/JWZProofs'
verifiablePresentations:
$ref: '#/components/schemas/VerifiablePresentations'



Expand Down Expand Up @@ -732,6 +735,35 @@ components:
type: string
example: '1234'

VerifiablePresentations:
type: array
items:
$ref: '#/components/schemas/VerifiablePresentation'

VerifiablePresentation:
type: object
required:
- proofType
- schemaContext
- schemaType
- credentialSubject
properties:
proofType:
type: string
example: 'VerifiablePresentation'
schemaContext:
type: array
items:
type: string
example: 'https://www.w3.org/2018/credentials/v1'
schemaType:
type: array
items:
type: string
example: 'KYCAgeCredential'
credentialSubject:
type: object

UUID:
type: string
x-go-type: uuid.UUID
Expand Down
16 changes: 14 additions & 2 deletions internal/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 47 additions & 16 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/iden3/go-iden3-auth/v2/pubsignals"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/iden3/go-jwz/v2"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
Expand All @@ -33,9 +34,6 @@ const (
statusPending = "pending"
statusSuccess = "success"
statusError = "error"
mumbaiNetwork = "80001"
mainnetNetwork = "137"
amoyNetwork = "80002"
defaultReason = "for testing purposes"
defaultBigIntBase = 10
)
Expand Down Expand Up @@ -201,14 +199,8 @@ func (s *Server) Status(_ context.Context, request StatusRequestObject) (StatusR
id := request.Params.SessionID
item, ok := s.cache.Get(id.String())
if !ok {
log.WithFields(log.Fields{
"sessionID": id,
}).Error("sessionID not found")
return Status404JSONResponse{
N404JSONResponse: N404JSONResponse{
Message: "sessionID not found",
},
}, nil
log.WithFields(log.Fields{"sessionID": id}).Error("sessionID not found")
return Status404JSONResponse{N404JSONResponse: N404JSONResponse{Message: "sessionID not found"}}, nil
}

switch value := item.(type) {
Expand All @@ -222,11 +214,45 @@ func (s *Server) Status(_ context.Context, request StatusRequestObject) (StatusR
Message: common.ToPointer(value.Error()),
}, nil
case models.VerificationResponse:
return getStatusVerificationResponse(value), nil
vps, err := getVerifiablePresentations(value.Jwz)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("failed to get verifiable presentations")
return Status200JSONResponse{
Status: statusError,
Message: common.ToPointer(err.Error()),
}, nil
}
return getStatusVerificationResponse(value, vps), nil
}
return nil, nil
}

func getVerifiablePresentations(jwzToken string) (VerifiablePresentations, error) {
token, err := jwz.Parse(jwzToken)
if err != nil {
return nil, err
}

var payload models.JWZPayload
if err := json.Unmarshal(token.GetPayload(), &payload); err != nil {
return nil, err
}

resp := make(VerifiablePresentations, 0, len(payload.Body.Scope))
for _, scope := range payload.Body.Scope {
if scope.Vp.VerifiableCredential.CredentialSubject == nil {
continue
}
resp = append(resp, VerifiablePresentation{
CredentialSubject: scope.Vp.VerifiableCredential.CredentialSubject,
ProofType: scope.Vp.Type,
SchemaContext: scope.Vp.VerifiableCredential.Context,
SchemaType: scope.Vp.VerifiableCredential.Type,
})
}
return resp, nil
}

func documentation(w http.ResponseWriter, _ *http.Request) {
writeFile("api/spec.html", "text/html; charset=UTF-8", w)
}
Expand Down Expand Up @@ -323,7 +349,7 @@ func getInvokeContractQRCode(request protocol.ContractInvokeRequestMessage) QRCo

func validateOffChainRequest(request SignInRequestObject) error {
if request.Body.ChainID == nil {
return fmt.Errorf("field chainId is empty expected %s or %s or %s", mumbaiNetwork, mainnetNetwork, amoyNetwork)
return errors.New("field chainId is empty")
}

if err := validateRequestQuery(true, request.Body.Scope); err != nil {
Expand Down Expand Up @@ -592,8 +618,13 @@ func getVerificationResponseScopes(scopes []protocol.ZeroKnowledgeProofResponse)
return resp, nil
}

func getStatusVerificationResponse(verification models.VerificationResponse) Status200JSONResponse {
jwzMetadata := &JWZMetadata{UserDID: verification.UserDID}
func getStatusVerificationResponse(verification models.VerificationResponse, vcs VerifiablePresentations) Status200JSONResponse {
jwzMetadata := &JWZMetadata{
UserDID: verification.UserDID,
}
if len(vcs) > 0 {
jwzMetadata.VerifiablePresentations = vcs
}

if len(verification.Scopes) > 0 {
nullifiers := make([]JWZProofs, 0, len(verification.Scopes))
Expand All @@ -608,8 +639,8 @@ func getStatusVerificationResponse(verification models.VerificationResponse) Sta
}

return Status200JSONResponse{
Status: statusSuccess,
Jwz: common.ToPointer(verification.Jwz),
JwzMetadata: jwzMetadata,
Status: statusSuccess,
}
}
5 changes: 4 additions & 1 deletion internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
"github.com/0xPolygonID/verifier-backend/internal/common"
)

const mumbaiSenderDID = "did:polygonid:polygon:mumbai:2qCU58EJgrELdThzMyykDwT5kWff6XSbpSWtTQ7oS8"
const (
mumbaiSenderDID = "did:polygonid:polygon:mumbai:2qCU58EJgrELdThzMyykDwT5kWff6XSbpSWtTQ7oS8"
mumbaiNetwork = "80001"
)

func TestSignIn(t *testing.T) {
ctx := context.Background()
Expand Down
50 changes: 50 additions & 0 deletions internal/models/payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package models

// JWZPayload is the struct that represents the payload of the message
type JWZPayload struct {
Id string `json:"id"`
Typ string `json:"typ"`
Type string `json:"type"`
Thid string `json:"thid"`
Body struct {
DidDoc struct {
Context []string `json:"context"`
Id string `json:"id"`
Service []struct {
Id string `json:"id"`
Type string `json:"type"`
ServiceEndpoint string `json:"serviceEndpoint"`
Metadata struct {
Devices []struct {
Ciphertext string `json:"ciphertext"`
Alg string `json:"alg"`
} `json:"devices"`
} `json:"metadata"`
} `json:"service"`
} `json:"did_doc"`
Message interface{} `json:"message"`
Scope []struct {
Proof struct {
PiA []string `json:"pi_a"`
PiB [][]string `json:"pi_b"`
PiC []string `json:"pi_c"`
Protocol string `json:"protocol"`
Curve string `json:"curve"`
} `json:"proof"`
PubSignals []string `json:"pub_signals"`
Id int `json:"id"`
CircuitId string `json:"circuitId"`
Vp struct {
Context []string `json:"@context"`
Type string `json:"@type"`
VerifiableCredential struct {
Context []string `json:"@context"`
Type []string `json:"@type"`
CredentialSubject map[string]any `json:"credentialSubject"`
} `json:"verifiableCredential"`
} `json:"vp,omitempty"`
} `json:"scope"`
} `json:"body"`
From string `json:"from"`
To string `json:"to"`
}
Loading