diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go new file mode 100644 index 00000000..74fa41ba --- /dev/null +++ b/cmd/utils/utils.go @@ -0,0 +1,114 @@ +// Copyright 2017 AMIS Technologies +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package utils + +import ( + "crypto/ecdsa" + "crypto/rand" + "fmt" + "log" + "os" + "path/filepath" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + uuid "github.com/satori/go.uuid" +) + +const ( + defaultLocalDir = "/tmp/gdata" + clientIdentifier = "geth" + nodekeyFileName = "nodekey" +) + +func GenerateRandomDir() (string, error) { + err := os.MkdirAll(filepath.Join(defaultLocalDir), 0700) + if err != nil { + log.Fatal(err) + } + + instanceDir := filepath.Join(defaultLocalDir, fmt.Sprintf("%s-%s", clientIdentifier, uuid.NewV4().String())) + if err := os.MkdirAll(instanceDir, 0700); err != nil { + log.Println(fmt.Sprintf("Failed to create instance dir: %v", err)) + return "", err + } + + return instanceDir, nil +} + +func GenerateKeys(num int) (keys []*ecdsa.PrivateKey, nodekeys []string, addrs []common.Address) { + for i := 0; i < num; i++ { + nodekey := RandomHex() + nodekeys = append(nodekeys, nodekey) + + key, err := crypto.HexToECDSA(nodekey) + if err != nil { + log.Fatalf("couldn't generate key: " + err.Error()) + } + keys = append(keys, key) + + addr := crypto.PubkeyToAddress(key.PublicKey) + addrs = append(addrs, addr) + } + + return keys, nodekeys, addrs +} + +// func GenerateKeys(num int) (keys []*ecdsa.PrivateKey, addrs []common.Address) { +// for i := 0; i < num; i++ { +// key, err := crypto.GenerateKey() +// if err != nil { +// log.Fatalf("couldn't generate key: " + err.Error()) +// } +// keys = append(keys, key) + +// addr := crypto.PubkeyToAddress(key.PublicKey) +// addrs = append(addrs, addr) +// } + +// return keys, addrs +// } + +func SaveNodeKey(key *ecdsa.PrivateKey, dataDir string) error { + keyDir := filepath.Join(dataDir, clientIdentifier) + if err := os.MkdirAll(keyDir, 0700); err != nil { + log.Println(fmt.Sprintf("Failed to create key dir: %v", err)) + return err + } + + keyfile := filepath.Join(keyDir, nodekeyFileName) + if err := crypto.SaveECDSA(keyfile, key); err != nil { + log.Println(fmt.Sprintf("Failed to persist node key: %v", err)) + return err + } + return nil +} + +func RandomHex() string { + b, _ := RandomBytes(32) + return common.BytesToHash(b).Hex() +} + +func RandomBytes(len int) ([]byte, error) { + b := make([]byte, len) + _, err := rand.Read(b) + if err != nil { + log.Fatalln(err) + } + + return b, nil +} diff --git a/container/blockchain.go b/container/blockchain.go index 13203096..4e00e7eb 100644 --- a/container/blockchain.go +++ b/container/blockchain.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/phayes/freeport" + "github.com/getamis/istanbul-tools/cmd/utils" "github.com/getamis/istanbul-tools/genesis" ) @@ -137,7 +138,7 @@ func NewDefaultBlockchainWithFaulty(network *DockerNetwork, numOfNormal int, num log.Fatalf("Failed to get free ip addresses, err: %v", err) } - keys, addrs := generateKeys(totalNodes) + keys, _, addrs := utils.GenerateKeys(totalNodes) bc.setupGenesis(addrs) // Create normal validators bc.opts = normalOpts @@ -319,7 +320,7 @@ func (bc *blockchain) CreateNodes(num int, options ...Option) (nodes []Ethereum, opts = append(opts, options...) // Host data directory - dataDir, err := generateRandomDir() + dataDir, err := utils.GenerateRandomDir() if err != nil { log.Println("Failed to create data dir", err) return nil, err @@ -353,7 +354,7 @@ func (bc *blockchain) addValidators(numOfValidators int) error { if err != nil { return err } - keys, addrs := generateKeys(numOfValidators) + keys, _, addrs := utils.GenerateKeys(numOfValidators) bc.setupGenesis(addrs) bc.setupValidators(ips, keys, bc.opts...) @@ -394,7 +395,7 @@ func (bc *blockchain) setupValidators(ips []net.IP, keys []*ecdsa.PrivateKey, op opts = append(opts, options...) // Host data directory - dataDir, err := generateRandomDir() + dataDir, err := utils.GenerateRandomDir() if err != nil { log.Fatal("Failed to create data dir", err) } diff --git a/container/ethereum.go b/container/ethereum.go index 4f1717f3..1a0dbab2 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -43,6 +43,7 @@ import ( "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/p2p/discover" + istutils "github.com/getamis/istanbul-tools/cmd/utils" "github.com/getamis/istanbul-tools/genesis" "github.com/getamis/istanbul-tools/istclient" ) @@ -146,7 +147,7 @@ type ethereum struct { } func (eth *ethereum) Init(genesisFile string) error { - if err := saveNodeKey(eth.key, eth.dataDir); err != nil { + if err := istutils.SaveNodeKey(eth.key, eth.dataDir); err != nil { log.Fatal("Failed to save nodekey", err) return err } diff --git a/container/utils.go b/container/utils.go index 31456368..ea30c0c3 100644 --- a/container/utils.go +++ b/container/utils.go @@ -17,74 +17,15 @@ package container import ( - "crypto/ecdsa" - "fmt" - "log" - "os" - "path/filepath" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/istanbul" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/rlp" - uuid "github.com/satori/go.uuid" "github.com/getamis/istanbul-tools/cmd/istanbul/extra" ) -const ( - defaultLocalDir = "/tmp/gdata" - clientIdentifier = "geth" - nodekeyFileName = "nodekey" -) - -func generateRandomDir() (string, error) { - err := os.MkdirAll(filepath.Join(defaultLocalDir), 0700) - if err != nil { - log.Fatal(err) - } - - instanceDir := filepath.Join(defaultLocalDir, fmt.Sprintf("%s-%s", clientIdentifier, uuid.NewV4().String())) - if err := os.MkdirAll(instanceDir, 0700); err != nil { - log.Println(fmt.Sprintf("Failed to create instance dir: %v", err)) - return "", err - } - - return instanceDir, nil -} - -func generateKeys(num int) (keys []*ecdsa.PrivateKey, addrs []common.Address) { - for i := 0; i < num; i++ { - key, err := crypto.GenerateKey() - if err != nil { - log.Fatalf("couldn't generate key: " + err.Error()) - } - keys = append(keys, key) - - addr := crypto.PubkeyToAddress(key.PublicKey) - addrs = append(addrs, addr) - } - - return keys, addrs -} - -func saveNodeKey(key *ecdsa.PrivateKey, dataDir string) error { - keyDir := filepath.Join(dataDir, clientIdentifier) - if err := os.MkdirAll(keyDir, 0700); err != nil { - log.Println(fmt.Sprintf("Failed to create key dir: %v", err)) - return err - } - - keyfile := filepath.Join(keyDir, nodekeyFileName) - if err := crypto.SaveECDSA(keyfile, key); err != nil { - log.Println(fmt.Sprintf("Failed to persist node key: %v", err)) - return err - } - return nil -} - func sigHash(header *types.Header) (hash common.Hash) { hasher := sha3.NewKeccak256()