Merge pull request #11904 from Chia-Network/paninaro.wallet_show_did_id

`chia wallet show` output now displays the DID ID for NFT/DID wallets
This commit is contained in:
William Allen 2022-06-14 13:54:19 -05:00 committed by GitHub
commit 06ee0f02e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 5 deletions

View File

@ -539,13 +539,23 @@ async def print_balances(args: dict, wallet_client: WalletRpcClient, fingerprint
balances["unconfirmed_wallet_balance"], scale, address_prefix
)
spendable_balance: str = print_balance(balances["spendable_balance"], scale, address_prefix)
my_did: Optional[str] = None
print()
print(f"{summary['name']}:")
print(f"{indent}{'-Total Balance:'.ljust(23)} {total_balance}")
print(f"{indent}{'-Pending Total Balance:'.ljust(23)} " f"{unconfirmed_wallet_balance}")
print(f"{indent}{'-Spendable:'.ljust(23)} {spendable_balance}")
print(f"{indent}{'-Type:'.ljust(23)} {typ.name}")
if len(asset_id) > 0:
if typ == WalletType.DISTRIBUTED_ID:
get_did_response = await wallet_client.get_did_id(wallet_id)
my_did = get_did_response["my_did"]
print(f"{indent}{'-DID ID:'.ljust(23)} {my_did}")
elif typ == WalletType.NFT:
get_did_response = await wallet_client.get_nft_wallet_did(wallet_id)
my_did = get_did_response["did_id"]
if my_did is not None and len(my_did) > 0:
print(f"{indent}{'-DID ID:'.ljust(23)} {my_did}")
elif len(asset_id) > 0:
print(f"{indent}{'-Asset ID:'.ljust(23)} {asset_id}")
print(f"{indent}{'-Wallet ID:'.ljust(23)} {wallet_id}")

View File

@ -137,6 +137,7 @@ class WalletRpcApi:
"/nft_mint_nft": self.nft_mint_nft,
"/nft_get_nfts": self.nft_get_nfts,
"/nft_get_by_did": self.nft_get_by_did,
"/nft_get_wallet_did": self.nft_get_wallet_did,
"/nft_get_wallets_with_dids": self.nft_get_wallets_with_dids,
"/nft_get_info": self.nft_get_info,
"/nft_transfer_nft": self.nft_transfer_nft,
@ -1404,6 +1405,20 @@ class WalletRpcApi:
return {"wallet_id": wallet.wallet_id, "success": True}
return {"error": f"Cannot find a NFT wallet DID = {did_id}", "success": False}
async def nft_get_wallet_did(self, request) -> Dict:
wallet_id = uint32(request["wallet_id"])
assert self.service.wallet_state_manager is not None
nft_wallet: NFTWallet = self.service.wallet_state_manager.wallets[wallet_id]
if nft_wallet is not None:
if nft_wallet.type() != WalletType.NFT.value:
return {"success": False, "error": f"Wallet {wallet_id} is not an NFT wallet"}
did_bytes: Optional[bytes32] = nft_wallet.get_did()
did_id = ""
if did_bytes is not None:
did_id = encode_puzzle_hash(did_bytes, DID_HRP)
return {"success": True, "did_id": None if len(did_id) == 0 else did_id}
return {"success": False, "error": f"Wallet {wallet_id} not found"}
async def nft_get_wallets_with_dids(self, request) -> Dict:
assert self.service.wallet_state_manager is not None
all_wallets = self.service.wallet_state_manager.wallets.values()
@ -1424,7 +1439,7 @@ class WalletRpcApi:
did_nft_wallets.append(
{
"wallet_id": wallet.id(),
"did_id": nft_wallet_did.hex(),
"did_id": encode_puzzle_hash(nft_wallet_did, DID_HRP),
"did_wallet_id": did_wallet_id,
}
)

View File

@ -655,3 +655,8 @@ class WalletRpcClient(RpcClient):
request: Dict[str, Any] = {"wallet_id": wallet_id, "did_id": did_id, "nft_coin_id": nft_coin_id, "fee": fee}
response = await self.fetch("nft_set_nft_did", request)
return response
async def get_nft_wallet_did(self, wallet_id):
request: Dict[str, Any] = {"wallet_id": wallet_id}
response = await self.fetch("nft_get_wallet_did", request)
return response

View File

@ -16,6 +16,7 @@ from chia.types.peer_info import PeerInfo
from chia.util.bech32m import encode_puzzle_hash
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint16, uint32, uint64
from chia.wallet.did_wallet.did_info import DID_HRP
from chia.wallet.did_wallet.did_wallet import DIDWallet
from chia.wallet.nft_wallet.nft_wallet import NFTWallet
from chia.wallet.util.compute_memos import compute_memos
@ -612,9 +613,13 @@ async def test_nft_with_did_wallet_creation(two_wallet_nodes: Any, trusted: Any)
res = await api_0.nft_get_wallets_with_dids({})
assert res.get("success")
assert res.get("nft_wallets") == [
{"wallet_id": nft_wallet_0_id, "did_id": hex_did_id, "did_wallet_id": did_wallet.id()}
{"wallet_id": nft_wallet_0_id, "did_id": hmr_did_id, "did_wallet_id": did_wallet.id()}
]
res = await api_0.nft_get_wallet_did({"wallet_id": nft_wallet_0_id})
assert res.get("success")
assert res.get("did_id") == hmr_did_id
# Create a NFT with DID
nft_ph: bytes32 = await wallet_0.get_new_puzzlehash()
resp = await api_0.nft_mint_nft(
@ -879,8 +884,9 @@ async def test_nft_transfer_nft_with_did(two_wallet_nodes: Any, trusted: Any) ->
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(ph))
await time_out_assert(15, wallet_0.get_pending_change_balance, 0)
hex_did_id = did_wallet.get_my_DID()
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), DID_HRP)
res = await api_0.create_new_wallet(dict(wallet_type="nft_wallet", name="NFT WALLET 1", did_id=hex_did_id))
res = await api_0.create_new_wallet(dict(wallet_type="nft_wallet", name="NFT WALLET 1", did_id=hmr_did_id))
assert isinstance(res, dict)
assert res.get("success")
nft_wallet_0_id = res["wallet_id"]
@ -1004,8 +1010,9 @@ async def test_update_metadata_for_nft_did(two_wallet_nodes: Any, trusted: Any)
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(ph))
await time_out_assert(15, wallet_0.get_pending_change_balance, 0)
hex_did_id = did_wallet.get_my_DID()
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), DID_HRP)
res = await api_0.create_new_wallet(dict(wallet_type="nft_wallet", name="NFT WALLET 1", did_id=hex_did_id))
res = await api_0.create_new_wallet(dict(wallet_type="nft_wallet", name="NFT WALLET 1", did_id=hmr_did_id))
assert isinstance(res, dict)
assert res.get("success")
nft_wallet_0_id = res["wallet_id"]