async def get_new_puzzle(self) -> Program: return puzzle_for_pk( bytes( self.wallet_state_manager.get_unused_derivation_record( self.wallet_info.id ).pubkey ) )
def can_generate_puzzle_hash(self, hash): return any( map( lambda child: hash == puzzle_for_pk( bytes( self.extended_secret_key.public_child(child). get_public_key())).get_tree_hash(), reversed(range(self.next_address)), ))
def get_new_puzzle(self): next_address_index = 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 test_p2_delegated_puzzle_simple(self): key_lookup = KeyTool() payments, conditions = default_payments_and_conditions(1, key_lookup) pk = public_key_for_index(1, key_lookup) puzzle = p2_delegated_puzzle.puzzle_for_pk(pk) solution = p2_delegated_puzzle.solution_for_conditions(conditions) do_test_spend(puzzle, solution, payments, key_lookup)
def test_p2_delegated_puzzle_graftroot(self): key_lookup = KeyTool() payments, conditions = default_payments_and_conditions(1, key_lookup) delegated_puzzle = p2_delegated_conditions.puzzle_for_pk(public_key_for_index(8, key_lookup)) delegated_solution = p2_delegated_conditions.solution_for_conditions(conditions) puzzle_program = p2_delegated_puzzle.puzzle_for_pk(public_key_for_index(1, key_lookup)) solution = p2_delegated_puzzle.solution_for_delegated_puzzle(delegated_puzzle, delegated_solution) do_test_spend(puzzle_program, solution, payments, key_lookup)
def can_generate_puzzle_hash(self, hash): return any( map( lambda child: hash == puzzle_for_pk( bytes( master_sk_to_wallet_sk(self.private_key, uint32(child)).get_g1() ) ).get_tree_hash(), reversed(range(self.next_address)), ) )
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}")
def get_keys(self, puzzle_hash): if puzzle_hash in self.puzzle_pk_cache: child = self.puzzle_pk_cache[puzzle_hash] pubkey = self.extended_secret_key.public_child( child).get_public_key() private = self.extended_secret_key.private_child( child).get_private_key() return pubkey, private else: for child in range(self.next_address): pubkey = self.extended_secret_key.public_child( child).get_public_key() if puzzle_hash == puzzle_for_pk(bytes(pubkey)).get_tree_hash(): return ( pubkey, self.extended_secret_key.private_child( child).get_private_key(), )
def generate_unsigned_transaction( self, amount, newpuzzlehash, coin: Coin, condition_dic: Dict[ConditionOpcode, List[ConditionVarPair]], fee: int = 0, secretkey=None, ): spends = [] spend_value = coin.amount puzzle_hash = coin.puzzle_hash if secretkey is None: pubkey, secretkey = self.get_keys(puzzle_hash) else: pubkey = secretkey.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, newpuzzlehash, int_to_bytes(amount) ) condition_dic[output.opcode].append(output) amount_total = sum( int_from_bytes(cvp.var2) for cvp in condition_dic[ConditionOpcode.CREATE_COIN] ) change = spend_value - amount_total - fee if change > 0: changepuzzlehash = self.get_new_puzzlehash() change_output = ConditionVarPair( ConditionOpcode.CREATE_COIN, changepuzzlehash, int_to_bytes(change) ) condition_dic[output.opcode].append(change_output) solution = self.make_solution(condition_dic) else: solution = self.make_solution(condition_dic) spends.append((puzzle, CoinSolution(coin, solution))) return spends
def puzzle_program_for_index(index): return p2_delegated_puzzle.puzzle_for_pk(public_key_bytes_for_index(index))
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 puzzle_for_pk(self, pubkey: bytes) -> Program: return puzzle_for_pk(pubkey)
secret_key: PrivateKey = PrivateKey.from_seed(bytes([2] * 32)) puzzles = [] solutions = [] private_keys = [] public_keys = [] for i in range(0, 1000): private_key: PrivateKey = master_sk_to_wallet_sk(secret_key, uint32(i)) public_key = private_key.public_key() solution = wallet_tool.make_solution({ ConditionOpcode.ASSERT_MY_COIN_ID: [ ConditionVarPair(ConditionOpcode.ASSERT_MY_COIN_ID, token_bytes(), None) ] }) puzzle = puzzle_for_pk(bytes(public_key)) puzzles.append(puzzle) solutions.append(solution) private_keys.append(private_key) public_keys.append(public_key) # Run Puzzle 1000 times puzzle_start = time.time() clvm_cost = 0 for i in range(0, 1000): cost_run, sexp = run_program(puzzles[i], solutions[i]) clvm_cost += cost_run puzzle_end = time.time() puzzle_time = puzzle_end - puzzle_start print(f"Puzzle_time is: {puzzle_time}")
def create_puzzlehash_for_pk(pub_key: BLSPublicKey) -> bytes32: return puzzle_for_pk(pub_key).get_tree_hash()
def puzzle_for_pk(self, pubkey: bytes): return puzzle_for_pk(pubkey)
async def generate_unsigned_transaction( self, amount: uint64, newpuzzlehash: bytes32, fee: uint64 = uint64(0), origin_id: bytes32 = None, coins: Set[Coin] = None, ) -> List[Tuple[Program, CoinSolution]]: """ Generates a unsigned transaction in form of List(Puzzle, Solutions) """ if coins is None: coins = await self.select_coins(amount + fee) if coins is None: self.log.info("coins is None") return [] self.log.info(f"coins is not None {coins}") spend_value = sum([coin.amount for coin in coins]) change = spend_value - amount - fee spends: List[Tuple[Program, CoinSolution]] = [] output_created = False for coin in coins: self.log.info(f"coin from coins {coin}") # Get keys for puzzle_hash puzzle_hash = coin.puzzle_hash maybe = await self.wallet_state_manager.get_keys(puzzle_hash) if not maybe: self.log.error( f"Wallet couldn't find keys for puzzle_hash {puzzle_hash}" ) return [] # Get puzzle for pubkey pubkey, secretkey = maybe puzzle: Program = puzzle_for_pk(bytes(pubkey)) # Only one coin creates outputs if output_created is False and origin_id is None: primaries = [{"puzzlehash": newpuzzlehash, "amount": amount}] if change > 0: changepuzzlehash = await self.get_new_puzzlehash() primaries.append({"puzzlehash": changepuzzlehash, "amount": change}) if fee > 0: solution = self.make_solution(primaries=primaries, fee=fee) else: solution = self.make_solution(primaries=primaries) output_created = True elif output_created is False and origin_id == coin.name(): primaries = [{"puzzlehash": newpuzzlehash, "amount": amount}] if change > 0: changepuzzlehash = await self.get_new_puzzlehash() primaries.append({"puzzlehash": changepuzzlehash, "amount": change}) if fee > 0: solution = self.make_solution(primaries=primaries, fee=fee) else: solution = self.make_solution(primaries=primaries) output_created = True else: solution = self.make_solution() spends.append((puzzle, CoinSolution(coin, solution))) self.log.info(f"Spends is {spends}") return spends
def throwaway_puzzle_hash(index: int, key_lookup: KeyTool) -> bytes32: return p2_delegated_puzzle.puzzle_for_pk(public_key_for_index(index, key_lookup)).get_tree_hash()
puzzles = [] solutions = [] private_keys = [] public_keys = [] for i in range(0, 1000): private_key: BLSPrivateKey = BLSPrivateKey( extended_secret_key.private_child(i).get_private_key()) public_key = private_key.public_key() solution = wallet_tool.make_solution({ ConditionOpcode.ASSERT_MY_COIN_ID: [ ConditionVarPair(ConditionOpcode.ASSERT_MY_COIN_ID, token_bytes(), None) ] }) puzzle = puzzle_for_pk(public_key) puzzles.append(puzzle) solutions.append(solution) private_keys.append(private_key) public_keys.append(public_key) # Run Puzzle 1000 times puzzle_start = time.time() clvm_cost = 0 for i in range(0, 1000): cost_run, sexp = run_program(puzzles[i], solutions[i]) clvm_cost += cost_run puzzle_end = time.time() puzzle_time = puzzle_end - puzzle_start print(f"Puzzle_time is: {puzzle_time}")
def get_first_puzzle(self): pubkey = master_sk_to_wallet_sk(self.private_key, self.next_address).get_g1() puzzle = puzzle_for_pk(bytes(pubkey)) self.puzzle_pk_cache[puzzle.get_tree_hash()] = 0 return puzzle
def create_puzzlehash_for_pk(pub_key: G1Element) -> bytes32: return puzzle_for_pk(bytes(pub_key)).get_tree_hash()
def puzzle_program_for_index(index: uint32): return p2_delegated_puzzle.puzzle_for_pk( bytes(master_sk_to_wallet_sk(MASTER_KEY, index).get_g1()))
def get_new_puzzle(self): pubkey_a = self.get_next_public_key() pubkey = bytes(pubkey_a) puzzle = puzzle_for_pk(pubkey) self.puzzle_pk_cache[puzzle.get_tree_hash()] = self.next_address - 1 return puzzle