Skip to content

Commit

Permalink
Port vc_mint
Browse files Browse the repository at this point in the history
  • Loading branch information
Quexington committed Dec 3, 2024
1 parent ac45a37 commit d59f328
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 61 deletions.
13 changes: 6 additions & 7 deletions chia/_tests/cmds/wallet/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from chia._tests.cmds.cmd_test_utils import TestRpcClients, TestWalletRpcClient, logType, run_cli_command_and_assert
from chia._tests.cmds.wallet.test_consts import FINGERPRINT_ARG, STD_TX, STD_UTX, get_bytes32
from chia.rpc.wallet_request_types import VCMintResponse, VCRevokeResponse, VCSpendResponse
from chia.rpc.wallet_request_types import VCMint, VCMintResponse, VCRevokeResponse, VCSpendResponse
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import encode_puzzle_hash
from chia.util.ints import uint32, uint64
Expand All @@ -29,14 +29,13 @@ def test_vcs_mint(capsys: object, get_test_cli_clients: tuple[TestRpcClients, Pa
class VcsMintRpcClient(TestWalletRpcClient):
async def vc_mint(
self,
did_id: bytes32,
request: VCMint,
tx_config: TXConfig,
target_address: Optional[bytes32] = None,
fee: uint64 = uint64(0),
push: bool = True,
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> VCMintResponse:
self.add_to_log("vc_mint", (did_id, tx_config, target_address, fee, push, timelock_info))
self.add_to_log(
"vc_mint", (did_id, tx_config, request.target_address, request.fee, request.push, timelock_info)
)

return VCMintResponse(
[STD_UTX],
Expand Down Expand Up @@ -81,7 +80,7 @@ async def vc_mint(
]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"vc_mint": [(did_bytes, DEFAULT_TX_CONFIG, target_bytes, 500000000000, True, test_condition_valid_times)]
"vc_mint": [(did_id, DEFAULT_TX_CONFIG, target_addr, 500000000000, True, test_condition_valid_times)]
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)

Expand Down
16 changes: 14 additions & 2 deletions chia/_tests/wallet/cat_wallet/test_trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
from chia.consensus.cost_calculator import NPCResult
from chia.consensus.default_constants import DEFAULT_CONSTANTS
from chia.full_node.bundle_tools import simple_solution_generator
from chia.rpc.wallet_request_types import VCMint
from chia.types.blockchain_format.program import INFINITE_COST, Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.spend_bundle import SpendBundle
from chia.util.bech32m import encode_puzzle_hash
from chia.util.hash import std_hash
from chia.util.ints import uint32, uint64
from chia.wallet.cat_wallet.cat_wallet import CATWallet
Expand Down Expand Up @@ -237,12 +239,22 @@ async def test_cat_trades(
# Mint some VCs that can spend the CR-CATs
vc_record_maker = (
await client_maker.vc_mint(
did_id_maker, wallet_environments.tx_config, target_address=await wallet_maker.get_new_puzzlehash()
VCMint(
did_id=encode_puzzle_hash(did_id_maker, "did"),
target_address=encode_puzzle_hash(await wallet_maker.get_new_puzzlehash(), "txch"),
push=True,
),
wallet_environments.tx_config,
)
).vc_record
vc_record_taker = (
await client_taker.vc_mint(
did_id_taker, wallet_environments.tx_config, target_address=await wallet_taker.get_new_puzzlehash()
VCMint(
did_id=encode_puzzle_hash(did_id_taker, "did"),
target_address=encode_puzzle_hash(await wallet_taker.get_new_puzzlehash(), "txch"),
push=True,
),
wallet_environments.tx_config,
)
).vc_record
await wallet_environments.process_pending_states(
Expand Down
18 changes: 14 additions & 4 deletions chia/_tests/wallet/vc_wallet/test_vc_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from chia._tests.environments.wallet import WalletEnvironment, WalletStateTransition, WalletTestFramework
from chia._tests.util.time_out_assert import time_out_assert_not_none
from chia.rpc.wallet_request_types import VCMint
from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.simulator.full_node_simulator import FullNodeSimulator
from chia.types.blockchain_format.coin import coin_as_list
Expand Down Expand Up @@ -162,10 +163,13 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
# Mint a VC
vc_record = (
await client_0.vc_mint(
did_id,
VCMint(
did_id=encode_puzzle_hash(did_id, "did"),
target_address=encode_puzzle_hash(await wallet_0.get_new_puzzlehash(), "txch"),
fee=uint64(1_750_000_000_000),
push=True,
),
wallet_environments.tx_config,
target_address=await wallet_0.get_new_puzzlehash(),
fee=uint64(1_750_000_000_000),
)
).vc_record

Expand Down Expand Up @@ -670,7 +674,13 @@ async def test_self_revoke(wallet_environments: WalletTestFramework) -> None:

vc_record = (
await client_0.vc_mint(
did_id, wallet_environments.tx_config, target_address=await wallet_0.get_new_puzzlehash(), fee=uint64(200)
VCMint(
did_id=encode_puzzle_hash(did_id, "did"),
target_address=encode_puzzle_hash(await wallet_0.get_new_puzzlehash(), "txch"),
fee=uint64(200),
push=True,
),
wallet_environments.tx_config,
)
).vc_record
await wallet_environments.process_pending_states(
Expand Down
12 changes: 7 additions & 5 deletions chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from chia.cmds.param_types import CliAddress, CliAmount
from chia.cmds.peer_funcs import print_connections
from chia.cmds.units import units
from chia.rpc.wallet_request_types import CATSpendResponse, GetNotifications, SendTransactionResponse
from chia.rpc.wallet_request_types import CATSpendResponse, GetNotifications, SendTransactionResponse, VCMint
from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import bech32_decode, decode_puzzle_hash, encode_puzzle_hash
Expand Down Expand Up @@ -1582,11 +1582,13 @@ async def mint_vc(
) -> list[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
res = await wallet_client.vc_mint(
did.validate_address_type_get_ph(AddressType.DID),
VCMint(
did_id=did.validate_address_type(AddressType.DID),
target_address=target_address.validate_address_type(AddressType.XCH) if target_address else None,
fee=fee,
push=push,
),
CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
target_address.validate_address_type_get_ph(AddressType.XCH) if target_address else None,
fee,
push=push,
timelock_info=condition_valid_times,
)

Expand Down
19 changes: 13 additions & 6 deletions chia/rpc/wallet_request_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,19 @@ class NFTTransferBulkResponse(TransactionEndpointResponse):
spend_bundle: WalletSpendBundle


@streamable
@dataclass(frozen=True)
class VCMint(TransactionEndpointRequest):
did_id: str = field(default_factory=default_raise)
target_address: Optional[str] = None


@streamable
@dataclass(frozen=True)
class VCMintResponse(TransactionEndpointResponse):
vc_record: VCRecord


# TODO: The section below needs corresponding request types
# TODO: The section below should be added to the API (currently only for client)
@streamable
Expand Down Expand Up @@ -749,12 +762,6 @@ class DAOExitLockupResponse(TransactionEndpointResponse):
tx: TransactionRecord


@streamable
@dataclass(frozen=True)
class VCMintResponse(TransactionEndpointResponse):
vc_record: VCRecord


@streamable
@dataclass(frozen=True)
class VCSpendResponse(TransactionEndpointResponse):
Expand Down
30 changes: 10 additions & 20 deletions chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
SplitCoinsResponse,
SubmitTransactions,
SubmitTransactionsResponse,
VCMint,
VCMintResponse,
)
from chia.server.outbound_message import NodeType
from chia.server.ws_connection import WSChiaConnection
Expand Down Expand Up @@ -4523,42 +4525,30 @@ async def dl_verify_proof(
# Verified Credential
##########################################################################################
@tx_endpoint(push=True)
@marshal
async def vc_mint(
self,
request: dict[str, Any],
request: VCMint,
action_scope: WalletActionScope,
extra_conditions: tuple[Condition, ...] = tuple(),
) -> EndpointResult:
) -> VCMintResponse:
"""
Mint a verified credential using the assigned DID
:param request: We require 'did_id' that will be minting the VC and options for a new 'target_address' as well
as a 'fee' for the mint tx
:return: a 'vc_record' containing all the information of the soon-to-be-confirmed vc as well as any relevant
'transactions'
"""

@streamable
@dataclasses.dataclass(frozen=True)
class VCMint(Streamable):
did_id: str
target_address: Optional[str] = None
fee: uint64 = uint64(0)

parsed_request = VCMint.from_json_dict(request)

did_id = decode_puzzle_hash(parsed_request.did_id)
did_id = decode_puzzle_hash(request.did_id)
puzhash: Optional[bytes32] = None
if parsed_request.target_address is not None:
puzhash = decode_puzzle_hash(parsed_request.target_address)
if request.target_address is not None:
puzhash = decode_puzzle_hash(request.target_address)

vc_wallet: VCWallet = await self.service.wallet_state_manager.get_or_create_vc_wallet()
vc_record = await vc_wallet.launch_new_vc(
did_id, action_scope, puzhash, parsed_request.fee, extra_conditions=extra_conditions
did_id, action_scope, puzhash, request.fee, extra_conditions=extra_conditions
)
return {
"vc_record": vc_record.to_json_dict(),
"transactions": None, # tx_endpoint wrapper will take care of this
}
return VCMintResponse([], [], vc_record)

async def vc_get(self, request: dict[str, Any]) -> EndpointResult:
"""
Expand Down
23 changes: 6 additions & 17 deletions chia/rpc/wallet_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
SubmitTransactions,
SubmitTransactionsResponse,
TakeOfferResponse,
VCMint,
VCMintResponse,
VCRevokeResponse,
VCSpendResponse,
Expand All @@ -95,7 +96,6 @@
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.coin_record import CoinRecord
from chia.util.bech32m import encode_puzzle_hash
from chia.util.ints import uint16, uint32, uint64
from chia.wallet.conditions import Condition, ConditionValidTimes, conditions_to_json_dicts
from chia.wallet.puzzles.clawback.metadata import AutoClaimSettings
Expand Down Expand Up @@ -1662,27 +1662,16 @@ async def dao_adjust_filter_level(self, wallet_id: int, filter_level: int) -> di

async def vc_mint(
self,
did_id: bytes32,
request: VCMint,
tx_config: TXConfig,
target_address: Optional[bytes32] = None,
fee: uint64 = uint64(0),
extra_conditions: tuple[Condition, ...] = tuple(),
timelock_info: ConditionValidTimes = ConditionValidTimes(),
push: bool = True,
) -> VCMintResponse:
response = await self.fetch(
"vc_mint",
{
"did_id": encode_puzzle_hash(did_id, "rpc"),
"target_address": encode_puzzle_hash(target_address, "rpc") if target_address is not None else None,
"fee": fee,
"extra_conditions": conditions_to_json_dicts(extra_conditions),
"push": push,
**tx_config.to_json_dict(),
**timelock_info.to_json_dict(),
},
return VCMintResponse.from_json_dict(
await self.fetch(
"vc_mint", {**request.json_serialize_for_transport(tx_config, extra_conditions, timelock_info)}
)
)
return json_deserialize_with_clvm_streamable(response, VCMintResponse)

async def vc_get(self, vc_id: bytes32) -> Optional[VCRecord]:
response = await self.fetch("vc_get", {"vc_id": vc_id.hex()})
Expand Down

0 comments on commit d59f328

Please sign in to comment.