Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHIA-428] Port chia wallet did ... to @tx_out_cmd #18041

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions chia/_tests/cmds/wallet/test_did.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -286,6 +288,7 @@ async def did_message_spend(
*(CreateCoinAnnouncement(ann) for ann in c_announcements),
*(CreatePuzzleAnnouncement(ann) for ann in puz_announcements),
),
True,
)
],
}
Expand All @@ -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],
Expand Down Expand Up @@ -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)
31 changes: 19 additions & 12 deletions chia/cmds/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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] = []
Expand All @@ -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(",")
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -912,6 +918,7 @@ def did_transfer_did(
target_address,
reset_recovery is False,
True if reuse else None,
push=push,
)
)

Expand Down
37 changes: 26 additions & 11 deletions chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions chia/rpc/wallet_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
Loading