def as_make_puzzle(self, as_pubkey_sender, as_pubkey_receiver, as_amount, as_timelock_block, as_secret_hash): as_pubkey_sender_cl = "0x%s" % ( hexlify(as_pubkey_sender).decode('ascii')) as_pubkey_receiver_cl = "0x%s" % ( hexlify(as_pubkey_receiver).decode('ascii')) as_payout_puzzlehash_receiver = ProgramHash( puzzle_for_pk(as_pubkey_receiver)) as_payout_puzzlehash_sender = ProgramHash( puzzle_for_pk(as_pubkey_sender)) payout_receiver = "(c (q 0x%s) (c (q 0x%s) (c (q %d) (q ()))))" % ( hexlify(ConditionOpcode.CREATE_COIN).decode('ascii'), hexlify(as_payout_puzzlehash_receiver).decode('ascii'), as_amount) payout_sender = "(c (q 0x%s) (c (q 0x%s) (c (q %d) (q ()))))" % ( hexlify(ConditionOpcode.CREATE_COIN).decode('ascii'), hexlify(as_payout_puzzlehash_sender).decode('ascii'), as_amount) aggsig_receiver = "(c (q 0x%s) (c (q %s) (c (sha256 (wrap (a))) (q ()))))" % ( hexlify(ConditionOpcode.AGG_SIG).decode('ascii'), as_pubkey_receiver_cl) aggsig_sender = "(c (q 0x%s) (c (q %s) (c (sha256 (wrap (a))) (q ()))))" % ( hexlify( ConditionOpcode.AGG_SIG).decode('ascii'), as_pubkey_sender_cl) receiver_puz = ("((c (i (= (sha256 (f (r (a)))) (q %s)) (q (c " + aggsig_receiver + " (c " + payout_receiver + " (q ())))) (q (x (q 'invalid secret')))) (a))) ) " ) % (as_secret_hash) timelock = "(c (q 0x%s) (c (q %d) (q ()))) " % (hexlify( ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS).decode('ascii'), as_timelock_block) sender_puz = "(c " + aggsig_sender + " (c " + timelock + " (c " + payout_sender + " (q ()))))" as_puz_sender = "((c (i (= (f (a)) (q 77777)) (q " + sender_puz + ") (q (x (q 'not a valid option'))) ) (a)))" as_puz = "((c (i (= (f (a)) (q 33333)) (q " + receiver_puz + " (q " + as_puz_sender + ")) (a)))" return Program(binutils.assemble(as_puz))
def can_generate_puzzle_hash(self, hash): return any( map( lambda child: hash == ProgramHash( puzzle_for_pk( bytes(self.extended_secret_key.public_child(child)))), reversed(range(self.next_address))))
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_created = False 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 = puzzle_for_pk(pubkey) if output_created is False: 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 = self.make_solution(primaries=primaries) output_created = True else: solution = self.make_solution(consumed=[coin.name()]) spends.append((puzzle, CoinSolution(coin, solution))) self.temp_balance -= amount return spends
def get_keys(self, hash, a_pubkey_used=None, b_pubkey_used=None): for child in reversed(range(self.next_address)): pubkey = self.extended_secret_key.public_child(child) if hash == ProgramHash(puzzle_for_pk(bytes(pubkey))): return (pubkey, self.extended_secret_key.private_child(child)) if a_pubkey_used is not None and b_pubkey_used is None: if hash == ProgramHash(ap_make_puzzle(a_pubkey_used, bytes(pubkey))): return (pubkey, self.extended_secret_key.private_child(child)) elif a_pubkey_used is None and b_pubkey_used is not None: if hash == ProgramHash(ap_make_puzzle(bytes(pubkey), b_pubkey_used)): return (pubkey, self.extended_secret_key.private_child(child))
def as_make_puzzle(self, as_pubkey_sender, as_pubkey_receiver, as_amount, as_timelock_block, as_secret_hash): as_pubkey_sender_cl = f"0x{as_pubkey_sender.hex()}" as_pubkey_receiver_cl = f"0x{as_pubkey_receiver.hex()}" as_payout_puzzlehash_receiver = ProgramHash( puzzle_for_pk(as_pubkey_receiver)) as_payout_puzzlehash_sender = ProgramHash( puzzle_for_pk(as_pubkey_sender)) payout_receiver = f"(c (q 0x{ConditionOpcode.CREATE_COIN.hex()}) (c (q 0x{as_payout_puzzlehash_receiver.hex()}) (c (q {as_amount}) (q ()))))" payout_sender = f"(c (q 0x{ConditionOpcode.CREATE_COIN.hex()}) (c (q 0x{as_payout_puzzlehash_sender.hex()}) (c (q {as_amount}) (q ()))))" aggsig_receiver = f"(c (q 0x{ConditionOpcode.AGG_SIG.hex()}) (c (q {as_pubkey_receiver_cl}) (c (sha256tree (a)) (q ()))))" aggsig_sender = f"(c (q 0x{ConditionOpcode.AGG_SIG.hex()}) (c (q {as_pubkey_sender_cl}) (c (sha256tree (a)) (q ()))))" receiver_puz = ( f"((c (i (= (sha256 (f (r (a)))) (q {as_secret_hash})) (q (c " + aggsig_receiver + " (c " + payout_receiver + " (q ())))) (q (x (q 'invalid secret')))) (a))) ) ") timelock = f"(c (q 0x{ConditionOpcode.ASSERT_BLOCK_INDEX_EXCEEDS.hex()}) (c (q {as_timelock_block}) (q ()))) " sender_puz = "(c " + aggsig_sender + " (c " + timelock + " (c " + payout_sender + " (q ()))))" as_puz_sender = "((c (i (= (f (a)) (q 77777)) (q " + sender_puz + ") (q (x (q 'not a valid option'))) ) (a)))" as_puz = "((c (i (= (f (a)) (q 33333)) (q " + receiver_puz + " (q " + as_puz_sender + ")) (a)))" return Program(binutils.assemble(as_puz))
def get_keys(self, hash, as_pubkey_sender=None, as_pubkey_receiver=None, as_amount=None, as_timelock_t=None, as_secret_hash=None): for child in reversed(range(self.next_address)): pubkey = self.extended_secret_key.public_child( child).get_public_key() if hash == ProgramHash(puzzle_for_pk(pubkey.serialize())): return (pubkey, self.extended_secret_key.private_child( child).get_private_key()) elif as_pubkey_sender is not None and as_pubkey_receiver is not None and as_amount is not None and as_timelock_t is not None and as_secret_hash is not None: if hash == ProgramHash( self.as_make_puzzle(as_pubkey_sender, as_pubkey_receiver, as_amount, as_timelock_t, as_secret_hash)): return (pubkey, self.extended_secret_key.private_child( child).get_private_key())
def get_new_puzzle(self): pubkey = self.get_next_public_key().serialize() puzzle = puzzle_for_pk(pubkey) return puzzle
def get_new_puzzle(self): pubkey = bytes(self.get_next_public_key()) puzzle = puzzle_for_pk(pubkey) return puzzle
def puzzle_for_pk(self, pubkey): return puzzle_for_pk(pubkey)
def get_keys(self, hash): for child in range(self.next_address): pubkey = self.extended_secret_key.public_child(child) if hash == ProgramHash(puzzle_for_pk(bytes(pubkey))): return (pubkey, self.extended_secret_key.private_child(child))
def puzzle_program_for_index(index): return p2_delegated_puzzle.puzzle_for_pk(public_key_bytes_for_index(index))
def get_puzzle_for_pk(self, pubkey): puzzle = puzzle_for_pk(pubkey) return puzzle