/
utils.go
98 lines (82 loc) · 2.56 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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 <http://www.gnu.org/licenses/>.
package common
import (
"crypto/ecdsa"
"crypto/rand"
"fmt"
"github.com/klaytn/klaytn/log"
"io/ioutil"
"os"
"path/filepath"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/satori/go.uuid"
)
const (
defaultLocalDir = "/tmp/kdata"
clientIdentifier = "klay"
)
var logger = log.NewModuleLogger(log.CMDIstanbul)
func GenerateRandomDir() (string, error) {
err := os.MkdirAll(filepath.Join(defaultLocalDir), 0700)
if err != nil {
logger.Error("Failed to create dir", "dir", defaultLocalDir, "err", err)
return "", err
}
instanceDir := filepath.Join(defaultLocalDir, fmt.Sprintf("%s-%s", clientIdentifier, uuid.NewV4().String()))
if err := os.MkdirAll(instanceDir, 0700); err != nil {
logger.Error("Failed to create dir", "dir", instanceDir, "err", 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()[2:]
nodekeys = append(nodekeys, nodekey)
key, err := crypto.HexToECDSA(nodekey)
if err != nil {
logger.Error("Failed to generate key", "err", err)
return nil, nil, nil
}
keys = append(keys, key)
addr := crypto.PubkeyToAddress(key.PublicKey)
addrs = append(addrs, addr)
}
return keys, nodekeys, addrs
}
func RandomHex() string {
b, _ := RandomBytes(32)
return common.BytesToHash(b).Hex()
}
func RandomBytes(len int) ([]byte, error) {
b := make([]byte, len)
_, _ = rand.Read(b)
return b, nil
}
func copyFile(src string, dst string) {
data, err := ioutil.ReadFile(src)
if err != nil {
logger.Error("Failed to read file", "file", src, "err", err)
return
}
err = ioutil.WriteFile(dst, data, 0644)
if err != nil {
logger.Error("Failed to write file", "file", dst, "err", err)
return
}
}