Replace NFT_HRP and DID_HRP with AddressType (#12711)

* Replaced DID_HRP with AddressType.DID.hrp()

* Replaced NFT_HRP with AddressType.NFT.hrp()
This commit is contained in:
Jeff 2022-08-01 09:19:49 -07:00 committed by GitHub
parent baaa77b71d
commit 0fc63d62d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 35 deletions

View File

@ -37,10 +37,9 @@ from chia.wallet.derive_keys import (
master_sk_to_singleton_owner_sk,
match_address_to_sk,
)
from chia.wallet.did_wallet.did_info import DID_HRP
from chia.wallet.did_wallet.did_wallet import DIDWallet
from chia.wallet.nft_wallet import nft_puzzles
from chia.wallet.nft_wallet.nft_info import NFT_HRP, NFTInfo
from chia.wallet.nft_wallet.nft_info import NFTInfo
from chia.wallet.nft_wallet.nft_puzzles import get_metadata_and_phs
from chia.wallet.nft_wallet.nft_wallet import NFTWallet, NFTCoinInfo
from chia.wallet.nft_wallet.uncurry_nft import UncurriedNFT
@ -50,6 +49,7 @@ from chia.wallet.rl_wallet.rl_wallet import RLWallet
from chia.wallet.trade_record import TradeRecord
from chia.wallet.trading.offer import Offer
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.util.address_type import AddressType
from chia.wallet.util.transaction_type import TransactionType
from chia.wallet.util.wallet_types import AmountWithPuzzlehash, WalletType
from chia.wallet.wallet_info import WalletInfo
@ -535,7 +535,9 @@ class WalletRpcApi:
uint64(request.get("fee", 0)),
)
my_did_id = encode_puzzle_hash(bytes32.fromhex(did_wallet.get_my_DID()), DID_HRP)
my_did_id = encode_puzzle_hash(
bytes32.fromhex(did_wallet.get_my_DID()), AddressType.DID.hrp(self.service.config)
)
await NFTWallet.create_new_nft_wallet(
wallet_state_manager,
main_wallet,
@ -1196,7 +1198,7 @@ class WalletRpcApi:
async def did_get_did(self, request) -> EndpointResult:
wallet_id = uint32(request["wallet_id"])
wallet: DIDWallet = self.service.wallet_state_manager.wallets[wallet_id]
my_did: str = encode_puzzle_hash(bytes32.fromhex(wallet.get_my_DID()), DID_HRP)
my_did: str = encode_puzzle_hash(bytes32.fromhex(wallet.get_my_DID()), AddressType.DID.hrp(self.service.config))
async with self.service.wallet_state_manager.lock:
coins = await wallet.select_coins(uint64(1))
if coins is None or coins == set():
@ -1211,7 +1213,7 @@ class WalletRpcApi:
recovery_list = wallet.did_info.backup_ids
recovery_dids = []
for backup_id in recovery_list:
recovery_dids.append(encode_puzzle_hash(backup_id, DID_HRP))
recovery_dids.append(encode_puzzle_hash(backup_id, AddressType.DID.hrp(self.service.config)))
return {
"success": True,
"wallet_id": wallet_id,
@ -1297,7 +1299,9 @@ class WalletRpcApi:
async def did_get_information_needed_for_recovery(self, request) -> EndpointResult:
wallet_id = uint32(request["wallet_id"])
did_wallet: DIDWallet = self.service.wallet_state_manager.wallets[wallet_id]
my_did = encode_puzzle_hash(bytes32.from_hexstr(did_wallet.get_my_DID()), DID_HRP)
my_did = encode_puzzle_hash(
bytes32.from_hexstr(did_wallet.get_my_DID()), AddressType.DID.hrp(self.service.config)
)
# TODO: this ignore should be dealt with
coin_name = did_wallet.did_info.temp_coin.name().hex() # type: ignore[union-attr]
return {
@ -1313,7 +1317,9 @@ class WalletRpcApi:
async def did_get_current_coin_info(self, request) -> EndpointResult:
wallet_id = uint32(request["wallet_id"])
did_wallet: DIDWallet = self.service.wallet_state_manager.wallets[wallet_id]
my_did = encode_puzzle_hash(bytes32.from_hexstr(did_wallet.get_my_DID()), DID_HRP)
my_did = encode_puzzle_hash(
bytes32.from_hexstr(did_wallet.get_my_DID()), AddressType.DID.hrp(self.service.config)
)
did_coin_threeple = await did_wallet.get_info_for_recovery()
assert my_did is not None
assert did_coin_threeple is not None
@ -1457,7 +1463,7 @@ class WalletRpcApi:
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)
did_id = encode_puzzle_hash(did_bytes, AddressType.DID.hrp(self.service.config))
return {"success": True, "did_id": None if len(did_id) == 0 else did_id}
return {"success": False, "error": f"Wallet {wallet_id} not found"}
@ -1480,7 +1486,7 @@ class WalletRpcApi:
did_nft_wallets.append(
{
"wallet_id": wallet.id(),
"did_id": encode_puzzle_hash(nft_wallet_did, DID_HRP),
"did_id": encode_puzzle_hash(nft_wallet_did, AddressType.DID.hrp(self.service.config)),
"did_wallet_id": did_wallet_id,
}
)
@ -1510,7 +1516,7 @@ class WalletRpcApi:
nft_wallet: NFTWallet = self.service.wallet_state_manager.wallets[wallet_id]
try:
nft_coin_id = request["nft_coin_id"]
if nft_coin_id.startswith(NFT_HRP):
if nft_coin_id.startswith(AddressType.NFT.hrp(self.service.config)):
nft_coin_id = decode_puzzle_hash(nft_coin_id)
else:
nft_coin_id = bytes32.from_hexstr(nft_coin_id)
@ -1539,7 +1545,7 @@ class WalletRpcApi:
if "coin_id" not in request:
return {"success": False, "error": "Coin ID is required."}
coin_id = request["coin_id"]
if coin_id.startswith(NFT_HRP):
if coin_id.startswith(AddressType.NFT.hrp(self.service.config)):
coin_id = decode_puzzle_hash(coin_id)
else:
coin_id = bytes32.from_hexstr(coin_id)
@ -1631,7 +1637,7 @@ class WalletRpcApi:
uri = request["uri"]
key = request["key"]
nft_coin_id = request["nft_coin_id"]
if nft_coin_id.startswith(NFT_HRP):
if nft_coin_id.startswith(AddressType.NFT.hrp(self.service.config)):
nft_coin_id = decode_puzzle_hash(nft_coin_id)
else:
nft_coin_id = bytes32.from_hexstr(nft_coin_id)

View File

@ -8,8 +8,6 @@ from chia.wallet.lineage_proof import LineageProof
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.coin import Coin
DID_HRP = "did:chia:"
@streamable
@dataclass(frozen=True)

View File

@ -13,8 +13,6 @@ LAUNCHER_PUZZLE = load_clvm("singleton_launcher.clvm")
IN_TRANSACTION_STATUS = "IN_TRANSACTION"
DEFAULT_STATUS = "DEFAULT"
NFT_HRP = "nft"
@streamable
@dataclass(frozen=True)

View File

@ -49,7 +49,6 @@ from chia.wallet.derive_keys import (
master_sk_to_wallet_sk_unhardened_intermediate,
_derive_path_unhardened,
)
from chia.wallet.did_wallet.did_info import DID_HRP
from chia.wallet.did_wallet.did_wallet import DIDWallet
from chia.wallet.did_wallet.did_wallet_puzzles import DID_INNERPUZ_MOD, create_fullpuz, match_did_puzzle
from chia.wallet.key_val_store import KeyValStore
@ -64,6 +63,7 @@ from chia.wallet.rl_wallet.rl_wallet import RLWallet
from chia.wallet.settings.user_settings import UserSettings
from chia.wallet.trade_manager import TradeManager
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.util.address_type import AddressType
from chia.wallet.util.compute_hints import compute_coin_hints
from chia.wallet.util.transaction_type import TransactionType
from chia.wallet.util.wallet_sync_utils import last_change_height_cs
@ -774,7 +774,7 @@ class WalletStateManager:
launch_coin.coin,
did_puzzle,
coin_spend,
f"DID {encode_puzzle_hash(launch_id, DID_HRP)}",
f"DID {encode_puzzle_hash(launch_id, AddressType.DID.hrp(self.config))}",
)
wallet_id = did_wallet.id()
wallet_type = WalletType(did_wallet.type())

View File

@ -18,9 +18,9 @@ from chia.types.spend_bundle import SpendBundle
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.address_type import AddressType
from chia.wallet.util.compute_memos import compute_memos
from chia.wallet.util.wallet_types import WalletType
@ -389,7 +389,6 @@ async def test_nft_wallet_rpc_creation_and_list(two_wallet_nodes: Any, trusted:
@pytest.mark.asyncio
async def test_nft_wallet_rpc_update_metadata(two_wallet_nodes: Any, trusted: Any) -> None:
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.wallet.nft_wallet.nft_info import NFT_HRP
num_blocks = 3
full_nodes, wallets, _ = two_wallet_nodes
@ -474,7 +473,9 @@ async def test_nft_wallet_rpc_update_metadata(two_wallet_nodes: Any, trusted: An
)
# add another URI using a bech32m nft_coin_id
await time_out_assert(15, wallet_0.get_pending_change_balance, 0)
nft_coin_id = encode_puzzle_hash(bytes32.from_hexstr(coin["nft_coin_id"]), NFT_HRP)
nft_coin_id = encode_puzzle_hash(
bytes32.from_hexstr(coin["nft_coin_id"]), AddressType.NFT.hrp(api_0.service.config)
)
tr1 = await api_0.nft_add_uri(
{"wallet_id": nft_wallet_0_id, "nft_coin_id": nft_coin_id, "uri": "http://metadata", "key": "mu"}
)
@ -589,7 +590,7 @@ async def test_nft_with_did_wallet_creation(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)
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), AddressType.DID.hrp(wallet_node_0.config))
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)
@ -706,8 +707,6 @@ async def test_nft_with_did_wallet_creation(two_wallet_nodes: Any, trusted: Any)
)
@pytest.mark.asyncio
async def test_nft_rpc_mint(two_wallet_nodes: Any, trusted: Any) -> None:
from chia.wallet.did_wallet.did_info import DID_HRP
num_blocks = 3
full_nodes, wallets, _ = two_wallet_nodes
full_node_api: FullNodeSimulator = full_nodes[0]
@ -754,7 +753,7 @@ async def test_nft_rpc_mint(two_wallet_nodes: Any, trusted: Any) -> None:
for _ in range(1, num_blocks):
await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(ph))
await time_out_assert(15, wallet_0.get_pending_change_balance, 0)
did_id = encode_puzzle_hash(bytes32.from_hexstr(did_wallet.get_my_DID()), DID_HRP)
did_id = encode_puzzle_hash(bytes32.from_hexstr(did_wallet.get_my_DID()), AddressType.DID.hrp(wallet_node_0.config))
res = await api_0.create_new_wallet(dict(wallet_type="nft_wallet", name="NFT WALLET 1", did_id=did_id))
assert isinstance(res, dict)
@ -872,7 +871,7 @@ 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)
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), AddressType.DID.hrp(wallet_node_0.config))
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)
@ -1023,7 +1022,7 @@ 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)
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), AddressType.DID.hrp(wallet_node_0.config))
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)
@ -1148,7 +1147,7 @@ async def test_nft_set_did(two_wallet_nodes: Any, trusted: Any) -> None:
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)
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), AddressType.DID.hrp(wallet_node_0.config))
res = await api_0.create_new_wallet(dict(wallet_type="nft_wallet", name="NFT WALLET 1"))
assert isinstance(res, dict)
@ -1220,7 +1219,7 @@ async def test_nft_set_did(two_wallet_nodes: Any, trusted: Any) -> None:
# Test set DID1 -> DID2
hex_did_id = did_wallet1.get_my_DID()
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), DID_HRP)
hmr_did_id = encode_puzzle_hash(bytes32.from_hexstr(hex_did_id), AddressType.DID.hrp(wallet_node_0.config))
resp = await api_0.nft_set_nft_did(
dict(wallet_id=nft_wallet_1_id, did_id=hmr_did_id, nft_coin_id=nft_coin_id.hex())
)

