Skip to content

Commit

Permalink
removed CredentialProof
Browse files Browse the repository at this point in the history
Signed-off-by: pasquale95 <[email protected]>
  • Loading branch information
pasquale95 committed Nov 18, 2024
1 parent 5981688 commit d289cd5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 105 deletions.
19 changes: 7 additions & 12 deletions internal/core/signature_proof_suite_2020.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,21 +330,16 @@ func (s *SignatureProofSuite2020) createVerifyDocumentData(credential model.Json
// unsignedProof *model.CredentialProof
// normalizedProof []string
// err error
func (s *SignatureProofSuite2020) createVerifyProofData(proof model.JsonLdProof) (*model.CredentialProof, []string, error) {
unsignedProof, err := model.CredentialProofFromMap(proof, false)
if err != nil {
return nil, nil, err
}
func (s *SignatureProofSuite2020) createVerifyProofData(proof model.JsonLdProof) (model.JsonLdProof, []string, error) {
unsignedProof := deepCopyMap(proof)

unsignedProof.Nonce = ""
unsignedProof.ProofValue = ""
// add proof context if it is compacted
model.AddContextToJsonLdProof(unsignedProof)

unsignedProofMap, err := model.CredentialProofToMap(unsignedProof)
if err != nil {
return nil, nil, err
}
delete(unsignedProof, c.CredentialFieldNonce)
delete(unsignedProof, c.CredentialFieldProofValue)

proofBytes, err := json.Marshal(unsignedProofMap)
proofBytes, err := json.Marshal(unsignedProof)
if err != nil {
return nil, nil, err
}
Expand Down
26 changes: 13 additions & 13 deletions internal/core/signature_suite_2020.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func (s *SignatureSuite2020) Sign(credential model.JsonLdCredentialNoProof) (mod
return nil, "", err
}

proof.ProofValue = signature
proof.Context = nil // Delete context since it is not needed for representation -> compact proof format
// TODO: support the possibility to add the new proof to the list of existing proofs -> support array of proofs
model.DeleteContextFromJsonLdProof(proof) // Delete context since it is not needed for representation -> compact proof format
proof[c.CredentialFieldProofValue] = signature
credCopy[c.CredentialFieldProof] = proof

jsonLdDoc, err := json.Marshal(credCopy)
Expand Down Expand Up @@ -113,12 +113,15 @@ func (s *SignatureSuite2020) ProvideSigningData(credential model.JsonLdCredentia
delete(credCopy, c.CredentialFieldProof)
delete(proof, c.CredentialFieldProofValue)

fullProof, err := model.CredentialProofFromMap(proof, false)
if err != nil {
return nil, err
// add proof context if it is compacted
if proof[c.CredentialFieldContext] == nil {
proof[c.CredentialFieldContext] = []string{
c.ContextCredentialV1,
c.ContextSecurityBbsV1,
}
}

return s.prepareDataForSigning(credCopy, fullProof)
return s.prepareDataForSigning(credCopy, proof)
}

// Verify verifies a signed JSON-LD credential.
Expand Down Expand Up @@ -173,18 +176,15 @@ func (s *SignatureSuite2020) Verify(credential model.JsonLdCredential) *model.Ve
//
// proof *model.CredentialProof
// err error
func (s *SignatureSuite2020) createUnsignedProof() (*model.CredentialProof, error) {
func (s *SignatureSuite2020) createUnsignedProof() (model.JsonLdProof, error) {
// TODO add option to use custom verification method
verificationMethod, err := s.keyEncoder.CreateDidKeyVerificationMethod(s.publicKey)
if err != nil {
return nil, err
}
proof := model.CreateDefaultJsonLDProof(verificationMethod, false)

partialProof := model.JsonLdProof{
c.CredentialFieldVerificationMethod: verificationMethod,
}

return model.CredentialProofFromMap(partialProof, false)
return proof, nil
}

// prepareDataForSigning Transform a JSON-LD credential and the associated proof to a list of normalized messages
Expand All @@ -197,7 +197,7 @@ func (s *SignatureSuite2020) createUnsignedProof() (*model.CredentialProof, erro
//
// messages [][]byte
// err error
func (s *SignatureSuite2020) prepareDataForSigning(credential model.JsonLdCredentialNoProof, unsignedProof *model.CredentialProof) ([][]byte, error) {
func (s *SignatureSuite2020) prepareDataForSigning(credential model.JsonLdCredentialNoProof, unsignedProof model.JsonLdProof) ([][]byte, error) {
// 1. Normalize the JSON-LD unsigned credential
credCopy, err := json.Marshal(credential)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions model/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ type JsonLdCredentialNoProof = map[string]interface{}

// JsonLdFrame The JSON-LD frame document.
type JsonLdFrame = map[string]interface{}

// JsonLdProof The JSON-LD Proof.
type JsonLdProof = map[string]interface{}
105 changes: 28 additions & 77 deletions model/proof.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,53 @@
package model

import (
"encoding/json"
"fmt"
"time"

c "github.com/hyperledger-labs/jsonld-vc-bbs-go/constants"
)

// CredentialProof The JSON-LD Proof.
type CredentialProof struct {
Context []string `json:"@context,omitempty"`
Type string `json:"type"`
Created string `json:"created"`
ProofPurpose string `json:"proofPurpose"`
VerificationMethod string `json:"verificationMethod"`
Nonce string `json:"nonce,omitempty"`
ProofValue string `json:"proofValue,omitempty"`
}
// JsonLdProof The JSON-LD Proof.
type JsonLdProof = map[string]interface{}

// CredentialProofFromMap Convert a map to a proper CredentialProof.
// AddContextToJsonLdProof Add the default context to the JSON-LD proof, if none is provided.
//
// proof JsonLDProof The proof as map.
// compact bool If true, compact the returned proof removing the '@context' field.
//
// returns:
//
// credentialProof *CredentialProof
// err error
func CredentialProofFromMap(proof JsonLdProof, compact bool) (*CredentialProof, error) {
now := time.Now().UTC().Format(c.ProofTimestampFormat)

defaultProof := &CredentialProof{
Created: now,
Type: c.CredentialProofTypeBbsBlsSig2020,
ProofPurpose: c.CredentialProofPurpose,
}

if val, ok := proof[c.CredentialFieldCreated]; ok {
defaultProof.Created = val.(string)
}

if val, ok := proof[c.CredentialFieldType]; ok {
defaultProof.Type = val.(string)
}

if val, ok := proof[c.CredentialFieldProofPurpose]; ok {
defaultProof.ProofPurpose = val.(string)
}

if !compact {
// add context only if the proof does not need to be compact
if val, ok := proof[c.CredentialFieldContext]; ok && !compact {
defaultProof.Context = val.([]string)
} else {
defaultProof.Context = []string{
c.ContextCredentialV1,
c.ContextSecurityBbsV1,
}
func AddContextToJsonLdProof(proof JsonLdProof) {
// add proof context if it is compacted
if proof[c.CredentialFieldContext] == nil {
proof[c.CredentialFieldContext] = []string{
c.ContextCredentialV1,
c.ContextSecurityBbsV1,
}
}
}

if val, ok := proof[c.CredentialFieldNonce]; ok {
defaultProof.Nonce = val.(string)
}

if val, ok := proof[c.CredentialFieldVerificationMethod]; ok {
defaultProof.VerificationMethod = val.(string)
} else {
return nil, fmt.Errorf("verification method is required")
}

if val, ok := proof[c.CredentialFieldProofValue]; ok {
defaultProof.ProofValue = val.(string)
}

return defaultProof, nil
// DeleteContextFromJsonLdProof Delete the '@context' field frmo the JSON-LD Proof.
//
// proof JsonLDProof The proof as map.
func DeleteContextFromJsonLdProof(proof JsonLdProof) {
delete(proof, c.CredentialFieldContext)
}

// CredentialProofToMap Convert a CredentialProof to a map.
// CreateDefaultJsonLDProof Create a default JSON-LD Proof object.
//
// proof *CredentialProof The proof to convert.
// verificationMethod string The verification method to embed in the proof.
// compact bool If true, skip the addition of the '@context' in the proof object.
//
// returns:
//
// proofMap JsonLDProof
// err error
func CredentialProofToMap(proof *CredentialProof) (JsonLdProof, error) {
var proofMap JsonLdProof
proofBytes, err := json.Marshal(proof)
if err != nil {
return nil, err
// proof JsonLdProof The JSON-LD proof.
func CreateDefaultJsonLDProof(verificationMethod string, compact bool) JsonLdProof {
defaultProof := JsonLdProof{
c.CredentialFieldCreated: time.Now().UTC().Format(c.ProofTimestampFormat),
c.CredentialFieldVerificationMethod: verificationMethod,
c.CredentialFieldType: c.CredentialProofTypeBbsBlsSig2020,
c.CredentialFieldProofPurpose: c.CredentialProofPurpose,
}

err = json.Unmarshal(proofBytes, &proofMap)
if err != nil {
return nil, err
if !compact {
AddContextToJsonLdProof(defaultProof)
}

return proofMap, nil
return defaultProof
}

0 comments on commit d289cd5

Please sign in to comment.