Esempio n. 1
0
    def ap_generate_signed_aggregation_transaction(self):
        list_of_coinsolutions = []
        if self.aggregation_coins is False:  # empty sets evaluate to false in python
            return
        consolidating_coin = self.aggregation_coins.pop()

        pubkey, secretkey = self.get_keys(
            self.temp_coin.puzzle_hash, self.a_pubkey)

        # Spend wallet coin
        puzzle = ap_make_puzzle(self.a_pubkey, bytes(pubkey))
        solution = self.ap_make_solution_mode_2(self.temp_coin.puzzle_hash, consolidating_coin.parent_coin_info,
                                                consolidating_coin.puzzle_hash, consolidating_coin.amount, self.temp_coin.parent_coin_info, self.temp_coin.amount)
        signature = secretkey.sign(ProgramHash(solution))
        list_of_coinsolutions.append(CoinSolution(
            self.temp_coin, clvm.to_sexp_f([puzzle, solution])))

        # Spend consolidating coin
        puzzle = ap_make_aggregation_puzzle(self.temp_coin.puzzle_hash)
        solution = self.ac_make_aggregation_solution(consolidating_coin.name(
        ), self.temp_coin.parent_coin_info, self.temp_coin.amount)
        list_of_coinsolutions.append(CoinSolution(
            consolidating_coin, clvm.to_sexp_f([puzzle, solution])))
        # Spend lock
        puzstring = f"(r (c (q 0x{consolidating_coin.name().hex()}) (q ())))"
        puzzle = Program(binutils.assemble(puzstring))
        solution = Program(binutils.assemble("()"))
        list_of_coinsolutions.append(CoinSolution(Coin(self.temp_coin, ProgramHash(
            puzzle), 0), clvm.to_sexp_f([puzzle, solution])))

        self.temp_coin = Coin(self.temp_coin, self.temp_coin.puzzle_hash,
                              self.temp_coin.amount + consolidating_coin.amount)
        aggsig = BLSSignature.aggregate([signature])
        solution_list = CoinSolutionList(list_of_coinsolutions)
        return SpendBundle(solution_list, aggsig)
Esempio n. 2
0
    def rl_generate_signed_aggregation_transaction(self):
        list_of_coinsolutions = []
        if self.aggregation_coins is False:  # empty sets evaluate to false in python
            return
        consolidating_coin = self.aggregation_coins.pop()

        pubkey, secretkey = self.get_keys(self.rl_coin.puzzle_hash)
        # Spend wallet coin
        puzzle = self.rl_puzzle_for_pk(pubkey.serialize(), self.limit,
                                       self.interval, self.rl_origin,
                                       self.rl_clawback_pk)

        if isinstance(self.rl_parent, Coin):
            solution = self.rl_make_solution_mode_2(
                self.rl_coin.puzzle_hash, consolidating_coin.parent_coin_info,
                consolidating_coin.puzzle_hash, consolidating_coin.amount,
                self.rl_coin.parent_coin_info, self.rl_coin.amount,
                self.rl_parent.amount, self.rl_parent.parent_coin_info)
        else:
            solution = self.rl_make_solution_mode_2(
                self.rl_coin.puzzle_hash, consolidating_coin.parent_coin_info,
                consolidating_coin.puzzle_hash, consolidating_coin.amount,
                self.rl_coin.parent_coin_info, self.rl_coin.amount,
                self.rl_parent["amount"], self.rl_parent["parent_coin_info"])
        signature = BLSPrivateKey(secretkey).sign(ProgramHash(solution))
        list_of_coinsolutions.append(
            CoinSolution(self.rl_coin, clvm.to_sexp_f([puzzle, solution])))

        # Spend consolidating coin
        puzzle = self.rl_make_aggregation_puzzle(self.rl_coin.puzzle_hash)
        solution = self.rl_make_aggregation_solution(
            consolidating_coin.name(), self.rl_coin.parent_coin_info,
            self.rl_coin.amount)
        list_of_coinsolutions.append(
            CoinSolution(consolidating_coin, clvm.to_sexp_f([puzzle,
                                                             solution])))
        # Spend lock
        puzstring = "(r (c (q 0x" + hexlify(
            consolidating_coin.name()).decode('ascii') + ") (q ())))"

        puzzle = Program(binutils.assemble(puzstring))
        solution = Program(binutils.assemble("()"))
        list_of_coinsolutions.append(
            CoinSolution(Coin(self.rl_coin, ProgramHash(puzzle), 0),
                         clvm.to_sexp_f([puzzle, solution])))

        aggsig = BLSSignature.aggregate([signature])
        solution_list = CoinSolutionList(list_of_coinsolutions)

        return SpendBundle(solution_list, aggsig)
