/
offline.go
458 lines (422 loc) · 13.2 KB
/
offline.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package wallet
import (
"bytes"
"errors"
"math"
"go.sia.tech/siad/crypto"
"go.sia.tech/siad/modules"
"go.sia.tech/siad/types"
)
// UnspentOutputs returns the unspent outputs tracked by the wallet.
func (w *Wallet) UnspentOutputs() ([]modules.UnspentOutput, error) {
if err := w.tg.Add(); err != nil {
return nil, err
}
defer w.tg.Done()
w.mu.Lock()
defer w.mu.Unlock()
// ensure durability of reported outputs
if err := w.syncDB(); err != nil {
return nil, err
}
// build initial list of confirmed outputs
var outputs []modules.UnspentOutput
dbForEachSiacoinOutput(w.dbTx, func(scoid types.SiacoinOutputID, sco types.SiacoinOutput) {
outputs = append(outputs, modules.UnspentOutput{
FundType: types.SpecifierSiacoinOutput,
ID: types.OutputID(scoid),
UnlockHash: sco.UnlockHash,
Value: sco.Value,
})
})
dbForEachSiafundOutput(w.dbTx, func(sfoid types.SiafundOutputID, sfo types.SiafundOutput) {
outputs = append(outputs, modules.UnspentOutput{
FundType: types.SpecifierSiafundOutput,
ID: types.OutputID(sfoid),
UnlockHash: sfo.UnlockHash,
Value: sfo.Value,
})
})
// don't include outputs marked as spent in pending transactions
pending := make(map[types.OutputID]struct{})
for _, pt := range w.unconfirmedProcessedTransactions {
for _, input := range pt.Inputs {
if input.WalletAddress {
pending[input.ParentID] = struct{}{}
}
}
}
filtered := outputs[:0]
for _, o := range outputs {
if _, ok := pending[o.ID]; !ok {
filtered = append(filtered, o)
}
}
outputs = filtered
// set the confirmation height for each output
outer:
for i, o := range outputs {
txnIndices, err := dbGetAddrTransactions(w.dbTx, o.UnlockHash)
if err != nil {
return nil, err
}
for _, j := range txnIndices {
pt, err := dbGetProcessedTransaction(w.dbTx, j)
if err != nil {
return nil, err
}
for _, sco := range pt.Outputs {
if sco.ID == o.ID {
outputs[i].ConfirmationHeight = pt.ConfirmationHeight
continue outer
}
}
}
}
// add unconfirmed outputs, except those that are spent in pending
// transactions
for _, pt := range w.unconfirmedProcessedTransactions {
for _, o := range pt.Outputs {
if _, ok := pending[o.ID]; !ok && o.WalletAddress {
outputs = append(outputs, modules.UnspentOutput{
FundType: types.SpecifierSiacoinOutput,
ID: o.ID,
UnlockHash: o.RelatedAddress,
Value: o.Value,
ConfirmationHeight: types.BlockHeight(math.MaxUint64), // unconfirmed
})
}
}
}
// mark the watch-only outputs
for i, o := range outputs {
_, ok := w.watchedAddrs[o.UnlockHash]
outputs[i].IsWatchOnly = ok
}
return outputs, nil
}
// UnlockConditions returns the UnlockConditions for the specified address, if
// they are known to the wallet.
func (w *Wallet) UnlockConditions(addr types.UnlockHash) (uc types.UnlockConditions, err error) {
if err := w.tg.Add(); err != nil {
return types.UnlockConditions{}, err
}
defer w.tg.Done()
w.mu.RLock()
defer w.mu.RUnlock()
if !w.unlocked {
return types.UnlockConditions{}, modules.ErrLockedWallet
}
if sk, ok := w.keys[addr]; ok {
uc = sk.UnlockConditions
} else {
// not in memory; try database
uc, err = dbGetUnlockConditions(w.dbTx, addr)
if err != nil {
return types.UnlockConditions{}, errors.New("no record of UnlockConditions for that UnlockHash")
}
}
// make a copy of the public key slice; otherwise the caller can modify it
uc.PublicKeys = append([]types.SiaPublicKey(nil), uc.PublicKeys...)
return uc, nil
}
// AddUnlockConditions adds a set of UnlockConditions to the wallet database.
func (w *Wallet) AddUnlockConditions(uc types.UnlockConditions) error {
if err := w.tg.Add(); err != nil {
return err
}
defer w.tg.Done()
w.mu.RLock()
defer w.mu.RUnlock()
if !w.unlocked {
return modules.ErrLockedWallet
}
return dbPutUnlockConditions(w.dbTx, uc)
}
// SignTransaction signs txn using secret keys known to the wallet. The
// transaction should be complete with the exception of the Signature fields
// of each TransactionSignature referenced by toSign. For convenience, if
// toSign is empty, SignTransaction signs everything that it can.
func (w *Wallet) SignTransaction(txn *types.Transaction, toSign []crypto.Hash) error {
if err := w.tg.Add(); err != nil {
return err
}
defer w.tg.Done()
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
return modules.ErrLockedWallet
}
consensusHeight, err := dbGetConsensusHeight(w.dbTx)
if err != nil {
return err
}
// if toSign is empty, sign all inputs that we have keys for
if len(toSign) == 0 {
for _, sci := range txn.SiacoinInputs {
if _, ok := w.keys[sci.UnlockConditions.UnlockHash()]; ok {
toSign = append(toSign, crypto.Hash(sci.ParentID))
}
}
for _, sfi := range txn.SiafundInputs {
if _, ok := w.keys[sfi.UnlockConditions.UnlockHash()]; ok {
toSign = append(toSign, crypto.Hash(sfi.ParentID))
}
}
}
return signTransaction(txn, w.keys, toSign, consensusHeight)
}
// SignTransaction signs txn using secret keys derived from seed. The
// transaction should be complete with the exception of the Signature fields
// of each TransactionSignature referenced by toSign, which must not be empty.
//
// SignTransaction must derive all of the keys from scratch, so it is
// appreciably slower than calling the Wallet.SignTransaction method. Only the
// first 1 million keys are derived.
func SignTransaction(txn *types.Transaction, seed modules.Seed, toSign []crypto.Hash, height types.BlockHeight) error {
if len(toSign) == 0 {
// unlike the wallet method, we can't simply "sign all inputs we have
// keys for," because without generating all of the keys up front, we
// don't know how many inputs we actually have keys for.
return errors.New("toSign cannot be empty")
}
// generate keys in batches up to 1e6 before giving up
keys := make(map[types.UnlockHash]spendableKey, 1e6)
var keyIndex uint64
const keysPerBatch = 1000
for len(keys) < 1e6 {
for _, sk := range generateKeys(seed, keyIndex, keyIndex+keysPerBatch) {
keys[sk.UnlockConditions.UnlockHash()] = sk
}
keyIndex += keysPerBatch
if err := signTransaction(txn, keys, toSign, height); err == nil {
return nil
}
}
return signTransaction(txn, keys, toSign, height)
}
// signTransaction signs the specified inputs of txn using the specified keys.
// It returns an error if any of the specified inputs cannot be signed.
func signTransaction(txn *types.Transaction, keys map[types.UnlockHash]spendableKey, toSign []crypto.Hash, height types.BlockHeight) error {
// helper function to lookup unlock conditions in the txn associated with
// a transaction signature's ParentID
findUnlockConditions := func(id crypto.Hash) (types.UnlockConditions, bool) {
for _, sci := range txn.SiacoinInputs {
if crypto.Hash(sci.ParentID) == id {
return sci.UnlockConditions, true
}
}
for _, sfi := range txn.SiafundInputs {
if crypto.Hash(sfi.ParentID) == id {
return sfi.UnlockConditions, true
}
}
return types.UnlockConditions{}, false
}
// helper function to lookup the secret key that can sign
findSigningKey := func(uc types.UnlockConditions, pubkeyIndex uint64) (crypto.SecretKey, bool) {
if pubkeyIndex >= uint64(len(uc.PublicKeys)) {
return crypto.SecretKey{}, false
}
pk := uc.PublicKeys[pubkeyIndex]
sk, ok := keys[uc.UnlockHash()]
if !ok {
return crypto.SecretKey{}, false
}
for _, key := range sk.SecretKeys {
pubKey := key.PublicKey()
if bytes.Equal(pk.Key, pubKey[:]) {
return key, true
}
}
return crypto.SecretKey{}, false
}
for _, id := range toSign {
// find associated txn signature
sigIndex := -1
for i, sig := range txn.TransactionSignatures {
if sig.ParentID == id {
sigIndex = i
break
}
}
if sigIndex == -1 {
return errors.New("toSign references signatures not present in transaction")
}
// find associated input
uc, ok := findUnlockConditions(id)
if !ok {
return errors.New("toSign references IDs not present in transaction")
}
// lookup the signing key
sk, ok := findSigningKey(uc, txn.TransactionSignatures[sigIndex].PublicKeyIndex)
if !ok {
return errors.New("could not locate signing key for " + id.String())
}
// add signature
//
// NOTE: it's possible that the Signature field will already be filled
// out. Although we could save a bit of work by not signing it, in
// practice it's probably best to overwrite any existing signatures,
// since we know that ours will be valid.
sigHash := txn.SigHash(sigIndex, height)
encodedSig := crypto.SignHash(sigHash, sk)
txn.TransactionSignatures[sigIndex].Signature = encodedSig[:]
}
return nil
}
// AddWatchAddresses instructs the wallet to begin tracking a set of
// addresses, in addition to the addresses it was previously tracking. If none
// of the addresses have appeared in the blockchain, the unused flag may be
// set to true. Otherwise, the wallet must rescan the blockchain to search for
// transactions containing the addresses.
func (w *Wallet) AddWatchAddresses(addrs []types.UnlockHash, unused bool) error {
if err := w.tg.Add(); err != nil {
return modules.ErrWalletShutdown
}
defer w.tg.Done()
err := func() error {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
return modules.ErrLockedWallet
}
// update in-memory map
for _, addr := range addrs {
w.watchedAddrs[addr] = struct{}{}
}
// update db
alladdrs := make([]types.UnlockHash, 0, len(w.watchedAddrs))
for addr := range w.watchedAddrs {
alladdrs = append(alladdrs, addr)
}
if err := dbPutWatchedAddresses(w.dbTx, alladdrs); err != nil {
return err
}
if !unused {
// prepare to rescan
if err := w.dbTx.DeleteBucket(bucketProcessedTransactions); err != nil {
return err
}
if _, err := w.dbTx.CreateBucket(bucketProcessedTransactions); err != nil {
return err
}
w.unconfirmedProcessedTransactions = nil
if err := dbPutConsensusChangeID(w.dbTx, modules.ConsensusChangeBeginning); err != nil {
return err
}
if err := dbPutConsensusHeight(w.dbTx, 0); err != nil {
return err
}
}
return w.syncDB()
}()
if err != nil {
return err
}
if !unused {
// rescan the blockchain
w.cs.Unsubscribe(w)
w.tpool.Unsubscribe(w)
done := make(chan struct{})
go w.rescanMessage(done)
defer close(done)
if err := w.cs.ConsensusSetSubscribe(w, modules.ConsensusChangeBeginning, w.tg.StopChan()); err != nil {
return err
}
w.tpool.TransactionPoolSubscribe(w)
}
return nil
}
// RemoveWatchAddresses instructs the wallet to stop tracking a set of
// addresses and delete their associated transactions. If none of the
// addresses have appeared in the blockchain, the unused flag may be set to
// true. Otherwise, the wallet must rescan the blockchain to rebuild its
// transaction history.
func (w *Wallet) RemoveWatchAddresses(addrs []types.UnlockHash, unused bool) error {
if err := w.tg.Add(); err != nil {
return modules.ErrWalletShutdown
}
defer w.tg.Done()
err := func() error {
w.mu.Lock()
defer w.mu.Unlock()
if !w.unlocked {
return modules.ErrLockedWallet
}
// update in-memory map
for _, addr := range addrs {
delete(w.watchedAddrs, addr)
}
// update db
alladdrs := make([]types.UnlockHash, 0, len(w.watchedAddrs))
for addr := range w.watchedAddrs {
alladdrs = append(alladdrs, addr)
}
if err := dbPutWatchedAddresses(w.dbTx, alladdrs); err != nil {
return err
}
if !unused {
// outputs associated with the addresses may be present in the
// SiacoinOutputs bucket. Iterate through the bucket and remove
// any outputs that we are no longer watching.
var outputIDs []types.SiacoinOutputID
dbForEachSiacoinOutput(w.dbTx, func(scoid types.SiacoinOutputID, sco types.SiacoinOutput) {
if !w.isWalletAddress(sco.UnlockHash) {
outputIDs = append(outputIDs, scoid)
}
})
for _, scoid := range outputIDs {
if err := dbDeleteSiacoinOutput(w.dbTx, scoid); err != nil {
return err
}
}
// prepare to rescan
if err := w.dbTx.DeleteBucket(bucketProcessedTransactions); err != nil {
return err
}
if _, err := w.dbTx.CreateBucket(bucketProcessedTransactions); err != nil {
return err
}
w.unconfirmedProcessedTransactions = nil
if err := dbPutConsensusChangeID(w.dbTx, modules.ConsensusChangeBeginning); err != nil {
return err
}
if err := dbPutConsensusHeight(w.dbTx, 0); err != nil {
return err
}
}
return w.syncDB()
}()
if err != nil {
return err
}
if !unused {
// rescan the blockchain
w.cs.Unsubscribe(w)
w.tpool.Unsubscribe(w)
done := make(chan struct{})
go w.rescanMessage(done)
defer close(done)
if err := w.cs.ConsensusSetSubscribe(w, modules.ConsensusChangeBeginning, w.tg.StopChan()); err != nil {
return err
}
w.tpool.TransactionPoolSubscribe(w)
}
return nil
}
// WatchAddresses returns the set of addresses that the wallet is currently
// watching.
func (w *Wallet) WatchAddresses() ([]types.UnlockHash, error) {
if err := w.tg.Add(); err != nil {
return nil, modules.ErrWalletShutdown
}
defer w.tg.Done()
w.mu.RLock()
defer w.mu.RUnlock()
addrs := make([]types.UnlockHash, 0, len(w.watchedAddrs))
for addr := range w.watchedAddrs {
addrs = append(addrs, addr)
}
return addrs, nil
}