-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
213 lines (182 loc) · 4.73 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"crypto/rand"
"errors"
"fmt"
"html/template"
rand2 "math/rand"
"net/http"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"time"
"github.com/core-coin/go-core/v2/accounts/keystore"
"github.com/core-coin/go-core/v2/common"
"github.com/core-coin/go-core/v2/crypto"
)
type DecryptedWalletData struct {
PrivateKey string
PublicKey string
Address string
IsError bool
ErrorMsg string
}
type EncryptedWalletData struct {
FilePath string
Address string
IsError bool
ErrorMsg string
}
func main() {
rand2.Seed(time.Now().Unix())
http.HandleFunc("/", indexHandler)
http.HandleFunc("/generate_raw", rawDataHandler)
http.HandleFunc("/generate_encrypted", encryptedDataHandler)
http.HandleFunc("/exit", exitHandler)
go open("http://localhost:8080/")
panic(http.ListenAndServe(":8080", nil))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
returnPageToClient(w, nil, baseHTML, indexHTML, cssHTML)
}
func rawDataHandler(w http.ResponseWriter, r *http.Request) {
_, _, _, err := getFormValues(r) // set network ID
if err != nil {
returnPageToClient(
w,
DecryptedWalletData{IsError: true, ErrorMsg: err.Error()},
baseHTML, indexHTML, cssHTML,
)
return
}
PrivateKey, err := crypto.GenerateKey(rand.Reader)
if err != nil {
returnPageToClient(
w,
DecryptedWalletData{IsError: true, ErrorMsg: fmt.Sprintf("Cannot generate private key: %v ", err)},
baseHTML, indexHTML, cssHTML,
)
return
}
returnPageToClient(
w,
DecryptedWalletData{
PrivateKey: common.Bytes2Hex(PrivateKey.PrivateKey()),
PublicKey: common.Bytes2Hex(PrivateKey.PublicKey()[:]),
Address: PrivateKey.Address().Hex(),
},
baseHTML,
decryptedHTML,
cssHTML,
)
}
func exitHandler(w http.ResponseWriter, req *http.Request) {
os.Exit(1)
}
func encryptedDataHandler(w http.ResponseWriter, r *http.Request) {
keyPath, password, _, err := getFormValues(r)
if err != nil {
returnPageToClient(
w,
DecryptedWalletData{IsError: true, ErrorMsg: err.Error()},
baseHTML, indexHTML, cssHTML,
)
return
}
account, err := keystore.StoreKey(keyPath, password, keystore.StandardScryptN, keystore.StandardScryptP)
if err != nil {
returnPageToClient(
w,
DecryptedWalletData{IsError: true, ErrorMsg: "Cannot create a wallet file: " + err.Error()},
baseHTML, indexHTML, cssHTML,
)
return
}
returnPageToClient(
w,
EncryptedWalletData{
FilePath: account.URL.Path,
Address: account.Address.Hex(),
},
baseHTML,
encryptedHTML,
cssHTML,
)
}
func returnPageToClient(w http.ResponseWriter, data interface{}, templates ...string) {
tmpl, err := renderTemplates(templates...)
if err != nil {
fmt.Fprintf(w, "Cannot render templates: %v", err)
return
}
err = tmpl.ExecuteTemplate(w, "base", data)
if err != nil {
fmt.Fprintf(w, "Cannot execute template: %v", err)
}
}
func getFormValues(r *http.Request) (keyPath, password, passwordRepeat string, err error) {
if err := r.ParseForm(); err != nil {
return "", "", "", errors.New(fmt.Sprintf("Cannot parse data from form: %v", err))
}
// Setup NetworkID
network := r.FormValue("network_id")
if network == "" {
return "", "", "", errors.New("You need to define network ID: 1=Mainnet, 3=Devin, 4+=Enterprise")
}
networkId, err := strconv.Atoi(network)
if err != nil {
return "", "", "", errors.New(fmt.Sprintf("Wrong network id: %v ", err))
}
if networkId == 2 {
return "", "", "", errors.New("Network ID 2 is not existent!")
}
common.DefaultNetworkID = common.NetworkID(networkId)
password = r.FormValue("pass")
passwordRepeat = r.FormValue("pass_repeat")
if password != passwordRepeat {
return "", "", "", errors.New("Passwords does not match")
}
keyPath = r.FormValue("path")
if keyPath == "" {
keyPath, err = os.Getwd()
if err != nil {
return "", "", "", errors.New(fmt.Sprintf("Cannot get current directory: %v ", err))
}
return
}
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
if !path.IsAbs(keyPath) {
return "", "", "", errors.New("Зath for keyfile is not absolute")
}
}
return
}
func renderTemplates(templates ...string) (*template.Template, error) {
var err error
tmpl := template.New("")
for _, oneTemplate := range templates {
tmpl, err = tmpl.Parse(oneTemplate)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot parse template: %v ", err))
}
}
return tmpl, nil
}
// open opens the specified URL in the default browser of the user.
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}