-
Notifications
You must be signed in to change notification settings - Fork 15
/
walletsModel.go
348 lines (312 loc) · 10.1 KB
/
walletsModel.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
package models
import (
hardware "github.com/fibercrypto/fibercryptowallet/src/contrib/skywallet"
fccore "github.com/fibercrypto/fibercryptowallet/src/core"
wlcore "github.com/fibercrypto/fibercryptowallet/src/main"
"github.com/fibercrypto/fibercryptowallet/src/util/logging"
"github.com/fibercrypto/skywallet-go/src/integration/proxy"
skyWallet "github.com/fibercrypto/skywallet-go/src/skywallet"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/qml"
"time"
)
const (
Name = int(core.Qt__UserRole) + iota + 1
EncryptionEnabled
Sky
CoinHours
FileName
Expand
HasHardwareWallet
)
var logWalletsModel = logging.MustGetLogger("Wallets Model")
var dev skyWallet.Devicer
var hadHwConnected = false
var hwConnectedOn []int
type WalletModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*QWallet `property:"wallets"`
_ func(*QWallet) `slot:"addWallet"`
_ func(row int, name string, encryptionEnabled bool, sky string, coinHours string) `slot:"editWallet"`
_ func(row int) `slot:"removeWallet"`
_ func([]*QWallet) `slot:"loadModel"`
_ func([]*QWallet) `slot:"updateModel"`
_ func() `slot:"sniffHw"`
_ func(string) `slot:"changeExpanded"`
_ int `property:"count"`
receivChannel chan *updateWalletInfo
walletByName map[string]*QWallet
}
type QWallet struct {
core.QObject
_ string `property:"name"`
_ int `property:"encryptionEnabled"`
_ string `property:"sky"`
_ string `property:"coinHours"`
_ string `property:"fileName"`
_ bool `property:"expand"`
_ bool `property:"hasHardwareWallet"`
}
func (walletModel *WalletModel) init() {
logWalletsModel.Info("Initialize Wallet model")
dev = proxy.NewSequencer(skyWallet.NewDevice(skyWallet.DeviceTypeUSB), true, func() string {
return "not implemented"
})
walletModel.SetRoles(map[int]*core.QByteArray{
Name: core.NewQByteArray2("name", -1),
EncryptionEnabled: core.NewQByteArray2("encryptionEnabled", -1),
Sky: core.NewQByteArray2("sky", -1),
CoinHours: core.NewQByteArray2("coinHours", -1),
FileName: core.NewQByteArray2("fileName", -1),
Expand: core.NewQByteArray2("expand", -1),
HasHardwareWallet: core.NewQByteArray2("hasHardwareWallet", -1),
})
qml.QQmlEngine_SetObjectOwnership(walletModel, qml.QQmlEngine__CppOwnership)
walletModel.ConnectData(walletModel.data)
walletModel.ConnectSetData(walletModel.setData)
walletModel.ConnectRowCount(walletModel.rowCount)
walletModel.ConnectColumnCount(walletModel.columnCount)
walletModel.ConnectRoleNames(walletModel.roleNames)
walletModel.ConnectAddWallet(walletModel.addWallet)
walletModel.ConnectEditWallet(walletModel.editWallet)
walletModel.ConnectRemoveWallet(walletModel.removeWallet)
walletModel.ConnectLoadModel(walletModel.loadModel)
walletModel.ConnectUpdateModel(walletModel.updateModel)
walletModel.ConnectChangeExpanded(walletModel.changeExpanded)
walletModel.receivChannel = walletManager.suscribe()
walletModel.walletByName = make(map[string]*QWallet, 0)
go func() {
for {
wi := <-walletModel.receivChannel
if wi.isNew {
//walletModel.addWallet(wi.wallet)
} else {
encrypted := false
if wi.wallet.EncryptionEnabled() == 1 {
encrypted = true
}
walletModel.editWallet(wi.row, wi.wallet.Name(), encrypted, wi.wallet.Sky(), wi.wallet.CoinHours())
walletModel.ConnectSniffHw(walletModel.sniffHw)
}
}
}()
}
// attachHwAsSigner add a hw as signer
func attachHwAsSigner(wlt fccore.Wallet, dev skyWallet.Devicer) error {
hw := hardware.NewSkyWallet(wlt, dev)
am := wlcore.LoadAltcoinManager()
if err := am.AttachSignService(hw); err != nil {
logSignersModel.Errorln("error registering hardware wallet as signer")
return err
}
return nil
}
// sniffHw notify the model about available hardware wallet device if any
func (walletModel *WalletModel) sniffHw() {
checkForDerivationType := func(dt string) {
addr, err := hardware.HwFirstAddr(dev, dt)
if err == nil {
wlt, err := walletManager.WalletEnv.LookupWallet(addr)
if err != nil {
logSignersModel.Warnln("can not find a wallet matching the hardware one")
// FIXME handle this scenario with a wallet registration.
return
}
err = attachHwAsSigner(wlt, dev)
if err != nil {
logSignersModel.WithError(err).Errorln("unable to attach signer")
return
}
hadHwConnected = true
walletModel.updateWallet(wlt.GetId())
} else {
if hadHwConnected {
hadHwConnected = false
hwConnectedOn = []int{}
beginIndex := walletModel.Index(0, 0, core.NewQModelIndex())
endIndex := walletModel.Index(walletModel.rowCount(core.NewQModelIndex())-1, 0, core.NewQModelIndex())
walletModel.DataChanged(beginIndex, endIndex, []int{HasHardwareWallet})
logSignersModel.WithError(err).Info("connection to hardware wallet was lose")
}
}
}
go func() {
for {
hwConnectedOn = []int{}
checkForDerivationType(skyWallet.WalletTypeDeterministic)
//checkForDerivationType(skyWallet.WalletTypeBip44)
time.Sleep(time.Millisecond * 500)
}
}()
}
func (walletModel *WalletModel) updateWallet(fn string) {
index := &core.QModelIndex{}
for row := 0; row < walletModel.rowCount(core.NewQModelIndex()); row++ {
index = walletModel.Index(row, 0, core.NewQModelIndex())
fileName := walletModel.data(index, FileName)
if fileName.ToString() == fn {
hwConnectedOn = append(hwConnectedOn, row)
walletModel.DataChanged(index, index, []int{HasHardwareWallet})
break
}
}
}
func (walletModel *WalletModel) changeExpanded(id string) {
w := walletModel.walletByName[id]
w.SetExpand(!w.IsExpand())
}
func (walletModel *WalletModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(walletModel.Wallets()) {
return core.NewQVariant()
}
var w = walletModel.Wallets()[index.Row()]
switch role {
case Name:
{
return core.NewQVariant1(w.Name())
}
case EncryptionEnabled:
{
return core.NewQVariant1(w.EncryptionEnabled())
}
case Sky:
{
return core.NewQVariant1(w.Sky())
}
case CoinHours:
{
return core.NewQVariant1(w.CoinHours())
}
case FileName:
{
return core.NewQVariant1(w.FileName())
}
case HasHardwareWallet:
{
valInSlice := func() bool {
for idx := range hwConnectedOn {
if hwConnectedOn[idx] == index.Row() && hadHwConnected {
return true
}
}
return false
}
return core.NewQVariant1(valInSlice())
}
case Expand:
{
return core.NewQVariant1(w.IsExpand())
}
default:
{
return core.NewQVariant()
}
}
}
func (walletModel *WalletModel) setData(index *core.QModelIndex, value *core.QVariant, role int) bool {
if !index.IsValid() {
return false
}
if index.Row() >= len(walletModel.Wallets()) {
return false
}
var w = walletModel.Wallets()[index.Row()]
switch role {
case Name:
{
w.SetName(value.ToString())
}
case EncryptionEnabled:
{
w.SetEncryptionEnabled(value.ToInt(nil))
}
case Sky:
{
w.SetSky(value.ToString())
}
case CoinHours:
{
w.SetCoinHours(value.ToString())
}
case FileName:
{
w.SetFileName(value.ToString())
}
case Expand:
{
w.SetExpand(value.ToBool())
}
default:
{
return false
}
}
walletModel.DataChanged(index, index, []int{role})
return true
}
func (walletModel *WalletModel) rowCount(parent *core.QModelIndex) int {
return len(walletModel.Wallets())
}
func (walletModel *WalletModel) columnCount(parent *core.QModelIndex) int {
return 1
}
func (walletModel *WalletModel) roleNames() map[int]*core.QByteArray {
return walletModel.Roles()
}
func (walletModel *WalletModel) addWallet(w *QWallet) {
logWalletsModel.Info("Add Wallet")
if w.Pointer() == nil {
return
}
walletModel.walletByName[w.FileName()] = w
walletModel.BeginInsertRows(core.NewQModelIndex(), len(walletModel.Wallets()), len(walletModel.Wallets()))
qml.QQmlEngine_SetObjectOwnership(w, qml.QQmlEngine__CppOwnership)
walletModel.SetWallets(append(walletModel.Wallets(), w))
walletModel.SetCount(walletModel.Count() + 1)
walletModel.EndInsertRows()
}
func (walletModel *WalletModel) editWallet(row int, name string, encrypted bool, sky string, coinHours string) {
logWalletsModel.Info("Edit Wallet")
pIndex := walletModel.Index(0, 0, core.NewQModelIndex())
lIndex := walletModel.Index(len(walletModel.Wallets())-1, 0, core.NewQModelIndex())
w := walletModel.Wallets()[row]
w.SetName(name)
if encrypted {
w.SetEncryptionEnabled(1)
} else {
w.SetEncryptionEnabled(0)
}
w.SetSky(sky)
w.SetCoinHours(coinHours)
walletModel.DataChanged(pIndex, lIndex, []int{Name, EncryptionEnabled, Sky, CoinHours})
}
func (walletModel *WalletModel) removeWallet(row int) {
logWalletsModel.Info("Remove wallets for index")
walletModel.BeginRemoveRows(core.NewQModelIndex(), row, row)
walletModel.SetWallets(append(walletModel.Wallets()[:row], walletModel.Wallets()[row+1:]...))
walletModel.SetCount(walletModel.Count() - 1)
walletModel.EndRemoveRows()
}
func (walletModel *WalletModel) updateModel(wallets []*QWallet) {
for i, wlt := range wallets {
walletModel.editWallet(i, wlt.Name(), wlt.EncryptionEnabled() == 1, wlt.Sky(), wlt.CoinHours())
}
}
func (walletModel *WalletModel) loadModel(wallets []*QWallet) {
logWalletsModel.Info("Loading wallets")
for _, wlt := range wallets {
qml.QQmlEngine_SetObjectOwnership(wlt, qml.QQmlEngine__CppOwnership)
}
for _, w := range wallets {
walletModel.walletByName[w.FileName()] = w
}
walletModel.BeginResetModel()
walletModel.SetWallets(wallets)
walletModel.EndResetModel()
walletModel.SetCount(len(walletModel.Wallets()))
}