forked from decred/dcrwallet
/
upgrades.go
352 lines (314 loc) · 12.1 KB
/
upgrades.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
// Copyright (c) 2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package udb
import (
"crypto/sha256"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrutil/hdkeychain"
"github.com/decred/dcrwallet/apperrors"
"github.com/decred/dcrwallet/snacl"
"github.com/decred/dcrwallet/walletdb"
)
// Note: all manager functions always use the latest version of the database.
// Therefore it is extremely important when adding database upgrade code to
// never call any methods of the managers and instead only use the db primitives
// with the correct version passed as parameters.
const (
initialVersion = 1
// lastUsedAddressIndexVersion is the second version of the database. It
// adds indexes for the last used address of BIP0044 accounts, removes the
// next to use address indexes, removes all references to address pools, and
// removes all per-address usage tracking.
//
// See lastUsedAddressIndexUpgrade for the code that implements the upgrade
// path.
lastUsedAddressIndexVersion = 2
// votingPreferencesVersion is the third version of the database. It
// removes all per-ticket vote bits, replacing them with vote preferences
// for choices on individual agendas from the current stake version.
votingPreferencesVersion = 3
// noEncryptedSeedVersion is the fourth version of the database. It removes
// the encrypted seed that earlier versions may have saved in the database
// (or more commonly, encrypted zeros on mainnet wallets).
noEncryptedSeedVersion = 4
// DBVersion is the latest version of the database that is understood by the
// program. Databases with recorded versions higher than this will fail to
// open (meaning any upgrades prevent reverting to older software).
DBVersion = noEncryptedSeedVersion
)
// upgrades maps between old database versions and the upgrade function to
// upgrade the database to the next version.
var upgrades = [...]func(walletdb.ReadWriteTx, []byte) error{
initialVersion: lastUsedAddressIndexUpgrade,
lastUsedAddressIndexVersion: votingPreferencesUpgrade,
votingPreferencesVersion: noEncryptedSeedUpgrade,
}
func lastUsedAddressIndexUpgrade(tx walletdb.ReadWriteTx, publicPassphrase []byte) error {
const oldVersion = 1
const newVersion = 2
metadataBucket := tx.ReadWriteBucket(unifiedDBMetadata{}.rootBucketKey())
addrmgrBucket := tx.ReadWriteBucket(waddrmgrBucketKey)
addressBucket := addrmgrBucket.NestedReadBucket(addrBucketName)
usedAddrBucket := addrmgrBucket.NestedReadBucket(usedAddrBucketName)
addressKey := func(hash160 []byte) []byte {
sha := sha256.Sum256(hash160)
return sha[:]
}
// Assert that this function is only called on version 1 databases.
dbVersion, err := unifiedDBMetadata{}.getVersion(metadataBucket)
if err != nil {
return err
}
if dbVersion != oldVersion {
const str = "lastUsedAddressIndexUpgrade inappropriately called"
return apperrors.E{ErrorCode: apperrors.ErrUpgrade, Description: str, Err: nil}
}
masterKeyPubParams, _, err := fetchMasterKeyParams(addrmgrBucket)
if err != nil {
return err
}
var masterKeyPub snacl.SecretKey
err = masterKeyPub.Unmarshal(masterKeyPubParams)
if err != nil {
const str = "failed to unmarshal master public key parameters"
return apperrors.E{ErrorCode: apperrors.ErrData, Description: str, Err: err}
}
err = masterKeyPub.DeriveKey(&publicPassphrase)
if err != nil {
str := "invalid passphrase for master public key"
return apperrors.E{ErrorCode: apperrors.ErrWrongPassphrase, Description: str, Err: nil}
}
cryptoPubKeyEnc, _, _, err := fetchCryptoKeys(addrmgrBucket)
if err != nil {
return err
}
cryptoPubKeyCT, err := masterKeyPub.Decrypt(cryptoPubKeyEnc)
if err != nil {
const str = "failed to decrypt public data crypto key using master key"
return apperrors.E{ErrorCode: apperrors.ErrCrypto, Description: str, Err: err}
}
cryptoPubKey := &cryptoKey{snacl.CryptoKey{}}
copy(cryptoPubKey.CryptoKey[:], cryptoPubKeyCT)
// Determine how many BIP0044 accounts have been created. Each of these
// accounts must be updated.
lastAccount, err := fetchLastAccount(addrmgrBucket)
if err != nil {
return err
}
// Perform account updates on all BIP0044 accounts created thus far.
for account := uint32(0); account <= lastAccount; account++ {
// Load the old account info.
row, err := fetchAccountInfo(addrmgrBucket, account, oldVersion)
if err != nil {
return err
}
// Use the crypto public key to decrypt the account public extended key
// and each branch key.
serializedKeyPub, err := cryptoPubKey.Decrypt(row.pubKeyEncrypted)
if err != nil {
const str = "failed to decrypt extended public key"
return apperrors.E{ErrorCode: apperrors.ErrCrypto, Description: str, Err: err}
}
xpub, err := hdkeychain.NewKeyFromString(string(serializedKeyPub))
if err != nil {
const str = "failed to create extended public key"
return apperrors.E{ErrorCode: apperrors.ErrKeyChain, Description: str, Err: err}
}
xpubExtBranch, err := xpub.Child(ExternalBranch)
if err != nil {
const str = "failed to derive external branch extended public key"
return apperrors.E{ErrorCode: apperrors.ErrKeyChain, Description: str, Err: err}
}
xpubIntBranch, err := xpub.Child(InternalBranch)
if err != nil {
const str = "failed to derive internal branch extended public key"
return apperrors.E{ErrorCode: apperrors.ErrKeyChain, Description: str, Err: err}
}
// Determine the last used internal and external address indexes. The
// sentinel value ^uint32(0) means that there has been no usage at all.
lastUsedExtIndex := ^uint32(0)
lastUsedIntIndex := ^uint32(0)
for child := uint32(0); child < hdkeychain.HardenedKeyStart; child++ {
xpubChild, err := xpubExtBranch.Child(child)
if err == hdkeychain.ErrInvalidChild {
continue
}
if err != nil {
const str = "unexpected error deriving child key"
return apperrors.E{ErrorCode: apperrors.ErrKeyChain, Description: str, Err: err}
}
// This can't error because the function always passes good input to
// dcrutil.NewAddressPubKeyHash. Also, while it looks like a
// mistake to hardcode the mainnet parameters here, it doesn't make
// any difference since only the pubkey hash is used. (Why is there
// no exported method to just return the serialized public key?)
addr, _ := xpubChild.Address(&chaincfg.MainNetParams)
if addressBucket.Get(addressKey(addr.Hash160()[:])) == nil {
// No more recorded addresses for this account.
break
}
if usedAddrBucket.Get(addressKey(addr.Hash160()[:])) != nil {
lastUsedExtIndex = child
}
}
for child := uint32(0); child < hdkeychain.HardenedKeyStart; child++ {
// Same as above but search the internal branch.
xpubChild, err := xpubIntBranch.Child(child)
if err == hdkeychain.ErrInvalidChild {
continue
}
if err != nil {
const str = "unexpected error deriving child key"
return apperrors.E{ErrorCode: apperrors.ErrKeyChain, Description: str, Err: err}
}
addr, _ := xpubChild.Address(&chaincfg.MainNetParams)
if addressBucket.Get(addressKey(addr.Hash160()[:])) == nil {
break
}
if usedAddrBucket.Get(addressKey(addr.Hash160()[:])) != nil {
lastUsedIntIndex = child
}
}
// Convert account row values to the new serialization format that
// replaces the next to use indexes with the last used indexes.
row = bip0044AccountInfo(row.pubKeyEncrypted, row.privKeyEncrypted,
0, 0, lastUsedExtIndex, lastUsedIntIndex, row.name, newVersion)
err = putAccountInfo(addrmgrBucket, account, row)
if err != nil {
return err
}
// Remove all data saved for address pool handling.
addrmgrMetaBucket := addrmgrBucket.NestedReadWriteBucket(metaBucketName)
err = addrmgrMetaBucket.Delete(accountNumberToAddrPoolKey(false, account))
if err != nil {
return err
}
err = addrmgrMetaBucket.Delete(accountNumberToAddrPoolKey(true, account))
if err != nil {
return err
}
}
// Remove the used address tracking bucket.
err = addrmgrBucket.DeleteNestedBucket(usedAddrBucketName)
if err != nil {
const str = "failed to remove used address tracking bucket"
return apperrors.E{ErrorCode: apperrors.ErrDatabase, Description: str, Err: err}
}
// Write the new database version.
return unifiedDBMetadata{}.putVersion(metadataBucket, newVersion)
}
func votingPreferencesUpgrade(tx walletdb.ReadWriteTx, publicPassphrase []byte) error {
const oldVersion = 2
const newVersion = 3
metadataBucket := tx.ReadWriteBucket(unifiedDBMetadata{}.rootBucketKey())
stakemgrBucket := tx.ReadWriteBucket(wstakemgrBucketKey)
ticketPurchasesBucket := stakemgrBucket.NestedReadWriteBucket(sstxRecordsBucketName)
// Assert that this function is only called on version 2 databases.
dbVersion, err := unifiedDBMetadata{}.getVersion(metadataBucket)
if err != nil {
return err
}
if dbVersion != oldVersion {
const str = "votingPreferencesUpgrade inappropriately called"
return apperrors.E{ErrorCode: apperrors.ErrUpgrade, Description: str, Err: nil}
}
// Update every ticket purchase with the new database version. This removes
// all per-ticket vote bits.
ticketPurchases := make(map[chainhash.Hash]*sstxRecord)
c := ticketPurchasesBucket.ReadCursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
var hash chainhash.Hash
copy(hash[:], k)
ticketPurchase, err := fetchSStxRecord(stakemgrBucket, &hash, oldVersion)
if err != nil {
return err
}
ticketPurchases[hash] = ticketPurchase
}
for _, ticketPurchase := range ticketPurchases {
err := putSStxRecord(stakemgrBucket, ticketPurchase, newVersion)
if err != nil {
return err
}
}
// Create the top level bucket for agenda preferences.
_, err = tx.CreateTopLevelBucket(agendaPreferences.rootBucketKey())
if err != nil {
return err
}
// Write the new database version.
return unifiedDBMetadata{}.putVersion(metadataBucket, newVersion)
}
func noEncryptedSeedUpgrade(tx walletdb.ReadWriteTx, publicPassphrase []byte) error {
const oldVersion = 3
const newVersion = 4
metadataBucket := tx.ReadWriteBucket(unifiedDBMetadata{}.rootBucketKey())
addrmgrBucket := tx.ReadWriteBucket(waddrmgrBucketKey)
mainBucket := addrmgrBucket.NestedReadWriteBucket(mainBucketName)
// Assert that this function is only called on version 3 databases.
dbVersion, err := unifiedDBMetadata{}.getVersion(metadataBucket)
if err != nil {
return err
}
if dbVersion != oldVersion {
const str = "noEncryptedSeedUpgrade inappropriately called"
return apperrors.E{ErrorCode: apperrors.ErrUpgrade, Description: str, Err: nil}
}
// Remove encrypted seed (or encrypted zeros).
err = mainBucket.Delete(seedName)
if err != nil {
return err
}
// Write the new database version.
return unifiedDBMetadata{}.putVersion(metadataBucket, newVersion)
}
// Upgrade checks whether the any upgrades are necessary before the database is
// ready for application usage. If any are, they are performed.
func Upgrade(db walletdb.DB, publicPassphrase []byte) error {
var version uint32
err := walletdb.View(db, func(tx walletdb.ReadTx) error {
var err error
metadataBucket := tx.ReadBucket(unifiedDBMetadata{}.rootBucketKey())
if metadataBucket == nil {
// This could indicate either an unitialized db or one that hasn't
// yet been migrated.
const str = "metadata bucket missing"
return apperrors.E{ErrorCode: apperrors.ErrNoExist, Description: str, Err: nil}
}
version, err = unifiedDBMetadata{}.getVersion(metadataBucket)
return err
})
switch err.(type) {
case nil:
case apperrors.E:
return err
default:
const str = "db view failed"
return apperrors.E{ErrorCode: apperrors.ErrDatabase, Description: str, Err: err}
}
if version >= DBVersion {
// No upgrades necessary.
return nil
}
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
// Execute all necessary upgrades in order.
for _, upgrade := range upgrades[version:] {
err := upgrade(tx, publicPassphrase)
if err != nil {
return err
}
}
return nil
})
switch err.(type) {
case nil:
return nil
case apperrors.E:
return err
default:
const str = "db update failed"
return apperrors.E{ErrorCode: apperrors.ErrDatabase, Description: str, Err: err}
}
}