waddrmgr: extend deletePrivateKeys to delete HD key and scope keys

This commit is contained in:
Olaoluwa Osuntokun 2018-02-13 21:06:19 -08:00
parent 946ca2da1e
commit 57cf4274ee

View file

@ -1679,13 +1679,28 @@ func deletePrivateKeys(ns walletdb.ReadWriteBucket) error {
str := "failed to delete crypto script key" str := "failed to delete crypto script key"
return managerError(ErrDatabase, str, err) return managerError(ErrDatabase, str, err)
} }
if err := bucket.Delete(coinTypePrivKeyName); err != nil { if err := bucket.Delete(masterHDPrivName); err != nil {
str := "failed to delete master HD priv key"
return managerError(ErrDatabase, str, err)
}
// With the master key and meta encryption keys deleted, we'll need to
// delete the keys for all known scopes as well.
scopeBucket := ns.NestedReadWriteBucket(scopeBucketName)
err := scopeBucket.ForEach(func(scopeKey, _ []byte) error {
if len(scopeKey) != 8 {
return nil
}
managerScopeBucket := scopeBucket.NestedReadWriteBucket(scopeKey)
if err := managerScopeBucket.Delete(coinTypePrivKeyName); err != nil {
str := "failed to delete cointype private key" str := "failed to delete cointype private key"
return managerError(ErrDatabase, str, err) return managerError(ErrDatabase, str, err)
} }
// Delete the account extended private key for all accounts. // Delete the account extended private key for all accounts.
bucket = ns.NestedReadWriteBucket(acctBucketName) bucket = managerScopeBucket.NestedReadWriteBucket(acctBucketName)
err := bucket.ForEach(func(k, v []byte) error { err := bucket.ForEach(func(k, v []byte) error {
// Skip buckets. // Skip buckets.
if v == nil { if v == nil {
@ -1699,18 +1714,19 @@ func deletePrivateKeys(ns walletdb.ReadWriteBucket) error {
} }
switch row.acctType { switch row.acctType {
case actBIP0044: case accountDefault:
arow, err := deserializeBIP0044AccountRow(k, row) arow, err := deserializeDefaultAccountRow(k, row)
if err != nil { if err != nil {
return err return err
} }
// Reserialize the account without the private key and // Reserialize the account without the private key and
// store it. // store it.
row.rawData = serializeBIP0044AccountRow( row.rawData = serializeDefaultAccountRow(
arow.pubKeyEncrypted, nil, arow.pubKeyEncrypted, nil,
arow.nextExternalIndex, arow.nextInternalIndex, arow.nextExternalIndex, arow.nextInternalIndex,
arow.name) arow.name,
)
err = bucket.Put(k, serializeAccountRow(row)) err = bucket.Put(k, serializeAccountRow(row))
if err != nil { if err != nil {
str := "failed to delete account private key" str := "failed to delete account private key"
@ -1725,7 +1741,7 @@ func deletePrivateKeys(ns walletdb.ReadWriteBucket) error {
} }
// Delete the private key for all imported addresses. // Delete the private key for all imported addresses.
bucket = ns.NestedReadWriteBucket(addrBucketName) bucket = managerScopeBucket.NestedReadWriteBucket(addrBucketName)
err = bucket.ForEach(func(k, v []byte) error { err = bucket.ForEach(func(k, v []byte) error {
// Skip buckets. // Skip buckets.
if v == nil { if v == nil {
@ -1779,6 +1795,12 @@ func deletePrivateKeys(ns walletdb.ReadWriteBucket) error {
return maybeConvertDbError(err) return maybeConvertDbError(err)
} }
return nil
})
if err != nil {
return maybeConvertDbError(err)
}
return nil return nil
} }