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

     1  package secp256r1
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	cmtcrypto "github.com/cometbft/cometbft/crypto"
     7  	"github.com/cosmos/gogoproto/proto"
     8  
     9  	ecdsa "github.com/cosmos/cosmos-sdk/crypto/keys/internal/ecdsa"
    10  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    11  )
    12  
    13  // customProtobufType is here to make sure that ecdsaPK and ecdsaSK implement the
    14  // gogoproto customtype interface.
    15  type customProtobufType interface {
    16  	Marshal() ([]byte, error)
    17  	MarshalTo(data []byte) (n int, err error)
    18  	Unmarshal(data []byte) error
    19  	Size() int
    20  
    21  	MarshalJSON() ([]byte, error)
    22  	UnmarshalJSON(data []byte) error
    23  }
    24  
    25  var _ customProtobufType = (*ecdsaPK)(nil)
    26  
    27  // String implements proto.Message interface.
    28  func (m *PubKey) String() string {
    29  	return m.Key.String(name)
    30  }
    31  
    32  // Bytes implements SDK PubKey interface.
    33  func (m *PubKey) Bytes() []byte {
    34  	if m == nil {
    35  		return nil
    36  	}
    37  	return m.Key.Bytes()
    38  }
    39  
    40  // Equals implements SDK PubKey interface.
    41  func (m *PubKey) Equals(other cryptotypes.PubKey) bool {
    42  	pk2, ok := other.(*PubKey)
    43  	if !ok {
    44  		return false
    45  	}
    46  	return m.Key.Equal(&pk2.Key.PublicKey)
    47  }
    48  
    49  // Address implements SDK PubKey interface.
    50  func (m *PubKey) Address() cmtcrypto.Address {
    51  	return m.Key.Address(proto.MessageName(m))
    52  }
    53  
    54  // Type returns key type name. Implements SDK PubKey interface.
    55  func (m *PubKey) Type() string {
    56  	return name
    57  }
    58  
    59  // VerifySignature implements SDK PubKey interface.
    60  func (m *PubKey) VerifySignature(msg, sig []byte) bool {
    61  	return m.Key.VerifySignature(msg, sig)
    62  }
    63  
    64  type ecdsaPK struct {
    65  	ecdsa.PubKey
    66  }
    67  
    68  // Marshal implements customProtobufType.
    69  func (pk ecdsaPK) Marshal() ([]byte, error) {
    70  	return pk.PubKey.Bytes(), nil
    71  }
    72  
    73  // MarshalJSON implements customProtobufType.
    74  func (pk ecdsaPK) MarshalJSON() ([]byte, error) {
    75  	b64 := base64.StdEncoding.EncodeToString(pk.PubKey.Bytes())
    76  	return []byte("\"" + b64 + "\""), nil
    77  }
    78  
    79  // UnmarshalJSON implements customProtobufType.
    80  func (pk *ecdsaPK) UnmarshalJSON(data []byte) error {
    81  	// the string is quoted so we need to remove them
    82  	bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	return pk.PubKey.Unmarshal(bz, secp256r1, pubKeySize)
    88  }
    89  
    90  // Size implements proto.Marshaler interface
    91  func (pk *ecdsaPK) Size() int {
    92  	if pk == nil {
    93  		return 0
    94  	}
    95  	return pubKeySize
    96  }
    97  
    98  // Unmarshal implements proto.Marshaler interface
    99  func (pk *ecdsaPK) Unmarshal(bz []byte) error {
   100  	return pk.PubKey.Unmarshal(bz, secp256r1, pubKeySize)
   101  }