View File

@ -39,6 +39,7 @@ from chia.wallet.nft_wallet.nft_wallet import NFTWallet
from chia.wallet.trading.trade_status import TradeStatus
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.transaction_sorting import SortKey
from chia.wallet.util.address_type import AddressType
from chia.wallet.util.compute_memos import compute_memos
from chia.wallet.util.wallet_types import WalletType
from chia.wallet.wallet import Wallet
@ -767,8 +768,6 @@ async def test_offer_endpoints(wallet_rpc_environment: WalletRpcTestEnvironment)
@pytest.mark.asyncio
async def test_did_endpoints(wallet_rpc_environment: WalletRpcTestEnvironment):
from chia.wallet.did_wallet.did_info import DID_HRP
env: WalletRpcTestEnvironment = wallet_rpc_environment
wallet_1: Wallet = env.wallet_1.wallet
@ -858,7 +857,10 @@ async def test_did_endpoints(wallet_rpc_environment: WalletRpcTestEnvironment):
)
)
did_wallet_2: DIDWallet = wallet_2_node.wallet_state_manager.wallets[did_wallets[0].id]
assert encode_puzzle_hash(bytes32.from_hexstr(did_wallet_2.get_my_DID()), DID_HRP) == did_id_0
assert (
encode_puzzle_hash(bytes32.from_hexstr(did_wallet_2.get_my_DID()), AddressType.DID.hrp(wallet_2_node.config))
== did_id_0
)
metadata = json.loads(did_wallet_2.did_info.metadata)
assert metadata["Twitter"] == "Https://test"
@ -866,8 +868,6 @@ async def test_did_endpoints(wallet_rpc_environment: WalletRpcTestEnvironment):
@pytest.mark.asyncio
async def test_nft_endpoints(wallet_rpc_environment: WalletRpcTestEnvironment):
from chia.wallet.nft_wallet.nft_info import NFT_HRP
env: WalletRpcTestEnvironment = wallet_rpc_environment
wallet_1_node: WalletNode = env.wallet_1.node
wallet_1_rpc: WalletRpcClient = env.wallet_1.rpc_client
@ -905,7 +905,9 @@ async def test_nft_endpoints(wallet_rpc_environment: WalletRpcTestEnvironment):
nft_info = (await wallet_1_rpc.get_nft_info(nft_id))["nft_info"]
assert nft_info["nft_coin_id"][2:] == nft_wallet.get_current_nfts()[0].coin.name().hex()
# Test with the bech32m version of nft_id
hmr_nft_id = encode_puzzle_hash(nft_wallet.get_current_nfts()[0].coin.name(), NFT_HRP)
hmr_nft_id = encode_puzzle_hash(
nft_wallet.get_current_nfts()[0].coin.name(), AddressType.NFT.hrp(wallet_1_node.config)
)
nft_info = (await wallet_1_rpc.get_nft_info(hmr_nft_id))["nft_info"]
assert nft_info["nft_coin_id"][2:] == nft_wallet.get_current_nfts()[0].coin.name().hex()