github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/middleware/presentproof/middlewares_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package presentproof 8 9 import ( 10 "encoding/base64" 11 "errors" 12 "fmt" 13 "testing" 14 "time" 15 16 "github.com/golang/mock/gomock" 17 "github.com/google/uuid" 18 "github.com/stretchr/testify/require" 19 20 "github.com/hyperledger/aries-framework-go/pkg/crypto/tinkcrypto" 21 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 22 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator" 23 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/presentproof" 24 "github.com/hyperledger/aries-framework-go/pkg/doc/did" 25 "github.com/hyperledger/aries-framework-go/pkg/doc/presexch" 26 "github.com/hyperledger/aries-framework-go/pkg/doc/util" 27 "github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" 28 mocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/didcomm/protocol/middleware/presentproof" 29 mocksvdr "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/framework/aries/api/vdr" 30 mocksstore "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/store/verifiable" 31 "github.com/hyperledger/aries-framework-go/pkg/internal/ldtestutil" 32 "github.com/hyperledger/aries-framework-go/pkg/kms/localkms" 33 mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms" 34 "github.com/hyperledger/aries-framework-go/pkg/mock/storage" 35 "github.com/hyperledger/aries-framework-go/pkg/secretlock/noop" 36 ) 37 38 // nolint: gochecknoglobals 39 var pubKey = did.VerificationMethod{ 40 ID: "key-1", 41 Value: []byte{ 42 4, 16, 126, 7, 23, 189, 115, 35, 32, 21, 198, 238, 70, 16, 248, 224, 89, 186, 32, 183, 203, 43 165, 131, 195, 151, 195, 0, 166, 188, 161, 91, 84, 2, 110, 107, 201, 231, 131, 158, 200, 66, 150, 158, 47, 44 18, 116, 124, 119, 239, 120, 165, 79, 150, 153, 183, 193, 209, 149, 230, 216, 19, 197, 26, 135, 19, 45 }, 46 } 47 48 const vpJWS = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiIsImtpZCI6ImRpZDpleGFtcGxlOmViZmViMWY3MTJlYmM2ZjFjMjc2ZTEyZWMyMSNrZXktMSJ9.eyJpc3MiOiJkaWQ6ZXhhbXBsZTplYmZlYjFmNzEyZWJjNmYxYzI3NmUxMmVjMjEiLCJqdGkiOiJ1cm46dXVpZDozOTc4MzQ0Zi04NTk2LTRjM2EtYTk3OC04ZmNhYmEzOTAzYzUiLCJ2cCI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSIsImh0dHBzOi8vd3d3LnczLm9yZy8yMDE4L2NyZWRlbnRpYWxzL2V4YW1wbGVzL3YxIl0sInR5cGUiOlsiVmVyaWZpYWJsZVByZXNlbnRhdGlvbiIsIlVuaXZlcnNpdHlEZWdyZWVDcmVkZW50aWFsIl0sInZlcmlmaWFibGVDcmVkZW50aWFsIjpbeyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSIsImh0dHBzOi8vd3d3LnczLm9yZy8yMDE4L2NyZWRlbnRpYWxzL2V4YW1wbGVzL3YxIl0sImNyZWRlbnRpYWxTY2hlbWEiOltdLCJjcmVkZW50aWFsU3ViamVjdCI6eyJkZWdyZWUiOnsidHlwZSI6IkJhY2hlbG9yRGVncmVlIiwidW5pdmVyc2l0eSI6Ik1JVCJ9LCJpZCI6ImRpZDpleGFtcGxlOmViZmViMWY3MTJlYmM2ZjFjMjc2ZTEyZWMyMSIsIm5hbWUiOiJKYXlkZW4gRG9lIiwic3BvdXNlIjoiZGlkOmV4YW1wbGU6YzI3NmUxMmVjMjFlYmZlYjFmNzEyZWJjNmYxIn0sImV4cGlyYXRpb25EYXRlIjoiMjAyMC0wMS0wMVQxOToyMzoyNFoiLCJpZCI6Imh0dHA6Ly9leGFtcGxlLmVkdS9jcmVkZW50aWFscy8xODcyIiwiaXNzdWFuY2VEYXRlIjoiMjAxMC0wMS0wMVQxOToyMzoyNFoiLCJpc3N1ZXIiOnsiaWQiOiJkaWQ6ZXhhbXBsZTo3NmUxMmVjNzEyZWJjNmYxYzIyMWViZmViMWYiLCJuYW1lIjoiRXhhbXBsZSBVbml2ZXJzaXR5In0sInJlZmVyZW5jZU51bWJlciI6ODMyOTQ4NDcsInR5cGUiOlsiVmVyaWZpYWJsZUNyZWRlbnRpYWwiLCJVbml2ZXJzaXR5RGVncmVlQ3JlZGVudGlhbCJdfV19fQ.3_YUGWINd75e5ijLpLNGJGVpg8-eSrEsPpwrKfiakx28OVoP0Hn1U4kGzT2WUzO32M9O7CsvxQMZBJvJNDnLSA" //nolint:lll 49 50 // nolint: gochecknoglobals 51 var ( 52 strFilterType = "string" 53 arrFilterType = "array" 54 ) 55 56 func TestSavePresentation(t *testing.T) { 57 ctrl := gomock.NewController(t) 58 defer ctrl.Finish() 59 60 provider := mocks.NewMockProvider(ctrl) 61 provider.EXPECT().VDRegistry().Return(nil).AnyTimes() 62 provider.EXPECT().VerifiableStore().Return(nil).AnyTimes() 63 provider.EXPECT().JSONLDDocumentLoader().Return(nil).AnyTimes() 64 65 next := presentproof.HandlerFunc(func(metadata presentproof.Metadata) error { 66 return nil 67 }) 68 69 t.Run("Ignores processing", func(t *testing.T) { 70 metadata := mocks.NewMockMetadata(ctrl) 71 metadata.EXPECT().StateName().Return("state-name") 72 require.NoError(t, SavePresentation(provider)(next).Handle(metadata)) 73 }) 74 75 t.Run("Presentations not provided", func(t *testing.T) { 76 metadata := mocks.NewMockMetadata(ctrl) 77 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 78 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV2{ 79 Type: presentproof.PresentationMsgTypeV2, 80 })) 81 82 err := SavePresentation(provider)(next).Handle(metadata) 83 require.EqualError(t, err, "presentations were not provided") 84 }) 85 86 t.Run("Marshal presentation error", func(t *testing.T) { 87 metadata := mocks.NewMockMetadata(ctrl) 88 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 89 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV2{ 90 Type: presentproof.PresentationMsgTypeV2, 91 PresentationsAttach: []decorator.Attachment{ 92 {Data: decorator.AttachmentData{JSON: struct{ C chan int }{}}}, 93 }, 94 })) 95 96 err := SavePresentation(provider)(next).Handle(metadata) 97 require.Contains(t, fmt.Sprintf("%v", err), "json: unsupported type") 98 }) 99 100 t.Run("Decode error", func(t *testing.T) { 101 metadata := mocks.NewMockMetadata(ctrl) 102 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 103 metadata.EXPECT().Message().Return(service.DIDCommMsgMap{"@type": map[int]int{}}) 104 105 err := SavePresentation(provider)(next).Handle(metadata) 106 require.Contains(t, fmt.Sprintf("%v", err), "got unconvertible type") 107 }) 108 109 t.Run("Invalid presentation", func(t *testing.T) { 110 metadata := mocks.NewMockMetadata(ctrl) 111 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 112 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV2{ 113 Type: presentproof.PresentationMsgTypeV2, 114 PresentationsAttach: []decorator.Attachment{ 115 {Data: decorator.AttachmentData{JSON: &verifiable.Presentation{ 116 Context: []string{"https://www.w3.org/2018/presentation/v1"}, 117 }}}, 118 }, 119 })) 120 121 err := SavePresentation(provider)(next).Handle(metadata) 122 require.Contains(t, fmt.Sprintf("%v", err), "to verifiable presentation") 123 }) 124 125 t.Run("DB error", func(t *testing.T) { 126 const ( 127 vcName = "vp-name" 128 errMsg = "error message" 129 ) 130 131 metadata := mocks.NewMockMetadata(ctrl) 132 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 133 metadata.EXPECT().PresentationNames().Return([]string{vcName}).Times(2) 134 metadata.EXPECT().Properties().Return(map[string]interface{}{ 135 myDIDKey: myDIDKey, 136 theirDIDKey: theirDIDKey, 137 }) 138 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV2{ 139 Type: presentproof.PresentationMsgTypeV2, 140 PresentationsAttach: []decorator.Attachment{ 141 {Data: decorator.AttachmentData{Base64: base64.StdEncoding.EncodeToString([]byte(vpJWS))}}, 142 }, 143 })) 144 145 verifiableStore := mocksstore.NewMockStore(ctrl) 146 verifiableStore.EXPECT().SavePresentation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). 147 Return(errors.New(errMsg)) 148 149 loader, err := ldtestutil.DocumentLoader() 150 require.NoError(t, err) 151 152 registry := mocksvdr.NewMockRegistry(ctrl) 153 registry.EXPECT().Resolve("did:example:ebfeb1f712ebc6f1c276e12ec21").Return( 154 &did.DocResolution{DIDDocument: &did.Doc{VerificationMethod: []did.VerificationMethod{pubKey}}}, nil) 155 156 provider := mocks.NewMockProvider(ctrl) 157 provider.EXPECT().VDRegistry().Return(registry).AnyTimes() 158 provider.EXPECT().VerifiableStore().Return(verifiableStore) 159 provider.EXPECT().JSONLDDocumentLoader().Return(loader) 160 161 require.EqualError(t, SavePresentation(provider)(next).Handle(metadata), "save presentation: "+errMsg) 162 }) 163 164 t.Run("No DIDs", func(t *testing.T) { 165 metadata := mocks.NewMockMetadata(ctrl) 166 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 167 metadata.EXPECT().Properties().Return(map[string]interface{}{}) 168 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV2{ 169 Type: presentproof.PresentationMsgTypeV2, 170 PresentationsAttach: []decorator.Attachment{ 171 {Data: decorator.AttachmentData{Base64: base64.StdEncoding.EncodeToString([]byte(vpJWS))}}, 172 }, 173 })) 174 175 registry := mocksvdr.NewMockRegistry(ctrl) 176 registry.EXPECT().Resolve("did:example:ebfeb1f712ebc6f1c276e12ec21"). 177 Return(&did.DocResolution{DIDDocument: &did.Doc{VerificationMethod: []did.VerificationMethod{pubKey}}}, nil) 178 179 loader, err := ldtestutil.DocumentLoader() 180 require.NoError(t, err) 181 182 provider := mocks.NewMockProvider(ctrl) 183 provider.EXPECT().VDRegistry().Return(registry).AnyTimes() 184 provider.EXPECT().VerifiableStore().Return(mocksstore.NewMockStore(ctrl)) 185 provider.EXPECT().JSONLDDocumentLoader().Return(loader) 186 187 require.EqualError(t, SavePresentation(provider)(next).Handle(metadata), "myDID or theirDID is absent") 188 }) 189 190 t.Run("Success v2", func(t *testing.T) { 191 const vcName = "vc-name" 192 193 props := map[string]interface{}{ 194 myDIDKey: myDIDKey, 195 theirDIDKey: theirDIDKey, 196 } 197 198 metadata := mocks.NewMockMetadata(ctrl) 199 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 200 metadata.EXPECT().PresentationNames().Return([]string{vcName}).Times(2) 201 metadata.EXPECT().Properties().Return(props) 202 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV2{ 203 Type: presentproof.PresentationMsgTypeV2, 204 PresentationsAttach: []decorator.Attachment{ 205 {Data: decorator.AttachmentData{Base64: base64.StdEncoding.EncodeToString([]byte(vpJWS))}}, 206 }, 207 })) 208 209 verifiableStore := mocksstore.NewMockStore(ctrl) 210 verifiableStore.EXPECT().SavePresentation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). 211 Return(nil) 212 213 loader, err := ldtestutil.DocumentLoader() 214 require.NoError(t, err) 215 216 registry := mocksvdr.NewMockRegistry(ctrl) 217 registry.EXPECT().Resolve("did:example:ebfeb1f712ebc6f1c276e12ec21").Return( 218 &did.DocResolution{DIDDocument: &did.Doc{VerificationMethod: []did.VerificationMethod{pubKey}}}, nil) 219 220 provider := mocks.NewMockProvider(ctrl) 221 provider.EXPECT().VDRegistry().Return(registry).AnyTimes() 222 provider.EXPECT().VerifiableStore().Return(verifiableStore) 223 provider.EXPECT().JSONLDDocumentLoader().Return(loader) 224 225 require.NoError(t, SavePresentation(provider)(next).Handle(metadata)) 226 require.Equal(t, props["names"], []string{vcName}) 227 }) 228 229 t.Run("Success v3", func(t *testing.T) { 230 const vcName = "vc-name" 231 232 props := map[string]interface{}{ 233 myDIDKey: myDIDKey, 234 theirDIDKey: theirDIDKey, 235 } 236 237 metadata := mocks.NewMockMetadata(ctrl) 238 metadata.EXPECT().StateName().Return(stateNamePresentationReceived) 239 metadata.EXPECT().PresentationNames().Return([]string{vcName}).Times(2) 240 metadata.EXPECT().Properties().Return(props) 241 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.PresentationV3{ 242 Type: presentproof.PresentationMsgTypeV3, 243 Attachments: []decorator.AttachmentV2{ 244 {Data: decorator.AttachmentData{Base64: base64.StdEncoding.EncodeToString([]byte(vpJWS))}}, 245 }, 246 })) 247 248 verifiableStore := mocksstore.NewMockStore(ctrl) 249 verifiableStore.EXPECT().SavePresentation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()). 250 Return(nil) 251 252 loader, err := ldtestutil.DocumentLoader() 253 require.NoError(t, err) 254 255 registry := mocksvdr.NewMockRegistry(ctrl) 256 registry.EXPECT().Resolve("did:example:ebfeb1f712ebc6f1c276e12ec21").Return( 257 &did.DocResolution{DIDDocument: &did.Doc{VerificationMethod: []did.VerificationMethod{pubKey}}}, nil) 258 259 provider := mocks.NewMockProvider(ctrl) 260 provider.EXPECT().VDRegistry().Return(registry).AnyTimes() 261 provider.EXPECT().VerifiableStore().Return(verifiableStore) 262 provider.EXPECT().JSONLDDocumentLoader().Return(loader) 263 264 require.NoError(t, SavePresentation(provider)(next).Handle(metadata)) 265 require.Equal(t, props["names"], []string{vcName}) 266 }) 267 } 268 269 func TestPresentationDefinition(t *testing.T) { 270 ctrl := gomock.NewController(t) 271 defer ctrl.Finish() 272 273 cr, err := tinkcrypto.New() 274 require.NoError(t, err) 275 276 kmsProvider, err := mockkms.NewProviderForKMS(storage.NewMockStoreProvider(), &noop.NoLock{}) 277 require.NoError(t, err) 278 279 km, err := localkms.New("local-lock://custom/master/key/", kmsProvider) 280 require.NoError(t, err) 281 282 loader, err := ldtestutil.DocumentLoader() 283 require.NoError(t, err) 284 285 provider := mocks.NewMockProvider(ctrl) 286 provider.EXPECT().VDRegistry().Return(nil).AnyTimes() 287 provider.EXPECT().KMS().Return(km).AnyTimes() 288 provider.EXPECT().Crypto().Return(cr).AnyTimes() 289 provider.EXPECT().JSONLDDocumentLoader().Return(loader).AnyTimes() 290 291 require.NotNil(t, defaultPdOptions().addProof) 292 require.NoError(t, defaultPdOptions().addProof(nil)) 293 294 next := presentproof.HandlerFunc(func(metadata presentproof.Metadata) error { 295 return nil 296 }) 297 298 t.Run("Ignores processing", func(t *testing.T) { 299 metadata := mocks.NewMockMetadata(ctrl) 300 metadata.EXPECT().StateName().Return("state-name") 301 require.NoError(t, PresentationDefinition(provider)(next).Handle(metadata)) 302 }) 303 304 t.Run("Ignores processing (no presentation)", func(t *testing.T) { 305 metadata := mocks.NewMockMetadata(ctrl) 306 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 307 metadata.EXPECT().Presentation().Return(nil) 308 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV2{ 309 Type: presentproof.RequestPresentationMsgTypeV2, 310 })) 311 312 require.Nil(t, PresentationDefinition(provider)(next).Handle(metadata)) 313 }) 314 315 t.Run("Message decode (error)", func(t *testing.T) { 316 metadata := mocks.NewMockMetadata(ctrl) 317 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 318 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(struct { 319 Type chan struct{} `json:"@type"` 320 }{})) 321 322 require.Error(t, PresentationDefinition(provider)(next).Handle(metadata)) 323 }) 324 325 t.Run("No attachment", func(t *testing.T) { 326 metadata := mocks.NewMockMetadata(ctrl) 327 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 328 metadata.EXPECT().Presentation().Return(&presentproof.PresentationV2{}).AnyTimes() 329 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV2{ 330 Formats: []presentproof.Format{{ 331 AttachID: uuid.New().String(), 332 Format: peDefinitionFormat, 333 }}, 334 Type: presentproof.RequestPresentationMsgTypeV2, 335 })) 336 337 const errMsg = "get attachment by format: not found" 338 require.EqualError(t, PresentationDefinition(provider)(next).Handle(metadata), errMsg) 339 }) 340 341 t.Run("Attachment marshal (error)", func(t *testing.T) { 342 ID := uuid.New().String() 343 344 metadata := mocks.NewMockMetadata(ctrl) 345 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 346 metadata.EXPECT().Presentation().Return(&presentproof.PresentationV2{}).AnyTimes() 347 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV2{ 348 Formats: []presentproof.Format{{ 349 AttachID: ID, 350 Format: peDefinitionFormat, 351 }}, 352 Type: presentproof.RequestPresentationMsgTypeV2, 353 RequestPresentationsAttach: []decorator.Attachment{{ 354 ID: ID, 355 Data: decorator.AttachmentData{ 356 Base64: "ew==", 357 }, 358 }}, 359 })) 360 361 const errMsg = "unmarshal definition: unexpected end of JSON input" 362 require.EqualError(t, PresentationDefinition(provider)(next).Handle(metadata), errMsg) 363 }) 364 365 t.Run("No credentials", func(t *testing.T) { 366 ID := uuid.New().String() 367 368 metadata := mocks.NewMockMetadata(ctrl) 369 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 370 metadata.EXPECT().Presentation().Return(&presentproof.PresentationV2{ 371 PresentationsAttach: []decorator.Attachment{{ 372 MimeType: mimeTypeApplicationLdJSON, 373 Data: decorator.AttachmentData{ 374 JSON: &verifiable.Credential{ 375 ID: uuid.New().URN(), 376 Context: []string{verifiable.ContextURI}, 377 Types: []string{verifiable.VCType}, 378 Subject: verifiable.Subject{}, 379 Issuer: verifiable.Issuer{ID: uuid.New().URN()}, 380 Issued: util.NewTime(time.Now()), 381 CustomFields: map[string]interface{}{ 382 "first_name": "Jesse", 383 }, 384 }, 385 }, 386 }}, 387 }).AnyTimes() 388 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV2{ 389 Formats: []presentproof.Format{{ 390 AttachID: ID, 391 Format: peDefinitionFormat, 392 }}, 393 Type: presentproof.RequestPresentationMsgTypeV2, 394 RequestPresentationsAttach: []decorator.Attachment{{ 395 ID: ID, 396 Data: decorator.AttachmentData{ 397 JSON: map[string]interface{}{ 398 "presentation_definition": &presexch.PresentationDefinition{ 399 ID: uuid.New().String(), 400 InputDescriptors: []*presexch.InputDescriptor{{ 401 Schema: []*presexch.Schema{{ 402 URI: verifiable.ContextURI, 403 }}, 404 ID: uuid.New().String(), 405 Constraints: &presexch.Constraints{ 406 Fields: []*presexch.Field{{ 407 Path: []string{"$.credentialSubject.givenName", "$.credentialSubject.familyName"}, 408 Filter: &presexch.Filter{Type: &strFilterType}, 409 }, { 410 Path: []string{"$.credentialSubject.type"}, 411 Filter: &presexch.Filter{Type: &arrFilterType}, 412 }}, 413 }, 414 }}, 415 }, 416 }, 417 }, 418 }}, 419 })) 420 421 const errMsg = "create VP: credentials do not satisfy requirements" 422 require.EqualError(t, PresentationDefinition(provider)(next).Handle(metadata), errMsg) 423 }) 424 t.Run("Sign error", func(t *testing.T) { 425 ID := uuid.New().String() 426 427 metadata := mocks.NewMockMetadata(ctrl) 428 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 429 metadata.EXPECT().GetAddProofFn().Return(func(presentation *verifiable.Presentation) error { 430 return errors.New("test") 431 }) 432 metadata.EXPECT().Presentation().Return(&presentproof.PresentationV2{ 433 PresentationsAttach: []decorator.Attachment{{ 434 MimeType: mimeTypeApplicationLdJSON, 435 Data: decorator.AttachmentData{ 436 JSON: &verifiable.Credential{ 437 ID: "http://example.edu/credentials/1872", 438 Context: []string{verifiable.ContextURI}, 439 Types: []string{verifiable.VCType}, 440 Subject: "did:example:76e12ec712ebc6f1c221ebfeb1f", 441 Issued: &util.TimeWrapper{ 442 Time: time.Now(), 443 }, 444 Issuer: verifiable.Issuer{ 445 ID: "did:example:76e12ec712ebc6f1c221ebfeb1f", 446 }, 447 CustomFields: map[string]interface{}{ 448 "first_name": "First name", 449 "last_name": "Last name", 450 "info": "Info", 451 }, 452 }, 453 }, 454 }, { 455 MimeType: "application/json", 456 Data: decorator.AttachmentData{ 457 JSON: map[string]struct{}{}, 458 }, 459 }}, 460 }).AnyTimes() 461 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV2{ 462 Formats: []presentproof.Format{{ 463 AttachID: ID, 464 Format: peDefinitionFormat, 465 }}, 466 Type: presentproof.RequestPresentationMsgTypeV2, 467 RequestPresentationsAttach: []decorator.Attachment{{ 468 ID: ID, 469 Data: decorator.AttachmentData{ 470 JSON: map[string]interface{}{ 471 "presentation_definition": &presexch.PresentationDefinition{ 472 ID: uuid.New().String(), 473 InputDescriptors: []*presexch.InputDescriptor{{ 474 ID: uuid.New().String(), 475 Schema: []*presexch.Schema{{ 476 URI: fmt.Sprintf("%s#%s", verifiable.ContextID, verifiable.VCType), 477 }}, 478 Constraints: &presexch.Constraints{ 479 Fields: []*presexch.Field{{ 480 Path: []string{"$.first_name"}, 481 Filter: &presexch.Filter{Type: &strFilterType}, 482 }, { 483 Path: []string{"$.last_name"}, 484 Filter: &presexch.Filter{Type: &strFilterType}, 485 }}, 486 }, 487 }}, 488 }, 489 }, 490 }, 491 }}, 492 })) 493 494 require.EqualError(t, PresentationDefinition(provider)(next).Handle(metadata), "add proof: test") 495 }) 496 497 t.Run("Success v2", func(t *testing.T) { 498 ID := uuid.New().String() 499 500 metadata := mocks.NewMockMetadata(ctrl) 501 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 502 metadata.EXPECT().GetAddProofFn().Return(nil) 503 metadata.EXPECT().Presentation().Return(&presentproof.PresentationV2{ 504 PresentationsAttach: []decorator.Attachment{{ 505 MimeType: mimeTypeApplicationLdJSON, 506 Data: decorator.AttachmentData{ 507 JSON: &verifiable.Credential{ 508 ID: "http://example.edu/credentials/1872", 509 Context: []string{verifiable.ContextURI}, 510 Types: []string{verifiable.VCType}, 511 Subject: "did:example:76e12ec712ebc6f1c221ebfeb1f", 512 Issued: &util.TimeWrapper{ 513 Time: time.Now(), 514 }, 515 Issuer: verifiable.Issuer{ 516 ID: "did:example:76e12ec712ebc6f1c221ebfeb1f", 517 }, 518 CustomFields: map[string]interface{}{ 519 "first_name": "First name", 520 "last_name": "Last name", 521 "info": "Info", 522 }, 523 }, 524 }, 525 }, { 526 MimeType: "application/json", 527 Data: decorator.AttachmentData{ 528 JSON: map[string]struct{}{}, 529 }, 530 }}, 531 }).AnyTimes() 532 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV2{ 533 Formats: []presentproof.Format{{ 534 AttachID: ID, 535 Format: peDefinitionFormat, 536 }}, 537 Type: presentproof.RequestPresentationMsgTypeV2, 538 RequestPresentationsAttach: []decorator.Attachment{{ 539 ID: ID, 540 Data: decorator.AttachmentData{ 541 JSON: map[string]interface{}{ 542 "presentation_definition": &presexch.PresentationDefinition{ 543 ID: uuid.New().String(), 544 InputDescriptors: []*presexch.InputDescriptor{{ 545 ID: uuid.New().String(), 546 Schema: []*presexch.Schema{{ 547 URI: fmt.Sprintf("%s#%s", verifiable.ContextID, verifiable.VCType), 548 }}, 549 Constraints: &presexch.Constraints{ 550 Fields: []*presexch.Field{{ 551 Path: []string{"$.first_name"}, 552 Filter: &presexch.Filter{Type: &strFilterType}, 553 }, { 554 Path: []string{"$.last_name"}, 555 Filter: &presexch.Filter{Type: &strFilterType}, 556 }}, 557 }, 558 }}, 559 }, 560 }, 561 }, 562 }}, 563 })) 564 565 require.Nil(t, PresentationDefinition(provider, WithAddProofFn(AddBBSProofFn(provider)))(next).Handle(metadata)) 566 }) 567 568 t.Run("Success v3", func(t *testing.T) { 569 ID := uuid.New().String() 570 571 metadata := mocks.NewMockMetadata(ctrl) 572 metadata.EXPECT().StateName().Return(stateNameRequestReceived) 573 metadata.EXPECT().GetAddProofFn().Return(nil) 574 metadata.EXPECT().PresentationV3().Return(&presentproof.PresentationV3{ 575 Attachments: []decorator.AttachmentV2{{ 576 MediaType: mimeTypeApplicationLdJSON, 577 Data: decorator.AttachmentData{ 578 JSON: &verifiable.Credential{ 579 ID: "http://example.edu/credentials/1872", 580 Context: []string{verifiable.ContextURI}, 581 Types: []string{verifiable.VCType}, 582 Subject: "did:example:76e12ec712ebc6f1c221ebfeb1f", 583 Issued: &util.TimeWrapper{ 584 Time: time.Now(), 585 }, 586 Issuer: verifiable.Issuer{ 587 ID: "did:example:76e12ec712ebc6f1c221ebfeb1f", 588 }, 589 CustomFields: map[string]interface{}{ 590 "first_name": "First name", 591 "last_name": "Last name", 592 "info": "Info", 593 }, 594 }, 595 }, 596 }, { 597 MediaType: "application/json", 598 Data: decorator.AttachmentData{ 599 JSON: map[string]struct{}{}, 600 }, 601 }}, 602 }).AnyTimes() 603 metadata.EXPECT().Message().Return(service.NewDIDCommMsgMap(presentproof.RequestPresentationV3{ 604 Type: presentproof.RequestPresentationMsgTypeV3, 605 Attachments: []decorator.AttachmentV2{{ 606 ID: ID, 607 Format: peDefinitionFormat, 608 Data: decorator.AttachmentData{ 609 JSON: map[string]interface{}{ 610 "presentation_definition": &presexch.PresentationDefinition{ 611 ID: uuid.New().String(), 612 InputDescriptors: []*presexch.InputDescriptor{{ 613 ID: uuid.New().String(), 614 Schema: []*presexch.Schema{{ 615 URI: fmt.Sprintf("%s#%s", verifiable.ContextID, verifiable.VCType), 616 }}, 617 Constraints: &presexch.Constraints{ 618 Fields: []*presexch.Field{{ 619 Path: []string{"$.first_name"}, 620 Filter: &presexch.Filter{Type: &strFilterType}, 621 }, { 622 Path: []string{"$.last_name"}, 623 Filter: &presexch.Filter{Type: &strFilterType}, 624 }}, 625 }, 626 }}, 627 }, 628 }, 629 }, 630 }}, 631 })) 632 633 require.Nil(t, PresentationDefinition(provider, WithAddProofFn(AddBBSProofFn(provider)))(next).Handle(metadata)) 634 }) 635 }