waddrmgr: update database version to 5

This commit is contained in:
Olaoluwa Osuntokun 2018-02-13 21:08:58 -08:00
parent 9f71d472a3
commit f5de14f16b

View file

@ -20,7 +20,7 @@ import (
const ( const (
// LatestMgrVersion is the most recent manager version. // LatestMgrVersion is the most recent manager version.
LatestMgrVersion = 4 LatestMgrVersion = 5
) )
var ( var (
@ -2240,6 +2240,11 @@ func upgradeManager(db walletdb.DB, namespaceKey []byte, pubPassPhrase []byte,
// * acctIDIdxBucketName // * acctIDIdxBucketName
// * metaBucketName // * metaBucketName
func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPassPhrase []byte, chainParams *chaincfg.Params) error { func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPassPhrase []byte, chainParams *chaincfg.Params) error {
priorScope := &KeyScope{
Purpose: 44,
Coin: chainParams.HDCoinType,
}
err := func() error { err := func() error {
currentMgrVersion := uint32(3) currentMgrVersion := uint32(3)
@ -2262,7 +2267,9 @@ func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPas
} }
// Derive the cointype key according to BIP0044. // Derive the cointype key according to BIP0044.
coinTypeKeyPriv, err := deriveCoinTypeKey(root, chainParams.HDCoinType) coinTypeKeyPriv, err := deriveCoinTypeKey(
root, *priorScope,
)
if err != nil { if err != nil {
str := "failed to derive cointype extended key" str := "failed to derive cointype extended key"
return managerError(ErrKeyChain, str, err) return managerError(ErrKeyChain, str, err)
@ -2288,7 +2295,7 @@ func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPas
} }
// Save the encrypted cointype keys to the database. // Save the encrypted cointype keys to the database.
err = putCoinTypeKeys(ns, coinTypePubEnc, coinTypePrivEnc) err = putCoinTypeKeys(ns, priorScope, coinTypePubEnc, coinTypePrivEnc)
if err != nil { if err != nil {
return err return err
} }
@ -2312,22 +2319,22 @@ func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPas
} }
// Initialize metadata for all keys // Initialize metadata for all keys
if err := putLastAccount(ns, DefaultAccountNum); err != nil { if err := putLastAccount(ns, priorScope, DefaultAccountNum); err != nil {
return err return err
} }
// Update default account indexes // Update default account indexes
if err := putAccountIDIndex(ns, DefaultAccountNum, defaultAccountName); err != nil { if err := putAccountIDIndex(ns, priorScope, DefaultAccountNum, defaultAccountName); err != nil {
return err return err
} }
if err := putAccountNameIndex(ns, DefaultAccountNum, defaultAccountName); err != nil { if err := putAccountNameIndex(ns, priorScope, DefaultAccountNum, defaultAccountName); err != nil {
return err return err
} }
// Update imported account indexes // Update imported account indexes
if err := putAccountIDIndex(ns, ImportedAddrAccount, ImportedAddrAccountName); err != nil { if err := putAccountIDIndex(ns, priorScope, ImportedAddrAccount, ImportedAddrAccountName); err != nil {
return err return err
} }
if err := putAccountNameIndex(ns, ImportedAddrAccount, ImportedAddrAccountName); err != nil { if err := putAccountNameIndex(ns, priorScope, ImportedAddrAccount, ImportedAddrAccountName); err != nil {
return err return err
} }
@ -2337,7 +2344,7 @@ func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPas
} }
// Save "" alias for default account name for backward compat // Save "" alias for default account name for backward compat
return putAccountNameIndex(ns, DefaultAccountNum, "") return putAccountNameIndex(ns, priorScope, DefaultAccountNum, "")
}() }()
if err != nil { if err != nil {
return maybeConvertDbError(err) return maybeConvertDbError(err)
@ -2349,6 +2356,11 @@ func upgradeToVersion3(ns walletdb.ReadWriteBucket, seed, privPassPhrase, pubPas
// default account remains unchanged (even if it was modified by the user), but // default account remains unchanged (even if it was modified by the user), but
// the empty string alias to the default account is removed. // the empty string alias to the default account is removed.
func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error { func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error {
priorScope := &KeyScope{
Purpose: 44,
Coin: 0,
}
err := func() error { err := func() error {
// Write new manager version. // Write new manager version.
err := putManagerVersion(ns, 4) err := putManagerVersion(ns, 4)
@ -2358,11 +2370,11 @@ func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error
// Lookup the old account info to determine the real name of the // Lookup the old account info to determine the real name of the
// default account. All other names will be removed. // default account. All other names will be removed.
acctInfoIface, err := fetchAccountInfo(ns, DefaultAccountNum) acctInfoIface, err := fetchAccountInfo(ns, priorScope, DefaultAccountNum)
if err != nil { if err != nil {
return err return err
} }
acctInfo, ok := acctInfoIface.(*dbBIP0044AccountRow) acctInfo, ok := acctInfoIface.(*dbDefaultAccountRow)
if !ok { if !ok {
str := fmt.Sprintf("unsupported account type %T", acctInfoIface) str := fmt.Sprintf("unsupported account type %T", acctInfoIface)
return managerError(ErrDatabase, str, nil) return managerError(ErrDatabase, str, nil)
@ -2398,7 +2410,7 @@ func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error
// The account number to name index may map to the wrong name, // The account number to name index may map to the wrong name,
// so rewrite the entry with the true name from the account row // so rewrite the entry with the true name from the account row
// instead of leaving it set to an incorrect alias. // instead of leaving it set to an incorrect alias.
err = putAccountIDIndex(ns, DefaultAccountNum, acctInfo.name) err = putAccountIDIndex(ns, priorScope, DefaultAccountNum, acctInfo.name)
if err != nil { if err != nil {
const str = "account number to name index could not be " + const str = "account number to name index could not be " +
"rewritten with actual account name" "rewritten with actual account name"
@ -2407,7 +2419,7 @@ func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error
// Ensure that the true name for the default account maps // Ensure that the true name for the default account maps
// forwards and backwards to the default account number. // forwards and backwards to the default account number.
name, err := fetchAccountName(ns, DefaultAccountNum) name, err := fetchAccountName(ns, priorScope, DefaultAccountNum)
if err != nil { if err != nil {
return err return err
} }
@ -2415,7 +2427,7 @@ func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error
const str = "account name index does not map default account number to correct name" const str = "account name index does not map default account number to correct name"
return managerError(ErrUpgrade, str, nil) return managerError(ErrUpgrade, str, nil)
} }
acct, err := fetchAccountByName(ns, acctInfo.name) acct, err := fetchAccountByName(ns, priorScope, acctInfo.name)
if err != nil { if err != nil {
return err return err
} }
@ -2426,7 +2438,7 @@ func upgradeToVersion4(ns walletdb.ReadWriteBucket, pubPassPhrase []byte) error
// Ensure that looking up the default account by the old name // Ensure that looking up the default account by the old name
// cannot succeed. // cannot succeed.
_, err = fetchAccountByName(ns, oldName) _, err = fetchAccountByName(ns, priorScope, oldName)
if err == nil { if err == nil {
const str = "default account exists under old name" const str = "default account exists under old name"
return managerError(ErrUpgrade, str, nil) return managerError(ErrUpgrade, str, nil)