/
seed_backup_page.go
559 lines (507 loc) · 15.5 KB
/
seed_backup_page.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
package ui
import (
"fmt"
"image/color"
"math/rand"
"strings"
"time"
"github.com/raedahgroup/godcr/ui/values"
"gioui.org/text"
"github.com/raedahgroup/dcrlibwallet"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"github.com/raedahgroup/godcr/ui/decredmaterial"
"github.com/raedahgroup/godcr/wallet"
)
const (
PageSeedBackup = "seedbackup"
infoView = iota
seedView
verifyView
successView
)
type (
seedGroup struct {
selected int
buttons []decredmaterial.Button
}
viewText struct {
title string
action string
steps string
instruction string
}
)
type backupPage struct {
theme *decredmaterial.Theme
wal *wallet.Wallet
info *wallet.MultiWalletInfo
backButton decredmaterial.IconButton
title decredmaterial.Label
steps decredmaterial.Label
instruction decredmaterial.Label
successMessage decredmaterial.Label
successInfo decredmaterial.Label
action decredmaterial.Button
checkBoxes []decredmaterial.CheckBoxStyle
checkIcon *widget.Icon
infoList *layout.List
viewList *layout.List
seedPhraseListLeft *layout.List
seedPhraseListRight *layout.List
verifyList *layout.List
suggestions []seedGroup
seedPhrase []string
selectedSeeds []string
allSuggestions []string
active int
error string
}
func (win *Window) BackupPage(c pageCommon) layout.Widget {
b := &backupPage{
theme: c.theme,
wal: c.wallet,
info: c.info,
action: c.theme.Button(new(widget.Clickable), "View seed phrase"),
backButton: c.theme.PlainIconButton(new(widget.Clickable), c.icons.navigationArrowBack),
title: c.theme.H5("Keep in mind"),
steps: c.theme.Body1("Step 1/2"),
instruction: c.theme.H6("Write down all 33 words in the correct order"),
successMessage: c.theme.H4("Your seed phrase backup is verified"),
successInfo: c.theme.Body2("Be sure to store your seed phrase backup in a secure location."),
checkIcon: c.icons.actionCheckCircle,
active: infoView,
selectedSeeds: make([]string, 0, 33),
}
b.checkIcon.Color = c.theme.Color.Success
b.steps.Color = c.theme.Color.Hint
b.successMessage.Alignment = text.Middle
b.successInfo.Alignment = text.Middle
b.successInfo.Color = b.theme.Color.Hint
b.backButton.Color = c.theme.Color.Hint
b.backButton.Size = values.MarginPadding30
b.backButton.Inset = layout.UniformInset(values.MarginPadding0)
b.action.Background = c.theme.Color.Hint
b.checkBoxes = []decredmaterial.CheckBoxStyle{
c.theme.CheckBox(new(widget.Bool), "The 33-word seed phrase is EXTREMELY IMPORTANT."),
c.theme.CheckBox(new(widget.Bool), "Seed phrase is the only way to restore your wallet."),
c.theme.CheckBox(new(widget.Bool), "It is recommended to store your seed phrase in a physical format (e.g. write down on a paper)."),
c.theme.CheckBox(new(widget.Bool), "It is highly discouraged to store your seed phrase in any digital format (e.g. screenshot)."),
c.theme.CheckBox(new(widget.Bool), "Anyone with your seed phrase can steal your funds. DO NOT show it to anyone."),
}
b.instruction.Alignment = text.Middle
b.allSuggestions = dcrlibwallet.PGPWordList()
for _, cb := range b.checkBoxes {
cb.IconColor = c.theme.Color.Success
cb.Color = c.theme.Color.Success
}
for i := 0; i < 33; i++ {
var bg []decredmaterial.Button
for j := 0; j < 3; j++ {
bg = append(bg, c.theme.Button(new(widget.Clickable), ""))
}
b.suggestions = append(b.suggestions, seedGroup{selected: -1, buttons: bg})
b.selectedSeeds = append(b.selectedSeeds, "-")
}
b.infoList = &layout.List{Axis: layout.Vertical}
b.viewList = &layout.List{Axis: layout.Horizontal}
b.seedPhraseListLeft = &layout.List{Axis: layout.Vertical}
b.seedPhraseListRight = &layout.List{Axis: layout.Vertical}
b.verifyList = &layout.List{Axis: layout.Vertical}
return func(gtx C) layout.Dimensions {
b.handle(c)
return b.layout(gtx)
}
}
func (pg *backupPage) activeButton() {
pg.action.Background = pg.theme.Color.Primary
pg.action.Color = pg.theme.Color.InvText
}
func (pg *backupPage) clearButton() {
pg.action.Background = color.RGBA{}
pg.action.Color = pg.theme.Color.Primary
}
func (pg *backupPage) layout(gtx layout.Context) layout.Dimensions {
dims := pg.theme.Surface(gtx, func(gtx C) D {
toMax(gtx)
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Start}.Layout(gtx,
layout.Rigid(func(gtx C) D {
pg.action.Background = pg.theme.Color.Hint
pg.action.Color = pg.theme.Color.InvText
switch pg.active {
case infoView:
if pg.verifyCheckBoxes() {
pg.activeButton()
}
return pg.infoView(gtx)
case seedView:
pg.activeButton()
return pg.seedView(gtx)
case verifyView:
if checkSlice(pg.selectedSeeds) {
pg.activeButton()
}
return pg.verifyView(gtx)
case successView:
pg.activeButton()
return pg.successView(gtx)
default:
if pg.verifyCheckBoxes() {
pg.activeButton()
}
return pg.infoView(gtx)
}
}),
)
})
return dims
}
func (pg *backupPage) pageTitle(gtx layout.Context) layout.Dimensions {
return layout.Inset{Bottom: values.MarginPadding5, Top: values.MarginPadding20}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Start}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return pg.backButton.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return pg.title.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
if pg.active != infoView {
return layout.Inset{Left: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return pg.steps.Layout(gtx)
})
}
return layout.Dimensions{}
}),
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
if pg.active != infoView {
return layout.Inset{Right: values.MarginPadding30, Top: values.MarginPadding20}.Layout(gtx, func(gtx C) D {
return pg.instruction.Layout(gtx)
})
}
return layout.Dimensions{}
}),
)
}),
)
})
}
func (pg *backupPage) viewTemplate(gtx layout.Context, content layout.Widget) layout.Dimensions {
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
return layout.Inset{Left: values.MarginPadding10, Right: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if pg.active != successView {
return pg.pageTitle(gtx)
}
return layout.Dimensions{}
}),
layout.Rigid(func(gtx C) D {
return content(gtx)
}),
)
}),
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.Inset{Bottom: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return pg.action.Layout(gtx)
})
}),
)
}),
layout.Stacked(func(gtx C) D {
if len(pg.error) > 0 {
return layout.Inset{Top: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return pg.centralize(gtx, func(gtx C) D {
return pg.theme.ErrorAlert(gtx, pg.error)
})
})
}
return layout.Dimensions{}
}),
)
})
}
func (pg *backupPage) infoView(gtx layout.Context) layout.Dimensions {
return pg.viewTemplate(gtx, func(gtx C) D {
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
return pg.centralize(gtx, func(gtx C) D {
return pg.infoList.Layout(gtx, len(pg.checkBoxes), func(gtx C, i int) D {
return layout.Inset{Bottom: values.MarginPadding20}.Layout(gtx, func(gtx C) D {
return pg.checkBoxes[i].Layout(gtx)
})
})
})
})
}
func (pg *backupPage) seedView(gtx layout.Context) layout.Dimensions {
return pg.viewTemplate(gtx, func(gtx C) D {
return pg.centralize(gtx, func(gtx C) D {
return pg.viewList.Layout(gtx, 1, func(gtx C, i int) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
gtx.Constraints.Max.X = gtx.Constraints.Max.X / 2
return pg.seedPhraseListLeft.Layout(gtx, len(pg.seedPhrase), func(gtx C, i int) D {
if i < 17 {
return pg.seedText(gtx, i)
}
return layout.Dimensions{}
})
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: values.MarginPadding30}.Layout(gtx, func(gtx C) D {
return pg.seedPhraseListRight.Layout(gtx, len(pg.seedPhrase), func(gtx C, i int) D {
if i > 16 {
return pg.seedText(gtx, i)
}
return layout.Dimensions{}
})
})
}),
)
})
})
})
}
func (pg *backupPage) verifyView(gtx layout.Context) layout.Dimensions {
return pg.viewTemplate(gtx, func(gtx C) D {
toMax(gtx)
return pg.verifyList.Layout(gtx, len(pg.suggestions), func(gtx C, i int) D {
s := pg.suggestions[i]
return layout.Center.Layout(gtx, func(gtx C) D {
return layout.Inset{Bottom: values.MarginPadding30}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: values.MarginPadding15, Bottom: values.MarginPadding15}.Layout(gtx, func(gtx C) D {
return pg.theme.H6(fmt.Sprintf("%d. %s", i+1, pg.selectedSeeds[i])).Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Flexed(0.3, func(gtx C) D {
return pg.suggestionButtonGroup(gtx, s, 0)
}),
layout.Flexed(0.3, func(gtx C) D {
return pg.suggestionButtonGroup(gtx, s, 1)
}),
layout.Flexed(0.3, func(gtx C) D {
return pg.suggestionButtonGroup(gtx, s, 2)
}),
)
}),
)
})
})
})
})
}
func (pg *backupPage) successView(gtx layout.Context) layout.Dimensions {
return pg.viewTemplate(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
return layout.Center.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.Inset{
Bottom: values.MarginPadding50,
Right: values.MarginPadding50,
}.Layout(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding20).Layout(gtx, func(gtx C) D {
pg.checkIcon.Color = pg.theme.Color.Success
return pg.checkIcon.Layout(gtx, unit.Px(float32(150)))
})
})
})
}),
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return pg.successMessage.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return pg.successInfo.Layout(gtx)
}),
)
})
}),
)
})
}
func (pg *backupPage) seedText(gtx layout.Context, index int) layout.Dimensions {
return layout.Inset{Bottom: values.MarginPadding10, Left: values.MarginPadding20}.Layout(gtx,
func(gtx C) D {
seedLabel := pg.theme.H6(fmt.Sprintf("%d. %s", index+1, pg.seedPhrase[index]))
seedLabel.Alignment = text.Middle
return seedLabel.Layout(gtx)
},
)
}
func (pg *backupPage) centralize(gtx layout.Context, content layout.Widget) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
return layout.Center.Layout(gtx, content)
}),
)
}
func (pg *backupPage) suggestionButtonGroup(gtx layout.Context, sg seedGroup, buttonIndex int) layout.Dimensions {
button := sg.buttons[buttonIndex]
button.Background = pg.theme.Color.Hint
button.TextSize = values.TextSize18
if sg.selected == buttonIndex {
button.Background = pg.theme.Color.Primary
}
return layout.Inset{Right: values.MarginPadding15, Left: values.MarginPadding15}.Layout(gtx, func(gtx C) D {
return button.Layout(gtx)
})
}
func (pg *backupPage) verifyCheckBoxes() bool {
for _, cb := range pg.checkBoxes {
if !cb.CheckBox.Value {
return false
}
}
return true
}
func (pg *backupPage) randomSeeds() []string {
var randomSeeds []string
for i := 0; i < 3; i++ {
random := rand.Intn(len(pg.allSuggestions))
randomSeeds = append(randomSeeds, pg.allSuggestions[random])
}
return randomSeeds
}
func (pg *backupPage) populateSuggestionSeeds() {
rand.Seed(time.Now().Unix())
for k := range pg.seedPhrase {
seeds := pg.randomSeeds()
s := pg.suggestions[k]
for i := range s.buttons {
s.buttons[i].Text = seeds[i]
}
s.buttons[rand.Intn(len(seeds))].Text = pg.seedPhrase[k]
}
}
func viewTexts(active int) viewText {
switch active {
case infoView:
return viewText{
title: "Keep in mind",
action: "View seed phrase",
}
case seedView:
return viewText{
title: "Write down seed phrase",
action: "I have written down all 33 words",
steps: fmt.Sprintf("Steps %d/2", seedView-1),
instruction: "Write down all 33 words in the correct order",
}
case verifyView:
return viewText{
title: "Verify seed phrase",
action: "Verify",
steps: fmt.Sprintf("Steps %d/2", verifyView-1),
instruction: "Select the correct words to verify",
}
case successView:
return viewText{
action: "Back to Wallets",
}
}
return viewText{}
}
func (pg *backupPage) updateViewTexts() {
t := viewTexts(pg.active)
pg.title.Text = t.title
pg.action.Text = t.action
pg.steps.Text = t.steps
pg.instruction.Text = t.instruction
}
func (pg *backupPage) clearError() {
time.AfterFunc(time.Second*3, func() {
pg.error = ""
})
}
func checkSlice(s []string) bool {
for _, v := range s {
if v == "-" {
return false
}
}
return true
}
func (pg *backupPage) resetPage(c pageCommon) {
*c.page = PageWallet
pg.active = infoView
pg.seedPhrase = []string{}
pg.selectedSeeds = make([]string, 33)
for _, cb := range pg.checkBoxes {
cb.CheckBox.Value = false
}
for i := range pg.suggestions {
pg.suggestions[i].selected = -1
}
for i := range pg.selectedSeeds {
pg.selectedSeeds[i] = "-"
}
pg.updateViewTexts()
}
func (pg *backupPage) handle(c pageCommon) {
if pg.backButton.Button.Clicked() {
pg.resetPage(c)
}
if pg.action.Button.Clicked() && pg.verifyCheckBoxes() {
switch pg.active {
case infoView:
s := pg.wal.GetWalletSeedPhrase(pg.info.Wallets[*c.selectedWallet].ID)
pg.seedPhrase = strings.Split(s, " ")
pg.populateSuggestionSeeds()
pg.active++
case verifyView:
if !checkSlice(pg.selectedSeeds) {
return
}
errMessage := "Failed to verify. Please go through every word and try again."
s := strings.Join(pg.selectedSeeds, " ")
if !dcrlibwallet.VerifySeed(s) {
pg.error = errMessage
pg.clearError()
return
}
err := pg.wal.VerifyWalletSeedPhrase(pg.info.Wallets[*c.selectedWallet].ID, s)
if err != nil {
pg.error = errMessage
pg.clearError()
return
}
pg.info.Wallets[*c.selectedWallet].Seed = ""
pg.active++
case successView:
pg.resetPage(c)
default:
pg.active++
}
pg.updateViewTexts()
}
for i := range pg.suggestions {
suggestion := pg.suggestions[i]
for s := range suggestion.buttons {
if suggestion.buttons[s].Button.Clicked() {
pg.suggestions[i].selected = s
pg.selectedSeeds[i] = suggestion.buttons[s].Text
}
}
}
}