From 7f12ac702f67f07c1f315f642bdfee67f4041fed Mon Sep 17 00:00:00 2001 From: zelig Date: Fri, 23 Jan 2015 13:00:02 +0000 Subject: [PATCH] add -crypto option to switch encryption on by a flag --- cmd/ethereum/flags.go | 2 ++ cmd/ethereum/main.go | 1 + eth/backend.go | 20 +++++++++++--------- eth/protocol.go | 2 +- p2p/peer.go | 3 +++ p2p/server.go | 3 +++ 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/ethereum/flags.go b/cmd/ethereum/flags.go index f829744dc9..94aa765576 100644 --- a/cmd/ethereum/flags.go +++ b/cmd/ethereum/flags.go @@ -65,6 +65,7 @@ var ( SHH bool Dial bool PrintVersion bool + Encryption bool ) // flags specific to cli client @@ -115,6 +116,7 @@ func Init() { flag.BoolVar(&ShowGenesis, "genesis", false, "Dump the genesis block") flag.StringVar(&ImportChain, "chain", "", "Imports given chain") + flag.BoolVar(&Encryption, "crypto", false, "whether to use encryption (experimental, temporary)") flag.BoolVar(&Dump, "dump", false, "output the ethereum state in JSON format. Sub args [number, hash]") flag.StringVar(&DumpHash, "hash", "", "specify arg in hex") flag.IntVar(&DumpNumber, "number", -1, "specify arg in number") diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index b816c678e7..2eec4695ff 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -75,6 +75,7 @@ func main() { KeyRing: KeyRing, Shh: SHH, Dial: Dial, + Encryption: Encryption, }) if err != nil { diff --git a/eth/backend.go b/eth/backend.go index c3c7d12878..4cbf30bbe3 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -36,8 +36,9 @@ type Config struct { NATType string PMPGateway string - Shh bool - Dial bool + Shh bool + Dial bool + Encryption bool KeyManager *crypto.KeyManager } @@ -107,7 +108,7 @@ func New(config *Config) (*Ethereum, error) { keyManager.Init(config.KeyRing, 0, false) // Create a new client id for this instance. This will help identifying the node on the network - clientId := p2p.NewSimpleClientIdentity(config.Name, config.Version, config.Identifier, keyManager.PublicKey()) + clientId := p2p.NewSimpleClientIdentity(config.Name, config.Version, config.Identifier, keyManager.PrivateKey(), keyManager.PublicKey()) saveProtocolVersion(db) //ethutil.Config.Db = db @@ -143,12 +144,13 @@ func New(config *Config) (*Ethereum, error) { fmt.Println(nat) eth.net = &p2p.Server{ - Identity: clientId, - MaxPeers: config.MaxPeers, - Protocols: protocols, - Blacklist: eth.blacklist, - NAT: p2p.UPNP(), - NoDial: !config.Dial, + Identity: clientId, + MaxPeers: config.MaxPeers, + Protocols: protocols, + Blacklist: eth.blacklist, + NAT: p2p.UPNP(), + NoDial: !config.Dial, + Encryption: config.Encryption, } if len(config.Port) > 0 { diff --git a/eth/protocol.go b/eth/protocol.go index 24a0f0a8e8..3ade8a2ec8 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -97,7 +97,7 @@ func runEthProtocol(txPool txPool, chainManager chainManager, blockPool blockPoo blockPool: blockPool, rw: rw, peer: peer, - id: fmt.Sprintf("%x", peer.Identity().Pubkey()[:8]), + id: fmt.Sprintf("%x", peer.PublicKey()[:8]), } err = self.handleStatus() if err == nil { diff --git a/p2p/peer.go b/p2p/peer.go index a9afe4322a..94da74b503 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -107,6 +107,9 @@ func newServerPeer(server *Server, conn net.Conn, dialAddr *peerAddr) *Peer { p.otherPeers = server.Peers p.pubkeyHook = server.verifyPeer p.runBaseProtocol = true + if server.Encryption { + p.CryptoType = EthCrypto + } // laddr can be updated concurrently by NAT traversal. // newServerPeer must be called with the server lock held. diff --git a/p2p/server.go b/p2p/server.go index 9a05a7619d..a553862363 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -62,6 +62,9 @@ type Server struct { // If NoDial is true, the server will not dial any peers. NoDial bool + // whether or not use encryption + Encryption bool + // Hook for testing. This is useful because we can inhibit // the whole protocol stack. newPeerFunc peerFunc