Esempio n. 3
0
    def generate_recovery_transaction(self, coins, root_public_key, secret_key,
                                      escrow_duration):
        recovery_pubkey = root_public_key.public_child(
            0).get_public_key().serialize()
        signatures = []
        coin_solutions = []
        secret_key = BLSPrivateKey(secret_key)
        for coin in coins:
            pubkey = self.find_pubkey_for_escrow_puzzle(
                coin, root_public_key, escrow_duration)
            puzzle = self.get_escrow_puzzle_with_params(
                recovery_pubkey, pubkey.serialize(), escrow_duration)

            op_create_coin = ConditionOpcode.CREATE_COIN[0]
            puzzlehash = f'0x' + str(hexbytes(self.get_new_puzzlehash()))
            solution_src = sexp(
                quote(sexp(sexp(op_create_coin, puzzlehash, coin.amount))),
                sexp(), 1)
            solution = Program(binutils.assemble(solution_src))

            puzzle_solution_list = clvm.to_sexp_f([puzzle, solution])
            coin_solution = CoinSolution(coin, puzzle_solution_list)
            coin_solutions.append(coin_solution)

            conditions_dict = conditions_by_opcode(
                conditions_for_solution(puzzle_solution_list))
            for _ in hash_key_pairs_for_conditions_dict(conditions_dict):
                signature = secret_key.sign(_.message_hash)
                signatures.append(signature)

        coin_solution_list = CoinSolutionList(coin_solutions)
        aggsig = BLSSignature.aggregate(signatures)
        spend_bundle = SpendBundle(coin_solution_list, aggsig)
        return spend_bundle
Esempio n. 4
0
 def generate_unsigned_clawback_transaction(self):
     spends = []
     coin = self.latest_clawback_coin
     puzzle = self.rl_puzzle_for_pk(self.rl_receiver_pk, self.clawback_limit, self.clawback_interval, self.clawback_origin, self.clawback_pk)
     solution = self.make_clawback_solution(self.get_new_puzzlehash(), self.latest_clawback_coin.amount)
     spends.append((puzzle, CoinSolution(coin, solution)))
     return spends
Esempio n. 5
0
    def generate_unsigned_transaction_without_recipient(self, amount):
        stake_factor = self.get_stake_factor()
        utxos = self.select_coins(amount)
        spends = []
        output_id = None
        spend_value = sum([coin.amount for coin in utxos])
        change = spend_value - amount
        for coin in utxos:
            puzzle_hash = coin.puzzle_hash

            pubkey, secretkey = self.get_keys(puzzle_hash)
            puzzle = self.get_new_puzzle_with_params(
                pubkey.serialize(), self.get_stake_factor(),
                self.get_escrow_duration())
            if output_id is None:
                primaries = []
                if change > 0:
                    changepuzzlehash = self.get_new_puzzlehash()
                    primaries.append({
                        'puzzlehash': changepuzzlehash,
                        'amount': change
                    })
                solution = make_solution(coin.parent_coin_info,
                                         coin.puzzle_hash,
                                         coin.amount,
                                         stake_factor,
                                         primaries=primaries)
                output_id = True
            else:
                solution = make_solution(coin.parent_coin_info,
                                         coin.puzzle_hash, coin.amount,
                                         stake_factor)
            spends.append((puzzle, CoinSolution(coin, solution)))
        return spends
