github.com/cosmos/cosmos-sdk@v0.50.10/crypto/keys/secp256r1/privkey.go (about)

     1  package secp256r1
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	"github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
     7  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
     8  )
     9  
    10  var _ customProtobufType = (*ecdsaSK)(nil)
    11  
    12  // GenPrivKey generates a new secp256r1 private key. It uses operating system randomness.
    13  func GenPrivKey() (*PrivKey, error) {
    14  	key, err := ecdsa.GenPrivKey(secp256r1)
    15  	return &PrivKey{&ecdsaSK{key}}, err
    16  }
    17  
    18  // PubKey implements SDK PrivKey interface.
    19  func (m *PrivKey) PubKey() cryptotypes.PubKey {
    20  	return &PubKey{&ecdsaPK{m.Secret.PubKey()}}
    21  }
    22  
    23  // String implements SDK proto.Message interface.
    24  func (m *PrivKey) String() string {
    25  	return m.Secret.String(name)
    26  }
    27  
    28  // Type returns key type name. Implements SDK PrivKey interface.
    29  func (m *PrivKey) Type() string {
    30  	return name
    31  }
    32  
    33  // Sign hashes and signs the message usign ECDSA. Implements sdk.PrivKey interface.
    34  func (m *PrivKey) Sign(msg []byte) ([]byte, error) {
    35  	return m.Secret.Sign(msg)
    36  }
    37  
    38  // Bytes serialize the private key.
    39  func (m *PrivKey) Bytes() []byte {
    40  	if m == nil {
    41  		return nil
    42  	}
    43  	return m.Secret.Bytes()
    44  }
    45  
    46  // Equals implements SDK PrivKey interface.
    47  func (m *PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {
    48  	sk2, ok := other.(*PrivKey)
    49  	if !ok {
    50  		return false
    51  	}
    52  	return m.Secret.Equal(&sk2.Secret.PrivateKey)
    53  }
    54  
    55  type ecdsaSK struct {
    56  	ecdsa.PrivKey
    57  }
    58  
    59  // Marshal implements customProtobufType.
    60  func (sk ecdsaSK) Marshal() ([]byte, error) {
    61  	return sk.PrivKey.Bytes(), nil
    62  }
    63  
    64  // MarshalJSON implements customProtobufType.
    65  func (sk ecdsaSK) MarshalJSON() ([]byte, error) {
    66  	b64 := base64.StdEncoding.EncodeToString(sk.PrivKey.Bytes())
    67  	return []byte("\"" + b64 + "\""), nil
    68  }
    69  
    70  // UnmarshalJSON implements customProtobufType.
    71  func (sk *ecdsaSK) UnmarshalJSON(data []byte) error {
    72  	bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	return sk.PrivKey.Unmarshal(bz, secp256r1, fieldSize)
    78  }
    79  
    80  // Size implements proto.Marshaler interface
    81  func (sk *ecdsaSK) Size() int {
    82  	if sk == nil {
    83  		return 0
    84  	}
    85  	return fieldSize
    86  }
    87  
    88  // Unmarshal implements proto.Marshaler interface
    89  func (sk *ecdsaSK) Unmarshal(bz []byte) error {
    90  	return sk.PrivKey.Unmarshal(bz, secp256r1, fieldSize)
    91  }