github.com/annchain/OG@v0.0.9/p2p/onode/secp256K1.go (about)

     1  package onode
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto"
     6  	"github.com/annchain/OG/types/msg"
     7  	"github.com/tinylib/msgp/msgp"
     8  )
     9  
    10  // Secp256k1 is the "secp256k1" key, which holds a public key.
    11  type Secp256k1 ecdsa.PublicKey
    12  
    13  func (v Secp256k1) ENRKey() string { return "secp256k1" }
    14  
    15  // EncodeRLP implements rlp.Encoder.
    16  func (v Secp256k1) MarshalMsg(b []byte) ([]byte, error) {
    17  	key := ogcrypto2.CompressPubkey((*ecdsa.PublicKey)(&v))
    18  	keyBytes := msg.Bytes(key)
    19  	return keyBytes.MarshalMsg(b)
    20  }
    21  
    22  // DecodeRLP implements rlp.Decoder.
    23  func (v *Secp256k1) UnmarshalMsg(b []byte) ([]byte, error) {
    24  	var keyBytes msg.Bytes
    25  	d, err := keyBytes.UnmarshalMsg(b)
    26  	if err != nil {
    27  		return d, err
    28  	}
    29  	pk, err := ogcrypto2.DecompressPubkey(keyBytes)
    30  	if err != nil {
    31  		return d, err
    32  	}
    33  	*v = (Secp256k1)(*pk)
    34  	return d, nil
    35  }
    36  
    37  func (v *Secp256k1) DecodeMsg(en *msgp.Reader) (err error) {
    38  	var keyBytes msg.Bytes
    39  	err = keyBytes.DecodeMsg(en)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	pk, err := ogcrypto2.DecompressPubkey(keyBytes)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	*v = (Secp256k1)(*pk)
    48  	return nil
    49  }
    50  
    51  func (v Secp256k1) EncodeMsg(en *msgp.Writer) (err error) {
    52  	key := ogcrypto2.CompressPubkey((*ecdsa.PublicKey)(&v))
    53  	keyBytes := msg.Bytes(key)
    54  	return keyBytes.EncodeMsg(en)
    55  }
    56  
    57  func (v Secp256k1) Msgsize() int {
    58  	key := ogcrypto2.CompressPubkey((*ecdsa.PublicKey)(&v))
    59  	keyBytes := msg.Bytes(key)
    60  	return keyBytes.Msgsize()
    61  }