Esempio n. 6
0
    def generate_unsigned_transaction(self, amount, newpuzzlehash):
        if self.temp_balance < amount:
            return None  # TODO: Should we throw a proper error here, or just return None?
        utxos = self.select_coins(amount)
        spends = []
        output_id = None
        spend_value = sum([coin.amount for coin in utxos])
        change = spend_value - amount
        for coin in utxos:
            puzzle_hash = coin.puzzle_hash

            pubkey, secretkey = self.get_keys(puzzle_hash)
            puzzle = self.puzzle_for_pk(pubkey.serialize())
            if output_id is None:
                primaries = [{'puzzlehash': newpuzzlehash, 'amount': amount}]
                if change > 0:
                    changepuzzlehash = self.get_new_puzzlehash()
                    primaries.append({
                        'puzzlehash': changepuzzlehash,
                        'amount': change
                    })
                    # add change coin into temp_utxo set
                    self.temp_utxos.add(Coin(coin, changepuzzlehash, change))
                solution = make_solution(primaries=primaries)
                output_id = sha256(coin.name() + newpuzzlehash)
            else:
                solution = make_solution(consumed=[coin.name()])
            spends.append((puzzle, CoinSolution(coin, solution)))
        self.temp_balance -= amount
        return spends
Esempio n. 7
0
    def ap_generate_unsigned_transaction(self, puzzlehash_amount_list):
        # we only have/need one coin in this wallet at any time - this code can be improved
        spends = []
        coin = self.temp_coin
        puzzle_hash = coin.puzzle_hash

        pubkey, secretkey = self.get_keys(puzzle_hash, self.a_pubkey)
        puzzle = ap_make_puzzle(self.a_pubkey, pubkey.serialize())
        solution = self.ap_make_solution_mode_1(
            puzzlehash_amount_list, coin.parent_coin_info, puzzle_hash)
        spends.append((puzzle, CoinSolution(coin, solution)))
        return spends
Esempio n. 8
0
 def sign_clawback_transaction(self, spends: (Program, [CoinSolution]), clawback_pubkey):
     sigs = []
     for puzzle, solution in spends:
         pubkey, secretkey = self.get_keys_pk(clawback_pubkey)
         signature = secretkey.sign(
             ProgramHash(Program(solution.solution)))
         sigs.append(signature)
     aggsig = BLSSignature.aggregate(sigs)
     solution_list = CoinSolutionList(
         [CoinSolution(coin_solution.coin, clvm.to_sexp_f([puzzle, coin_solution.solution])) for
          (puzzle, coin_solution) in spends])
     spend_bundle = SpendBundle(solution_list, aggsig)
     return spend_bundle
Esempio n. 9
0
 def rl_generate_unsigned_transaction(self, to_puzzlehash, amount):
     spends = []
     coin = self.rl_coin
     puzzle_hash = coin.puzzle_hash
     pubkey, secretkey = self.get_keys(puzzle_hash)
     puzzle = self.rl_puzzle_for_pk(bytes(pubkey), self.limit, self.interval, self.rl_origin, self.rl_clawback_pk)
     if isinstance(self.rl_parent, Coin):
         solution = self.solution_for_rl(coin.parent_coin_info, puzzle_hash, coin.amount, to_puzzlehash, amount,
                                     self.rl_parent.parent_coin_info, self.rl_parent.amount)
     else:
         solution = self.solution_for_rl(coin.parent_coin_info, puzzle_hash, coin.amount, to_puzzlehash, amount,
                                         self.rl_parent["parent_coin_info"], self.rl_parent["amount"])
     spends.append((puzzle, CoinSolution(coin, solution)))
     return spends
Esempio n. 10
0
def maximal_solution_for_coin(wallet, index, coin, conditions):
    """
    Create a "maximal" solution for coin. This is an N of N
    solution which doesn't actually work (unless M == N), but
    gives insight to the signers as to which N public keys would
    be necessary.
    """
    delegated_puzzle = puzzle_for_conditions(conditions)
    delegated_solution = solution_for_conditions(conditions)

    pub_keys = wallet.pub_keys_for_index(index)
    n = len(pub_keys)
    maximal_solution = solution_for_delegated_puzzle(
        n, pub_keys, [1] * n, delegated_puzzle, delegated_solution
    )
    return CoinSolution(coin, maximal_solution), pub_keys
