-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
115 lines (103 loc) · 2.51 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path"
"sync"
)
func GenerateKeyPair(dir string, wg *sync.WaitGroup) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0700)
if err != nil {
panic(err)
}
}
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
_, err := fmt.Fprint(os.Stderr, err)
if err != nil {
panic(err)
}
return
}
pub := fmt.Sprintf("0x%X", FromECDSAPub(&k.PublicKey))
err = ioutil.WriteFile(path.Join(dir, "pub_key.pub"), []byte(pub), 0600)
if err != nil {
panic(err)
}
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
_, err := fmt.Fprint(os.Stderr, err)
if err != nil {
panic(err)
}
return
}
pemBlock := &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
data := pem.EncodeToMemory(pemBlock)
err = ioutil.WriteFile(path.Join(dir, "priv_key.pem"), data, 0600)
if err != nil {
panic(err)
}
wg.Done()
}
func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
return elliptic.Marshal(elliptic.P256(), pub.X, pub.Y)
}
func visitF(evm bool, file *os.File, genesisFile *os.File, seen uint64, hosts arrayHosts) func(string, os.FileInfo, error) error {
return func(p string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() && path.Base(p) == "pub_key.pub" {
var evmAccount []byte
pubKeyHex, e := ioutil.ReadFile(p)
if e != nil {
return e
}
if evm {
evmAccount, e = ioutil.ReadFile(path.Join(path.Dir(p), "eth", "evm-address"))
if e != nil {
return e
}
}
idx := uint64(len(hosts)) - seen
seen--
var endl string
if seen > 0 {
endl = ",\n"
} else {
endl = "\n"
}
if _, err = fmt.Fprintf(file, " {\n \"NetAddr\": \"%s\",\n \"PubKeyHex\": \"%s\"\n }%s",
hosts[idx], pubKeyHex, endl); err != nil {
panic(err)
}
if evm {
evmlFile, err := os.OpenFile(path.Join(path.Dir(p), "eth", "evml.toml"), os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer func() {
if err = evmlFile.Close(); err != nil {
panic(err)
}
}()
if _, err = fmt.Fprintf(evmlFile, "listen = \"%s\"", hosts[idx]); err != nil {
panic(err)
} else if _, err = fmt.Fprintf(genesisFile, "\t\t\"%s\": {\n", evmAccount); err != nil {
panic(err)
} else if _, err = fmt.Fprintf(genesisFile, "\t\t\t\"balance\": \"2019000000000000000000\"\n\t\t}%s", endl); err != nil {
panic(err)
}
}
}
return nil
}
}