forked from secretnamebasis/simple-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
246 lines (200 loc) · 5.53 KB
/
create.go
File metadata and controls
246 lines (200 loc) · 5.53 KB
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
package main
import (
"runtime"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/deroproject/derohe/cryptography/crypto"
"github.com/deroproject/derohe/transaction"
"github.com/deroproject/derohe/walletapi"
)
func create() {
program.dialogues.login.Dismiss()
pass := widget.NewPasswordEntry()
if program.entries.wallet.Text != "" || pass.Text != "" {
program.entries.wallet.SetText("")
}
pass.SetText(randomWords(4, "-"))
content := container.NewVBox(
layout.NewSpacer(),
program.entries.wallet,
pass,
program.hyperlinks.save,
layout.NewSpacer(),
)
new_account := dialog.NewCustom("Create Wallet", dismiss,
content, program.window)
// if they press enter, it is as if they pressed save
pass.OnSubmitted = func(s string) {
new_account.Dismiss()
create_account(s)
// dump entries
program.entries.wallet.SetText("")
pass.SetText("")
}
program.hyperlinks.save.Alignment = fyne.TextAlignCenter
program.hyperlinks.save.OnTapped = func() {
create_account(pass.Text)
// dump entries
program.entries.wallet.SetText("")
pass.SetText("")
new_account.Dismiss()
}
new_account.Resize(fyne.NewSize(program.size.Width/3, program.size.Height/3))
new_account.SetOnClosed(func() {
// don't want to write over every password field
pass.SetText("")
})
new_account.Show()
}
func create_account(password string) {
var err error
// get entries
filename := program.entries.wallet.Text
program.wallet, err = walletapi.Create_Encrypted_Wallet(
filename,
password,
crypto.RandomScalarBNRed(),
)
if err != nil {
showError(err, program.window)
} else {
// now save the wallet
if err := program.wallet.Save_Wallet(); err != nil {
// if that doesn't work...
showError(err, program.window)
return
} else { // follow logged in workflow
loggedIn()
updateHeader(program.hyperlinks.home)
setContentAsHome()
}
}
}
func register() *fyne.Container {
// let's make a registration button
icon := theme.UploadIcon()
program.buttons.register = widget.NewButtonWithIcon("REGISTER", icon, nil)
// here is what happens when we push the register button...
program.buttons.register.OnTapped = registration
// and here is the simple registration container
return container.NewVBox(
program.activities.registration,
program.buttons.register,
program.labels.counter,
)
}
func registration() {
// we are going to do this as a go routine
go reg() // so the gui doesn't lock up
}
func reg() {
// while in the go routine, update the widget accordingly
fyne.DoAndWait(func() {
program.buttons.register.Hide()
program.labels.counter.Show()
program.activities.registration.Start()
program.activities.registration.Show()
})
// we are going to set some expectations
// there is a registration transaction
var reg_tx = new(transaction.Transaction)
// there is a channel of success
var success = make(chan *transaction.Transaction)
// we are going to track wins and fails
var wins, fails uint64
// reserve 2 threads for os, app management
var reserved int = 2
// we are going to use almost all threads
max_threads := runtime.GOMAXPROCS(0)
desired_threads := max_threads - reserved
// we estimate that roughly 21M hashes have to be attempted,
// it is usually less... like 7-14M
estimate := int64(21000000)
play := func() { /*
for as long as we haven't won,
we will persist
*/
var hash crypto.Hash
// can't do this in the app...
// rendering too slow...
// show a speed gauge
for wins == 0 {
// if the wallet isn't present or is registered... stop
if program.wallet == nil ||
program.wallet.IsRegistered() ||
!program.activities.registration.Visible() {
break
}
// start by making an attempt
attempt := program.wallet.GetRegistrationTX()
// get the hash
hash = attempt.GetHash()
// you win if the first 3 bytes of the hash are 0
winner := hash[0] == 0 && hash[1] == 0 && hash[2] == 0
if winner {
// pass the attempt down the success channel
success <- attempt
// record a win
wins++
// team break
break
} else {
// decrement the estimate by 1
estimate--
// increment the fails by 1
fails++
}
if fails%atomic_units == 0 || wins > 0 {
logger.Info("registration",
"Threads:",
strconv.Itoa(desired_threads),
"HASH:",
truncator(hash.String()),
"ESTIMATED:",
strconv.Itoa(int(estimate)),
"COUNTER:",
strconv.Itoa(int(fails)),
"Success:",
strconv.Itoa(int(wins)),
)
}
}
}
// for each thread
for range desired_threads {
go play()
}
// seeing as we have a successful attempt coming down the channel
reg_tx = <-success
// update the widgets in the go routine
fyne.DoAndWait(func() {
program.activities.registration.Stop()
program.activities.registration.Hide()
// change the counter
program.labels.counter.SetText(
"Sending Registration Tx: " + truncator(reg_tx.GetHash().String()),
)
})
// ship the registration transaction over the network
if err := program.wallet.SendTransaction(reg_tx); err != nil {
// if an error, show it
showError(err, program.window)
return
} else {
// if successful, shout for joy!
showInfo("Registration", "registration successful", program.window)
// update the display in the go routine
fyne.DoAndWait(func() {
program.containers.send.Show()
program.buttons.assets.Enable()
program.buttons.transactions.Enable()
program.containers.register.Hide()
})
return
}
}