Esempio n. 11
0
 def ap_sign_transaction(self, spends: (Program, [CoinSolution]), signatures_from_a):
     sigs = []
     for puzzle, solution in spends:
         pubkey, secretkey = self.get_keys(
             solution.coin.puzzle_hash, self.a_pubkey)
         signature = secretkey.sign(
             ProgramHash(Program(solution.solution)))
         sigs.append(signature)
     for s in signatures_from_a:
         sigs.append(s)
     aggsig = BLSSignature.aggregate(sigs)
     solution_list = CoinSolutionList(
         [CoinSolution(coin_solution.coin, clvm.to_sexp_f([puzzle, coin_solution.solution])) for
          (puzzle, coin_solution) in spends])
     spend_bundle = SpendBundle(solution_list, aggsig)
     return spend_bundle
Esempio n. 12
0
 def sign_transaction(self, spends: (Program, [CoinSolution])):
     sigs = []
     for puzzle, solution in spends:
         pubkey, secretkey = self.get_keys(solution.coin.puzzle_hash)
         code_ = [puzzle, solution.solution]
         sexp = clvm.to_sexp_f(code_)
         conditions_dict = conditions_by_opcode(
             conditions_for_solution(sexp))
         for _ in hash_key_pairs_for_conditions_dict(conditions_dict):
             signature = secretkey.sign(_.message_hash)
             sigs.append(signature)
     aggsig = BLSSignature.aggregate(sigs)
     solution_list = CoinSolutionList(
         [CoinSolution(coin_solution.coin, clvm.to_sexp_f([puzzle, coin_solution.solution])) for
          (puzzle, coin_solution) in spends])
     spend_bundle = SpendBundle(solution_list, aggsig)
     return spend_bundle
Esempio n. 13
0
    def generate_recovery_to_escrow_transaction(self, coin, recovery_pubkey,
                                                pubkey, stake_factor,
                                                escrow_duration):
        solution = make_solution(coin.parent_coin_info,
                                 coin.puzzle_hash,
                                 coin.amount,
                                 stake_factor,
                                 recovery=True)
        puzzle = self.get_new_puzzle_with_params_and_root(
            recovery_pubkey, pubkey, stake_factor, escrow_duration)

        sexp = clvm.to_sexp_f([puzzle, solution])
        destination_puzzle_hash = get_destination_puzzle_hash(sexp)
        staked_amount = math.ceil(coin.amount * (stake_factor - 1))
        spends = self.generate_unsigned_transaction_without_recipient(
            staked_amount)
        spends.append((puzzle, CoinSolution(coin, solution)))
        return spends, destination_puzzle_hash, coin.amount + staked_amount
Esempio n. 14
0
 def cp_sign_transaction(self,
                         spends: (Program, [CoinSolution]),
                         approval=None):
     sigs = []
     for puzzle, solution in spends:
         pubkey, secretkey = self.get_keys(solution.coin.puzzle_hash)
         signature = secretkey.sign(ProgramHash(Program(solution.solution)))
         sigs.append(signature)
     if approval is not None:
         app = BLSSignature(approval)
         sigs.append(app)
     aggsig = BLSSignature.aggregate(sigs)
     solution_list = CoinSolutionList([
         CoinSolution(coin_solution.coin,
                      clvm.to_sexp_f([puzzle, coin_solution.solution]))
         for (puzzle, coin_solution) in spends
     ])
     spend_bundle = SpendBundle(solution_list, aggsig)
     return spend_bundle
