From 52ad57c3517f1dc3cde5d76491b85ae8723f169a Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 26 Mar 2024 07:45:16 -0700 Subject: [PATCH] Port `chia wallet did ...` to @tx_out_cmd --- chia/_tests/cmds/wallet/test_did.py | 22 ++++++++++------- chia/cmds/wallet.py | 31 ++++++++++++++---------- chia/cmds/wallet_funcs.py | 37 ++++++++++++++++++++--------- chia/rpc/wallet_rpc_api.py | 3 +++ chia/rpc/wallet_rpc_client.py | 2 ++ 5 files changed, 64 insertions(+), 31 deletions(-) diff --git a/chia/_tests/cmds/wallet/test_did.py b/chia/_tests/cmds/wallet/test_did.py index 190cff16f268..63072fba841d 100644 --- a/chia/_tests/cmds/wallet/test_did.py +++ b/chia/_tests/cmds/wallet/test_did.py @@ -32,10 +32,11 @@ async def create_new_did_wallet( name: Optional[str] = "DID Wallet", backup_ids: Optional[List[str]] = None, required_num: int = 0, + push: bool = True, ) -> Dict[str, Union[str, int]]: if backup_ids is None: backup_ids = [] - self.add_to_log("create_new_did_wallet", (amount, fee, name, backup_ids, required_num)) + self.add_to_log("create_new_did_wallet", (amount, fee, name, backup_ids, required_num, push)) return {"wallet_id": 3, "my_did": "did:chia:testdid123456"} inst_rpc_client = DidCreateRpcClient() # pylint: disable=no-value-for-parameter @@ -48,7 +49,7 @@ async def create_new_did_wallet( ] run_cli_command_and_assert(capsys, root_dir, command_args, assert_list) expected_calls: logType = { - "create_new_did_wallet": [(3, 100000000000, "test", [], 0)], + "create_new_did_wallet": [(3, 100000000000, "test", [], 0, True)], } test_rpc_clients.wallet_rpc_client.check_log(expected_calls) @@ -180,8 +181,9 @@ async def update_did_metadata( wallet_id: int, metadata: Dict[str, object], tx_config: TXConfig, + push: bool = True, ) -> DIDUpdateMetadataResponse: - self.add_to_log("update_did_metadata", (wallet_id, metadata, tx_config)) + self.add_to_log("update_did_metadata", (wallet_id, metadata, tx_config, push)) return DIDUpdateMetadataResponse([STD_UTX], [STD_TX], SpendBundle([], G2Element()), uint32(wallet_id)) inst_rpc_client = DidUpdateMetadataRpcClient() # pylint: disable=no-value-for-parameter @@ -203,7 +205,7 @@ async def update_did_metadata( assert_list = [f"Successfully updated DID wallet ID: {w_id}, Spend Bundle: {STD_TX.spend_bundle.to_json_dict()}"] run_cli_command_and_assert(capsys, root_dir, command_args, assert_list) expected_calls: logType = { - "update_did_metadata": [(w_id, {"test": True}, DEFAULT_TX_CONFIG.override(reuse_puzhash=True))], + "update_did_metadata": [(w_id, {"test": True}, DEFAULT_TX_CONFIG.override(reuse_puzhash=True), True)], } test_rpc_clients.wallet_rpc_client.check_log(expected_calls) @@ -252,9 +254,9 @@ def test_did_message_spend(capsys: object, get_test_cli_clients: Tuple[TestRpcCl # set RPC Client class DidMessageSpendRpcClient(TestWalletRpcClient): async def did_message_spend( - self, wallet_id: int, tx_config: TXConfig, extra_conditions: Tuple[Condition, ...] + self, wallet_id: int, tx_config: TXConfig, extra_conditions: Tuple[Condition, ...], push: bool ) -> DIDMessageSpendResponse: - self.add_to_log("did_message_spend", (wallet_id, tx_config, extra_conditions)) + self.add_to_log("did_message_spend", (wallet_id, tx_config, extra_conditions, True)) return DIDMessageSpendResponse([STD_UTX], [STD_TX], SpendBundle([], G2Element())) inst_rpc_client = DidMessageSpendRpcClient() # pylint: disable=no-value-for-parameter @@ -286,6 +288,7 @@ async def did_message_spend( *(CreateCoinAnnouncement(ann) for ann in c_announcements), *(CreatePuzzleAnnouncement(ann) for ann in puz_announcements), ), + True, ) ], } @@ -304,8 +307,9 @@ async def did_transfer_did( fee: int, with_recovery: bool, tx_config: TXConfig, + push: bool, ) -> DIDTransferDIDResponse: - self.add_to_log("did_transfer_did", (wallet_id, address, fee, with_recovery, tx_config)) + self.add_to_log("did_transfer_did", (wallet_id, address, fee, with_recovery, tx_config, push)) return DIDTransferDIDResponse( [STD_UTX], [STD_TX], @@ -340,6 +344,8 @@ async def did_transfer_did( ] run_cli_command_and_assert(capsys, root_dir, command_args, assert_list) expected_calls: logType = { - "did_transfer_did": [(w_id, t_address, 500000000000, True, DEFAULT_TX_CONFIG.override(reuse_puzhash=True))], + "did_transfer_did": [ + (w_id, t_address, 500000000000, True, DEFAULT_TX_CONFIG.override(reuse_puzhash=True), True) + ], } test_rpc_clients.wallet_rpc_client.check_log(expected_calls) diff --git a/chia/cmds/wallet.py b/chia/cmds/wallet.py index 7edbc1fe56e8..20bd8480db73 100644 --- a/chia/cmds/wallet.py +++ b/chia/cmds/wallet.py @@ -650,12 +650,13 @@ def did_cmd() -> None: show_default=True, callback=validate_fee, ) +@tx_out_cmd def did_create_wallet_cmd( - wallet_rpc_port: Optional[int], fingerprint: int, name: Optional[str], amount: int, fee: str -) -> None: + wallet_rpc_port: Optional[int], fingerprint: int, name: Optional[str], amount: int, fee: str, push: bool +) -> List[TransactionRecord]: from .wallet_funcs import create_did_wallet - asyncio.run(create_did_wallet(wallet_rpc_port, fingerprint, Decimal(fee), name, amount)) + return asyncio.run(create_did_wallet(wallet_rpc_port, fingerprint, Decimal(fee), name, amount, push=push)) @did_cmd.command("sign_message", help="Sign a message by a DID") @@ -750,12 +751,13 @@ def did_get_details_cmd(wallet_rpc_port: Optional[int], fingerprint: int, coin_i is_flag=True, default=False, ) +@tx_out_cmd def did_update_metadata_cmd( - wallet_rpc_port: Optional[int], fingerprint: int, id: int, metadata: str, reuse: bool -) -> None: + wallet_rpc_port: Optional[int], fingerprint: int, id: int, metadata: str, reuse: bool, push: bool +) -> List[TransactionRecord]: from .wallet_funcs import update_did_metadata - asyncio.run(update_did_metadata(wallet_rpc_port, fingerprint, id, metadata, reuse)) + return asyncio.run(update_did_metadata(wallet_rpc_port, fingerprint, id, metadata, reuse, push=push)) @did_cmd.command("find_lost", help="Find the did you should own and recovery the DID wallet") @@ -830,13 +832,15 @@ def did_find_lost_cmd( type=str, required=False, ) +@tx_out_cmd def did_message_spend_cmd( wallet_rpc_port: Optional[int], fingerprint: int, id: int, puzzle_announcements: Optional[str], coin_announcements: Optional[str], -) -> None: + push: bool, +) -> List[TransactionRecord]: from .wallet_funcs import did_message_spend puzzle_list: List[str] = [] @@ -849,7 +853,7 @@ def did_message_spend_cmd( bytes.fromhex(announcement) except ValueError: print("Invalid puzzle announcement format, should be a list of hex strings.") - return + return [] if coin_announcements is not None: try: coin_list = coin_announcements.split(",") @@ -858,9 +862,9 @@ def did_message_spend_cmd( bytes.fromhex(announcement) except ValueError: print("Invalid coin announcement format, should be a list of hex strings.") - return + return [] - asyncio.run(did_message_spend(wallet_rpc_port, fingerprint, id, puzzle_list, coin_list)) + return asyncio.run(did_message_spend(wallet_rpc_port, fingerprint, id, puzzle_list, coin_list, push=push)) @did_cmd.command("transfer", help="Transfer a DID") @@ -892,6 +896,7 @@ def did_message_spend_cmd( is_flag=True, default=False, ) +@tx_out_cmd def did_transfer_did( wallet_rpc_port: Optional[int], fingerprint: int, @@ -900,10 +905,11 @@ def did_transfer_did( reset_recovery: bool, fee: str, reuse: bool, -) -> None: + push: bool, +) -> List[TransactionRecord]: from .wallet_funcs import transfer_did - asyncio.run( + return asyncio.run( transfer_did( wallet_rpc_port, fingerprint, @@ -912,6 +918,7 @@ def did_transfer_did( target_address, reset_recovery is False, True if reuse else None, + push=push, ) ) diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index 61aeb5a3f775..74111714580d 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -923,18 +923,20 @@ async def print_balances( async def create_did_wallet( - wallet_rpc_port: Optional[int], fp: Optional[int], d_fee: Decimal, name: Optional[str], amount: int -) -> None: + wallet_rpc_port: Optional[int], fp: Optional[int], d_fee: Decimal, name: Optional[str], amount: int, push: bool +) -> List[TransactionRecord]: async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config): fee: int = int(d_fee * units["chia"]) try: - response = await wallet_client.create_new_did_wallet(amount, fee, name) + response = await wallet_client.create_new_did_wallet(amount, fee, name, push=push) wallet_id = response["wallet_id"] my_did = response["my_did"] print(f"Successfully created a DID wallet with name {name} and id {wallet_id} on key {fingerprint}") print(f"Successfully created a DID {my_did} in the newly created DID wallet") + return [] # TODO: fix this endpoint to return transactions except Exception as e: print(f"Failed to create DID wallet: {e}") + return [] async def did_set_wallet_name(wallet_rpc_port: Optional[int], fp: Optional[int], wallet_id: int, name: str) -> None: @@ -985,7 +987,8 @@ async def update_did_metadata( did_wallet_id: int, metadata: str, reuse_puzhash: bool, -) -> None: + push: bool = True, +) -> List[TransactionRecord]: async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config): try: response = await wallet_client.update_did_metadata( @@ -995,12 +998,15 @@ async def update_did_metadata( reuse_puzhash=reuse_puzhash, ).to_tx_config(units["chia"], config, fingerprint), ) - print( - f"Successfully updated DID wallet ID: {response.wallet_id}, " - f"Spend Bundle: {response.spend_bundle.to_json_dict()}" - ) + if push: + print( + f"Successfully updated DID wallet ID: {response.wallet_id}, " + f"Spend Bundle: {response.spend_bundle.to_json_dict()}" + ) + return response.transactions except Exception as e: print(f"Failed to update DID metadata: {e}") + return [] async def did_message_spend( @@ -1009,7 +1015,8 @@ async def did_message_spend( did_wallet_id: int, puzzle_announcements: List[str], coin_announcements: List[str], -) -> None: + push: bool = True, +) -> List[TransactionRecord]: async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config): try: response = await wallet_client.did_message_spend( @@ -1019,10 +1026,13 @@ async def did_message_spend( *(CreateCoinAnnouncement(hexstr_to_bytes(ca)) for ca in coin_announcements), *(CreatePuzzleAnnouncement(hexstr_to_bytes(pa)) for pa in puzzle_announcements), ), + push=push, ) print(f"Message Spend Bundle: {response.spend_bundle.to_json_dict()}") + return response.transactions except Exception as e: print(f"Failed to update DID metadata: {e}") + return [] async def transfer_did( @@ -1033,7 +1043,8 @@ async def transfer_did( target_address: str, with_recovery: bool, reuse_puzhash: Optional[bool], -) -> None: + push: bool = True, +) -> List[TransactionRecord]: fee: int = int(d_fee * units["chia"]) async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config): @@ -1046,12 +1057,16 @@ async def transfer_did( tx_config=CMDTXConfigLoader( reuse_puzhash=reuse_puzhash, ).to_tx_config(units["chia"], config, fingerprint), + push=push, ) - print(f"Successfully transferred DID to {target_address}") + if push: + print(f"Successfully transferred DID to {target_address}") print(f"Transaction ID: {response.transaction_id.hex()}") print(f"Transaction: {response.transaction.to_json_dict_convenience(config)}") + return response.transactions except Exception as e: print(f"Failed to transfer DID: {e}") + return [] async def find_lost_did( diff --git a/chia/rpc/wallet_rpc_api.py b/chia/rpc/wallet_rpc_api.py index f115189e6b48..e1d16aa97d59 100644 --- a/chia/rpc/wallet_rpc_api.py +++ b/chia/rpc/wallet_rpc_api.py @@ -763,6 +763,9 @@ async def create_new_wallet( if type(request["metadata"]) is dict: metadata = request["metadata"] + if not push: + raise ValueError("Creation of DID wallet must be automatically pushed for now.") + async with self.service.wallet_state_manager.lock: did_wallet_name: str = request.get("wallet_name", None) if did_wallet_name is not None: diff --git a/chia/rpc/wallet_rpc_client.py b/chia/rpc/wallet_rpc_client.py index 79ac3cb78b7a..8ebb0dc74c79 100644 --- a/chia/rpc/wallet_rpc_client.py +++ b/chia/rpc/wallet_rpc_client.py @@ -413,6 +413,7 @@ async def create_new_did_wallet( name: Optional[str] = "DID Wallet", backup_ids: List[str] = [], required_num: int = 0, + push: bool = True, ) -> Dict[str, Any]: request = { "wallet_type": "did_wallet", @@ -422,6 +423,7 @@ async def create_new_did_wallet( "amount": amount, "fee": fee, "wallet_name": name, + "push": push, } response = await self.fetch("create_new_wallet", request) return response