github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/secp256k1/secp256k1_key_templates.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package secp256k1
     8  
     9  import (
    10  	"github.com/golang/protobuf/proto"
    11  	commonpb "github.com/google/tink/go/proto/common_go_proto"
    12  	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
    13  
    14  	secp256k1pb "github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/proto/secp256k1_go_proto"
    15  )
    16  
    17  // This file contains pre-generated KeyTemplates for Signer and Verifier.
    18  // One can use these templates to generate new Keysets.
    19  
    20  // DERKeyTemplate is a KeyTemplate that generates a new ECDSA secp256k1 private key with the following parameters:
    21  //   - Hash function: SHA256
    22  //   - Curve: secp256k1
    23  //   - Signature encoding: DER
    24  //   - Output prefix type: TINK
    25  func DERKeyTemplate() (*tinkpb.KeyTemplate, error) {
    26  	return createECDSAKeyTemplate(commonpb.HashType_SHA256,
    27  		secp256k1pb.BitcoinCurveType_SECP256K1,
    28  		secp256k1pb.Secp256K1SignatureEncoding_Bitcoin_DER,
    29  		tinkpb.OutputPrefixType_TINK)
    30  }
    31  
    32  // IEEEP1363KeyTemplate is a KeyTemplate that generates a new ECDSA secp256k1 private key with the following parameters:
    33  //   - Hash function: SHA256
    34  //   - Curve: secp256k1
    35  //   - Signature encoding: IEEE-P1363
    36  //   - Output prefix type: TINK
    37  func IEEEP1363KeyTemplate() (*tinkpb.KeyTemplate, error) {
    38  	return createECDSAKeyTemplate(commonpb.HashType_SHA256,
    39  		secp256k1pb.BitcoinCurveType_SECP256K1,
    40  		secp256k1pb.Secp256K1SignatureEncoding_Bitcoin_IEEE_P1363,
    41  		tinkpb.OutputPrefixType_TINK)
    42  }
    43  
    44  // createECDSAKeyTemplate creates a KeyTemplate containing a Secp256K1KeyFormat with the given parameters.
    45  func createECDSAKeyTemplate(hashType commonpb.HashType, curve secp256k1pb.BitcoinCurveType,
    46  	encoding secp256k1pb.Secp256K1SignatureEncoding, prefixType tinkpb.OutputPrefixType) (*tinkpb.KeyTemplate, error) {
    47  	params := &secp256k1pb.Secp256K1Params{
    48  		HashType: hashType,
    49  		Curve:    curve,
    50  		Encoding: encoding,
    51  	}
    52  	format := &secp256k1pb.Secp256K1KeyFormat{Params: params}
    53  
    54  	serializedFormat, err := proto.Marshal(format)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return &tinkpb.KeyTemplate{
    60  		TypeUrl:          secp256k1SignerTypeURL,
    61  		Value:            serializedFormat,
    62  		OutputPrefixType: prefixType,
    63  	}, nil
    64  }