def spendables_for_address(self, address): if address == "mgMy4vmqChGT8XeKPb1zD6RURWsiNmoNvR": script = ScriptPayToAddress( bitcoin_address_to_hash160_sec(address, address_prefix_for_netcode('XTN'))) return [Spendable(coin_value=10000, script=script.script(), tx_out_index=0, tx_hash=b'2'*32)] else: return []
def spendables_for_address(self, address): if address == "mgMy4vmqChGT8XeKPb1zD6RURWsiNmoNvR": script = ScriptPayToAddress( bitcoin_address_to_hash160_sec( address, address_prefix_for_netcode('XTN'))) return [ Spendable(coin_value=10000, script=script.script(), tx_out_index=0, tx_hash=b'2' * 32) ] else: return []
def script_obj_from_address(address, netcodes=None): netcode, key_type, data = netcode_and_type_for_text(address) if key_type == 'pay_to_script': return ScriptPayToScript(hash160=data) if key_type == 'address': return ScriptPayToAddress(hash160=data) raise ValueError("bad text")
def decode_spendables(address, results): spendables = [ Spendable(r['value'], ScriptPayToAddress(address), h2b_rev(r['tx_hash']), r['tx_pos']) for r in results['result'] ] return spendables
def spendables_for_addresses(self, addresses): results = {} for address in addresses: if address == "mgMy4vmqChGT8XeKPb1zD6RURWsiNmoNvR": results[address] =\ [Spendable(coin_value=10000, script=ScriptPayToAddress(bitcoin_address_to_hash160_sec(address, address_prefix_for_netcode('XTN'))).script(), tx_out_index=0, tx_hash=b'2'*32)] return results
def script_obj_from_address(address, netcodes=None): netcode, key_type, data = netcode_and_type_for_text(address, netcodes) if key_type == 'pay_to_script': return ScriptPayToScript(hash160=data) if key_type == 'address': return ScriptPayToAddress(hash160=data) if key_type == 'address_wit': return ScriptPayToAddressWit(version=data[:1], hash160=data[2:]) if key_type == 'pay_to_script_wit': return ScriptPayToScriptWit(version=data[:1], hash256=data[2:]) raise ValueError("bad text")
def address_from_spend(self, spend): script = None """:type: ScriptType""" try: script = ScriptPayToScript.from_script(spend.script) except Exception: script = ScriptPayToAddress.from_script(spend.script) # explicitly call info() because pycoin script.address(netcode) disregards the netcode addr = script.info(self.netcode)['address'] return addr
def spendables_for_address(self, address): if address == "1r1msgrPfqCMRAhg23cPBD9ZXH1UQ6jec": return [ Spendable( coin_value=10000, script=ScriptPayToAddress( bitcoin_address_to_hash160_sec(address)).script(), tx_out_index=0, tx_hash=b'2' * 32) ] else: return []
def create_change(self, address: str, existing_txouts, balance: int): new_txouts = [] change_size = 100 * 1000000 # 100 coins total_out = sum(txo.coin_value for txo in existing_txouts) balance = balance - total_out spendables = self.get_spendables(address, get_all=True) # Prune out spendables that we will spend. for spendable in spendables.copy(): if total_out == 0: continue if total_out < 0: raise Exception(f"total_out is negative. ({total_out})") if spendable.coin_value > total_out: spendables.remove(spendable) break elif total_out > spendable.coin_value: spendables.remove(spendable) total_out -= spendable.coin_value # XXX: Find a way to cleanly unify this. _, _, address_hash160 = netcode_and_type_for_text(address) # 15 TXs in our own wallet should be a good buffer to have. if len(spendables) >= 15: return [] # Create our payment to ourselves if we still have the coins. if (balance - (change_size * len(spendables))) < change_size: print("Warning: Low balance in address.") balance -= (change_size * len(spendables)) for x in range(len(spendables), 50): balance -= change_size if balance < 0: break script_pay = ScriptPayToAddress(hash160=address_hash160).script() new_txouts.append(TxOut(change_size, script_pay)) return new_txouts
def generate_addr_txouts(self, address_hash160, _payload, fee_needed): txs_out = [] payload = io.BytesIO(_payload) # XXX: constants file payload_size = 20 # Create all the payments to the data addresses. # Chunking from https://github.com/vilhelmgray/FamaMonetae/blob/master/famamonetae.py while True: chunk = payload.read(payload_size) chunk_size = len(chunk) # Break once our chunk is smaller than the payload size if chunk_size < payload_size: if chunk_size == 0: break chunk = chunk + (B'\x00') * (payload_size - chunk_size) script_data = ScriptPayToAddress(hash160=chunk).script() txs_out.append(TxOut(self.return_fee, script_data)) return txs_out
def round_change(self, address_hash160: str, txin_amount: int, txout_amount, fee_needed: int): total_payment = txin_amount - txout_amount - fee_needed script_pay = ScriptPayToAddress(hash160=address_hash160).script() return TxOut(total_payment, script_pay), total_payment