From e29cd3043204424ae79659f0a2f4cf13d9c065e0 Mon Sep 17 00:00:00 2001 From: "sudesh.shetty" Date: Fri, 7 May 2021 09:54:25 -0400 Subject: [PATCH] feat: wallet command - get all by collection - Part of #2770 Signed-off-by: sudesh.shetty --- pkg/controller/command/vcwallet/command.go | 3 +- .../command/vcwallet/command_test.go | 62 +++++++++++++++++++ pkg/controller/command/vcwallet/models.go | 10 +-- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/pkg/controller/command/vcwallet/command.go b/pkg/controller/command/vcwallet/command.go index adaba1c705..0d5c8e6cee 100644 --- a/pkg/controller/command/vcwallet/command.go +++ b/pkg/controller/command/vcwallet/command.go @@ -404,7 +404,8 @@ func (o *Command) GetAll(rw io.Writer, req io.Reader) command.Error { return command.NewExecuteError(GetAllFromWalletErrorCode, err) } - contents, err := vcWallet.GetAll(request.Auth, request.ContentType) + contents, err := vcWallet.GetAll(request.Auth, request.ContentType, + wallet.FilterByCollection(request.CollectionID)) if err != nil { logutil.LogInfo(logger, CommandName, GetAllMethod, err.Error()) diff --git a/pkg/controller/command/vcwallet/command_test.go b/pkg/controller/command/vcwallet/command_test.go index 6bbf73b851..b920519810 100644 --- a/pkg/controller/command/vcwallet/command_test.go +++ b/pkg/controller/command/vcwallet/command_test.go @@ -862,6 +862,68 @@ func TestCommand_AddRemoveGetGetAll(t *testing.T) { require.Len(t, response.Contents, count) }) + t.Run("get all credentials from wallet by collection ID", func(t *testing.T) { + const orgCollection = `{ + "@context": ["https://w3id.org/wallet/v1"], + "id": "did:example:acme123456789abcdefghi", + "type": "Organization", + "name": "Acme Corp.", + "image": "https://via.placeholder.com/150", + "description" : "A software company.", + "tags": ["professional", "organization"], + "correlation": ["4058a72a-9523-11ea-bb37-0242ac130002"] + }` + + const collectionID = "did:example:acme123456789abcdefghi" + + cmd := New(mockctx, &Config{}) + + // save a collection + var b bytes.Buffer + cmdErr := cmd.Add(&b, getReader(t, &AddContentRequest{ + Content: []byte(orgCollection), + ContentType: wallet.Collection, + WalletAuth: WalletAuth{UserID: sampleUser1, Auth: token1}, + })) + require.NoError(t, cmdErr) + + // save only 2 credentials by collection, + const count = 6 + for i := 1; i < count; i++ { + var cid string + + if i%2 == 0 { + cid = collectionID + } + + var vcb bytes.Buffer + cErr := cmd.Add(&vcb, getReader(t, &AddContentRequest{ + Content: []byte(strings.ReplaceAll(sampleUDCVC, `"http://example.edu/credentials/1877"`, + fmt.Sprintf(`"http://example.edu/credentials/18722%d"`, i))), + ContentType: "credential", + CollectionID: cid, + WalletAuth: WalletAuth{UserID: sampleUser1, Auth: token1}, + })) + require.NoError(t, cErr) + + vcb.Reset() + } + + b.Reset() + + cmdErr = cmd.GetAll(&b, getReader(t, &GetAllContentRequest{ + ContentType: "credential", + CollectionID: collectionID, + WalletAuth: WalletAuth{UserID: sampleUser1, Auth: token1}, + })) + require.NoError(t, cmdErr) + + var response GetAllContentResponse + require.NoError(t, json.NewDecoder(&b).Decode(&response)) + require.NotEmpty(t, response) + require.Len(t, response.Contents, 2) + }) + t.Run("remove a credential from wallet", func(t *testing.T) { cmd := New(mockctx, &Config{}) diff --git a/pkg/controller/command/vcwallet/models.go b/pkg/controller/command/vcwallet/models.go index 2329faaa59..dcac9c279c 100644 --- a/pkg/controller/command/vcwallet/models.go +++ b/pkg/controller/command/vcwallet/models.go @@ -60,11 +60,11 @@ type UnlockWalletRequest struct { // WebKMSAuth for authorizing acccess to web/remote kms. // Optional, to be used if profile for this wallet user is setup with web/remote KMS. - WebKMSAuth *UnlockAuth `json:"webKMSAuth,omitempty"` + WebKMSAuth *UnlockAuth `json:"webKMSAuth"` // Options for authorizing access to wallet's EDV content store. // Optional, to be used only if profile for this wallet user is setup to use EDV as content store. - EDVUnlock *UnlockAuth `json:"edvUnlock,omitempty"` + EDVUnlock *UnlockAuth `json:"edvUnlocks"` } // UnlockAuth contains different options for authorizing access to wallet's EDV content store & webkms. @@ -160,6 +160,9 @@ type GetAllContentRequest struct { // type of the contents to be returned from wallet. // supported types: collection, credential, didResolutionResponse, metadata, connection ContentType wallet.ContentType `json:"contentType"` + + // ID of the collection on which the response contents to be filtered. + CollectionID string `json:"collectionID,omitempty"` } // GetAllContentResponse response for get all content by content type wallet operation. @@ -179,8 +182,7 @@ type ContentQueryRequest struct { // ContentQueryResponse response for wallet content query. type ContentQueryResponse struct { - // contents retrieved from wallet content store. - // map of content ID to content. + // response presentation(s) containing query results. Results []*verifiable.Presentation `json:"results"` }