def puzzle_for_m_of_public_key_list(m, public_key_list): format_tuple = tuple( binutils.disassemble(Program.to(_)) for _ in (puzzle_prog_template, m, public_key_list)) puzzle_src = "((c (q %s) (c (q %s) (c (q %s) (a)))))" % format_tuple puzzle_prog = binutils.assemble(puzzle_src) return Program.to(puzzle_prog)
def name_puzzle_conditions_list(body_program): """ Return a list of tuples of (coin_name, solved_puzzle_hash, conditions_dict) """ try: sexp = Program.to(body_program).run([]) except EvalError: breakpoint() raise ConsensusError(Err.INVALID_BLOCK_SOLUTION, body_program) npc_list = [] for name_solution in sexp.as_iter(): _ = name_solution.as_python() if len(_) != 2: raise ConsensusError(Err.INVALID_COIN_SOLUTION, name_solution) if not isinstance(_[0], bytes) or len(_[0]) != 32: raise ConsensusError(Err.INVALID_COIN_SOLUTION, name_solution) coin_name = CoinName(_[0]) if not isinstance(_[1], list) or len(_[1]) != 2: raise ConsensusError(Err.INVALID_COIN_SOLUTION, name_solution) puzzle_solution_program = name_solution.rest().first() puzzle_program = puzzle_solution_program.first() puzzle_hash = ProgramHash(Program(puzzle_program)) try: conditions_dict = conditions_dict_for_solution( puzzle_solution_program) except EvalError: raise ConsensusError(Err.INVALID_COIN_SOLUTION, coin_name) npc_list.append((coin_name, puzzle_hash, conditions_dict)) return npc_list
def solution_with_hidden_puzzle(hidden_public_key, hidden_puzzle, solution_to_hidden_puzzle): synthetic_public_key = calculate_synthetic_public_key( hidden_public_key, hidden_puzzle) puzzle = puzzle_for_synthetic_public_key(synthetic_public_key) return Program.to([ puzzle, [hidden_public_key, hidden_puzzle, solution_to_hidden_puzzle] ])
def best_solution_program(bundle: SpendBundle): """ This could potentially do a lot of clever and complicated compression optimizations in conjunction with choosing the set of SpendBundles to include. For now, we just quote the solutions we know. """ r = [] for coin_solution in bundle.coin_solutions: entry = [coin_solution.coin.name(), coin_solution.solution] r.append(entry) return Program.to([binutils.assemble("#q"), r])
def conditions_to_sexp(conditions): return Program.to([binutils.assemble("#q"), conditions])
def solution_for_conditions(puzzle_reveal, conditions): return Program.to([puzzle_reveal, [conditions]])
def solution_with_delegated_puzzle(synthetic_public_key, delegated_puzzle, solution): puzzle = puzzle_for_synthetic_public_key(synthetic_public_key) return Program.to([puzzle, [[], delegated_puzzle, solution]])
def solution_for_delegated_puzzle(puzzle_reveal, delegated_solution): return Program.to([puzzle_reveal, delegated_solution])
def solution_for_conditions(puzzle_reveal, conditions): delegated_puzzle = p2_conditions.puzzle_for_conditions(conditions) solution = [] return Program.to([puzzle_reveal, [delegated_puzzle, solution]])
def puzzle_for_pk(public_key): aggsig = ConditionOpcode.AGG_SIG[0] TEMPLATE = ( f"(c (c (q {aggsig}) (c (q 0x%s) (c (sha256tree (f (a))) (q ())))) " f"((c (f (a)) (f (r (a))))))") return Program.to(binutils.assemble(TEMPLATE % public_key.hex()))
def solution_for_conditions(conditions): return Program.to([puzzle_for_conditions(conditions), 0])
def solution_for_puzzle_and_solution(underlying_puzzle, underlying_solution): underlying_puzzle_hash = ProgramHash(underlying_puzzle) puzzle_program = puzzle_for_puzzle_hash(underlying_puzzle_hash) return Program.to([puzzle_program, underlying_solution])
def solution_for_delegated_puzzle(m, public_key_list, selectors, puzzle, solution): puzzle_reveal = puzzle_for_m_of_public_key_list(m, public_key_list) return Program.to([puzzle_reveal, [selectors, puzzle, solution]])
def solution_for_contract(contract, puzzle_parameters, solution_parameters): cost, r = run_program( contract, Program.to((1, (puzzle_parameters, solution_parameters)))) return r
def puzzle_for_contract(contract, puzzle_parameters): env = Program.to([]).cons(Program.to(puzzle_parameters)) cost, r = run_program(contract, env) return Program.to(r)