github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/support_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 _ "embed" 10 "encoding/json" 11 "testing" 12 "time" 13 14 "github.com/stretchr/testify/require" 15 16 "github.com/hyperledger/aries-framework-go/pkg/crypto/tinkcrypto" 17 "github.com/hyperledger/aries-framework-go/pkg/doc/ld" 18 "github.com/hyperledger/aries-framework-go/pkg/doc/ldcontext" 19 jsonldsig "github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld" 20 "github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite" 21 "github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2018" 22 "github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier" 23 "github.com/hyperledger/aries-framework-go/pkg/doc/util/signature" 24 "github.com/hyperledger/aries-framework-go/pkg/internal/ldtestutil" 25 kmsapi "github.com/hyperledger/aries-framework-go/pkg/kms" 26 "github.com/hyperledger/aries-framework-go/pkg/kms/localkms" 27 mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms" 28 "github.com/hyperledger/aries-framework-go/pkg/mock/storage" 29 "github.com/hyperledger/aries-framework-go/pkg/secretlock/noop" 30 ) 31 32 //go:embed testdata/valid_credential.jsonld 33 var validCredential string //nolint:gochecknoglobals 34 35 //go:embed testdata/credential_without_issuancedate.jsonld 36 var credentialWithoutIssuanceDate string //nolint:gochecknoglobals 37 38 func (rc *rawCredential) stringJSON(t *testing.T) string { 39 bytes, err := json.Marshal(rc) 40 require.NoError(t, err) 41 42 return string(bytes) 43 } 44 45 func (vc *Credential) stringJSON(t *testing.T) string { 46 bytes, err := json.Marshal(vc) 47 require.NoError(t, err) 48 49 return string(bytes) 50 } 51 52 func (vc *Credential) byteJSON(t *testing.T) []byte { 53 bytes, err := json.Marshal(vc) 54 require.NoError(t, err) 55 56 return bytes 57 } 58 59 func (rp *rawPresentation) stringJSON(t *testing.T) string { 60 bytes, err := json.Marshal(rp) 61 require.NoError(t, err) 62 63 return string(bytes) 64 } 65 66 func (vp *Presentation) stringJSON(t *testing.T) string { 67 bytes, err := json.Marshal(vp) 68 require.NoError(t, err) 69 70 return string(bytes) 71 } 72 73 func createVCWithLinkedDataProof(t *testing.T) (*Credential, PublicKeyFetcher) { 74 t.Helper() 75 76 vc, err := ParseCredential([]byte(validCredential), 77 WithJSONLDDocumentLoader(createTestDocumentLoader(t)), 78 WithDisabledProofCheck()) 79 80 require.NoError(t, err) 81 82 created := time.Now() 83 84 signer, err := newCryptoSigner(kmsapi.ED25519Type) 85 if err != nil { 86 panic(err) 87 } 88 89 err = vc.AddLinkedDataProof(&LinkedDataProofContext{ 90 SignatureType: "Ed25519Signature2018", 91 Suite: ed25519signature2018.New(suite.WithSigner(signer)), 92 SignatureRepresentation: SignatureJWS, 93 Created: &created, 94 VerificationMethod: "did:123#any", 95 }, jsonldsig.WithDocumentLoader(createTestDocumentLoader(t))) 96 97 require.NoError(t, err) 98 99 return vc, SingleKey(signer.PublicKeyBytes(), kmsapi.ED25519) 100 } 101 102 func createVCWithTwoLinkedDataProofs(t *testing.T) (*Credential, PublicKeyFetcher) { 103 t.Helper() 104 105 vc, err := ParseCredential([]byte(validCredential), 106 WithJSONLDDocumentLoader(createTestDocumentLoader(t)), 107 WithDisabledProofCheck()) 108 109 require.NoError(t, err) 110 111 created := time.Now() 112 113 signer1, err := newCryptoSigner(kmsapi.ED25519Type) 114 if err != nil { 115 panic(err) 116 } 117 118 err = vc.AddLinkedDataProof(&LinkedDataProofContext{ 119 SignatureType: "Ed25519Signature2018", 120 Suite: ed25519signature2018.New(suite.WithSigner(signer1)), 121 SignatureRepresentation: SignatureJWS, 122 Created: &created, 123 VerificationMethod: "did:123#key1", 124 }, jsonldsig.WithDocumentLoader(createTestDocumentLoader(t))) 125 126 require.NoError(t, err) 127 128 signer2, err := newCryptoSigner(kmsapi.ED25519Type) 129 if err != nil { 130 panic(err) 131 } 132 133 err = vc.AddLinkedDataProof(&LinkedDataProofContext{ 134 SignatureType: "Ed25519Signature2018", 135 Suite: ed25519signature2018.New(suite.WithSigner(signer2)), 136 SignatureRepresentation: SignatureJWS, 137 Created: &created, 138 VerificationMethod: "did:123#key2", 139 }, jsonldsig.WithDocumentLoader(createTestDocumentLoader(t))) 140 141 require.NoError(t, err) 142 143 return vc, func(issuerID, keyID string) (*verifier.PublicKey, error) { 144 switch keyID { 145 case "#key1": 146 return &verifier.PublicKey{ 147 Type: "Ed25519Signature2018", 148 Value: signer1.PublicKeyBytes(), 149 }, nil 150 151 case "#key2": 152 return &verifier.PublicKey{ 153 Type: "Ed25519Signature2018", 154 Value: signer2.PublicKeyBytes(), 155 }, nil 156 } 157 158 panic("invalid keyID") 159 } 160 } 161 162 func createKMS() (*localkms.LocalKMS, error) { 163 p, err := mockkms.NewProviderForKMS(storage.NewMockStoreProvider(), &noop.NoLock{}) 164 if err != nil { 165 return nil, err 166 } 167 168 return localkms.New("local-lock://custom/master/key/", p) 169 } 170 171 func newCryptoSigner(keyType kmsapi.KeyType) (signature.Signer, error) { 172 localKMS, err := createKMS() 173 if err != nil { 174 return nil, err 175 } 176 177 tinkCrypto, err := tinkcrypto.New() 178 if err != nil { 179 return nil, err 180 } 181 182 return signature.NewCryptoSigner(tinkCrypto, localKMS, keyType) 183 } 184 185 func createTestDocumentLoader(t *testing.T, extraContexts ...ldcontext.Document) *ld.DocumentLoader { 186 t.Helper() 187 188 loader, err := ldtestutil.DocumentLoader(extraContexts...) 189 require.NoError(t, err) 190 191 return loader 192 } 193 194 func parseTestCredential(t *testing.T, vcData []byte, opts ...CredentialOpt) (*Credential, error) { 195 t.Helper() 196 197 return ParseCredential(vcData, 198 append([]CredentialOpt{WithJSONLDDocumentLoader(createTestDocumentLoader(t))}, opts...)...) 199 } 200 201 func newTestPresentation(t *testing.T, vpData []byte, opts ...PresentationOpt) (*Presentation, error) { 202 t.Helper() 203 204 return ParsePresentation(vpData, 205 append([]PresentationOpt{WithPresJSONLDDocumentLoader(createTestDocumentLoader(t))}, opts...)...) 206 }