-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
324 lines (290 loc) · 7.82 KB
/
state.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
package gui
import (
"crypto/cipher"
"encoding/json"
"io/ioutil"
"time"
"github.com/cybriq/p9/pkg/amt"
"github.com/cybriq/p9/pkg/btcaddr"
"github.com/cybriq/p9/pkg/chaincfg"
uberatomic "go.uber.org/atomic"
l "github.com/cybriq/p9/pkg/gel/gio/layout"
"github.com/cybriq/p9/pkg/btcjson"
"github.com/cybriq/p9/pkg/chainhash"
"github.com/cybriq/p9/pkg/gcm"
"github.com/cybriq/p9/pkg/transport"
"github.com/cybriq/p9/pkg/util/atom"
)
const ZeroAddress = "1111111111111111111114oLvT2"
// CategoryFilter marks which transactions to omit from the filtered transaction list
type CategoryFilter struct {
Send bool
Generate bool
Immature bool
Receive bool
Unknown bool
}
func (c *CategoryFilter) Filter(s string) (include bool) {
include = true
if c.Send && s == "send" {
include = false
}
if c.Generate && s == "generate" {
include = false
}
if c.Immature && s == "immature" {
include = false
}
if c.Receive && s == "receive" {
include = false
}
if c.Unknown && s == "unknown" {
include = false
}
return
}
type AddressEntry struct {
Address string `json:"address"`
Message string `json:"message,omitempty"`
Label string `json:"label,omitempty"`
Amount amt.Amount `json:"amount"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
TxID string `json:txid,omitempty'`
}
type State struct {
lastUpdated *atom.Time
bestBlockHeight *atom.Int32
bestBlockHash *atom.Hash
balance *atom.Float64
balanceUnconfirmed *atom.Float64
goroutines []l.Widget
allTxs *atom.ListTransactionsResult
filteredTxs *atom.ListTransactionsResult
filter CategoryFilter
filterChanged *atom.Bool
currentReceivingAddress *atom.Address
isAddress *atom.Bool
activePage *uberatomic.String
sendAddresses []AddressEntry
receiveAddresses []AddressEntry
}
func GetNewState(params *chaincfg.Params, activePage *uberatomic.String,
) *State {
fc := &atom.Bool{
Bool: uberatomic.NewBool(false),
}
return &State{
lastUpdated: atom.NewTime(time.Now()),
bestBlockHeight: &atom.Int32{Int32: uberatomic.NewInt32(0)},
bestBlockHash: atom.NewHash(chainhash.Hash{}),
balance: &atom.Float64{Float64: uberatomic.NewFloat64(0)},
balanceUnconfirmed: &atom.Float64{
Float64: uberatomic.NewFloat64(0),
},
goroutines: nil,
allTxs: atom.NewListTransactionsResult(
[]btcjson.ListTransactionsResult{},
),
filteredTxs: atom.NewListTransactionsResult(
[]btcjson.ListTransactionsResult{},
),
filter: CategoryFilter{},
filterChanged: fc,
currentReceivingAddress: atom.NewAddress(
&btcaddr.PubKeyHash{},
params,
),
isAddress: &atom.Bool{Bool: uberatomic.NewBool(false)},
activePage: activePage,
}
}
func (s *State) BumpLastUpdated() {
s.lastUpdated.Store(time.Now())
}
func (s *State) SetReceivingAddress(addr btcaddr.Address) {
s.currentReceivingAddress.Store(addr)
}
func (s *State) IsReceivingAddress() bool {
addr := s.currentReceivingAddress.String.Load()
if addr == ZeroAddress || addr == "" {
s.isAddress.Store(false)
} else {
s.isAddress.Store(true)
}
return s.isAddress.Load()
}
// Save the state to the specified file
func (s *State) Save(filename string, pass []byte, debug bool) (e error) {
D.Ln("saving state...")
marshalled := s.Marshal()
var j []byte
if j, e = json.MarshalIndent(marshalled, "", " "); E.Chk(e) {
return
}
// D.Ln(string(j))
var ciph cipher.AEAD
if ciph, e = gcm.GetCipher(pass); E.Chk(e) {
return
}
var nonce []byte
if nonce, e = transport.GetNonce(ciph); E.Chk(e) {
return
}
crypted := append(nonce, ciph.Seal(nil, nonce, j, nil)...)
var b []byte
_ = b
if b, e = ciph.Open(nil, nonce, crypted[len(nonce):], nil); E.Chk(e) {
// since it was just created it should not fail to decrypt
panic(e)
// interrupt.Request()
return
}
if e = ioutil.WriteFile(filename, crypted, 0600); E.Chk(e) {
}
if debug {
if e = ioutil.WriteFile(filename+".clear", j, 0600); E.Chk(e) {
}
}
return
}
// Load in the configuration from the specified file and decrypt using the given password
func (s *State) Load(filename string, pass []byte) (e error) {
D.Ln("loading state...")
var data []byte
var ciph cipher.AEAD
if data, e = ioutil.ReadFile(filename); E.Chk(e) {
return
}
D.Ln("cipher:", string(pass))
if ciph, e = gcm.GetCipher(pass); E.Chk(e) {
return
}
ns := ciph.NonceSize()
D.Ln("nonce size:", ns)
nonce := data[:ns]
data = data[ns:]
var b []byte
if b, e = ciph.Open(nil, nonce, data, nil); E.Chk(e) {
// interrupt.Request()
return
}
// yay, right password, now unmarshal
ss := &Marshalled{}
if e = json.Unmarshal(b, ss); E.Chk(e) {
return
}
// D.Ln(string(b))
ss.Unmarshal(s)
return
}
type Marshalled struct {
LastUpdated time.Time
BestBlockHeight int32
BestBlockHash chainhash.Hash
Balance float64
BalanceUnconfirmed float64
AllTxs []btcjson.ListTransactionsResult
Filter CategoryFilter
ReceivingAddress string
ActivePage string
ReceiveAddressBook []AddressEntry
SendAddressBook []AddressEntry
}
func (s *State) Marshal() (out *Marshalled) {
out = &Marshalled{
LastUpdated: s.lastUpdated.Load(),
BestBlockHeight: s.bestBlockHeight.Load(),
BestBlockHash: s.bestBlockHash.Load(),
Balance: s.balance.Load(),
BalanceUnconfirmed: s.balanceUnconfirmed.Load(),
AllTxs: s.allTxs.Load(),
Filter: s.filter,
ReceivingAddress: s.currentReceivingAddress.Load().EncodeAddress(),
ActivePage: s.activePage.Load(),
ReceiveAddressBook: s.receiveAddresses,
SendAddressBook: s.sendAddresses,
}
return
}
func (m *Marshalled) Unmarshal(s *State) {
s.lastUpdated.Store(m.LastUpdated)
s.bestBlockHeight.Store(m.BestBlockHeight)
s.bestBlockHash.Store(m.BestBlockHash)
s.balance.Store(m.Balance)
s.balanceUnconfirmed.Store(m.BalanceUnconfirmed)
if len(s.allTxs.Load()) < len(m.AllTxs) {
s.allTxs.Store(m.AllTxs)
}
s.receiveAddresses = m.ReceiveAddressBook
s.sendAddresses = m.SendAddressBook
s.filter = m.Filter
if m.ReceivingAddress != "1111111111111111111114oLvT2" {
var e error
var ra btcaddr.Address
if ra, e = btcaddr.Decode(m.ReceivingAddress,
s.currentReceivingAddress.ForNet,
); E.Chk(e) {
}
s.currentReceivingAddress.Store(ra)
}
s.SetActivePage(m.ActivePage)
return
}
func (s *State) Goroutines() []l.Widget {
return s.goroutines
}
func (s *State) SetGoroutines(gr []l.Widget) {
s.goroutines = gr
}
func (s *State) SetAllTxs(atxs []btcjson.ListTransactionsResult) {
s.allTxs.Store(atxs)
// generate filtered state
filteredTxs := make([]btcjson.ListTransactionsResult, 0,
len(s.allTxs.Load()),
)
for i := range atxs {
if s.filter.Filter(atxs[i].Category) {
filteredTxs = append(filteredTxs, atxs[i])
}
}
s.filteredTxs.Store(filteredTxs)
}
func (s *State) LastUpdated() time.Time {
return s.lastUpdated.Load()
}
func (s *State) BestBlockHeight() int32 {
return s.bestBlockHeight.Load()
}
func (s *State) BestBlockHash() *chainhash.Hash {
o := s.bestBlockHash.Load()
return &o
}
func (s *State) Balance() float64 {
return s.balance.Load()
}
func (s *State) BalanceUnconfirmed() float64 {
return s.balanceUnconfirmed.Load()
}
func (s *State) ActivePage() string {
return s.activePage.Load()
}
func (s *State) SetActivePage(page string) {
s.activePage.Store(page)
}
func (s *State) SetBestBlockHeight(height int32) {
s.BumpLastUpdated()
s.bestBlockHeight.Store(height)
}
func (s *State) SetBestBlockHash(h *chainhash.Hash) {
s.BumpLastUpdated()
s.bestBlockHash.Store(*h)
}
func (s *State) SetBalance(total float64) {
s.BumpLastUpdated()
s.balance.Store(total)
}
func (s *State) SetBalanceUnconfirmed(unconfirmed float64) {
s.BumpLastUpdated()
s.balanceUnconfirmed.Store(unconfirmed)
}