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,98 +1679,120 @@ 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 cointype private key" str := "failed to delete master HD priv key"
return managerError(ErrDatabase, str, err) return managerError(ErrDatabase, str, err)
} }
// Delete the account extended private key for all accounts. // With the master key and meta encryption keys deleted, we'll need to
bucket = ns.NestedReadWriteBucket(acctBucketName) // delete the keys for all known scopes as well.
err := bucket.ForEach(func(k, v []byte) error { scopeBucket := ns.NestedReadWriteBucket(scopeBucketName)
// Skip buckets. err := scopeBucket.ForEach(func(scopeKey, _ []byte) error {
if v == nil { if len(scopeKey) != 8 {
return nil return nil
} }
// Deserialize the account row first to determine the type. managerScopeBucket := scopeBucket.NestedReadWriteBucket(scopeKey)
row, err := deserializeAccountRow(k, v)
if err != nil { if err := managerScopeBucket.Delete(coinTypePrivKeyName); err != nil {
return err str := "failed to delete cointype private key"
return managerError(ErrDatabase, str, err)
} }
switch row.acctType { // Delete the account extended private key for all accounts.
case actBIP0044: bucket = managerScopeBucket.NestedReadWriteBucket(acctBucketName)
arow, err := deserializeBIP0044AccountRow(k, row) err := bucket.ForEach(func(k, v []byte) error {
// Skip buckets.
if v == nil {
return nil
}
// Deserialize the account row first to determine the type.
row, err := deserializeAccountRow(k, v)
if err != nil { if err != nil {
return err return err
} }
// Reserialize the account without the private key and switch row.acctType {
// store it. case accountDefault:
row.rawData = serializeBIP0044AccountRow( arow, err := deserializeDefaultAccountRow(k, row)
arow.pubKeyEncrypted, nil, if err != nil {
arow.nextExternalIndex, arow.nextInternalIndex, return err
arow.name) }
err = bucket.Put(k, serializeAccountRow(row))
if err != nil { // Reserialize the account without the private key and
str := "failed to delete account private key" // store it.
return managerError(ErrDatabase, str, err) row.rawData = serializeDefaultAccountRow(
arow.pubKeyEncrypted, nil,
arow.nextExternalIndex, arow.nextInternalIndex,
arow.name,
)
err = bucket.Put(k, serializeAccountRow(row))
if err != nil {
str := "failed to delete account private key"
return managerError(ErrDatabase, str, err)
}
} }
}
return nil
})
if err != nil {
return maybeConvertDbError(err)
}
// Delete the private key for all imported addresses.
bucket = ns.NestedReadWriteBucket(addrBucketName)
err = bucket.ForEach(func(k, v []byte) error {
// Skip buckets.
if v == nil {
return nil return nil
} })
// Deserialize the address row first to determine the field
// values.
row, err := deserializeAddressRow(v)
if err != nil { if err != nil {
return err return maybeConvertDbError(err)
} }
switch row.addrType { // Delete the private key for all imported addresses.
case adtImport: bucket = managerScopeBucket.NestedReadWriteBucket(addrBucketName)
irow, err := deserializeImportedAddress(row) err = bucket.ForEach(func(k, v []byte) error {
// Skip buckets.
if v == nil {
return nil
}
// Deserialize the address row first to determine the field
// values.
row, err := deserializeAddressRow(v)
if err != nil { if err != nil {
return err return err
} }
// Reserialize the imported address without the private switch row.addrType {
// key and store it. case adtImport:
row.rawData = serializeImportedAddress( irow, err := deserializeImportedAddress(row)
irow.encryptedPubKey, nil) if err != nil {
err = bucket.Put(k, serializeAddressRow(row)) return err
if err != nil { }
str := "failed to delete imported private key"
return managerError(ErrDatabase, str, err) // Reserialize the imported address without the private
// key and store it.
row.rawData = serializeImportedAddress(
irow.encryptedPubKey, nil)
err = bucket.Put(k, serializeAddressRow(row))
if err != nil {
str := "failed to delete imported private key"
return managerError(ErrDatabase, str, err)
}
case adtScript:
srow, err := deserializeScriptAddress(row)
if err != nil {
return err
}
// Reserialize the script address without the script
// and store it.
row.rawData = serializeScriptAddress(srow.encryptedHash,
nil)
err = bucket.Put(k, serializeAddressRow(row))
if err != nil {
str := "failed to delete imported script"
return managerError(ErrDatabase, str, err)
}
} }
case adtScript: return nil
srow, err := deserializeScriptAddress(row) })
if err != nil { if err != nil {
return err return maybeConvertDbError(err)
}
// Reserialize the script address without the script
// and store it.
row.rawData = serializeScriptAddress(srow.encryptedHash,
nil)
err = bucket.Put(k, serializeAddressRow(row))
if err != nil {
str := "failed to delete imported script"
return managerError(ErrDatabase, str, err)
}
} }
return nil return nil