private and public key method names consistent

This commit is contained in:
zelig 2015-01-22 00:53:22 +00:00
parent 9918dad19b
commit ba8318db6a
7 changed files with 27 additions and 27 deletions

View file

@ -7,9 +7,9 @@ import (
// ClientIdentity represents the identity of a peer. // ClientIdentity represents the identity of a peer.
type ClientIdentity interface { type ClientIdentity interface {
String() string // human readable identity String() string // human readable identity
Pubkey() []byte // 512-bit public key PublicKey() []byte // 512-bit public key represented in 65 byte format as per golang/elliptic.Marshal, first byte encodes curve
PrivKey() []byte // 512-bit private key PrivateKey() []byte // 256-bit private key
} }
type SimpleClientIdentity struct { type SimpleClientIdentity struct {
@ -53,11 +53,11 @@ func (c *SimpleClientIdentity) String() string {
c.implementation) c.implementation)
} }
func (c *SimpleClientIdentity) Privkey() []byte { func (c *SimpleClientIdentity) PrivateKey() []byte {
return c.privkey return c.privkey
} }
func (c *SimpleClientIdentity) Pubkey() []byte { func (c *SimpleClientIdentity) PublicKey() []byte {
return c.pubkey return c.pubkey
} }

View file

@ -9,11 +9,11 @@ import (
func TestClientIdentity(t *testing.T) { func TestClientIdentity(t *testing.T) {
clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test", []byte("privkey"), []byte("pubkey")) clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test", []byte("privkey"), []byte("pubkey"))
key := clientIdentity.Privkey() key := clientIdentity.PrivateKey()
if !bytes.Equal(key, []byte("privkey")) { if !bytes.Equal(key, []byte("privkey")) {
t.Errorf("Expected Privkey to be %x, got %x", key, []byte("privkey")) t.Errorf("Expected Privkey to be %x, got %x", key, []byte("privkey"))
} }
key = clientIdentity.Pubkey() key = clientIdentity.PublicKey()
if !bytes.Equal(key, []byte("pubkey")) { if !bytes.Equal(key, []byte("pubkey")) {
t.Errorf("Expected Pubkey to be %x, got %x", key, []byte("pubkey")) t.Errorf("Expected Pubkey to be %x, got %x", key, []byte("pubkey"))
} }

View file

@ -8,9 +8,9 @@ import (
"net" "net"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
ethlogger "github.com/ethereum/go-ethereum/logger" ethlogger "github.com/ethereum/go-ethereum/logger"
"github.com/obscuren/ecies" "github.com/obscuren/ecies"
"github.com/obscuren/secp256k1-go"
) )
var clogger = ethlogger.NewLogger("CRYPTOID") var clogger = ethlogger.NewLogger("CRYPTOID")
@ -49,7 +49,7 @@ newCryptoId(id ClientIdentity) initialises a crypto layer manager. This object h
*/ */
func newCryptoId(id ClientIdentity) (self *cryptoId, err error) { func newCryptoId(id ClientIdentity) (self *cryptoId, err error) {
// will be at server init // will be at server init
var prvKeyS []byte = id.PrivKey() var prvKeyS []byte = id.PrivateKey()
if prvKeyS == nil { if prvKeyS == nil {
err = fmt.Errorf("no private key for client") err = fmt.Errorf("no private key for client")
return return
@ -68,7 +68,7 @@ func newCryptoId(id ClientIdentity) (self *cryptoId, err error) {
// to be created at server init shared between peers and sessions // to be created at server init shared between peers and sessions
// for reuse, call wth ReadAt, no reset seek needed // for reuse, call wth ReadAt, no reset seek needed
} }
self.pubKeyS = id.Pubkey()[1:] self.pubKeyS = id.PublicKey()[1:]
clogger.Debugf("initialise crypto for NodeId %v", hexkey(self.pubKeyS)) clogger.Debugf("initialise crypto for NodeId %v", hexkey(self.pubKeyS))
clogger.Debugf("private-key %v\npublic key %v", hexkey(prvKeyS), hexkey(self.pubKeyS)) clogger.Debugf("private-key %v\npublic key %v", hexkey(prvKeyS), hexkey(self.pubKeyS))
return return

View file

@ -110,7 +110,7 @@ func newServerPeer(server *Server, conn net.Conn, dialAddr *peerAddr) *Peer {
// laddr can be updated concurrently by NAT traversal. // laddr can be updated concurrently by NAT traversal.
// newServerPeer must be called with the server lock held. // newServerPeer must be called with the server lock held.
if server.laddr != nil { if server.laddr != nil {
p.ourListenAddr = newPeerAddr(server.laddr, server.Identity.Pubkey()) p.ourListenAddr = newPeerAddr(server.laddr, server.Identity.PublicKey())
} }
return p return p
} }
@ -139,12 +139,12 @@ func (p *Peer) Identity() ClientIdentity {
return p.identity return p.identity
} }
func (self *Peer) Pubkey() (pubkey []byte) { func (self *Peer) PublicKey() (pubkey []byte) {
self.infolock.Lock() self.infolock.Lock()
defer self.infolock.Unlock() defer self.infolock.Unlock()
switch { switch {
case self.identity != nil: case self.identity != nil:
pubkey = self.identity.Pubkey()[1:] pubkey = self.identity.PublicKey()[1:]
case self.dialAddr != nil: case self.dialAddr != nil:
pubkey = self.dialAddr.Pubkey pubkey = self.dialAddr.Pubkey
case self.listenAddr != nil: case self.listenAddr != nil:
@ -323,7 +323,7 @@ func (p *Peer) handleCryptoHandshake() (crw func(net.Conn) MsgChanReadWriter, er
// run on peer // run on peer
// this bit handles the handshake and creates a secure communications channel with // this bit handles the handshake and creates a secure communications channel with
// var rw *secretRW // var rw *secretRW
if sessionToken, crw, err = crypto.NewSession(p.conn, p.Pubkey(), sessionToken, initiator); err != nil { if sessionToken, crw, err = crypto.NewSession(p.conn, p.PublicKey(), sessionToken, initiator); err != nil {
p.Debugf("unable to setup secure session: %v", err) p.Debugf("unable to setup secure session: %v", err)
return return
} }

View file

@ -60,11 +60,11 @@ type handshake struct {
func (h *handshake) String() string { func (h *handshake) String() string {
return h.ID return h.ID
} }
func (h *handshake) Pubkey() []byte { func (h *handshake) PublicKey() []byte {
return h.NodeID return h.NodeID
} }
func (h *handshake) PrivKey() []byte { func (h *handshake) PrivateKey() []byte {
return nil return nil
} }
@ -265,7 +265,7 @@ func (bp *baseProtocol) handshakeMsg() Msg {
bp.peer.ourID.String(), bp.peer.ourID.String(),
caps, caps,
port, port,
bp.peer.ourID.Pubkey()[1:], bp.peer.ourID.PublicKey()[1:],
) )
} }

View file

@ -11,14 +11,14 @@ import (
) )
type peerId struct { type peerId struct {
privKey, pubkey []byte privkey, pubkey []byte
} }
func (self *peerId) String() string { func (self *peerId) String() string {
return fmt.Sprintf("test peer %x", self.Pubkey()[:4]) return fmt.Sprintf("test peer %x", self.PublicKey()[:4])
} }
func (self *peerId) Pubkey() (pubkey []byte) { func (self *peerId) PublicKey() (pubkey []byte) {
pubkey = self.pubkey pubkey = self.pubkey
if len(pubkey) == 0 { if len(pubkey) == 0 {
pubkey = crypto.GenerateNewKeyPair().PublicKey pubkey = crypto.GenerateNewKeyPair().PublicKey
@ -27,11 +27,11 @@ func (self *peerId) Pubkey() (pubkey []byte) {
return return
} }
func (self *peerId) PrivKey() (privKey []byte) { func (self *peerId) PrivateKey() (privkey []byte) {
privKey = self.privKey privkey = self.privkey
if len(privKey) == 0 { if len(privkey) == 0 {
privKey = crypto.GenerateNewKeyPair().PublicKey privkey = crypto.GenerateNewKeyPair().PublicKey
self.privKey = privKey self.privkey = privkey
} }
return return
} }

View file

@ -399,7 +399,7 @@ func (srv *Server) verifyPeer(addr *peerAddr) error {
if srv.Blacklist.Exists(addr.Pubkey) { if srv.Blacklist.Exists(addr.Pubkey) {
return errors.New("blacklisted") return errors.New("blacklisted")
} }
if bytes.Equal(srv.Identity.Pubkey()[1:], addr.Pubkey) { if bytes.Equal(srv.Identity.PublicKey()[1:], addr.Pubkey) {
return newPeerError(errPubkeyForbidden, "not allowed to connect to srv") return newPeerError(errPubkeyForbidden, "not allowed to connect to srv")
} }
srv.lock.RLock() srv.lock.RLock()
@ -407,7 +407,7 @@ func (srv *Server) verifyPeer(addr *peerAddr) error {
for _, peer := range srv.peers { for _, peer := range srv.peers {
if peer != nil { if peer != nil {
id := peer.Identity() id := peer.Identity()
if id != nil && bytes.Equal(id.Pubkey(), addr.Pubkey) { if id != nil && bytes.Equal(id.PublicKey(), addr.Pubkey) {
return errors.New("already connected") return errors.New("already connected")
} }
} }