github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/chainbridge-core/crypto/secp256k1/secp256k1.go (about)

     1  // Copyright 2020 ChainSafe Systems
     2  // SPDX-License-Identifier: LGPL-3.0-only
     3  
     4  package secp256k1
     5  
     6  import (
     7  	"crypto/ecdsa"
     8  
     9  	"github.com/elastos/Elastos.ELA.SideChain.ESC/common"
    10  	"github.com/elastos/Elastos.ELA.SideChain.ESC/common/hexutil"
    11  
    12  	secp256k1 "github.com/elastos/Elastos.ELA.SideChain.ESC/crypto"
    13  )
    14  
    15  const PrivateKeyLength = 32
    16  
    17  type Keypair struct {
    18  	public  *ecdsa.PublicKey
    19  	private *ecdsa.PrivateKey
    20  }
    21  
    22  func NewKeypairFromPrivateKey(priv []byte) (*Keypair, error) {
    23  	pk, err := secp256k1.ToECDSA(priv)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	return &Keypair{
    29  		public:  pk.Public().(*ecdsa.PublicKey),
    30  		private: pk,
    31  	}, nil
    32  }
    33  
    34  // NewKeypairFromPrivateKey parses a string for a hex private key. Must be at least
    35  // PrivateKeyLength long.
    36  func NewKeypairFromString(priv string) (*Keypair, error) {
    37  	pk, err := secp256k1.HexToECDSA(priv)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return &Keypair{
    43  		public:  pk.Public().(*ecdsa.PublicKey),
    44  		private: pk,
    45  	}, nil
    46  }
    47  
    48  func NewKeypair(pk ecdsa.PrivateKey) *Keypair {
    49  	pub := pk.Public()
    50  
    51  	return &Keypair{
    52  		public:  pub.(*ecdsa.PublicKey),
    53  		private: &pk,
    54  	}
    55  }
    56  
    57  func GenerateKeypair() (*Keypair, error) {
    58  	priv, err := secp256k1.GenerateKey()
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return NewKeypair(*priv), nil
    64  }
    65  
    66  // Encode dumps the private key as bytes
    67  func (kp *Keypair) Encode() []byte {
    68  	return secp256k1.FromECDSA(kp.private)
    69  }
    70  
    71  // Decode initializes the keypair using the input
    72  func (kp *Keypair) Decode(in []byte) error {
    73  	key, err := secp256k1.ToECDSA(in)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	kp.public = key.Public().(*ecdsa.PublicKey)
    79  	kp.private = key
    80  
    81  	return nil
    82  }
    83  
    84  // Address returns the Ethereum address format
    85  func (kp *Keypair) Address() string {
    86  	return secp256k1.PubkeyToAddress(*kp.public).String()
    87  }
    88  
    89  // CommonAddress returns the Ethereum address in the common.Address Format
    90  func (kp *Keypair) CommonAddress() common.Address {
    91  	return secp256k1.PubkeyToAddress(*kp.public)
    92  }
    93  
    94  // PublicKey returns the public key hex encoded
    95  func (kp *Keypair) PublicKey() string {
    96  	return hexutil.Encode(secp256k1.CompressPubkey(kp.public))
    97  }
    98  
    99  func (kp *Keypair) PublicKeyBytes() []byte {
   100  	return secp256k1.CompressPubkey(kp.public)
   101  }
   102  
   103  // PrivateKey returns the keypair's private key
   104  func (kp *Keypair) PrivateKey() *ecdsa.PrivateKey {
   105  	return kp.private
   106  }