async def test_standard_tx(self): # this isn't a real public key, but we don't care public_key = bytes.fromhex( "af949b78fa6a957602c3593a3d6cb7711e08720415dad83" "1ab18adacaa9b27ec3dda508ee32e24bc811c0abc5781ae21") puzzle_program = SerializedProgram.from_bytes( p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_pk(public_key)) conditions = binutils.assemble( "((51 0x699eca24f2b6f4b25b16f7a418d0dc4fc5fce3b9145aecdda184158927738e3e 10)" " (51 0x847bb2385534070c39a39cc5dfdc7b35e2db472dc0ab10ab4dec157a2178adbf 0x00cbba106df6))" ) solution_program = SerializedProgram.from_bytes( p2_delegated_puzzle_or_hidden_puzzle.solution_for_conditions( conditions)) time_start = time.time() total_cost = 0 for i in range(0, 1000): cost, result = puzzle_program.run_with_cost(solution_program) total_cost += cost time_end = time.time() duration = time_end - time_start log.info(f"Time spent: {duration}") assert duration < 3
def generate_unsigned_transaction( self, amount: uint64, new_puzzle_hash: bytes32, coin: Coin, condition_dic: Dict[ConditionOpcode, List[ConditionVarPair]], fee: int = 0, secret_key=None, ) -> List[CoinSolution]: spends = [] spend_value = coin.amount puzzle_hash = coin.puzzle_hash if secret_key is None: secret_key = self.get_private_key_for_puzzle_hash(puzzle_hash) pubkey = secret_key.get_g1() puzzle = puzzle_for_pk(bytes(pubkey)) if ConditionOpcode.CREATE_COIN not in condition_dic: condition_dic[ConditionOpcode.CREATE_COIN] = [] output = ConditionVarPair(ConditionOpcode.CREATE_COIN, [new_puzzle_hash, int_to_bytes(amount)]) condition_dic[output.opcode].append(output) amount_total = sum(int_from_bytes(cvp.vars[1]) for cvp in condition_dic[ConditionOpcode.CREATE_COIN]) change = spend_value - amount_total - fee if change > 0: change_puzzle_hash = self.get_new_puzzlehash() change_output = ConditionVarPair(ConditionOpcode.CREATE_COIN, [change_puzzle_hash, int_to_bytes(change)]) condition_dic[output.opcode].append(change_output) solution = self.make_solution(condition_dic) else: solution = self.make_solution(condition_dic) puzzle_solution_pair = Program.to([puzzle, solution]) spends.append(CoinSolution(coin, puzzle_solution_pair)) return spends
def get_new_puzzle(self) -> bytes32: next_address_index: uint32 = self.get_next_address_index() pubkey = master_sk_to_wallet_sk(self.private_key, next_address_index).get_g1() self.pubkey_num_lookup[bytes(pubkey)] = next_address_index puzzle = puzzle_for_pk(bytes(pubkey)) self.puzzle_pk_cache[puzzle.get_tree_hash()] = next_address_index return puzzle
def get_private_key_for_puzzle_hash(self, puzzle_hash) -> PrivateKey: if puzzle_hash in self.puzzle_pk_cache: child = self.puzzle_pk_cache[puzzle_hash] private = master_sk_to_wallet_sk(self.private_key, uint32(child)) # pubkey = private.get_g1() return private else: for child in range(self.next_address): pubkey = master_sk_to_wallet_sk(self.private_key, uint32(child)).get_g1() if puzzle_hash == puzzle_for_pk(bytes(pubkey)).get_tree_hash(): return master_sk_to_wallet_sk(self.private_key, uint32(child)) raise ValueError(f"Do not have the keys for puzzle hash {puzzle_hash}")
async def test_strict_mode(self): wallet_tool = bt.get_pool_wallet_tool() ph = wallet_tool.get_new_puzzlehash() num_blocks = 3 blocks = bt.get_consecutive_blocks(num_blocks, [], guarantee_transaction_block=True, pool_reward_puzzle_hash=ph, farmer_reward_puzzle_hash=ph) coinbase = None for coin in blocks[2].get_included_reward_coins(): if coin.puzzle_hash == ph: coinbase = coin break assert coinbase is not None spend_bundle = wallet_tool.generate_signed_transaction( coinbase.amount, BURN_PUZZLE_HASH, coinbase, ) assert spend_bundle is not None pk = bytes.fromhex( "88bc9360319e7c54ab42e19e974288a2d7a817976f7633f4b43f36ce72074e59c4ab8ddac362202f3e366f0aebbb6280" ) puzzle = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_pk(pk) disassembly = binutils.disassemble(puzzle) program = SerializedProgram.from_bytes( binutils.assemble( f"(q . (((0x3d2331635a58c0d49912bc1427d7db51afe3f20a7b4bcaffa17ee250dcbcbfaa 300)" f" ({disassembly} (() (q . ((65 '00000000000000000000000000000000' 0x0cbba106e000))) ())))))" ).as_bin()) error, npc_list, cost = get_name_puzzle_conditions(program, True) assert error is not None error, npc_list, cost = get_name_puzzle_conditions(program, False) assert error is None coin_name = npc_list[0].coin_name error, puzzle, solution = get_puzzle_and_solution_for_coin( program, coin_name) assert error is None
def puzzle_for_pk(self, pubkey: bytes) -> Program: return puzzle_for_pk(pubkey)
def puzzle_program_for_index(index: uint32): return puzzle_for_pk( bytes(master_sk_to_wallet_sk(MASTER_KEY, index).get_g1()))
def puzzle_hash_for_index(index: int, puzzle_hash_db: dict) -> bytes: public_key = bytes(int_to_public_key(index)) puzzle = puzzle_for_pk(public_key) puzzle_hash = puzzle.get_tree_hash() puzzle_hash_db[puzzle_hash] = puzzle return puzzle_hash
def puzzle_hash_for_index(index: int, puzzle_hash_db: dict) -> bytes: public_key = bytes(blspy.G1Element.generator() * index) puzzle = puzzle_for_pk(public_key) puzzle_hash = puzzle.get_tree_hash() puzzle_hash_db[puzzle_hash] = puzzle return puzzle_hash
async def get_new_puzzle(self) -> Program: dr = await self.wallet_state_manager.get_unused_derivation_record( self.id()) return puzzle_for_pk(bytes(dr.pubkey))
async def puzzle_for_puzzle_hash(self, puzzle_hash: bytes32) -> Program: public_key = await self.hack_populate_secret_key_for_puzzle_hash( puzzle_hash) return puzzle_for_pk(bytes(public_key))
def create_puzzlehash_for_pk(pub_key: G1Element) -> bytes32: return puzzle_for_pk(bytes(pub_key)).get_tree_hash()