-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwallet_info.go
192 lines (176 loc) · 4.26 KB
/
wallet_info.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
package main
import (
"encoding/hex"
"sort"
"fyne.io/fyne"
"fyne.io/fyne/dialog"
"github.com/hectorchu/gonano/rpc"
"github.com/hectorchu/gonano/util"
"github.com/hectorchu/gonano/wallet"
"github.com/tyler-smith/go-bip39"
)
type walletInfo struct {
w *wallet.Wallet
Label string
Seed, Salt string
IsBip39, IsLedger bool
Accounts map[string]*accountInfo
accountsList []*accountInfo
}
type accountInfo struct {
address string
Index uint32
balance, pending util.NanoAmount
}
func (wi *walletInfo) init(password string) (err error) {
if wi.w != nil {
return
}
if wi.IsLedger {
if err = wi.initLedger(); err != nil {
return
}
} else {
var seed []byte
if seed, err = wi.decryptSeed(password); err != nil {
return
}
if wi.IsBip39 {
if err = wi.initBip39(seed, password); err != nil {
return
}
} else {
if err = wi.initSeed(seed); err != nil {
return
}
}
}
return wi.initAccountsList()
}
func (wi *walletInfo) decryptSeed(password string) (seed []byte, err error) {
enc, err := hex.DecodeString(wi.Seed)
if err != nil {
return
}
salt, err := hex.DecodeString(wi.Salt)
if err != nil {
return
}
key, _, err := deriveKey(password, salt)
if err != nil {
return
}
return decrypt(enc, key)
}
func (wi *walletInfo) initSeed(seed []byte) (err error) {
wi.w, err = wallet.NewWallet(seed)
wi.w.RPC.URL = rpcURL
return
}
func (wi *walletInfo) initBip39(entropy []byte, password string) (err error) {
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return
}
wi.w, err = wallet.NewBip39Wallet(mnemonic, password)
wi.w.RPC.URL = rpcURL
return
}
func (wi *walletInfo) initLedger() (err error) {
wi.w, err = wallet.NewLedgerWallet()
wi.w.RPC.URL = rpcURL
return
}
func (wi *walletInfo) initAccounts(win fyne.Window) (err error) {
prog := dialog.NewProgressInfinite(wi.Label, "Scanning for accounts...", win)
prog.Show()
err = wi.w.ScanForAccounts()
prog.Hide()
if err != nil {
return
}
if wi.Accounts == nil {
wi.Accounts = make(map[string]*accountInfo)
}
for _, a := range wi.w.GetAccounts() {
wi.Accounts[a.Address()] = &accountInfo{
address: a.Address(),
Index: a.Index(),
}
}
return wi.initAccountsList()
}
func (wi *walletInfo) initAccountsList() (err error) {
wi.accountsList = make([]*accountInfo, 0, len(wi.Accounts))
for _, ai := range wi.Accounts {
wi.accountsList = append(wi.accountsList, ai)
}
sort.Slice(wi.accountsList, func(i, j int) bool {
return wi.accountsList[i].Index < wi.accountsList[j].Index
})
return wi.getBalances()
}
func (wi *walletInfo) indexOf(ai *accountInfo) int {
return sort.Search(len(wi.accountsList), func(i int) bool {
return wi.accountsList[i].Index >= ai.Index
})
}
func (wi *walletInfo) addAccount() (err error) {
var a *wallet.Account
for {
if a, err = wi.w.NewAccount(nil); err != nil {
return
}
if _, ok := wi.Accounts[a.Address()]; !ok {
break
}
}
ai := &accountInfo{
address: a.Address(),
Index: a.Index(),
}
wi.Accounts[a.Address()] = ai
i := wi.indexOf(ai)
if i == len(wi.accountsList) {
wi.accountsList = append(wi.accountsList, ai)
} else {
wi.accountsList = append(wi.accountsList[:i+1], wi.accountsList[i:]...)
wi.accountsList[i] = ai
}
wi.updateBalance(ai.address)
return
}
func (wi *walletInfo) removeAccount(ai *accountInfo) {
delete(wi.Accounts, ai.address)
i := wi.indexOf(ai)
wi.accountsList = append(wi.accountsList[:i], wi.accountsList[i+1:]...)
}
func (wi *walletInfo) getBalances() (err error) {
if len(wi.accountsList) == 0 {
return
}
accounts := make([]string, len(wi.accountsList))
for i, ai := range wi.accountsList {
accounts[i] = ai.address
}
rpcClient := rpc.Client{URL: rpcURL}
balances, err := rpcClient.AccountsBalances(accounts)
if err != nil {
return
}
for address, ab := range balances {
ai := wi.Accounts[address]
ai.balance.Raw, ai.pending.Raw = &ab.Balance.Int, &ab.Pending.Int
}
return
}
func (wi *walletInfo) updateBalance(account string) (updated bool) {
if ai, ok := wi.Accounts[account]; ok {
rpcClient := rpc.Client{URL: rpcURL}
if balance, pending, err := rpcClient.AccountBalance(account); err == nil {
ai.balance.Raw, ai.pending.Raw = &balance.Int, &pending.Int
updated = true
}
}
return
}