Skip to content

Commit

Permalink
WIP: make DID wallet not derive its own keys, but use the standard wa…
Browse files Browse the repository at this point in the history
…llet for inner puzzles [skip ci]
  • Loading branch information
arvidn committed Jul 9, 2024
1 parent bb01ab5 commit dfc692b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
18 changes: 5 additions & 13 deletions chia/_tests/wallet/did_wallet/test_did.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ async def test_creation_from_backup_file(self, self_hostname, three_wallet_nodes
coin = await did_wallet_1.get_coin()
assert did_wallet_2.did_info.temp_coin == coin
newpuzhash = await did_wallet_2.get_new_did_inner_hash()
pubkey = bytes(
(await did_wallet_2.wallet_state_manager.get_unused_derivation_record(did_wallet_2.wallet_info.id)).pubkey
)
_, pubkey = await did_wallet_2.standard_wallet.get_new_puzzlehash_and_key()
message_tx, message_spend_bundle, attest_data = await did_wallet_0.create_attestment(
did_wallet_2.did_info.temp_coin.name(), newpuzhash, pubkey, DEFAULT_TX_CONFIG
)
Expand Down Expand Up @@ -368,9 +366,7 @@ async def test_did_recovery_with_multiple_backup_dids(self, self_hostname, two_w
)
assert did_wallet_4.get_name() == "Profile 2"

pubkey = (
await did_wallet_4.wallet_state_manager.get_unused_derivation_record(did_wallet_2.wallet_info.id)
).pubkey
_, pubkey = await did_wallet_4.standard_wallet.get_new_puzzlehash_and_key()
new_ph = did_wallet_4.did_info.temp_puzhash
message_tx, message_spend_bundle, attest1 = await did_wallet.create_attestment(
coin.name(), new_ph, pubkey, DEFAULT_TX_CONFIG
Expand Down Expand Up @@ -467,7 +463,7 @@ async def test_did_recovery_with_empty_set(self, self_hostname, two_wallet_nodes
await time_out_assert(15, did_wallet.get_unconfirmed_balance, 101)
coin = await did_wallet.get_coin()
info = Program.to([])
pubkey = (await did_wallet.wallet_state_manager.get_unused_derivation_record(did_wallet.wallet_info.id)).pubkey
_, pubkey = await did_wallet.standard_wallet.get_new_puzzlehash_and_key()
with pytest.raises(Exception): # We expect a CLVM 80 error for this test
await did_wallet.recovery_spend(coin, ph, info, pubkey, SpendBundle([], AugSchemeMPL.aggregate([])))

Expand Down Expand Up @@ -640,9 +636,7 @@ async def test_did_attest_after_recovery(self, self_hostname, two_wallet_nodes,
)
new_ph = await did_wallet_3.get_new_did_inner_hash()
coin = await did_wallet_2.get_coin()
pubkey = (
await did_wallet_3.wallet_state_manager.get_unused_derivation_record(did_wallet_3.wallet_info.id)
).pubkey
_, pubkey = await did_wallet_3.standard_wallet.get_new_puzzlehash_and_key()
await time_out_assert(15, did_wallet.get_confirmed_balance, 101)
message_tx, message_spend_bundle, attest_data = await did_wallet.create_attestment(
coin.name(), new_ph, pubkey, DEFAULT_TX_CONFIG
Expand Down Expand Up @@ -684,9 +678,7 @@ async def test_did_attest_after_recovery(self, self_hostname, two_wallet_nodes,
)
coin = await did_wallet.get_coin()
new_ph = await did_wallet_4.get_new_did_inner_hash()
pubkey = (
await did_wallet_4.wallet_state_manager.get_unused_derivation_record(did_wallet_4.wallet_info.id)
).pubkey
_, pubkey = await did_wallet_4.standard_wallet.get_new_puzzlehash_and_key()
message_tx, message_spend_bundle, attest1 = await did_wallet_3.create_attestment(
coin.name(), new_ph, pubkey, DEFAULT_TX_CONFIG
)
Expand Down
36 changes: 13 additions & 23 deletions chia/wallet/did_wallet/did_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,17 @@ async def coin_added(self, coin: Coin, _: uint32, peer: WSChiaConnection, parent
return
self.log.info(f"DID wallet has been notified that coin was added: {coin.name()}:{coin}")
inner_puzzle = await self.inner_puzzle_for_did_puzzle(coin.puzzle_hash)
print(f"inner puzzle:\n{inner_puzzle}")
# inner_puzzle = did_data.inner_puzzle
print(f"DID data inner puzzle:\n{did_data.inner_puzzle}")
assert inner_puzzle == did_data.inner_puzzle
# Check inner puzzle consistency
assert self.did_info.origin_coin is not None

# TODO: if not the first singleton, and solution mode == recovery
if not self._coin_is_first_singleton(coin):
full_puzzle = create_singleton_puzzle(inner_puzzle, self.did_info.origin_coin.name())
# ERROR, THIS ASSERT FAILS
assert full_puzzle.get_tree_hash() == coin.puzzle_hash
if self.did_info.temp_coin is not None:
self.wallet_state_manager.state_changed("did_coin_added", self.wallet_info.id)
Expand Down Expand Up @@ -449,27 +454,16 @@ def create_backup(self) -> str:
output_str += f":{self.did_info.metadata}"
return output_str

async def load_parent(self, did_info: DIDInfo):
async def load_parent(self, did_info: DIDInfo) -> None:
"""
Load the parent info when importing a DID
:param did_info: DID info
:return:
"""
# full_puz = did_wallet_puzzles.create_fullpuz(innerpuz, origin.name())
# All additions in this block here:

new_pubkey = (await self.wallet_state_manager.get_unused_derivation_record(self.wallet_info.id)).pubkey
new_puzhash = puzzle_for_pk(new_pubkey).get_tree_hash()
parent_info = None
assert did_info.origin_coin is not None
assert did_info.current_inner is not None
new_did_inner_puzhash = did_wallet_puzzles.get_inner_puzhash_by_p2(
p2_puzhash=new_puzhash,
recovery_list=did_info.backup_ids,
num_of_backup_ids_needed=did_info.num_of_backup_ids_needed,
launcher_id=did_info.origin_coin.name(),
metadata=did_wallet_puzzles.metadata_to_program(json.loads(self.did_info.metadata)),
)
new_did_inner_puzhash, new_pubkey = await self.standard_wallet.get_new_puzzlehash_and_key()
wallet_node = self.wallet_state_manager.wallet_node
parent_coin: Coin = did_info.origin_coin
while True:
Expand All @@ -495,7 +489,7 @@ async def load_parent(self, did_info: DIDInfo):
current_inner=did_info.current_inner,
temp_coin=child_coin,
temp_puzhash=new_did_inner_puzhash,
temp_pubkey=bytes(new_pubkey),
temp_pubkey=new_pubkey,
sent_recovery_transaction=did_info.sent_recovery_transaction,
metadata=did_info.metadata,
)
Expand Down Expand Up @@ -545,11 +539,6 @@ def puzzle_hash_for_pk(self, pubkey: G1Element) -> bytes32:
)
return create_singleton_puzzle_hash(innerpuz_hash, origin_coin_name)

async def get_new_puzzle(self) -> Program:
return self.puzzle_for_pk(
(await self.wallet_state_manager.get_unused_derivation_record(self.wallet_info.id)).pubkey
)

def get_my_DID(self) -> str:
assert self.did_info.origin_coin is not None
core = self.did_info.origin_coin.name()
Expand Down Expand Up @@ -1099,8 +1088,7 @@ async def recovery_spend(
return [did_record]

async def get_new_p2_inner_hash(self) -> bytes32:
puzzle = await self.get_new_p2_inner_puzzle()
return puzzle.get_tree_hash()
return await self.standard_wallet.get_new_puzzlehash()

async def get_new_p2_inner_puzzle(self) -> Program:
return await self.standard_wallet.get_new_puzzle()
Expand Down Expand Up @@ -1145,6 +1133,7 @@ async def inner_puzzle_for_did_puzzle(self, did_hash: bytes32) -> Program:
record: DerivationRecord = await self.wallet_state_manager.puzzle_store.get_derivation_record_for_puzzle_hash(
did_hash
)
assert record is None
assert self.did_info.origin_coin is not None
assert self.did_info.current_inner is not None
uncurried_args = uncurry_innerpuz(self.did_info.current_inner)
Expand All @@ -1155,6 +1144,7 @@ async def inner_puzzle_for_did_puzzle(self, did_hash: bytes32) -> Program:
record = await self.wallet_state_manager.puzzle_store.get_derivation_record_for_puzzle_hash(
p2_puzzle.get_tree_hash()
)
assert record is not None
if not (self.did_info.num_of_backup_ids_needed > 0 and len(self.did_info.backup_ids) == 0):
# We have the recovery list, don't reset it
old_recovery_list_hash = None
Expand All @@ -1169,7 +1159,7 @@ async def inner_puzzle_for_did_puzzle(self, did_hash: bytes32) -> Program:
)
return inner_puzzle

def get_parent_for_coin(self, coin) -> Optional[LineageProof]:
def get_parent_for_coin(self, coin: Coin) -> Optional[LineageProof]:
parent_info = None
for name, ccparent in self.did_info.parent_info:
if name == coin.parent_coin_info:
Expand Down Expand Up @@ -1461,7 +1451,7 @@ def deserialize_backup_data(backup_data: str) -> DIDInfo:
return did_info

def require_derivation_paths(self) -> bool:
return True
return False

async def get_coin(self) -> Coin:
spendable_coins: Set[WalletCoinRecord] = await self.wallet_state_manager.get_spendable_coins_for_wallet(
Expand Down
4 changes: 4 additions & 0 deletions chia/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ async def get_new_puzzlehash(self) -> bytes32:
puzhash = (await self.wallet_state_manager.get_unused_derivation_record(self.id())).puzzle_hash
return puzhash

async def get_new_puzzlehash_and_key(self) -> Tuple[bytes32, bytes]:
dr = await self.wallet_state_manager.get_unused_derivation_record(self.id())
return (dr.puzzle_hash, dr.pubkey if isinstance(dr.pubkey, bytes) else bytes(dr.pubkey))

def make_solution(
self,
primaries: List[Payment],
Expand Down

0 comments on commit dfc692b

Please sign in to comment.