github.com/klaytn/klaytn@v1.10.2/cmd/homi/common/utils.go (about)

     1  // Copyright 2017 AMIS Technologies
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package common
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"crypto/rand"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/klaytn/klaytn/log"
    28  
    29  	"github.com/klaytn/klaytn/common"
    30  	"github.com/klaytn/klaytn/crypto"
    31  	uuid "github.com/satori/go.uuid"
    32  )
    33  
    34  const (
    35  	defaultLocalDir  = "/tmp/kdata"
    36  	clientIdentifier = "klay"
    37  )
    38  
    39  var logger = log.NewModuleLogger(log.CMDIstanbul)
    40  
    41  func GenerateRandomDir() (string, error) {
    42  	err := os.MkdirAll(filepath.Join(defaultLocalDir), 0o700)
    43  	if err != nil {
    44  		logger.Error("Failed to create dir", "dir", defaultLocalDir, "err", err)
    45  		return "", err
    46  	}
    47  
    48  	instanceDir := filepath.Join(defaultLocalDir, fmt.Sprintf("%s-%s", clientIdentifier, uuid.NewV4().String()))
    49  	if err := os.MkdirAll(instanceDir, 0o700); err != nil {
    50  		logger.Error("Failed to create dir", "dir", instanceDir, "err", err)
    51  		return "", err
    52  	}
    53  
    54  	return instanceDir, nil
    55  }
    56  
    57  func GenerateKeys(num int) (keys []*ecdsa.PrivateKey, nodekeys []string, addrs []common.Address) {
    58  	for i := 0; i < num; i++ {
    59  		nodekey := RandomHex()[2:]
    60  		nodekeys = append(nodekeys, nodekey)
    61  
    62  		key, err := crypto.HexToECDSA(nodekey)
    63  		if err != nil {
    64  			logger.Error("Failed to generate key", "err", err)
    65  			return nil, nil, nil
    66  		}
    67  		keys = append(keys, key)
    68  
    69  		addr := crypto.PubkeyToAddress(key.PublicKey)
    70  		addrs = append(addrs, addr)
    71  	}
    72  
    73  	return keys, nodekeys, addrs
    74  }
    75  
    76  func RandomHex() string {
    77  	b, _ := RandomBytes(32)
    78  	return common.BytesToHash(b).Hex()
    79  }
    80  
    81  func RandomBytes(len int) ([]byte, error) {
    82  	b := make([]byte, len)
    83  	_, _ = rand.Read(b)
    84  
    85  	return b, nil
    86  }
    87  
    88  func copyFile(src string, dst string) {
    89  	data, err := ioutil.ReadFile(src)
    90  	if err != nil {
    91  		logger.Error("Failed to read file", "file", src, "err", err)
    92  		return
    93  	}
    94  	err = ioutil.WriteFile(dst, data, 0o644)
    95  	if err != nil {
    96  		logger.Error("Failed to write file", "file", dst, "err", err)
    97  		return
    98  	}
    99  }