Skip to content

Commit 18c1010

Browse files
authored
[LABS-285] Fix RPC key deletion endpoints to actually delete intended DBs (#20171)
Fix RPC key deletion endpoints to actually delete intended DBs
1 parent 8c93148 commit 18c1010

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
from chia.wallet.wallet import Wallet
102102
from chia.wallet.wallet_coin_record import WalletCoinRecord
103103
from chia.wallet.wallet_coin_store import GetCoinRecords
104-
from chia.wallet.wallet_node import WalletNode
104+
from chia.wallet.wallet_node import WalletNode, get_wallet_db_path
105105
from chia.wallet.wallet_protocol import WalletProtocol
106106
from chia.wallet.wallet_request_types import (
107107
AddKey,
@@ -2340,7 +2340,9 @@ async def test_key_and_address_endpoints(wallet_rpc_environment: WalletRpcTestEn
23402340
assert delete_key_resp.used_for_farmer_rewards is False
23412341
assert delete_key_resp.used_for_pool_rewards is False
23422342

2343+
assert get_wallet_db_path(wallet_node.root_path, wallet_node.config, str(pks[0])).exists()
23432344
await client.delete_key(DeleteKey(pks[0]))
2345+
assert not get_wallet_db_path(wallet_node.root_path, wallet_node.config, str(pks[0])).exists()
23442346
await client.log_in(LogIn(uint32(pks[1])))
23452347
assert len((await client.get_public_keys()).pk_fingerprints) == 1
23462348

@@ -2357,7 +2359,13 @@ async def test_key_and_address_endpoints(wallet_rpc_environment: WalletRpcTestEn
23572359
)
23582360

23592361
# Delete all keys
2362+
resp = await client.generate_mnemonic()
2363+
add_key_resp = await client.add_key(AddKey(resp.mnemonic))
2364+
assert get_wallet_db_path(wallet_node.root_path, wallet_node.config, str(pks[1])).exists()
2365+
assert get_wallet_db_path(wallet_node.root_path, wallet_node.config, str(add_key_resp.fingerprint)).exists()
23602366
await client.delete_all_keys()
2367+
assert not get_wallet_db_path(wallet_node.root_path, wallet_node.config, str(pks[1])).exists()
2368+
assert not get_wallet_db_path(wallet_node.root_path, wallet_node.config, str(add_key_resp.fingerprint)).exists()
23612369
assert len((await client.get_public_keys()).pk_fingerprints) == 0
23622370

23632371

chia/wallet/wallet_rpc_api.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from chia.util.errors import KeychainIsLocked
3232
from chia.util.hash import std_hash
3333
from chia.util.keychain import bytes_to_mnemonic, generate_mnemonic
34-
from chia.util.path import path_from_root
3534
from chia.util.streamable import Streamable, UInt32Range, streamable
3635
from chia.util.ws_message import WsRpcMessage, create_payload_dict
3736
from chia.wallet.cat_wallet.cat_constants import DEFAULT_CATS
@@ -104,7 +103,7 @@
104103
from chia.wallet.wallet_coin_record import WalletCoinRecord
105104
from chia.wallet.wallet_coin_store import CoinRecordOrder, GetCoinRecords, unspent_range
106105
from chia.wallet.wallet_info import WalletInfo
107-
from chia.wallet.wallet_node import WalletNode
106+
from chia.wallet.wallet_node import WalletNode, get_wallet_db_path
108107
from chia.wallet.wallet_protocol import WalletProtocol
109108
from chia.wallet.wallet_request_types import (
110109
AddKey,
@@ -835,9 +834,10 @@ async def delete_key(self, request: DeleteKey) -> Empty:
835834
except Exception as e:
836835
log.error(f"Failed to delete key by fingerprint: {e}")
837836
raise e
838-
path = path_from_root(
837+
path = get_wallet_db_path(
839838
self.service.root_path,
840-
f"{self.service.config['database_path']}-{request.fingerprint}",
839+
self.service.config,
840+
str(request.fingerprint),
841841
)
842842
if path.exists():
843843
path.unlink()
@@ -925,14 +925,20 @@ async def check_delete_key(self, request: CheckDeleteKey) -> CheckDeleteKeyRespo
925925
@marshal
926926
async def delete_all_keys(self, request: Empty) -> Empty:
927927
await self._stop_wallet()
928+
all_key_datas = await self.service.keychain_proxy.get_keys()
928929
try:
929930
await self.service.keychain_proxy.delete_all_keys()
930931
except Exception as e:
931932
log.error(f"Failed to delete all keys: {e}")
932933
raise e
933-
path = path_from_root(self.service.root_path, self.service.config["database_path"])
934-
if path.exists():
935-
path.unlink()
934+
for key_data in all_key_datas:
935+
path = get_wallet_db_path(
936+
self.service.root_path,
937+
self.service.config,
938+
str(key_data.fingerprint),
939+
)
940+
if path.exists():
941+
path.unlink()
936942
return Empty()
937943

938944
##########################################################################################

0 commit comments

Comments
 (0)