/
sign_message_page.go
275 lines (238 loc) · 7.38 KB
/
sign_message_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
package ui
import (
"fmt"
"github.com/raedahgroup/godcr/ui/values"
"gioui.org/layout"
"gioui.org/widget"
"github.com/raedahgroup/godcr/ui/decredmaterial"
"github.com/raedahgroup/godcr/wallet"
)
const PageSignMessage = "sign_message"
type signMessagePage struct {
container layout.List
wallet *wallet.Wallet
walletID int
errChannel chan error
isPasswordModalOpen, isSigningMessage bool
titleLabel, subtitleLabel, errorLabel, signedMessageLabel decredmaterial.Label
addressEditor, messageEditor decredmaterial.Editor
clearButton, signButton, copyButton decredmaterial.Button
passwordModal *decredmaterial.Password
result **wallet.Signature
backButton decredmaterial.IconButton
}
func (win *Window) SignMessagePage(common pageCommon) layout.Widget {
addressEditor := common.theme.Editor(new(widget.Editor), "Address")
addressEditor.IsVisible = true
addressEditor.IsRequired = true
addressEditor.Editor.SingleLine = true
messageEditor := common.theme.Editor(new(widget.Editor), "Message")
messageEditor.IsVisible = true
messageEditor.IsRequired = true
messageEditor.Editor.SingleLine = true
clearButton := common.theme.Button(new(widget.Clickable), "Clear all")
clearButton.Background = common.theme.Color.Background
clearButton.Color = common.theme.Color.Gray
errorLabel := common.theme.Caption("")
errorLabel.Color = common.theme.Color.Danger
pg := &signMessagePage{
container: layout.List{
Axis: layout.Vertical,
},
wallet: common.wallet,
passwordModal: common.theme.Password(),
titleLabel: common.theme.H5("Sign Message"),
subtitleLabel: common.theme.Body2("Enter the address and message you want to sign"),
signedMessageLabel: common.theme.H5(""),
errorLabel: errorLabel,
addressEditor: addressEditor,
messageEditor: messageEditor,
clearButton: clearButton,
signButton: common.theme.Button(new(widget.Clickable), "Sign"),
copyButton: common.theme.Button(new(widget.Clickable), "Copy"),
result: &win.signatureResult,
backButton: common.theme.PlainIconButton(new(widget.Clickable), common.icons.navigationArrowBack),
}
pg.backButton.Color = common.theme.Color.Hint
pg.backButton.Size = values.MarginPadding30
pg.backButton.Inset = layout.UniformInset(values.MarginPadding0)
return func(gtx C) D {
pg.handle(common)
pg.updateColors(common)
pg.validate(true)
return pg.Layout(gtx, common)
}
}
func (pg *signMessagePage) Layout(gtx layout.Context, common pageCommon) layout.Dimensions {
pg.walletID = common.info.Wallets[*common.selectedWallet].ID
pg.errChannel = common.errorChannels[PageSignMessage]
w := []func(gtx C) D{
func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.NW.Layout(gtx, func(gtx C) D {
return pg.backButton.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: values.MarginPadding20}.Layout(gtx, func(gtx C) D {
return pg.titleLabel.Layout(gtx)
})
}),
)
},
func(gtx C) D {
inset := layout.Inset{
Top: values.MarginPadding5,
Bottom: values.MarginPadding10,
}
return inset.Layout(gtx, func(gtx C) D {
return pg.subtitleLabel.Layout(gtx)
})
},
func(gtx C) D {
return pg.errorLabel.Layout(gtx)
},
func(gtx C) D {
return pg.addressEditor.Layout(gtx)
},
func(gtx C) D {
return pg.messageEditor.Layout(gtx)
},
func(gtx C) D {
return pg.drawButtonsRow(gtx)
},
func(gtx C) D {
return pg.drawResult(gtx)
},
}
body := common.Layout(gtx, func(gtx C) D {
return pg.container.Layout(gtx, len(w), func(gtx C, i int) D {
return layout.UniformInset(values.MarginPadding5).Layout(gtx, func(gtx C) D {
return w[i](gtx)
})
})
})
if pg.isPasswordModalOpen {
pg.walletID = common.info.Wallets[*common.selectedWallet].ID
return common.Modal(gtx, body, pg.passwordModal.Layout(gtx, pg.confirm, pg.cancel))
}
return body
}
func (pg *signMessagePage) drawButtonsRow(gtx layout.Context) layout.Dimensions {
return layout.E.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
inset := layout.Inset{
Right: values.MarginPadding5,
}
return inset.Layout(gtx, func(gtx C) D {
return pg.clearButton.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return pg.signButton.Layout(gtx)
}),
)
})
}
func (pg *signMessagePage) drawResult(gtx layout.Context) layout.Dimensions {
if pg.signedMessageLabel.Text == "" {
return layout.Dimensions{}
}
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return pg.signedMessageLabel.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return pg.copyButton.Layout(gtx)
}),
)
}
func (pg *signMessagePage) updateColors(common pageCommon) {
if pg.isSigningMessage || pg.addressEditor.Editor.Text() == "" || pg.messageEditor.Editor.Text() == "" {
pg.signButton.Background = common.theme.Color.Hint
} else {
pg.signButton.Background = common.theme.Color.Primary
}
}
func (pg *signMessagePage) handle(common pageCommon) {
for pg.clearButton.Button.Clicked() {
pg.clearForm()
}
for pg.signButton.Button.Clicked() {
if !pg.isSigningMessage && pg.validate(false) {
pg.isPasswordModalOpen = true
}
}
if pg.copyButton.Button.Clicked() {
go func() {
common.clipboard <- WriteClipboard{Text: pg.signedMessageLabel.Text}
}()
}
select {
case err := <-pg.errChannel:
fmt.Printf("SIGNMESSAGE PAGE ERROR! %v", err)
default:
}
if *pg.result != nil {
if (*pg.result).Err != nil {
pg.errorLabel.Text = (*pg.result).Err.Error()
} else {
pg.signedMessageLabel.Text = (*pg.result).Signature
}
*pg.result = nil
pg.isSigningMessage = false
pg.signButton.Text = "Sign"
}
if pg.backButton.Button.Clicked() {
pg.clearForm()
*common.page = PageWallet
}
}
func (pg *signMessagePage) confirm(password []byte) {
pg.isPasswordModalOpen = false
pg.isSigningMessage = true
pg.signButton.Text = "Signing..."
pg.wallet.SignMessage(pg.walletID, password, pg.addressEditor.Editor.Text(), pg.messageEditor.Editor.Text(), pg.errChannel)
}
func (pg *signMessagePage) cancel() {
pg.isPasswordModalOpen = false
}
func (pg *signMessagePage) validate(ignoreEmpty bool) bool {
isAddressValid := pg.validateAddress(ignoreEmpty)
isMessageValid := pg.validateMessage(ignoreEmpty)
if !isAddressValid || !isMessageValid {
return false
}
return true
}
func (pg *signMessagePage) validateAddress(ignoreEmpty bool) bool {
address := pg.addressEditor.Editor.Text()
pg.addressEditor.ErrorLabel.Text = ""
if address == "" && !ignoreEmpty {
pg.addressEditor.ErrorLabel.Text = "Please enter a valid address"
return false
}
if address != "" {
isValid, _ := pg.wallet.IsAddressValid(address)
if !isValid {
pg.addressEditor.ErrorLabel.Text = "Invalid address"
return false
}
}
return true
}
func (pg *signMessagePage) validateMessage(ignoreEmpty bool) bool {
message := pg.messageEditor.Editor.Text()
if message == "" && !ignoreEmpty {
pg.messageEditor.ErrorLabel.Text = "Please enter a message to sign"
return false
}
return true
}
func (pg *signMessagePage) clearForm() {
pg.addressEditor.Editor.SetText("")
pg.messageEditor.Editor.SetText("")
pg.errorLabel.Text = ""
}