This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkey-generator.go
411 lines (388 loc) · 9.91 KB
/
key-generator.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package cryptonym
import (
"bytes"
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
"github.com/fioprotocol/fio-go"
"github.com/fioprotocol/fio-go/eos"
"github.com/fioprotocol/fio-go/eos/ecc"
"github.com/skip2/go-qrcode"
"image"
"image/color"
"math"
"os"
"runtime"
"strconv"
"strings"
"time"
)
var imageSize = func() (size int) {
size = 196
scale := os.Getenv("FYNE_SCALE")
if scale != "" {
new, err := strconv.Atoi(scale)
if err != nil {
return
}
size = ((new * 10) * size) / 10
}
return
}()
var RefreshQr = make(chan bool)
func KeyGenTab() *widget.Box {
keyStr := ""
waitMsg := " ... waiting for entropy from operating system ... "
entry := widget.NewEntry()
var key *ecc.PrivateKey
var showingPriv bool
var err error
vanityQuit := make(chan bool)
vanityOpt := &vanityOptions{}
vanityOpt.threads = runtime.NumCPU()
vanitySearch := widget.NewSelect([]string{"Actor", "Pubkey", "Either"}, func(s string) {
switch s {
case "Actor":
vanityOpt.actor = true
vanityOpt.pub = false
case "Pubkey":
vanityOpt.actor = false
vanityOpt.pub = true
default:
vanityOpt.actor = true
vanityOpt.pub = true
}
})
vanitySearch.SetSelected("Actor")
vanitySearch.Hide()
vanityMatch := widget.NewCheck("match anywhere", func(b bool) {
if b {
vanityOpt.anywhere = true
return
}
vanityOpt.anywhere = false
})
vanityMatch.Hide()
vanityLabel := widget.NewLabel(" ")
vanityEntry := NewClickEntry(&widget.Button{})
vanityEntry.SetPlaceHolder("enter string to search for")
vanityEntry.OnChanged = func(s string) {
vanityLabel.SetText(" ")
if len(s) >= 6 {
vanityLabel.SetText("Note: searching for 6 or more characters can take a very long time.")
}
vanityOpt.word = strings.ToLower(s)
vanityLabel.Refresh()
}
vanityEntry.Hide()
vanityStopButton := &widget.Button{}
vanityStopButton = widget.NewButtonWithIcon("Stop Searching", theme.CancelIcon(), func() {
vanityQuit <- true
entry.SetText("Vanity key generation cancelled")
vanityStopButton.Hide()
})
vanityStopButton.Hide()
vanityCheck := widget.NewCheck("Generate Vanity Address", func(b bool) {
if b {
waitMsg = "Please wait, generating vanity key"
vanitySearch.Show()
vanityMatch.Show()
vanityEntry.Show()
return
}
waitMsg = " ... waiting for entropy from operating system ... "
vanitySearch.Hide()
vanityMatch.Hide()
vanityEntry.Hide()
vanityStopButton.Hide()
})
vanityBox := widget.NewVBox(
widget.NewHBox(
layout.NewSpacer(),
vanityCheck,
vanitySearch,
vanityMatch,
vanityEntry,
layout.NewSpacer(),
),
widget.NewHBox(
layout.NewSpacer(),
vanityLabel,
layout.NewSpacer(),
),
)
emptyQr := disabledImage(imageSize, imageSize)
qrImage := canvas.NewImageFromImage(emptyQr)
qrImage.FillMode = canvas.ImageFillOriginal
newQrPub := image.Image(emptyQr)
newQrPriv := image.Image(emptyQr)
copyToClip := widget.NewButton("", nil)
swapQrButton := widget.NewButton("", nil)
setWait := func(s string) {
keyStr = s
swapQrButton.Disable()
copyToClip.Disable()
qrImage.Image = emptyQr
entry.SetText(keyStr)
copyToClip.Refresh()
swapQrButton.Refresh()
qrImage.Refresh()
entry.Refresh()
}
setWait(waitMsg)
qrPriv := make([]byte, 0)
qrPub := make([]byte, 0)
qrLabel := widget.NewLabel("Public Key:")
qrLabel.Alignment = fyne.TextAlignCenter
swapQr := func() {
switch showingPriv {
case false:
swapQrButton.Text = "Show Pub Key QR Code"
qrLabel.SetText("Private Key:")
qrImage.Image = newQrPriv
showingPriv = true
case true:
swapQrButton.Text = "Show Priv Key QR Code"
qrLabel.SetText("Public Key:")
qrImage.Image = newQrPub
showingPriv = false
}
qrLabel.Refresh()
qrImage.Refresh()
swapQrButton.Refresh()
}
swapQrButton = widget.NewButtonWithIcon("Show Private Key QR Code", theme.VisibilityIcon(), swapQr)
regenButton := &widget.Button{}
newKey := true
var setBusy bool
setKey := func() {
time.Sleep(20 * time.Millisecond) // lame, but prevents a double event on darwin?!?
if setBusy {
return
}
setBusy = true
regenButton.Disable()
go func() {
defer func() {
vanityStopButton.Hide()
regenButton.Enable()
setBusy = false
}()
type ki struct {
kq []byte
pq []byte
k string
e error
}
result := make(chan ki)
go func() {
if vanityCheck.Checked {
if vanityOpt.word == "" {
keyStr = "empty search string provided!"
result <- ki{}
return
}
vanityStopButton.Show()
acc, err := vanityKey(vanityOpt, vanityQuit)
if err != nil {
keyStr = "Sorry, there was a problem generating the key\n" + err.Error()
result <- ki{}
return
}
if acc == nil || acc.KeyBag == nil || acc.KeyBag.Keys[0] == nil {
keyStr = "Sorry, there was a problem generating the key - got empty key!\n"
result <- ki{}
return
}
vanityStopButton.Hide()
key = acc.KeyBag.Keys[0]
} else if newKey {
key, err = ecc.NewRandomPrivateKey()
if err != nil {
keyStr = "Sorry, there was a problem generating the key\n" + err.Error()
result <- ki{}
return
}
}
newKey = true
keyInfo := ki{}
keyInfo.k, keyInfo.kq, keyInfo.pq, keyInfo.e = genKey(key)
result <- keyInfo
}()
// don't show the wait message right away to avoid flicker:
tick := time.NewTicker(time.Second)
var skipSetWait bool
for {
select {
case _ = <-tick.C:
if skipSetWait {
return
}
qrImage.Image = disabledImage(imageSize, imageSize)
qrImage.Refresh()
setWait(waitMsg)
case ki := <-result:
if ki.kq == nil {
return
}
skipSetWait = true
keyStr, qrPriv, qrPub, err = ki.k, ki.kq, ki.pq, ki.e
if err != nil {
keyStr = "Sorry, there was a problem generating the key\n" + err.Error()
}
qrReaderPriv := bytes.NewReader(qrPriv)
newQrPriv, _, err = image.Decode(qrReaderPriv)
if err != nil {
keyStr = "Sorry, there was a problem generating the qr code\n" + err.Error()
}
qrReader := bytes.NewReader(qrPub)
newQrPub, _, err = image.Decode(qrReader)
if err != nil {
keyStr = "Sorry, there was a problem generating the qr code\n" + err.Error()
}
qrImage.Image = newQrPub
swapQrButton.Enable()
copyToClip.Enable()
swapQrButton.Refresh()
copyToClip.Refresh()
entry.SetText(keyStr)
entry.OnChanged = func(string) {
entry.SetText(keyStr)
}
qrImage.Refresh()
showingPriv = true
swapQr()
return
}
}
}()
}
clipped := func() {
go func() {
clip := Win.Clipboard()
clip.SetContent(keyStr)
copyToClip.Text = "Copied!"
if keyStr != clip.Content() {
clip.SetContent("Failed to Copy!")
}
copyToClip.Refresh()
time.Sleep(2 * time.Second)
copyToClip.Text = "Copy To Clipboard"
copyToClip.Refresh()
}()
}
copyToClip = widget.NewButtonWithIcon("Copy To Clipboard", theme.ContentCopyIcon(), clipped)
go setKey()
go func() {
for {
select {
case <-RefreshQr:
newKey = false
setKey()
}
}
}()
regenButton = widget.NewButtonWithIcon("Regenerate", theme.ViewRefreshIcon(), setKey)
vanityEntry.Button = regenButton
return widget.NewVBox(
layout.NewSpacer(),
vanityBox,
fyne.NewContainerWithLayout(layout.NewGridLayout(3),
widget.NewHBox(
layout.NewSpacer(),
widget.NewVBox(
qrLabel,
qrImage,
swapQrButton,
),
),
widget.NewHBox(
widget.NewLabel(" "),
widget.NewVBox(
layout.NewSpacer(),
widget.NewHBox(
entry,
),
widget.NewHBox(
regenButton,
copyToClip,
vanityStopButton,
layout.NewSpacer(),
),
),
),
layout.NewSpacer(),
),
layout.NewSpacer(),
)
}
func disabledImage(w, h int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, w, h))
rr, gg, bb, _ := App.Settings().Theme().ButtonColor().RGBA()
c := color.RGBA{R: uint8(rr), G: uint8(gg), B: uint8(bb), A: math.MaxUint8}
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
img.Set(x, y, c)
}
}
return img
}
func genKey(key *ecc.PrivateKey) (keyinfo string, privQr []byte, pubQr []byte, e error) {
var err error
// convert the WIF into an EOS account structure
kb := eos.NewKeyBag()
err = kb.ImportPrivateKey(key.String())
if err != nil {
return "", nil, nil, err
}
// get the FIO actor name (an EOS account derived from the public key.)
// in FIO an account has a 1:1 relationship to an account, and the account is
// created automatically
fioActor, err := fio.ActorFromPub(kb.Keys[0].PublicKey().String())
if err != nil {
return "", nil, nil, err
}
// Public key URI formatted
var pngPub []byte
qPub, err := qrcode.New("fio:FIO"+kb.Keys[0].PublicKey().String()[3:]+"?label="+string(fioActor), qrcode.Medium)
if err != nil {
return "", nil, nil, err
}
if strings.Contains(WinSettings.T, "Dark") {
qPub.ForegroundColor = darkestGrey
qPub.BackgroundColor = lightestGrey
} else {
qPub.ForegroundColor = color.Black
qPub.BackgroundColor = color.White
}
pngPub, err = qPub.PNG(imageSize)
if err != nil {
return "", nil, nil, err
}
// Private Key
var pngPriv []byte
qPriv, err := qrcode.New(kb.Keys[0].String(), qrcode.Medium)
if err != nil {
return "", nil, nil, err
}
if strings.Contains(WinSettings.T, "Dark") {
qPriv.ForegroundColor = darkestGrey
qPriv.BackgroundColor = lightestGrey
} else {
qPriv.ForegroundColor = color.Black
qPriv.BackgroundColor = color.White
}
pngPriv, err = qPriv.PNG(imageSize)
if err != nil {
return "", nil, nil, err
}
b := bytes.NewBuffer([]byte{})
// print out the result: a FIO public key is really an EOS key, with FIO at the beginning
b.WriteString(fmt.Sprintln("Private Key: ", kb.Keys[0].String()))
b.WriteString(fmt.Sprintln("Public Key: FIO" + kb.Keys[0].PublicKey().String()[3:]))
b.WriteString(fmt.Sprintln("Account Name: ", fioActor))
return b.String(), pngPriv, pngPub, nil
}