func (o *Operation) importKeyHandler(rw http.ResponseWriter, req *http.Request) {
	o.logger.Debugf("handling request: %s", req.URL.String())
	k, err := o.kmsService.ResolveKeystore(req)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, resolveKeystoreFailure, err)
		return
	}
	var request importKeyReq
	if ok := o.parseRequest(&request, rw, req); !ok {
		return
	}
	keystoreID := mux.Vars(req)[keystoreIDQueryParam]
	keyBytes, err := base64.URLEncoding.DecodeString(request.KeyBytes)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusBadRequest, receivedBadRequest, err)
		return
	}
	keyID, err := k.ImportKey(keyBytes, arieskms.KeyType(request.KeyType), request.KeyID)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, importKeyFailure, err)
		return
	}
	location := keyLocation(o.baseURL, keystoreID, keyID)
	rw.Header().Set("Location", location)
	rw.WriteHeader(http.StatusCreated)
	o.writeResponse(rw, importKeyResp{
		Location: location,
	})
}
swagger:route POST /kms/keystores/{keystoreID}/import kms importKeyReq

Imports a private key.

Responses:
201: importKeyResp
default: errorResp
package operation
import (
	"context"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strings"
	"github.com/gorilla/mux"
	"github.com/hyperledger/aries-framework-go/pkg/crypto"
	arieskms "github.com/hyperledger/aries-framework-go/pkg/kms"
	"github.com/piprate/json-gold/ld"
	"github.com/trustbloc/edge-core/pkg/log"
	"github.com/trustbloc/edge-core/pkg/zcapld"
	zcapld2 "github.com/trustbloc/kms/pkg/auth/zcapld"
	"github.com/trustbloc/kms/pkg/internal/support"
	"github.com/trustbloc/kms/pkg/kms"
)
const (
	keystoreIDQueryParam = "keystoreID"
	keyIDQueryParam      = "keyID"
)
const (
	keysPath        = "/keys"
	capabilityPath  = "/capability"
	exportPath      = "/export"
	importPath      = "/import"
	signPath        = "/sign"
	verifyPath      = "/verify"
	encryptPath     = "/encrypt"
	decryptPath     = "/decrypt"
	computeMACPath  = "/computemac"
	verifyMACPath   = "/verifymac"
	wrapPath        = "/wrap"
	unwrapPath      = "/unwrap"
	easyPath        = "/easy"
	easyOpenPath    = "/easyopen"
	sealOpenPath    = "/sealopen"
	signMultiPath   = "/signmulti"
	verifyMultiPath = "/verifymulti"
	deriveProofPath = "/deriveproof"
	verifyProofPath = "/verifyproof"
	KMSBasePath        = "/kms"
	keystoresEndpoint  = "/keystores"
	keystoreEndpoint   = keystoresEndpoint + "/{" + keystoreIDQueryParam + "}"
	keysEndpoint       = keystoreEndpoint + keysPath
	capabilityEndpoint = keystoreEndpoint + capabilityPath
	keyEndpoint        = keysEndpoint + "/{" + keyIDQueryParam + "}"
	exportEndpoint     = keyEndpoint + exportPath
	importEndpoint     = keystoreEndpoint + importPath  
	signEndpoint       = keyEndpoint + signPath
	verifyEndpoint     = keyEndpoint + verifyPath
	encryptEndpoint    = keyEndpoint + encryptPath
	decryptEndpoint    = keyEndpoint + decryptPath
	computeMACEndpoint = keyEndpoint + computeMACPath
	verifyMACEndpoint  = keyEndpoint + verifyMACPath
	wrapEndpoint       = keystoreEndpoint + wrapPath  
	unwrapEndpoint     = keyEndpoint + unwrapPath     
	easyEndpoint     = keyEndpoint + easyPath
	easyOpenEndpoint = keystoreEndpoint + easyOpenPath
	sealOpenEndpoint = keystoreEndpoint + sealOpenPath
	signMultiEndpoint   = keyEndpoint + signMultiPath
	verifyMultiEndpoint = keyEndpoint + verifyMultiPath
	deriveProofEndpoint = keyEndpoint + deriveProofPath
	verifyProofEndpoint = keyEndpoint + verifyProofPath
)
const (
	receivedBadRequest         = "Received bad request: %s"
	createKeystoreFailure      = "Failed to create a keystore: %s"
	resolveKeystoreFailure     = "Failed to resolve a keystore: %s"
	getKeystoreFailure         = "Failed to get a keystore: %s"
	saveKeystoreFailure        = "Failed to get a keystore: %s"
	createKeyFailure           = "Failed to create a key: %s"
	createAndExportKeyFailure  = "Failed to create and export a key: %s"
	exportKeyFailure           = "Failed to export a public key: %s"
	importKeyFailure           = "Failed to import a private key: %s"
	signMessageFailure         = "Failed to sign a message: %s"
	verifyMessageFailure       = "Failed to verify a message: %s"
	encryptMessageFailure      = "Failed to encrypt a message: %s"
	decryptMessageFailure      = "Failed to decrypt a message: %s"
	computeMACFailure          = "Failed to compute MAC: %s"
	verifyMACFailure           = "Failed to verify MAC: %s"
	wrapMessageFailure         = "Failed to wrap a key: %s"
	unwrapMessageFailure       = "Failed to unwrap a key: %s"
	createZCAPFailure          = "Failed to create zcap: %s"
	easyMessageFailure         = "Failed to easy a message: %s"
	easyOpenMessageFailure     = "Failed to easyOpen a message: %s"
	sealOpenPayloadFailure     = "Failed to sealOpen a payload: %s"
	signMultiMessagesFailure   = "Failed to sign messages: %s"
	verifyMultiMessagesFailure = "Failed to verify messages: %s"
	deriveProofFailure         = "Failed to derive proof: %s"
	verifyProofFailure         = "Failed to verify proof: %s"
)
const (
	actionCreateKey       = "createKey"
	actionExportKey       = "exportKey"
	actionImportKey       = "importKey"
	actionSign            = "sign"
	actionVerify          = "verify"
	actionWrap            = "wrap"
	actionUnwrap          = "unwrap"
	actionComputeMac      = "computeMAC"
	actionVerifyMAC       = "verifyMAC"
	actionEncrypt         = "encrypt"
	actionDecrypt         = "decrypt"
	actionStoreCapability = "updateEDVCapability"
	actionEasy            = "easy"
	actionEasyOpen        = "easyOpen"
	actionSealOpen        = "sealOpen"
	actionSignMulti       = "signMulti"
	actionVerifyMulti     = "verifyMulti"
	actionDeriveProof     = "deriveProof"
	actionVerifyProof     = "verifyProof"
)
type Handler interface {
	Path() string
	Method() string
	Handle() http.HandlerFunc
	Name() string
}
type authService interface {
	CreateDIDKey(context.Context) (string, error)
	NewCapability(ctx context.Context, options ...zcapld.CapabilityOption) (*zcapld.Capability, error)
	KMS() arieskms.KeyManager
	Crypto() crypto.Crypto
	Resolve(string) (*zcapld.Capability, error)
}
type Operation struct {
	authService      authService
	kmsService       kms.Service
	cryptoBoxCreator func(keyManager arieskms.KeyManager) (arieskms.CryptoBox, error)
	jsonLDLoader     ld.DocumentLoader
	logger           log.Logger
	baseURL          string
	vdrResolver      zcapld.VDRResolver
}
type Config struct {
	AuthService      authService
	KMSService       kms.Service
	CryptoBoxCreator func(keyManager arieskms.KeyManager) (arieskms.CryptoBox, error)
	JSONLDLoader     ld.DocumentLoader
	Logger           log.Logger
	BaseURL          string
	VDRResolver      zcapld.VDRResolver
}
func New(config *Config) (*Operation, error) {
	op := &Operation{
		authService:      config.AuthService,
		kmsService:       config.KMSService,
		cryptoBoxCreator: config.CryptoBoxCreator,
		jsonLDLoader:     config.JSONLDLoader,
		logger:           config.Logger,
		baseURL:          config.BaseURL,
		vdrResolver:      config.VDRResolver,
	}
	return op, nil
}
func (o *Operation) GetRESTHandlers() []Handler {
	return []Handler{
		support.NewHTTPHandler(keystoresEndpoint, keystoresEndpoint, http.MethodPost, o.createKeystoreHandler),
		support.NewHTTPHandler(keysEndpoint, keysEndpoint, http.MethodPost, o.createKeyHandler),
		support.NewHTTPHandler(capabilityEndpoint, capabilityEndpoint, http.MethodPost, o.updateCapabilityHandler),
		support.NewHTTPHandler(exportEndpoint, exportEndpoint, http.MethodGet, o.exportKeyHandler),
		support.NewHTTPHandler(importEndpoint, importEndpoint, http.MethodPost, o.importKeyHandler),
		support.NewHTTPHandler(signEndpoint, signEndpoint, http.MethodPost, o.signHandler),
		support.NewHTTPHandler(verifyEndpoint, verifyEndpoint, http.MethodPost, o.verifyHandler),
		support.NewHTTPHandler(encryptEndpoint, encryptEndpoint, http.MethodPost, o.encryptHandler),
		support.NewHTTPHandler(decryptEndpoint, decryptEndpoint, http.MethodPost, o.decryptHandler),
		support.NewHTTPHandler(computeMACEndpoint, computeMACEndpoint, http.MethodPost, o.computeMACHandler),
		support.NewHTTPHandler(verifyMACEndpoint, verifyMACEndpoint, http.MethodPost, o.verifyMACHandler),
		support.NewHTTPHandler(wrapEndpoint, wrapEndpoint, http.MethodPost, o.wrapHandler),
		support.NewHTTPHandler(unwrapEndpoint, unwrapEndpoint, http.MethodPost, o.unwrapHandler),
		support.NewHTTPHandler(easyEndpoint, easyEndpoint, http.MethodPost, o.easyHandler),
		support.NewHTTPHandler(easyOpenEndpoint, easyOpenEndpoint, http.MethodPost, o.easyOpenHandler),
		support.NewHTTPHandler(sealOpenEndpoint, sealOpenEndpoint, http.MethodPost, o.sealOpenHandler),
		support.NewHTTPHandler(signMultiEndpoint, signMultiEndpoint, http.MethodPost, o.signMultiHandler),
		support.NewHTTPHandler(verifyMultiEndpoint, verifyMultiEndpoint, http.MethodPost, o.verifyMultiHandler),
		support.NewHTTPHandler(deriveProofEndpoint, deriveProofEndpoint, http.MethodPost, o.deriveProofHandler),
		support.NewHTTPHandler(verifyProofEndpoint, verifyProofEndpoint, http.MethodPost, o.verifyProofHandler),
	}
}
func (o *Operation) createKeystoreHandler(rw http.ResponseWriter, req *http.Request) {
	o.logger.Debugf("handling request: %s", req.URL.String())
	var request createKeystoreReq
	if ok := o.parseRequest(&request, rw, req); !ok {
		return
	}
	keystoreData, err := o.kmsService.CreateKeystore(request.Controller, request.VaultID)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, createKeystoreFailure, err)
		return
	}
	didKey, err := o.authService.CreateDIDKey(req.Context())
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, createKeystoreFailure, err)
		return
	}
	resource := keystoreLocation(o.baseURL, keystoreData.ID)
	zcap, err := o.newCompressedZCAP(req.Context(), resource, keystoreData.Controller)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, createZCAPFailure, err)
		return
	}
	rw.Header().Set("Location", resource)
	rw.Header().Set("Edvdidkey", didKey)
	rw.Header().Set("X-RootCapability", zcap)
	rw.WriteHeader(http.StatusCreated)
	o.logger.Debugf("finished handling request - keystore: %s", resource)
}
func (o *Operation) createKeyHandler(rw http.ResponseWriter, req *http.Request) {
	o.logger.Debugf("handling request: %s", req.URL.String())
	k, err := o.kmsService.ResolveKeystore(req)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, resolveKeystoreFailure, err)
		return
	}
	var request createKeyReq
	if ok := o.parseRequest(&request, rw, req); !ok {
		return
	}
	keystoreID := mux.Vars(req)[keystoreIDQueryParam]
	var (
		keyID    string
		keyBytes []byte
	)
	if request.ExportKey {
		keyID, keyBytes, err = k.CreateAndExportKey(arieskms.KeyType(request.KeyType))
		if err != nil {
			o.writeErrorResponse(rw, http.StatusInternalServerError, createAndExportKeyFailure, err)
			return
		}
	} else {
		keyID, err = k.CreateKey(arieskms.KeyType(request.KeyType))
		if err != nil {
			o.writeErrorResponse(rw, http.StatusInternalServerError, createKeyFailure, err)
			return
		}
	}
	location := keyLocation(o.baseURL, keystoreID, keyID)
	rw.Header().Set("Location", location)
	rw.WriteHeader(http.StatusCreated)
	o.writeResponse(rw, createKeyResp{
		Location:  location,
		PublicKey: base64.URLEncoding.EncodeToString(keyBytes),
	})
	o.logger.Debugf("Location: %s", location)
	o.logger.Debugf("finished handling request")
}
func (o *Operation) updateCapabilityHandler(rw http.ResponseWriter, req *http.Request) {
	var request UpdateCapabilityReq
	if ok := o.parseRequest(&request, rw, req); !ok {
		return
	}
	if len(request.EDVCapability) == 0 {
		o.writeErrorResponse(rw, http.StatusBadRequest, "edvCapability is empty",
			fmt.Errorf(""))
		return
	}
	keystoreID := mux.Vars(req)[keystoreIDQueryParam]
	keystoreData, err := o.kmsService.GetKeystoreData(keystoreID)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, getKeystoreFailure, err)
		return
	}
	keystoreData.EDVCapability = request.EDVCapability
	if err := o.kmsService.SaveKeystoreData(keystoreData); err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, saveKeystoreFailure, err)
		return
	}
	rw.WriteHeader(http.StatusOK)
}
func (o *Operation) exportKeyHandler(rw http.ResponseWriter, req *http.Request) {
	o.logger.Debugf("handle request: url=%s", req.RequestURI)
	k, err := o.kmsService.ResolveKeystore(req)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, resolveKeystoreFailure, err)
		return
	}
	keyID := mux.Vars(req)[keyIDQueryParam]
	keyBytes, err := k.ExportKey(keyID)
	if err != nil {
		o.writeErrorResponse(rw, http.StatusInternalServerError, exportKeyFailure, err)
		return
	}
	o.writeResponse(rw, exportKeyResp{
		PublicKey: base64.URLEncoding.EncodeToString(keyBytes),
	})
}