github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/presentation_ldp_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package verifiable
     7  
     8  import (
     9  	"encoding/json"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
    15  	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite"
    16  	"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2018"
    17  	jsonutil "github.com/hyperledger/aries-framework-go/pkg/doc/util/json"
    18  	"github.com/hyperledger/aries-framework-go/pkg/kms"
    19  )
    20  
    21  func TestParsePresentationFromLinkedDataProof(t *testing.T) {
    22  	r := require.New(t)
    23  
    24  	signer, err := newCryptoSigner(kms.ED25519Type)
    25  	r.NoError(err)
    26  
    27  	ss := ed25519signature2018.New(suite.WithSigner(signer),
    28  		suite.WithVerifier(ed25519signature2018.NewPublicKeyVerifier())) // todo use crypto verifier
    29  
    30  	ldpContext := &LinkedDataProofContext{
    31  		SignatureType:           "Ed25519Signature2018",
    32  		SignatureRepresentation: SignatureJWS,
    33  		Suite:                   ss,
    34  		VerificationMethod:      "did:example:123456#key1",
    35  	}
    36  
    37  	vc, err := newTestPresentation(t, []byte(validPresentation))
    38  	r.NoError(err)
    39  
    40  	err = vc.AddLinkedDataProof(ldpContext, jsonld.WithDocumentLoader(createTestDocumentLoader(t)))
    41  	r.NoError(err)
    42  
    43  	vcBytes, err := json.Marshal(vc)
    44  	r.NoError(err)
    45  
    46  	vcWithLdp, err := newTestPresentation(t, vcBytes,
    47  		WithPresEmbeddedSignatureSuites(ss),
    48  		WithPresPublicKeyFetcher(SingleKey(signer.PublicKeyBytes(), kms.ED25519)))
    49  	r.NoError(err)
    50  
    51  	r.NoError(err)
    52  	r.Equal(vc, vcWithLdp)
    53  
    54  	// signature suite is not passed, cannot make a proof check
    55  	vcWithLdp, err = newTestPresentation(t, vcBytes)
    56  	r.Error(err)
    57  	require.Nil(t, vcWithLdp)
    58  }
    59  
    60  func TestPresentation_AddLinkedDataProof(t *testing.T) {
    61  	r := require.New(t)
    62  
    63  	signer, err := newCryptoSigner(kms.ED25519Type)
    64  	r.NoError(err)
    65  
    66  	ldpContext := &LinkedDataProofContext{
    67  		SignatureType:           "Ed25519Signature2018",
    68  		SignatureRepresentation: SignatureProofValue,
    69  		Suite:                   ed25519signature2018.New(suite.WithSigner(signer)),
    70  	}
    71  
    72  	t.Run("Add a valid Linked Data proof to VC", func(t *testing.T) {
    73  		vp, err := newTestPresentation(t, []byte(validPresentation))
    74  		r.NoError(err)
    75  
    76  		err = vp.AddLinkedDataProof(ldpContext, jsonld.WithDocumentLoader(createTestDocumentLoader(t)))
    77  		r.NoError(err)
    78  
    79  		err = vp.AddLinkedDataProof(ldpContext, jsonld.WithDocumentLoader(createTestDocumentLoader(t)))
    80  		r.NoError(err)
    81  
    82  		vpJSON, err := vp.MarshalJSON()
    83  		r.NoError(err)
    84  
    85  		vpMap, err := jsonutil.ToMap(vpJSON)
    86  		r.NoError(err)
    87  
    88  		r.Contains(vpMap, "proof")
    89  		vpProof := vpMap["proof"]
    90  		vpProofs, ok := vpProof.([]interface{})
    91  		r.True(ok)
    92  		r.Len(vpProofs, 2)
    93  		newVPProof, ok := vpProofs[1].(map[string]interface{})
    94  		r.True(ok)
    95  		r.Contains(newVPProof, "created")
    96  		r.Contains(newVPProof, "proofValue")
    97  		r.Equal("Ed25519Signature2018", newVPProof["type"])
    98  	})
    99  }