Skip to content

Commit

Permalink
feat: wallet command - get all by collection
Browse files Browse the repository at this point in the history
- Part of hyperledger-archives#2770

Signed-off-by: sudesh.shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed May 7, 2021
1 parent cbadee0 commit 019f749
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/command/vcwallet/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
62 changes: 62 additions & 0 deletions pkg/controller/command/vcwallet/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/command/vcwallet/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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"`
}

Expand Down

0 comments on commit 019f749

Please sign in to comment.