Skip to content

Commit

Permalink
feat: vc wallet command controller for managin profiles
Browse files Browse the repository at this point in the history
- added create & update profile command controllers
- Part of hyperledger-archives#2770

Signed-off-by: sudesh.shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Oct 14, 2021
1 parent 4f4f1e9 commit a73172d
Show file tree
Hide file tree
Showing 3 changed files with 511 additions and 0 deletions.
152 changes: 152 additions & 0 deletions pkg/controller/command/vcwallet/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package vcwallet

import (
"encoding/json"
"io"

"github.com/piprate/json-gold/ld"

"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/controller/command"
"github.com/hyperledger/aries-framework-go/pkg/controller/internal/cmdutil"
"github.com/hyperledger/aries-framework-go/pkg/crypto"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
"github.com/hyperledger/aries-framework-go/pkg/internal/logutil"
"github.com/hyperledger/aries-framework-go/pkg/wallet"
"github.com/hyperledger/aries-framework-go/spi/storage"
)

var logger = log.New("aries-framework/command/vcwallet")

// Error codes.
const (
// InvalidRequestErrorCode is typically a code for invalid requests.
InvalidRequestErrorCode = command.Code(iota + command.VCWallet)

// CreateProfileErrorCode for errors during create wallet profile operations.
CreateProfileErrorCode

// UpdateProfileErrorCode for errors during update wallet profile operations.
UpdateProfileErrorCode
)

// All command operations.
const (
CommandName = "vcwallet"

// command methods.
CreateProfileMethod = "CreateProfile"
UpdateProfileMethod = "UpdateProfile"
)

// miscellaneous constants for the vc wallet command controller.
const ()

// provider contains dependencies for the verifiable credential wallet command controller
// and is typically created by using aries.Context().
type provider interface {
StorageProvider() storage.Provider
VDRegistry() vdr.Registry
Crypto() crypto.Crypto
JSONLDDocumentLoader() ld.DocumentLoader
}

// Command contains operations provided by verifiable credential wallet controller.
type Command struct {
ctx provider
}

// New returns new verifiable credential wallet controller command instance.
func New(p provider) *Command {
return &Command{ctx: p}
}

// GetHandlers returns list of all commands supported by this controller command.
func (o *Command) GetHandlers() []command.Handler {
return []command.Handler{
cmdutil.NewCommandHandler(CommandName, CreateProfileMethod, o.CreateProfile),
cmdutil.NewCommandHandler(CommandName, UpdateProfileMethod, o.UpdateProfile),
}
}

// CreateProfile creates new wallet profile for given user.
func (o *Command) CreateProfile(rw io.Writer, req io.Reader) command.Error {
request := &CreateOrUpdateProfileRequest{}

err := json.NewDecoder(req).Decode(request)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateProfileMethod, err.Error())

return command.NewValidationError(InvalidRequestErrorCode, err)
}

// create profile.
err = wallet.CreateProfile(request.UserID, o.ctx, prepareProfileOptions(request)...)
if err != nil {
logutil.LogInfo(logger, CommandName, CreateProfileMethod, err.Error())

return command.NewExecuteError(CreateProfileErrorCode, err)
}

// create EDV keys if profile is using local kms.
if request.LocalKMSPassphrase != "" && request.EDVConfiguration != nil {
err = wallet.CreateDataVaultKeyPairs(request.UserID, o.ctx, wallet.WithUnlockByPassphrase(request.LocalKMSPassphrase))
if err != nil {
logutil.LogInfo(logger, CommandName, CreateProfileMethod, err.Error())

return command.NewExecuteError(CreateProfileErrorCode, err)
}
}

return nil
}

// UpdateProfile updates an existing wallet profile for given user.
func (o *Command) UpdateProfile(rw io.Writer, req io.Reader) command.Error {
request := &CreateOrUpdateProfileRequest{}

err := json.NewDecoder(req).Decode(&request)
if err != nil {
logutil.LogInfo(logger, CommandName, UpdateProfileMethod, err.Error())

return command.NewValidationError(InvalidRequestErrorCode, err)
}

// update profile.
err = wallet.UpdateProfile(request.UserID, o.ctx, prepareProfileOptions(request)...)
if err != nil {
logutil.LogInfo(logger, CommandName, UpdateProfileMethod, err.Error())

return command.NewExecuteError(UpdateProfileErrorCode, err)
}

return nil
}

// prepareProfileOptions prepares options for creating wallet profile.
func prepareProfileOptions(rqst *CreateOrUpdateProfileRequest) []wallet.ProfileOptions {
var options []wallet.ProfileOptions

if rqst.LocalKMSPassphrase != "" {
options = append(options, wallet.WithPassphrase(rqst.LocalKMSPassphrase))
}

if rqst.KeyStoreURL != "" {
options = append(options, wallet.WithKeyServerURL(rqst.KeyStoreURL))
}

if rqst.EDVConfiguration != nil {
options = append(options, wallet.WithEDVStorage(
rqst.EDVConfiguration.ServerURL, rqst.EDVConfiguration.VaultID,
rqst.EDVConfiguration.EncryptionKeyID, rqst.EDVConfiguration.MACKeyID,
))
}

return options
}
Loading

0 comments on commit a73172d

Please sign in to comment.