// NodeKey retrieves the currently configured private key of the node, checking // first any manually set key, falling back to the one found in the configured // data folder. If no key can be found, a new one is generated. func (c *Config) NodeKey() *ecdsa.PrivateKey { // Use any specifically configured key if c.PrivateKey != nil { return c.PrivateKey } // Generate ephemeral key if no datadir is being used if c.DataDir == "" { key, err := crypto.GenerateKey() if err != nil { glog.Fatalf("Failed to generate ephemeral node key: %v", err) } return key } // Fall back to persistent key from the data directory keyfile := filepath.Join(c.DataDir, datadirPrivateKey) if key, err := crypto.LoadECDSA(keyfile); err == nil { return key } // No persistent key found, generate and store a new one key, err := crypto.GenerateKey() if err != nil { glog.Fatalf("Failed to generate node key: %v", err) } if err := crypto.SaveECDSA(keyfile, key); err != nil { glog.V(logger.Error).Infof("Failed to persist node key: %v", err) } return key }
func main() { logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.InfoLevel)) // Generate the peer identity key, err := crypto.GenerateKey() if err != nil { fmt.Printf("Failed to generate peer key: %v.\n", err) os.Exit(-1) } name := common.MakeName("whisper-go", "1.0") shh := whisper.New() // Create an Ethereum peer to communicate through server := p2p.Server{ PrivateKey: key, MaxPeers: 10, Name: name, Protocols: []p2p.Protocol{shh.Protocol()}, ListenAddr: ":30300", NAT: nat.Any(), } fmt.Println("Starting Ethereum peer...") if err := server.Start(); err != nil { fmt.Printf("Failed to start Ethereum peer: %v.\n", err) os.Exit(1) } // Send a message to self to check that something works payload := fmt.Sprintf("Hello world, this is %v. In case you're wondering, the time is %v", name, time.Now()) if err := selfSend(shh, []byte(payload)); err != nil { fmt.Printf("Failed to self message: %v.\n", err) os.Exit(-1) } }
// NewIdentity generates a new cryptographic identity for the client, and injects // it into the known identities for message decryption. func (self *Whisper) NewIdentity() *ecdsa.PrivateKey { key, err := crypto.GenerateKey() if err != nil { panic(err) } self.keys[string(crypto.FromECDSAPub(&key.PublicKey))] = key return key }
// Fetch initialises a new abi and returns the contract. It does not // deploy the contract, hence the name. func Fetch(db ethdb.Database, mux *event.TypeMux, blockchain *core.BlockChain) (*Channels, error) { contract := Channels{ blockchain: blockchain, channels: make(map[common.Hash]*Channel), filters: filters.NewFilterSystem(mux), } contract.callKey, _ = crypto.GenerateKey() var err error contract.abi, err = abi.JSON(strings.NewReader(jsonAbi)) if err != nil { return nil, err } return &contract, nil }