github.com/trustbloc/kms-go@v1.1.2/wrapper/localsuite/creator.go (about)

     1  /*
     2  Copyright Gen Digital Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package localsuite
     7  
     8  import (
     9  	"github.com/trustbloc/kms-go/doc/jose/jwk"
    10  	"github.com/trustbloc/kms-go/doc/jose/jwk/jwksupport"
    11  	"github.com/trustbloc/kms-go/spi/kms"
    12  	"github.com/trustbloc/kms-go/wrapper/api"
    13  )
    14  
    15  func newKeyCreator(kms keyCreator) api.RawKeyCreator {
    16  	return &keyCreatorImpl{kms: kms}
    17  }
    18  
    19  type keyCreatorImpl struct {
    20  	kms keyCreator
    21  }
    22  
    23  func (k *keyCreatorImpl) Create(keyType kms.KeyType) (*jwk.JWK, error) {
    24  	return createKey(k.kms, keyType)
    25  }
    26  
    27  func (k *keyCreatorImpl) CreateRaw(keyType kms.KeyType) (string, interface{}, error) {
    28  	kid, pkBytes, err := k.kms.CreateAndExportPubKeyBytes(keyType)
    29  	if err != nil {
    30  		return "", nil, err
    31  	}
    32  
    33  	raw, err := jwksupport.PubKeyBytesToKey(pkBytes, keyType)
    34  	if err != nil {
    35  		return "", nil, err
    36  	}
    37  
    38  	return kid, raw, nil
    39  }
    40  
    41  func createKey(creator keyCreator, keyType kms.KeyType) (*jwk.JWK, error) {
    42  	kid, pkBytes, err := creator.CreateAndExportPubKeyBytes(keyType)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	pk, err := jwksupport.PubKeyBytesToJWK(pkBytes, keyType)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	pk.KeyID = kid
    53  
    54  	return pk, nil
    55  }
    56  
    57  var _ api.KeyCreator = &keyCreatorImpl{}