Skip to content

Commit bc95722

Browse files
authored
[LABS-288] Less asset_id: str (#20261)
* Less `asset_id: str` * Missed a couple * Missed a couple more * Missed another?
1 parent 2b3c11e commit bc95722

File tree

15 files changed

+78
-85
lines changed

15 files changed

+78
-85
lines changed

chia/_tests/wallet/cat_wallet/test_cat_wallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async def mint_cat(
121121
)
122122

123123
cat_wallet = await wallet_type.get_or_create_wallet_for_cat(
124-
environment.wallet_state_manager, environment.xch_wallet, tail_hash.hex(), *extra_args
124+
environment.wallet_state_manager, environment.xch_wallet, tail_hash, *extra_args
125125
)
126126

127127
await wallet_environments.process_pending_states(
@@ -577,7 +577,7 @@ async def test_get_wallet_for_asset_id(wallet_environments: WalletTestFramework,
577577

578578
# Test that the a default CAT will initialize correctly
579579
asset = DEFAULT_CATS[next(iter(DEFAULT_CATS))]
580-
asset_id = asset["asset_id"]
580+
asset_id = bytes32.from_hexstr(asset["asset_id"])
581581
if wallet_type is RCATWallet:
582582
extra_args: Any = (bytes32.zeros,)
583583
else:

chia/_tests/wallet/cat_wallet/test_trades.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ async def test_cat_trades(
173173
cat_wallet_maker: CATWallet = await CRCATWallet.get_or_create_wallet_for_cat(
174174
wallet_node_maker.wallet_state_manager,
175175
wallet_maker,
176-
tail_maker.get_tree_hash().hex(),
176+
tail_maker.get_tree_hash(),
177177
None,
178178
authorized_providers,
179179
proofs_checker_maker,
180180
)
181181
new_cat_wallet_taker: CATWallet = await CRCATWallet.get_or_create_wallet_for_cat(
182182
wallet_node_taker.wallet_state_manager,
183183
wallet_taker,
184-
tail_taker.get_tree_hash().hex(),
184+
tail_taker.get_tree_hash(),
185185
None,
186186
authorized_providers,
187187
proofs_checker_taker,
@@ -417,14 +417,14 @@ async def test_cat_trades(
417417
# Create the trade parameters
418418
chia_for_cat: OfferSpecification = {
419419
wallet_maker.id(): -1,
420-
bytes32.from_hexstr(new_cat_wallet_maker.get_asset_id()): 2, # This is the CAT that the taker made
420+
new_cat_wallet_maker.get_asset_id(): 2, # This is the CAT that the taker made
421421
}
422422
cat_for_chia: OfferSpecification = {
423423
wallet_maker.id(): 3,
424424
cat_wallet_maker.id(): -4, # The taker has no knowledge of this CAT yet
425425
}
426426
cat_for_cat: OfferSpecification = {
427-
bytes32.from_hexstr(cat_wallet_maker.get_asset_id()): -5,
427+
cat_wallet_maker.get_asset_id(): -5,
428428
new_cat_wallet_maker.id(): 6,
429429
}
430430
chia_for_multiple_cat: OfferSpecification = {
@@ -445,10 +445,10 @@ async def test_cat_trades(
445445

446446
driver_dict: dict[bytes32, PuzzleInfo] = {}
447447
for wallet in (cat_wallet_maker, new_cat_wallet_maker):
448-
asset_id: str = wallet.get_asset_id()
448+
asset_id = wallet.get_asset_id()
449449
driver_item: dict[str, Any] = {
450450
"type": AssetType.CAT.value,
451-
"tail": "0x" + asset_id,
451+
"tail": "0x" + asset_id.hex(),
452452
}
453453
if credential_restricted:
454454
driver_item["also"] = {
@@ -460,7 +460,7 @@ async def test_cat_trades(
460460
else proofs_checker_taker.as_program()
461461
),
462462
}
463-
driver_dict[bytes32.from_hexstr(asset_id)] = PuzzleInfo(driver_item)
463+
driver_dict[asset_id] = PuzzleInfo(driver_item)
464464

465465
trade_manager_maker = env_maker.wallet_state_manager.trade_manager
466466
trade_manager_taker = env_taker.wallet_state_manager.trade_manager
@@ -1805,7 +1805,7 @@ async def test_trade_cancellation(wallet_environments: WalletTestFramework, wall
18051805
# This take should fail since we have no CATs to fulfill it with
18061806
with pytest.raises(
18071807
ValueError,
1808-
match=f"Do not have a wallet for asset ID: {cat_wallet_maker.get_asset_id()} to fulfill offer",
1808+
match=f"Do not have a wallet for asset ID: {cat_wallet_maker.get_asset_id().hex()} to fulfill offer",
18091809
):
18101810
async with env_taker.wallet_state_manager.new_action_scope(
18111811
wallet_environments.tx_config, push=False

chia/_tests/wallet/nft_wallet/test_nft_1_offers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ async def test_complex_nft_offer(
19821982
1: XCH_REQUESTED,
19831983
nft_to_offer_asset_id_taker_1: 1,
19841984
nft_to_offer_asset_id_taker_2: 1,
1985-
bytes32.from_hexstr(cat_wallet_taker.get_asset_id()): CAT_REQUESTED,
1985+
cat_wallet_taker.get_asset_id(): CAT_REQUESTED,
19861986
}
19871987

19881988
nft_taker_1_info = match_puzzle(uncurry_puzzle(taker_nfts[0].full_puzzle))
@@ -1992,10 +1992,10 @@ async def test_complex_nft_offer(
19921992
driver_dict = {
19931993
nft_to_offer_asset_id_taker_1: nft_taker_1_info,
19941994
nft_to_offer_asset_id_taker_2: nft_taker_2_info,
1995-
bytes32.from_hexstr(cat_wallet_taker.get_asset_id()): PuzzleInfo(
1995+
cat_wallet_taker.get_asset_id(): PuzzleInfo(
19961996
{
19971997
"type": "CAT",
1998-
"tail": "0x" + cat_wallet_taker.get_asset_id(),
1998+
"tail": "0x" + cat_wallet_taker.get_asset_id().hex(),
19991999
**(
20002000
{}
20012001
if wallet_type is CATWallet
@@ -2047,7 +2047,7 @@ async def test_complex_nft_offer(
20472047
},
20482048
{
20492049
None: uint64(XCH_REQUESTED),
2050-
bytes32.from_hexstr(cat_wallet_taker.get_asset_id()): uint64(CAT_REQUESTED),
2050+
cat_wallet_taker.get_asset_id(): uint64(CAT_REQUESTED),
20512051
},
20522052
)
20532053
taker_royalty_summary = NFTWallet.royalty_calculation(
@@ -2056,7 +2056,7 @@ async def test_complex_nft_offer(
20562056
nft_to_offer_asset_id_taker_2: (royalty_puzhash_taker, royalty_basis_pts_taker_2),
20572057
},
20582058
{
2059-
bytes32.from_hexstr(cat_wallet_maker.get_asset_id()): uint64(CAT_REQUESTED),
2059+
cat_wallet_maker.get_asset_id(): uint64(CAT_REQUESTED),
20602060
},
20612061
)
20622062
maker_xch_royalties_expected = maker_royalty_summary[nft_to_offer_asset_id_maker][0]["amount"]
@@ -2198,18 +2198,18 @@ async def test_complex_nft_offer(
21982198
complex_nft_offer = {
21992199
cat_wallet_maker.id(): CAT_REQUESTED * -1,
22002200
1: HALF_XCH_REQUESTED,
2201-
bytes32.from_hexstr(cat_wallet_taker.get_asset_id()): CAT_REQUESTED,
2201+
cat_wallet_taker.get_asset_id(): CAT_REQUESTED,
22022202
nft_to_offer_asset_id_maker: 1,
22032203
}
22042204

22052205
maker_nft_info = match_puzzle(uncurry_puzzle(taker_nfts[0].full_puzzle))
22062206
assert maker_nft_info is not None
22072207
driver_dict = {
22082208
nft_to_offer_asset_id_maker: maker_nft_info,
2209-
bytes32.from_hexstr(cat_wallet_taker.get_asset_id()): PuzzleInfo(
2209+
cat_wallet_taker.get_asset_id(): PuzzleInfo(
22102210
{
22112211
"type": "CAT",
2212-
"tail": "0x" + cat_wallet_taker.get_asset_id(),
2212+
"tail": "0x" + cat_wallet_taker.get_asset_id().hex(),
22132213
}
22142214
),
22152215
}

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ async def assert_get_balance(rpc_client: WalletRpcClient, wallet_node: WalletNod
239239
expected_balance_dict["fingerprint"] = wallet_node.logged_in_fingerprint
240240
if wallet.type() in {WalletType.CAT, WalletType.CRCAT}:
241241
assert isinstance(wallet, CATWallet)
242-
expected_balance_dict["asset_id"] = "0x" + wallet.get_asset_id()
242+
expected_balance_dict["asset_id"] = "0x" + wallet.get_asset_id().hex()
243243
else:
244244
expected_balance_dict["asset_id"] = None
245245
assert (
@@ -1137,7 +1137,7 @@ async def test_cat_endpoints(wallet_environments: WalletTestFramework, wallet_ty
11371137
asset_id = (await env_0.rpc_client.get_cat_asset_id(CATGetAssetID(cat_0_id))).asset_id
11381138
assert (
11391139
await env_0.rpc_client.get_cat_name(CATGetName(cat_0_id))
1140-
).name == wallet_type.default_wallet_name_for_unknown_cat(asset_id.hex())
1140+
).name == wallet_type.default_wallet_name_for_unknown_cat(asset_id)
11411141
await env_0.rpc_client.set_cat_name(CATSetName(cat_0_id, "My cat"))
11421142
assert (await env_0.rpc_client.get_cat_name(CATGetName(cat_0_id))).name == "My cat"
11431143
asset_to_name_response = await env_0.rpc_client.cat_asset_id_to_name(CATAssetIDToName(asset_id))

chia/_tests/wallet/test_wallet_test_framework.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from chia_rs import BlockRecord
5+
from chia_rs.sized_bytes import bytes32
56

67
from chia._tests.environments.wallet import (
78
BalanceCheckingError,
@@ -145,7 +146,7 @@ async def test_balance_checking(
145146
)
146147
]
147148
)
148-
await CATWallet.get_or_create_wallet_for_cat(env_0.wallet_state_manager, env_0.xch_wallet, "00" * 32)
149+
await CATWallet.get_or_create_wallet_for_cat(env_0.wallet_state_manager, env_0.xch_wallet, bytes32.zeros)
149150
with pytest.raises(KeyError, match="No wallet state for wallet id 2"):
150151
await env_0.check_balances()
151152

chia/_tests/wallet/vc_wallet/test_vc_wallet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ async def test_cat_wallet_conversion(
849849

850850
# Key point of test: create a normal CAT wallet first, and see if it gets converted to CR-CAT wallet
851851
await CATWallet.get_or_create_wallet_for_cat(
852-
wallet_node_0.wallet_state_manager, wallet_0, Program.NIL.get_tree_hash().hex()
852+
wallet_node_0.wallet_state_manager, wallet_0, Program.NIL.get_tree_hash()
853853
)
854854

855855
did_id = bytes32.zeros

chia/wallet/cat_wallet/cat_wallet.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class CATWallet:
9595
wallet_info_type: ClassVar[type[CATInfo]] = CATInfo
9696

9797
@staticmethod
98-
def default_wallet_name_for_unknown_cat(limitations_program_hash_hex: str) -> str:
99-
return f"CAT {limitations_program_hash_hex[:16]}..."
98+
def default_wallet_name_for_unknown_cat(limitations_program_hash: bytes32) -> str:
99+
return f"CAT {limitations_program_hash.hex()[:16]}..."
100100

101101
@staticmethod
102102
async def create_new_cat_wallet(
@@ -152,7 +152,7 @@ async def create_new_cat_wallet(
152152
# since we didn't yet know the TAIL. Now we know the TAIL, we can update the name
153153
# according to the template name for unknown/new CATs.
154154
if original_name is None:
155-
name = self.default_wallet_name_for_unknown_cat(self.cat_info.limitations_program_hash.hex())
155+
name = self.default_wallet_name_for_unknown_cat(self.cat_info.limitations_program_hash)
156156
await self.set_name(name)
157157

158158
# Change and actual CAT coin
@@ -200,30 +200,27 @@ async def get_or_create_wallet_for_cat(
200200
cls,
201201
wallet_state_manager: WalletStateManager,
202202
wallet: Wallet,
203-
limitations_program_hash_hex: str,
203+
limitations_program_hash: bytes32,
204204
name: str | None = None,
205205
) -> Self:
206206
self = cls()
207207
self.standard_wallet = wallet
208208
self.log = logging.getLogger(__name__)
209209

210-
limitations_program_hash_hex = bytes32.from_hexstr(limitations_program_hash_hex).hex() # Normalize the format
211-
212210
for id, w in wallet_state_manager.wallets.items():
213211
if w.type() == cls.type():
214212
assert isinstance(w, cls)
215-
if w.get_asset_id() == limitations_program_hash_hex:
213+
if w.get_asset_id() == limitations_program_hash:
216214
self.log.warning("Not creating wallet for already existing CAT wallet")
217215
return w
218216

219217
self.wallet_state_manager = wallet_state_manager
220-
if limitations_program_hash_hex in DEFAULT_CATS:
221-
cat_info = DEFAULT_CATS[limitations_program_hash_hex]
218+
if limitations_program_hash.hex() in DEFAULT_CATS:
219+
cat_info = DEFAULT_CATS[limitations_program_hash.hex()]
222220
name = cat_info["name"]
223221
elif name is None:
224-
name = self.default_wallet_name_for_unknown_cat(limitations_program_hash_hex)
222+
name = self.default_wallet_name_for_unknown_cat(limitations_program_hash)
225223

226-
limitations_program_hash = bytes32.from_hexstr(limitations_program_hash_hex)
227224
self.cat_info = cls.wallet_info_type(limitations_program_hash, None)
228225
info_as_string = bytes(self.cat_info).hex()
229226
self.wallet_info = await wallet_state_manager.user_store.create_wallet(name, self.wallet_type, info_as_string)
@@ -274,7 +271,7 @@ async def create_from_puzzle_info(
274271
return await cls.get_or_create_wallet_for_cat(
275272
wallet_state_manager,
276273
wallet,
277-
puzzle_driver["tail"].hex(),
274+
puzzle_driver["tail"],
278275
name,
279276
)
280277

@@ -356,8 +353,8 @@ async def set_name(self, new_name: str) -> None:
356353
self.wallet_info = new_info
357354
await self.wallet_state_manager.user_store.update_wallet(self.wallet_info)
358355

359-
def get_asset_id(self) -> str:
360-
return bytes(self.cat_info.limitations_program_hash).hex()
356+
def get_asset_id(self) -> bytes32:
357+
return self.cat_info.limitations_program_hash
361358

362359
async def coin_added(
363360
self, coin: Coin, height: uint32, peer: WSChiaConnection, parent_coin_data: CATCoinData | None
@@ -821,12 +818,12 @@ async def save_info(self, cat_info: CATInfo) -> None:
821818
async def match_puzzle_info(self, puzzle_driver: PuzzleInfo) -> bool:
822819
return (
823820
AssetType(puzzle_driver.type()) == AssetType.CAT
824-
and puzzle_driver["tail"] == bytes.fromhex(self.get_asset_id())
821+
and puzzle_driver["tail"] == self.get_asset_id()
825822
and puzzle_driver.also() is None
826823
)
827824

828825
async def get_puzzle_info(self, asset_id: bytes32) -> PuzzleInfo:
829-
return PuzzleInfo({"type": AssetType.CAT.value, "tail": "0x" + self.get_asset_id()})
826+
return PuzzleInfo({"type": AssetType.CAT.value, "tail": "0x" + self.get_asset_id().hex()})
830827

831828
async def get_coins_to_offer(
832829
self,

chia/wallet/cat_wallet/lineage_store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class CATLineageStore:
2121
table_name: str
2222

2323
@classmethod
24-
async def create(cls, db_wrapper: DBWrapper2, asset_id: str) -> CATLineageStore:
24+
async def create(cls, db_wrapper: DBWrapper2, asset_id: bytes32) -> CATLineageStore:
2525
self = cls()
26-
self.table_name = f"lineage_proofs_{asset_id}"
26+
self.table_name = f"lineage_proofs_{asset_id.hex()}"
2727
self.db_wrapper = db_wrapper
2828
async with self.db_wrapper.writer_maybe_transaction() as conn:
2929
await conn.execute(f"CREATE TABLE IF NOT EXISTS {self.table_name}(coin_id text PRIMARY KEY, lineage blob)")

chia/wallet/cat_wallet/r_cat_wallet.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class RCATWallet(CATWallet):
7171
create_new_cat_wallet = None # type: ignore[assignment]
7272

7373
@staticmethod
74-
def default_wallet_name_for_unknown_cat(limitations_program_hash_hex: str) -> str:
75-
return f"Revocable-CAT {limitations_program_hash_hex[:16]}..."
74+
def default_wallet_name_for_unknown_cat(limitations_program_hash: bytes32) -> str:
75+
return f"Revocable-CAT {limitations_program_hash.hex()[:16]}..."
7676

7777
# We need to override this with a different signature.
7878
# It's not immediately clear what is proper here, likely needs a bit of a refactor.
@@ -81,31 +81,28 @@ async def get_or_create_wallet_for_cat( # type: ignore[override]
8181
cls,
8282
wallet_state_manager: WalletStateManager,
8383
wallet: Wallet,
84-
limitations_program_hash_hex: str,
84+
limitations_program_hash: bytes32,
8585
hidden_puzzle_hash: bytes32,
8686
name: str | None = None,
8787
) -> Self:
8888
self = cls()
8989
self.standard_wallet = wallet
9090
self.log = logging.getLogger(__name__)
9191

92-
limitations_program_hash_hex = bytes32.from_hexstr(limitations_program_hash_hex).hex() # Normalize the format
93-
9492
for id, w in wallet_state_manager.wallets.items():
9593
if w.type() == cls.type():
9694
assert isinstance(w, cls)
97-
if w.get_asset_id() == limitations_program_hash_hex:
95+
if w.get_asset_id() == limitations_program_hash:
9896
self.log.warning("Not creating wallet for already existing CAT wallet")
9997
return w
10098

10199
self.wallet_state_manager = wallet_state_manager
102-
if limitations_program_hash_hex in DEFAULT_CATS:
103-
cat_info = DEFAULT_CATS[limitations_program_hash_hex]
100+
if limitations_program_hash.hex() in DEFAULT_CATS:
101+
cat_info = DEFAULT_CATS[limitations_program_hash.hex()]
104102
name = cat_info["name"]
105103
elif name is None:
106-
name = self.default_wallet_name_for_unknown_cat(limitations_program_hash_hex)
104+
name = self.default_wallet_name_for_unknown_cat(limitations_program_hash)
107105

108-
limitations_program_hash = bytes32.from_hexstr(limitations_program_hash_hex)
109106
self.cat_info = cls.wallet_info_type(limitations_program_hash, None, hidden_puzzle_hash)
110107
info_as_string = bytes(self.cat_info).hex()
111108
self.wallet_info = await wallet_state_manager.user_store.create_wallet(name, cls.wallet_type, info_as_string)
@@ -150,7 +147,7 @@ async def create_from_puzzle_info(
150147
return await cls.get_or_create_wallet_for_cat(
151148
wallet_state_manager,
152149
wallet,
153-
puzzle_driver["tail"].hex(),
150+
puzzle_driver["tail"],
154151
bytes32(rev_layer["hidden_puzzle_hash"]),
155152
name,
156153
)
@@ -247,7 +244,7 @@ async def get_puzzle_info(self, asset_id: bytes32) -> PuzzleInfo:
247244
return PuzzleInfo(
248245
{
249246
"type": AssetType.CAT.value,
250-
"tail": "0x" + self.get_asset_id(),
247+
"tail": "0x" + self.get_asset_id().hex(),
251248
"also": {
252249
"type": AssetType.REVOCATION_LAYER.value,
253250
"hidden_puzzle_hash": "0x" + self.cat_info.hidden_puzzle_hash.hex(),

chia/wallet/nft_wallet/nft_wallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ async def make_nft1_offer(
821821
if asset is None:
822822
wallet = wallet_state_manager.main_wallet
823823
else:
824-
wallet = await wallet_state_manager.get_wallet_for_asset_id(asset.hex())
824+
wallet = await wallet_state_manager.get_wallet_for_asset_id(asset)
825825
if asset in royalty_payments:
826826
royalty_amount: int = sum(p.amount for _, p in royalty_payments[asset])
827827
else:
@@ -872,7 +872,7 @@ async def make_nft1_offer(
872872
if asset is None:
873873
wallet = wallet_state_manager.main_wallet
874874
else:
875-
wallet = await wallet_state_manager.get_wallet_for_asset_id(asset.hex())
875+
wallet = await wallet_state_manager.get_wallet_for_asset_id(asset)
876876

877877
# First, sending all the coins to the OFFER_MOD
878878
async with wallet_state_manager.new_action_scope(

0 commit comments

Comments
 (0)