Esempio n. 15
0
 def cp_generate_unsigned_transaction(self, new_puzzle_hash, amount, mode):
     outputs = []
     output = new_puzzle_hash, amount
     outputs.append(output)
     change = self.cp_coin.amount - amount
     if change > 0:
         change_output = self.cp_coin.puzzle_hash, change
         outputs.append(change_output)
     spends = []
     puzzle_hash = self.cp_coin.puzzle_hash
     pubkey, secretkey = self.get_keys(puzzle_hash)
     puzzle = self.cp_puzzle(hexbytes(pubkey), self.pubkey_permission,
                             self.unlock_time)
     if mode == 1:
         solution = self.solution_for_cp_solo(outputs)
     else:
         solution = self.solution_for_cp_permission(outputs)
     spends.append((puzzle, CoinSolution(self.cp_coin, solution)))
     return spends
    def generate_unsigned_transaction(self, amount, newpuzzlehash):
        stake_factor = self.get_stake_factor()
        utxos = self.select_coins(amount)
        if utxos is None:
            raise InsufficientFundsError
        spends = []
        output_id = None
        spend_value = sum([coin.amount for coin in utxos])
        change = spend_value - amount
        for coin in utxos:
            puzzle_hash = coin.puzzle_hash

            pubkey, secretkey = self.get_keys(puzzle_hash)
            puzzle = self.get_new_puzzle_with_params(
                bytes(pubkey), stake_factor, self.get_escrow_duration(),
                self.get_duration_type())
            if output_id is None:
                primaries = [{'puzzlehash': newpuzzlehash, 'amount': amount}]
                if change > 0:
                    changepuzzlehash = self.get_new_puzzlehash()
                    primaries.append({
                        'puzzlehash': changepuzzlehash,
                        'amount': change
                    })
                solution = make_solution(coin.parent_coin_info,
                                         coin.puzzle_hash,
                                         coin.amount,
                                         stake_factor,
                                         primaries=primaries)
                output_id = hash_sha256(coin.name() + newpuzzlehash)
            else:
                solution = make_solution(coin.parent_coin_info,
                                         coin.puzzle_hash, coin.amount,
                                         stake_factor)
            spends.append((puzzle, CoinSolution(coin, solution)))
        return spends
Esempio n. 17
0
def build_spend_bundle(coin, solution, keychain=DEFAULT_KEYCHAIN):
    coin_solution = CoinSolution(coin, solution)
    signature = keychain.signature_for_solution(solution)
    return SpendBundle([coin_solution], signature)
Esempio n. 18
0
def finalize_pst(wallet, pst, sigs):
    """
    Return a pair (SpendBundle or None, summary_list).

    If we have a finalized SpendBundle, it's returned, otherwise None,
    The summary_list item is a list of items (coin, hkp_list, sigs_to_use, m)
    which allows the UI to give the end user information about which
    coins still need signatures.

    Note that hkp is short for hash_key_pair (ie. aggsig pair)
    """
    m = wallet.m()
    coin_solutions = []
    sig_dict = sigs_to_aggsig_sig_dict(wallet, pst, sigs)

    all_sigs_to_use = []

    summary_list = []

    for coin_solution in pst.get("coin_solutions"):
        coin, solution = coin_solution.coin, coin_solution.solution
        # run maximal_solution and get conditions
        conditions_dict = conditions_dict_for_solution(solution)
        # look for AGG_SIG conditions
        hkp_list = hash_key_pairs_for_conditions_dict(conditions_dict)
        # see if we have enough info to build signatures
        found_list = []
        sigs_to_use = []
        for aggsig_pair in hkp_list:
            add_me = 0
            if len(sigs_to_use) < m:
                if aggsig_pair in sig_dict:
                    sigs_to_use.append(sig_dict[aggsig_pair])
                    add_me = 1
            found_list.append(add_me)

        all_sigs_to_use.extend(sigs_to_use)

        conditions = pst.get("conditions")
        delegated_puzzle = puzzle_for_conditions(conditions)
        delegated_solution = solution_for_conditions(conditions)

        index = wallet.index_for_puzzle_hash(coin.puzzle_hash, GAP_LIMIT)
        pub_keys = wallet.pub_keys_for_index(index)
        actual_solution = solution_for_delegated_puzzle(
            m, pub_keys, found_list, delegated_puzzle, delegated_solution
        )

        coin_solution = CoinSolution(coin, actual_solution)
        coin_solutions.append(coin_solution)
        summary = (coin, hkp_list, sigs_to_use, m)
        summary_list.append(summary)

    if len(all_sigs_to_use) > 0:
        aggregated_sig = all_sigs_to_use[0].aggregate(all_sigs_to_use)
        spend_bundle = SpendBundle(coin_solutions, aggregated_sig)
        try:
            if validate_spend_bundle_signature(spend_bundle):
                return spend_bundle, summary_list
        except Exception:
            pass

    return None, summary_list
Esempio n. 19
0
def build_spend_bundle(coin, solution, sign_f=DEFAULT_SIGNER):
    coin_solution = CoinSolution(coin, solution)
    signature = signature_for_solution(solution, sign_f)
    return SpendBundle([coin_solution], signature)