[CHIA-2224] Add some extra safety into create_message_spend (#19153)

* Add some extra safety into `create_message_spend`

* Fix a poorly named variable
This commit is contained in:
Matt Hauff
2025-01-27 09:00:04 -07:00
committed by GitHub
parent 6f46131e5d
commit 7c01b87693
2 changed files with 26 additions and 8 deletions
+14 -6
View File
@@ -765,20 +765,28 @@ class DIDWallet:
assert self.did_info.origin_coin is not None
coin = await self.get_coin()
innerpuz: Program = self.did_info.current_inner
assert (
create_singleton_puzzle(
innerpuz,
self.did_info.origin_coin.name(),
).get_tree_hash()
== coin.puzzle_hash
)
uncurried = did_wallet_puzzles.uncurry_innerpuz(innerpuz)
assert uncurried is not None
p2_puzzle, id_list_hash, num_of_backup_ids_needed, _, metadata = uncurried
# Quote message puzzle & solution
if action_scope.config.tx_config.reuse_puzhash:
new_innerpuzzle_hash = innerpuz.get_tree_hash()
uncurried = did_wallet_puzzles.uncurry_innerpuz(innerpuz)
assert uncurried is not None
p2_ph = uncurried[0].get_tree_hash()
p2_ph = p2_puzzle.get_tree_hash()
else:
p2_ph = await self.standard_wallet.get_puzzle_hash(new=True)
new_innerpuzzle_hash = did_wallet_puzzles.get_inner_puzhash_by_p2(
p2_puzhash=p2_ph,
recovery_list=self.did_info.backup_ids,
num_of_backup_ids_needed=self.did_info.num_of_backup_ids_needed,
recovery_list_hash=bytes32(id_list_hash.as_atom()),
num_of_backup_ids_needed=uint64(num_of_backup_ids_needed.as_int()),
launcher_id=self.did_info.origin_coin.name(),
metadata=did_wallet_puzzles.metadata_to_program(json.loads(self.did_info.metadata)),
metadata=metadata,
)
p2_solution = self.standard_wallet.make_solution(
primaries=[CreateCoin(puzzle_hash=new_innerpuzzle_hash, amount=uint64(coin.amount), memos=[p2_ph])],
+12 -2
View File
@@ -66,10 +66,11 @@ def create_innerpuz(
def get_inner_puzhash_by_p2(
p2_puzhash: bytes32,
recovery_list: list[bytes32],
num_of_backup_ids_needed: uint64,
launcher_id: bytes32,
metadata: Program = Program.to([]),
recovery_list: Optional[list[bytes32]] = None,
recovery_list_hash: Optional[bytes32] = None,
) -> bytes32:
"""
Calculate DID inner puzzle hash based on a P2 puzzle hash
@@ -81,7 +82,16 @@ def get_inner_puzhash_by_p2(
:return: DID inner puzzle hash
"""
backup_ids_hash = shatree_atom_list(recovery_list)
if recovery_list is None and recovery_list_hash is None:
raise ValueError("Cannot construct DID inner puzzle without information about recovery list")
if recovery_list is not None and recovery_list_hash is not None:
raise ValueError("Must only specify recovery information a single way to construct DID inner puzzle")
if recovery_list is not None:
backup_ids_hash = shatree_atom_list(recovery_list)
else:
assert recovery_list_hash is not None
backup_ids_hash = recovery_list_hash
# singleton_struct = (MOD_HASH . (LAUNCHER_ID . LAUNCHER_PUZZLE_HASH))
singleton_struct = shatree_pair(