github.com/trustbloc/kms-go@v1.1.2/wrapper/localsuite/multisigner.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/wrapper/api"
    11  )
    12  
    13  func newKMSCryptoMultiSigner(kms keyGetter, crypto multiSigner) api.KMSCryptoMultiSigner {
    14  	return &multiSignerImpl{
    15  		kms:    kms,
    16  		crypto: crypto,
    17  	}
    18  }
    19  
    20  type multiSignerImpl struct {
    21  	kms    keyGetter
    22  	crypto multiSigner
    23  }
    24  
    25  func (m *multiSignerImpl) Sign(msg []byte, pub *jwk.JWK) ([]byte, error) {
    26  	kh, err := m.kms.Get(pub.KeyID)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return m.crypto.Sign(msg, kh)
    32  }
    33  
    34  func (m *multiSignerImpl) SignMulti(msgs [][]byte, pub *jwk.JWK) ([]byte, error) {
    35  	kh, err := m.kms.Get(pub.KeyID)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return m.crypto.SignMulti(msgs, kh)
    41  }
    42  
    43  func (m *multiSignerImpl) FixedKeyMultiSigner(pub *jwk.JWK) (api.FixedKeyMultiSigner, error) {
    44  	return m.FixedMultiSignerGivenKID(pub.KeyID)
    45  }
    46  
    47  func (m *multiSignerImpl) FixedMultiSignerGivenKID(kid string) (api.FixedKeyMultiSigner, error) {
    48  	return getFixedMultiSigner(m.kms, m.crypto, kid)
    49  }
    50  
    51  func getFixedMultiSigner(kms keyGetter, crypto multiSigner, kid string) (api.FixedKeyMultiSigner, error) {
    52  	kh, err := kms.Get(kid)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return &fixedMultiSignerImpl{
    58  		cr: crypto,
    59  		kh: kh,
    60  	}, nil
    61  }
    62  
    63  var _ api.KMSCryptoMultiSigner = &multiSignerImpl{}
    64  
    65  type fixedMultiSignerImpl struct {
    66  	cr multiSigner
    67  	kh interface{}
    68  }
    69  
    70  func (f *fixedMultiSignerImpl) SignMulti(msgs [][]byte) ([]byte, error) {
    71  	return f.cr.SignMulti(msgs, f.kh)
    72  }
    73  
    74  func (f *fixedMultiSignerImpl) Sign(msg []byte) ([]byte, error) {
    75  	return f.cr.Sign(msg, f.kh)
    76  }
    77  
    78  var _ api.FixedKeyMultiSigner = &fixedMultiSignerImpl{}