-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidgets.go
582 lines (485 loc) · 15 KB
/
widgets.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package main
import (
"errors"
"image/color"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/dReam-dApps/dReams/dwidget"
"github.com/dReam-dApps/dReams/gnomes"
"github.com/dReam-dApps/dReams/rpc"
dero "github.com/deroproject/derohe/rpc"
"github.com/deroproject/derohe/walletapi"
)
// / declare some widgets
var (
primes = []string{"MAINNET", "TESTNET", "SIMULATOR", "CUSTOM"} /// set select menu
dropDown = widget.NewSelect(primes, func(s string) { /// do when select changes
whichDaemon(s)
logger.Println("[dSlate] Daemon Set To:", s)
})
rpcLoginInput = widget.NewPasswordEntry()
rpcWalletInput = widget.NewSelectEntry([]string{WALLET_SIMULATOR_DEFAULT, WALLET_TESTNET_DEFAULT, WALLET_MAINNET_DEFAULT})
contractInput = widget.NewMultiLineEntry()
daemonCheckBox = widget.NewCheck("Daemon Connected", func(value bool) {
gnomon.Stop("dSlate")
})
walletCheckBox = widget.NewCheck("Wallet Connected", func(value bool) {
/// do something on change
})
currentHeight = widget.NewEntry()
walletBalance = widget.NewEntry()
gnomonEnabled = widget.NewRadioGroup([]string{}, func(s string) {})
debugEnabled = widget.NewCheck("Debug", func(b bool) {
if b {
debug = true
} else {
debug = false
}
})
)
// User:pass password entry
func rpcLoginEdit() fyne.Widget {
rpcLoginInput.SetPlaceHolder("RPC user:pass")
rpcLoginInput.Resize(fyne.NewSize(360, 45))
rpcLoginInput.Move(fyne.NewPos(10, 650))
this_pass := binding.BindString(&rpc.Wallet.UserPass)
rpcLoginInput.Bind(this_pass)
return rpcLoginInput
}
// Wallet rpc address entry
func rpcWalletEdit() fyne.Widget {
rpcWalletInput.SetPlaceHolder("Wallet RPC Address")
rpcWalletInput.Resize(fyne.NewSize(250, 45))
rpcWalletInput.Move(fyne.NewPos(10, 700))
return rpcWalletInput
}
// Wallet connect button
func rpcConnectButton() fyne.Widget {
button := widget.NewButton("Connect", func() { /// do on pressed
rpc.Wallet.Rpc = rpcWalletInput.Text
GetAddress()
})
button.Resize(fyne.NewSize(100, 42))
button.Move(fyne.NewPos(270, 702))
return button
}
// Daemon select menu
func daemonSelectOption() fyne.Widget {
dropDown.SetSelectedIndex(0)
dropDown.Resize(fyne.NewSize(180, 45))
dropDown.Move(fyne.NewPos(10, 550))
return dropDown
}
// Daemon check box
func daemonConnectBox() fyne.Widget {
daemonCheckBox.Resize(fyne.NewSize(30, 30))
daemonCheckBox.Move(fyne.NewPos(3, 595))
daemonCheckBox.Disable()
return daemonCheckBox
}
// Wallet check box
func walletConnectBox() fyne.Widget {
walletCheckBox.Resize(fyne.NewSize(30, 30))
walletCheckBox.Move(fyne.NewPos(3, 620))
walletCheckBox.Disable()
return walletCheckBox
}
// Height display entry is read only
func heightDisplay() fyne.Widget {
currentHeight.SetText("Height:")
currentHeight.Disable()
currentHeight.Resize(fyne.NewSize(170, 45))
currentHeight.Move(fyne.NewPos(200, 550))
return currentHeight
}
// Balance display entry is read only
func balanceDisplay() fyne.Widget {
walletBalance.SetText("Balance:")
walletBalance.Disable()
walletBalance.Resize(fyne.NewSize(170, 45))
walletBalance.Move(fyne.NewPos(200, 600))
return walletBalance
}
// SC entry
func contractEntry() fyne.Widget {
contractInput.SetPlaceHolder("Enter Contract Id:")
contractInput.Resize(fyne.NewSize(360, 45))
contractInput.Move(fyne.NewPos(10, 15))
contractInput.Wrapping = fyne.TextWrapWord
return contractInput
}
// Print SC code button
func codeButton() fyne.Widget {
button := widget.NewButton("SC Code", func() {
if len(contractInput.Text) == 64 {
getSCcode(contractInput.Text)
}
})
return button
}
// SC search button
func searchButton() fyne.Widget {
button := widget.NewButton("Search", func() {
logger.Println("[dSlate] Searching for: " + contractInput.Text)
p := &dero.GetSC_Params{
SCID: contractInput.Text,
Code: true,
Variables: true,
}
getSC(p)
})
button.Resize(fyne.NewSize(360, 42))
button.Move(fyne.NewPos(10, 63))
return button
}
// Main image
func builtOnImage() fyne.CanvasObject {
img := canvas.NewImageFromResource(resourceBuiltOnDeroPng)
img.FillMode = canvas.ImageFillOriginal
img.Resize(fyne.NewSize(380, 540))
img.Move(fyne.NewPos(10, 210))
return img
}
// Card image
func cardImage() fyne.CanvasObject {
img := canvas.NewImageFromResource(resourceDero1Png)
img.FillMode = canvas.ImageFillOriginal
img.Resize(fyne.NewSize(450, 330))
img.Move(fyne.NewPos(-33, 200))
return img
}
// Blank slate label
func blankWidget() fyne.Widget {
blank := widget.NewLabel("Something goes here...")
return blank
}
// Enable/Disable Gnomon radio group
func enableGnomon() fyne.CanvasObject {
label := widget.NewLabel("Gnomon")
label.Alignment = fyne.TextAlignCenter
gnomonEnabled = widget.NewRadioGroup([]string{"On", "Off"}, func(s string) {
switch s {
case "On":
if rpc.Daemon.Connect {
go gnomes.StartGnomon("dSlate", "boltdb", []string{}, 0, 0, nil)
} else {
gnomonEnabled.SetSelected("Off")
}
case "Off":
gnomon.Stop("dSlate")
default:
}
})
gnomonEnabled.Horizontal = true
cont := container.NewVBox(
label,
container.NewCenter(gnomonEnabled))
return cont
}
// Gnomon search objects
func gnomonOpts() fyne.CanvasObject {
label := widget.NewLabel("")
label.Wrapping = fyne.TextWrapWord
kv_entry := widget.NewEntry()
kv_entry.SetPlaceHolder("Key:")
korv := widget.NewRadioGroup([]string{"Key", "Value"}, func(s string) {})
korv.Horizontal = true
soru := widget.NewRadioGroup([]string{"String", "Uint64"}, func(s string) {})
soru.Horizontal = true
search := widget.NewButton("Search", func() {
if gnomon.IsInitialized() {
switch korv.Selected {
case "Key":
switch soru.Selected {
case "String":
logger.Println("[dSlate] Search results for string key "+kv_entry.Text+" on SCID "+contractInput.Text, searchByKey(contractInput.Text, kv_entry.Text, true))
label.SetText(searchByKey(contractInput.Text, kv_entry.Text, true))
case "Uint64":
logger.Println("[dSlate] Search results for uint64 key "+kv_entry.Text+" on SCID "+contractInput.Text, searchByKey(contractInput.Text, kv_entry.Text, false))
label.SetText(searchByKey(contractInput.Text, kv_entry.Text, false))
default:
logger.Println("[dSlate] Select string or uint64")
}
case "Value":
switch soru.Selected {
case "String":
logger.Println("[dSlate] Search results for string value "+kv_entry.Text+" on SCID "+contractInput.Text, searchByValue(contractInput.Text, kv_entry.Text, true))
label.SetText(searchByValue(contractInput.Text, kv_entry.Text, true))
case "Uint64":
logger.Println("[dSlate] Search results for uint64 value "+kv_entry.Text+" on SCID "+contractInput.Text, searchByValue(contractInput.Text, kv_entry.Text, false))
label.SetText(searchByValue(contractInput.Text, kv_entry.Text, false))
default:
logger.Println("[dSlate] Select string or uint64")
}
default:
logger.Println("[dSlate] Select key or value")
}
} else {
logger.Println("[dSlate] Gnomon not initialized")
}
})
cont := container.NewVBox(
label,
container.NewCenter(korv),
container.NewCenter(soru),
container.NewAdaptiveGrid(2, kv_entry, search))
return cont
}
// NFA objects
func nfaOpts() fyne.CanvasObject {
label := canvas.NewText("", color.White)
label.TextSize = 18
file_name := widget.NewEntry()
file_name.SetPlaceHolder("File Name:")
start := dwidget.NewDeroEntry("", 1, 0)
start.SetPlaceHolder("Starting at #:")
start.Validator = validation.NewRegexp(`^\d{1,}`, "Format Not Valid")
limit := dwidget.NewDeroEntry("", 1, 0)
limit.ExtendBaseWidget(limit)
limit.SetPlaceHolder("Ending at #:")
limit.Validator = validation.NewRegexp(`^\d{1,}`, "Format Not Valid")
stop := widget.NewButton("Stop Loop", func() {
logger.Println("[dSlate] Stopping install loop")
label.Text = "Stopping install loop..."
label.Refresh()
kill_process = true
})
extension := widget.NewSelect([]string{".jpg", ".png", ".gif", ".mp3", ".mp4", ".pdf", ".zip", ".7z", ".avi", ".mov", ".ogg"}, func(s string) {})
extension.PlaceHolder = "ext"
var install fyne.Widget
install = widget.NewButton("Install NFAs", func() {
if rpc.Wallet.Connect && rpc.Daemon.Connect {
if rpc.Wallet.Address == "" || len(rpc.Wallet.Address) != 66 || rpc.Wallet.Address[0:4] != "deto" {
dialog.NewInformation("Error", "Check wallet connection\n\ndSlate installs to testnet only", myWindow).Show()
return
}
go func() {
if limit.Validate() == nil && start.Validate() == nil {
if !process_on {
process_on = true
install.Hide()
stop.Show()
file_name.Disable()
start.Disable()
limit.Disable()
extension.Disable()
name := file_name.Text
lim := rpc.StringToInt(limit.Text)
inc := rpc.StringToInt(start.Text)
logger.Println("[dSlate] Starting install loop for", name+strconv.Itoa(inc)+".bas", "to", name+strconv.Itoa(lim)+".bas")
for i := 10; i > 0; i-- {
if kill_process {
break
}
label.Text = "Starting install loop in " + strconv.Itoa(i)
label.Refresh()
time.Sleep(1 * time.Second)
}
label.Text = ""
label.Refresh()
for {
if kill_process {
break
}
path := name + strconv.Itoa(inc) + ".bas"
if _, err := os.Stat(path); err == nil {
logger.Println("[dSlate] Installing", path)
label.Text = "Installing " + path
label.Refresh()
} else if errors.Is(err, os.ErrNotExist) {
logger.Errorln("[dSlate]", path, "Not Found")
break
}
file, err := os.ReadFile(path)
if err != nil {
logger.Errorln("[dSlate]", err)
break
}
if tx := uploadNFA(string(file)); tx == "" {
label.Text = ("Error installing " + path)
break
} else {
logger.Println("[dSlate] Confirming install TX")
rpc.ConfirmTx(tx, "dSlate", 45)
}
inc++
if inc > lim {
break
}
logger.Println("[dSlate] Waiting for block")
time.Sleep(6 * time.Second)
}
label.Text = ""
label.Refresh()
install.Show()
stop.Hide()
file_name.Enable()
start.Enable()
limit.Enable()
extension.Enable()
process_on = false
kill_process = false
logger.Println("[dSlate] Install loop complete")
} else {
logger.Println("[dSlate] Install already running")
}
} else {
stop.Hide()
logger.Println("[dSlate] Install entries not valid")
}
}()
}
})
stop.Hide()
wf_entry := widget.NewEntry()
wf_entry.SetPlaceHolder("Wallet file name:")
wf_pass := widget.NewPasswordEntry()
wf_pass.SetPlaceHolder("Wallet file password:")
verify_button := widget.NewButton("Verify Sign File", func() {
if wf, err := walletapi.Open_Encrypted_Wallet(wf_entry.Text, wf_pass.Text); err == nil {
input_file := file_name.Text
output_file := strings.TrimSuffix(input_file, ".sign")
if data, err := os.ReadFile(input_file); err != nil {
logger.Errorln("[dSlate] Cannot read input file", err)
} else if signer, message, err := wf.CheckSignature(data); err != nil {
logger.Errorln("[dSlate] Signature verify failed", input_file, err)
} else {
logger.Println("[dSlate] Signed by", "address", signer.String())
if os.WriteFile(output_file, message, 0600); err != nil {
logger.Errorln("[dSlate] Cannot write output file", output_file, err)
}
logger.Println("[dSlate] Successfully wrote message to file. please check", "file", output_file)
}
wf.Close_Encrypted_Wallet()
} else {
logger.Errorln("[dSlate] Wallet", err)
}
})
sign_button := widget.NewButton("Sign File", func() {
if limit.Validate() == nil && start.Validate() == nil {
if wf, err := walletapi.Open_Encrypted_Wallet(wf_entry.Text, wf_pass.Text); err == nil {
go func() {
if !process_on {
process_on = true
install.Hide()
stop.Show()
file_name.Disable()
start.Disable()
limit.Disable()
extension.Disable()
ext := extension.Selected
input := file_name.Text
lim := rpc.StringToInt(limit.Text)
inc := rpc.StringToInt(start.Text)
logger.Println("[dSlate] Starting sign loop for", input+strconv.Itoa(inc)+ext, "to", input+strconv.Itoa(lim)+ext)
for i := 10; i > 0; i-- {
if kill_process {
break
}
label.Text = "Starting sign loop in " + strconv.Itoa(i)
label.Refresh()
time.Sleep(1 * time.Second)
}
label.Text = ""
label.Refresh()
for {
if kill_process {
break
}
input_file := input + strconv.Itoa(inc) + ext
output_file := input_file + ".sign"
if data, err := os.ReadFile(input_file); err != nil {
logger.Errorln("[dSlate] Cannot read input file", err)
break
} else if err := os.WriteFile(output_file, wf.SignData(data), 0600); err != nil {
logger.Errorln("[dSlate] Cannot write output file", output_file)
break
} else {
logger.Println("[dSlate] Successfully signed file. please check", output_file)
}
inc++
if inc > lim {
break
}
time.Sleep(6 * time.Second)
}
label.Text = ""
label.Refresh()
install.Show()
stop.Hide()
file_name.Enable()
start.Enable()
limit.Enable()
extension.Enable()
process_on = false
kill_process = false
logger.Println("[dSlate] Sign loop complete")
} else {
logger.Println("[dSlate] Loop already running")
}
wf.Close_Encrypted_Wallet()
}()
} else {
logger.Errorln("[dSlate] Wallet", err)
}
} else {
logger.Println("[dSlate] Sign entries not valid")
}
})
return container.NewVBox(
container.NewBorder(nil, nil, nil, extension, file_name),
layout.NewSpacer(),
verify_button,
layout.NewSpacer(),
wf_entry,
wf_pass,
sign_button,
layout.NewSpacer(),
container.NewCenter(label),
start,
limit,
layout.NewSpacer(),
install,
stop,
layout.NewSpacer())
}
// Update button with select populated by .bas files in working directory
func updateButton() fyne.CanvasObject {
files, err := filepath.Glob("*.bas")
if err != nil {
logger.Errorln("[dSlate] Update files", err)
files = []string{}
}
opts := widget.NewSelect(files, nil)
update := widget.NewButton("Update Contract", func() {
path := opts.Selected
if _, err := os.Stat(path); err == nil {
logger.Println("[dSlate] Update Path", path)
file, err := os.ReadFile(path)
if err != nil {
logger.Errorln("[dSlate]", err)
return
}
code := string(file)
if code != "" {
updateContract(contractInput.Text, string(file))
} else {
logger.Errorln("[dSlate] Failed to update, code is empty string")
}
} else if errors.Is(err, os.ErrNotExist) {
logger.Errorln("[dSlate]", path, "Not Found")
}
})
return container.NewVBox(opts, update)
}