Skip to content

Commit

Permalink
[CHIA-2224] Add some extra safety into create_message_spend (#19153)
Browse files Browse the repository at this point in the history
* Add some extra safety into `create_message_spend`

* Fix a poorly named variable
  • Loading branch information
Quexington authored Jan 27, 2025
1 parent 6f46131 commit 7c01b87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 14 additions & 6 deletions chia/wallet/did_wallet/did_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,20 +765,28 @@ async def create_message_spend(
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])],
Expand Down
14 changes: 12 additions & 2 deletions chia/wallet/did_wallet/did_wallet_puzzles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 7c01b87

Please sign in to comment.