Skip to content

Commit

Permalink
Add Delete function to vcr wallet
Browse files Browse the repository at this point in the history
A new Delete function is added to the wallet in the vcr/holder/wallet.go file. This function allows a credential to be removed from the wallet. Additionally, an interface in the vcr/holder/interface.go file has been updated to include this Delete function. Proper error handling and stats consistency are ensured in this implementation.
  • Loading branch information
rolandgroen committed Jan 31, 2024
1 parent 7c286a3 commit cb1f773
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vcr/holder/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Wallet interface {

// IsEmpty returns true if the wallet contains no credentials at all (for all holder DIDs).
IsEmpty() (bool, error)

// Delete deletes a credential from the wallet
Delete(ctx context.Context, subjectDID did.DID, id ssi.URI) error
}

// PresentationOptions contains parameters used to create the right VerifiablePresentation
Expand Down
22 changes: 22 additions & 0 deletions vcr/holder/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ func (h wallet) IsEmpty() (bool, error) {
return count == 0, err
}

func (h wallet) Delete(ctx context.Context, subjectDID did.DID, id ssi.URI) error {
err := h.walletStore.Write(ctx, func(tx stoabs.WriteTx) error {
stats := tx.GetShelfWriter(statsShelf)
walletKey := stoabs.BytesKey(id.String())
walletShelf := tx.GetShelfWriter(subjectDID.String())
err := walletShelf.Delete(walletKey)
if err != nil {
return err
}
// Update stats
currentCount, err := h.readCredentialCount(stats)
if err != nil {
return fmt.Errorf("unable to read wallet credential count: %w", err)
}
return stats.Put(credentialCountStatsKey, binary.BigEndian.AppendUint32([]byte{}, currentCount+1))
}, stoabs.WithWriteLock()) // lock required for stats consistency
if err != nil {
return fmt.Errorf("unable to delete credential: %w", err)
}
return nil
}

func (h wallet) readCredentialCount(statsShelf stoabs.Reader) (uint32, error) {
countBytes, err := statsShelf.Get(credentialCountStatsKey)
if errors.Is(err, stoabs.ErrKeyNotFound) {
Expand Down

0 comments on commit cb1f773

Please sign in to comment.