Esempio n. 1
0
    async def get_removals(self,
                           peer: WSChiaConnection,
                           block_i,
                           additions,
                           removals,
                           request_all_removals=False) -> Optional[List[Coin]]:
        assert self.wallet_state_manager is not None
        # Check if we need all removals
        for coin in additions:
            puzzle_store = self.wallet_state_manager.puzzle_store
            record_info: Optional[
                DerivationRecord] = await puzzle_store.get_derivation_record_for_puzzle_hash(
                    coin.puzzle_hash)
            if record_info is not None and record_info.wallet_type == WalletType.COLOURED_COIN:
                # TODO why ?
                request_all_removals = True
                break
            if record_info is not None and record_info.wallet_type == WalletType.DISTRIBUTED_ID:
                request_all_removals = True
                break
        if len(removals) > 0 or request_all_removals:
            if request_all_removals:
                removals_request = wallet_protocol.RequestRemovals(
                    block_i.height, block_i.header_hash, None)
            else:
                removals_request = wallet_protocol.RequestRemovals(
                    block_i.height, block_i.header_hash, removals)
            removals_res: Optional[
                Union[RespondRemovals,
                      RejectRemovalsRequest]] = await peer.request_removals(
                          removals_request)
            if removals_res is None:
                return None
            elif isinstance(removals_res, RespondRemovals):
                validated = self.validate_removals(
                    removals_res.coins,
                    removals_res.proofs,
                    block_i.foliage_transaction_block.removals_root,
                )
                if validated is False:
                    await peer.close()
                    return None
                removed_coins = []
                for _, coins_l in removals_res.coins:
                    if coins_l is not None:
                        removed_coins.append(coins_l)

                return removed_coins
            elif isinstance(removals_res, RejectRemovalsRequest):
                return None
            else:
                return None

        else:
            return []
Esempio n. 2
0
    async def load_backup(self, filename: str):
        try:
            f = open(filename, "r")
            details = f.readline().split(":")
            f.close()
            origin = Coin(bytes.fromhex(details[0]), bytes.fromhex(details[1]),
                          uint64(int(details[2])))
            backup_ids = []
            for d in details[3].split(","):
                backup_ids.append(bytes.fromhex(d))
            num_of_backup_ids_needed = uint64(int(details[5]))
            if num_of_backup_ids_needed > len(backup_ids):
                raise Exception
            innerpuz: Program = Program.from_bytes(bytes.fromhex(details[4]))
            did_info: DIDInfo = DIDInfo(
                origin,
                backup_ids,
                num_of_backup_ids_needed,
                self.did_info.parent_info,
                innerpuz,
                None,
                None,
                None,
                False,
            )
            await self.save_info(did_info, False)
            await self.wallet_state_manager.update_wallet_puzzle_hashes(
                self.wallet_info.id)

            full_puz = did_wallet_puzzles.create_fullpuz(
                innerpuz, origin.name())
            full_puzzle_hash = full_puz.get_tree_hash()
            (
                sub_height,
                header_hash,
            ) = await self.wallet_state_manager.search_blockrecords_for_puzzlehash(
                full_puzzle_hash)
            assert sub_height is not None
            assert header_hash is not None
            full_nodes = self.wallet_state_manager.server.connection_by_type[
                NodeType.FULL_NODE]
            additions: Union[RespondAdditions, RejectAdditionsRequest,
                             None] = None
            for id, node in full_nodes.items():
                request = wallet_protocol.RequestAdditions(
                    sub_height, header_hash, None)
                additions = await node.request_additions(request)
                if additions is not None:
                    break
                if isinstance(additions, RejectAdditionsRequest):
                    continue

            assert additions is not None
            assert isinstance(additions, RespondAdditions)
            # All additions in this block here:
            new_puzhash = await self.get_new_inner_hash()
            new_pubkey = bytes(
                (await self.wallet_state_manager.get_unused_derivation_record(
                    self.wallet_info.id)).pubkey)

            all_parents: bytes32 = set()
            for puzzle_list_coin in additions.coins:
                puzzle_hash, coins = puzzle_list_coin
                for coin in coins:
                    all_parents.add(coin.parent_coin_info)
            parent_info = None
            for puzzle_list_coin in additions.coins:
                puzzle_hash, coins = puzzle_list_coin
                if puzzle_hash == full_puzzle_hash:
                    # our coin
                    for coin in coins:
                        future_parent = LineageProof(
                            coin.parent_coin_info,
                            innerpuz.get_tree_hash(),
                            coin.amount,
                        )
                        await self.add_parent(coin.name(), future_parent,
                                              False)
                        if coin.name() not in all_parents:
                            did_info = DIDInfo(
                                origin,
                                backup_ids,
                                num_of_backup_ids_needed,
                                self.did_info.parent_info,
                                innerpuz,
                                coin,
                                new_puzhash,
                                new_pubkey,
                                False,
                            )
                            await self.save_info(did_info, False)
                            removal_request = wallet_protocol.RequestRemovals(
                                sub_height, header_hash, None)
                            removals_response = await node.request_removals(
                                removal_request)
                            for coin_tuple in removals_response.coins:
                                if coin_tuple[0] == coin.parent_coin_info:
                                    puzzle_solution_request = wallet_protocol.RequestPuzzleSolution(
                                        coin.parent_coin_info, sub_height)
                                    response = await node.request_puzzle_solution(
                                        puzzle_solution_request)
                                    req_puz_sol = response.response
                                    assert req_puz_sol.puzzle is not None
                                    parent_innerpuz = did_wallet_puzzles.get_innerpuzzle_from_puzzle(
                                        req_puz_sol.puzzle)
                                    assert parent_innerpuz is not None
                                    parent_info = LineageProof(
                                        coin_tuple[1].parent_coin_info,
                                        parent_innerpuz.get_tree_hash(),
                                        coin_tuple[1].amount,
                                    )
                                    await self.add_parent(
                                        coin.parent_coin_info, parent_info,
                                        False)
                                    break

            assert parent_info is not None
            return None
        except Exception as e:
            raise e
    uint32(133681371),
)

request_block_header = wallet_protocol.RequestBlockHeader(uint32(3562957314), )

respond_header_block = wallet_protocol.RespondBlockHeader(header_block, )

reject_header_request = wallet_protocol.RejectHeaderRequest(uint32(17867635), )

request_removals = wallet_protocol.RequestRemovals(
    uint32(3500751918),
    bytes32(
        bytes.fromhex(
            "b44bc0e0fce20331a57081107dfd30ef39fc436e6e6ce4f6f0ab8db4f981d114")
    ),
    [
        bytes32(
            bytes.fromhex(
                "ab62cfb2abaf9e1a475b707c3d3de35d6ef4a298b31137802fd9ea47d48ff0d5"
            ))
    ],
)

respond_removals = wallet_protocol.RespondRemovals(
    uint32(461268095),
    bytes32(
        bytes.fromhex(
            "e2db23a6484b05d9ae1033efe8dcfcf5894fc600a6b93b03782fab8dd1cba8a4")
    ),
    [(bytes32(
        bytes.fromhex(