def bitcoin(public, private, address, amount): coins_from = [] coins_sources = blockchain_info.coin_sources_for_address(public) coins_from.extend(coins_sources) value = sum(cs[-1].coin_value for cs in coins_sources) secret_exponents = [encoding.wif_to_secret_exponent(private)] amount = btc_to_satoshi(amount) coins_to = [] coins_to.append((amount, address)) actual_tx_fee = value - amount if actual_tx_fee < 0: print( "not enough source coins (%s BTC) for destination (%s BTC). Short %s BTC" % (satoshi_to_btc(total_value), satoshi_to_btc(total_spent), satoshi_to_btc(-actual_tx_fee))) return None if actual_tx_fee > 0: coins_to.append((actual_tx_fee, public)) unsigned_tx = UnsignedTx.standard_tx(coins_from, coins_to) solver = SecretExponentSolver(secret_exponents) new_tx = unsigned_tx.sign(solver) s = io.BytesIO() new_tx.stream(s) tx_bytes = s.getvalue() tx_hex = binascii.hexlify(tx_bytes).decode("utf8") return tx_bytes
def create_tx_io(amount, addr, wallet_nodes, change_nodes, fee): txins = [] txouts = [] spendables = [] wifs = [] dust_threshold = Decimal('0.00000543') collected = Decimal(0) single = None assert amount > dust_threshold for op in unspents: op_amount = convention.satoshi_to_btc( op.as_dict()['coin_value'] ) if op_amount >= amount + fee: single = op collected = op_amount if not single: for op in unspents: spendables.append(op) op_amount = convention.satoshi_to_btc( op.as_dict()['coin_value'] ) collected += op_amount if collected >= amount + fee: break else: raise Exception("Insufficient funds!") else: spendables.append(single) for op in spendables: txin = op.tx_in() txins.append( txin ) op_script = serialize.h2b( op.as_dict()['script_hex'] ) hash160 = serialize.h2b( script.tools.opcode_list(op_script)[2] ) prev_addr = Key(hash160=hash160, netcode=NETCODE).address() # from pycoin import networks # address_prefix = networks.address_prefix_for_netcode(NETCODE) # prev_addr = txin.bitcoin_address(address_prefix=address_prefix) for nodes in (wallet_nodes, change_nodes): if prev_addr in nodes['used'].keys(): wifs.append( nodes['used'][prev_addr]['key'].wif() ) change = collected - (amount + fee) amount_btc = convention.btc_to_satoshi(amount) spend = TxOut( amount_btc, standard_tx_out_script(addr) ) txouts.append(spend) if change > dust_threshold: change_addr = get_unused_node(change_nodes, change=True)['key'].address() change_btc = convention.btc_to_satoshi(change) change_txout = TxOut( change_btc, standard_tx_out_script(change_addr) ) txouts.append(change_txout) return (txins, txouts, spendables, wifs)
def main(): parser = argparse.ArgumentParser(description="Create a Bitcoin transaction.") parser.add_argument('-s', "--source-address", help='source Bitcoin address', required=True, nargs="+", metavar='source_address') parser.add_argument('-d', "--destination-address", help='destination Bitcoin address/amount', required=True, metavar='dest_address/amount_in_btc', nargs="+") parser.add_argument('-f', "--wif-file", help='WIF items for source Bitcoin addresses', required=True, metavar="path-to-WIF-values", type=argparse.FileType('r')) args = parser.parse_args() total_value = 0 coins_from = [] for bca in args.source_address: coins_sources = blockchain_info.coin_sources_for_address(bca) coins_from.extend(coins_sources) total_value += sum(cs[-1].coin_value for cs in coins_sources) secret_exponents = [] for l in args.wif_file: print l secret_exponents.append(encoding.wif_to_secret_exponent(l[:-1])) coins_to = [] total_spent = 0 for daa in args.destination_address: address, amount = daa.split("/") amount = btc_to_satoshi(amount) total_spent += amount coins_to.append((amount, address)) actual_tx_fee = total_value - total_spent if actual_tx_fee < 0: print("not enough source coins (%s BTC) for destination (%s BTC). Short %s BTC" % (satoshi_to_btc(total_value), satoshi_to_btc(total_spent), satoshi_to_btc(-actual_tx_fee))) sys.exit(1) print("transaction fee: %s BTC" % satoshi_to_btc(actual_tx_fee)) unsigned_tx = UnsignedTx.standard_tx(coins_from, coins_to) solver = SecretExponentSolver(secret_exponents) new_tx = unsigned_tx.sign(solver) s = io.BytesIO() new_tx.stream(s) tx_bytes = s.getvalue() tx_hex = binascii.hexlify(tx_bytes).decode("utf8") recommended_tx_fee = tx_fee.recommended_fee_for_tx(new_tx) if actual_tx_fee > recommended_tx_fee: print("warning: transaction fee of exceeds expected value of %s BTC" % satoshi_to_btc(recommended_tx_fee)) elif actual_tx_fee < recommended_tx_fee: print("warning: transaction fee lower than (casually calculated) expected value of %s BTC, transaction might not propogate" % satoshi_to_btc(recommended_tx_fee)) print("copy the following hex to http://blockchain.info/pushtx to put the transaction on the network:\n") print(tx_hex)
def listunspent(addresses): try: response = requests.get("http://blockchain.info/unspent?active=" + "|".join(addresses)) if response.status_code == 500 and response.text.lower() == "no free outputs to spend": return None, None elif response.status_code != 200: raise Exception("Bad status code returned from blockchain.info: %s" % response.status_code) unspent_outputs = response.json()['unspent_outputs'] except requests.exceptions.RequestException as e: raise Exception("Problem getting unspent transactions from blockchain.info: %s" % e) #take the returned data to a format compatible with bitcoind's output listunspent = [] for output in unspent_outputs: if 'tx_hash' in output and output['tx_hash'] is not None: output['tx_hash'] = output['tx_hash'][::-1] #reverse string output['tx_hash'] = ''.join([output['tx_hash'][i:i+2][::-1] for i in range(0, len(output['tx_hash']), 2)]) #flip the character pairs within the string script = TxScript(h2b(output['script'])) listunspent.append({ 'address': script.bitcoin_address_for_script(), 'txid': output['tx_hash'], 'vout': output['tx_output_n'], 'scriptPubKey': output['script'], 'amount': float(satoshi_to_btc(output['value'])), 'confirmations': output['confirmations'] }) return listunspent
def main(): parser = argparse.ArgumentParser(description="Create a Bitcoin transaction.", epilog=EPILOG) parser.add_argument('-g', "--generate-unsigned", help='generate unsigned transaction', action='store_true') parser.add_argument('-f', "--private-key-file", help='file containing WIF or BIP0032 private keys', metavar="path-to-file-with-private-keys", type=argparse.FileType('r')) parser.add_argument('-p', "--private-key", help='WIF or BIP0032 private key', metavar="private-key", type=str, nargs="+") parser.add_argument('-c', "--coinbase", help='Create a (bogus) coinbase transaction. For testing purposes. You must include exactly one WIF in this case.', action='store_true') parser.add_argument("txinfo", help='either a hex dump of the unsigned transaction, or a list of bitcoin addresses with optional "/value" if they are destination addresses', nargs="+") args = parser.parse_args() if args.coinbase: new_tx = create_coinbase_tx(parser) tx_hash_hex = b2h(new_tx.hash()) tx_output_index = 0 tx_out_val = str(satoshi_to_btc(new_tx.txs_out[tx_output_index].coin_value)) tx_out_script_hex = b2h(new_tx.txs_out[tx_output_index].script) # product output in the form: # tx_hash_hex/tx_output_index_decimal/tx_out_val/tx_out_script_hex # which can be used as a fake input to a later transaction print("/".join([tx_hash_hex, str(tx_output_index), tx_out_val, tx_out_script_hex])) return unsigned_tx = get_unsigned_tx(parser) actual_tx_fee = check_fees(unsigned_tx) if actual_tx_fee < 0: sys.exit(1) print("transaction fee: %s BTC" % satoshi_to_btc(actual_tx_fee)) if args.generate_unsigned: s = io.BytesIO() unsigned_tx.stream(s) tx_bytes = s.getvalue() tx_hex = b2h(tx_bytes) print(tx_hex) sys.exit(0) secret_exponents = secret_exponents_iterator(args.private_key_file, args.private_key) solver = SecretExponentSolver(secret_exponents) new_tx = unsigned_tx.sign(solver) s = io.BytesIO() new_tx.stream(s) tx_bytes = s.getvalue() tx_hex = b2h(tx_bytes) print("copy the following hex to http://blockchain.info/pushtx to put the transaction on the network:\n") print(tx_hex)
def main(): parser = argparse.ArgumentParser(description="Create a Bitcoin transaction.", epilog=EPILOG) parser.add_argument('-g', "--generate-unsigned", help='generate unsigned transaction', action='store_true') parser.add_argument('-f', "--private-key-file", help='file containing WIF or BIP0032 private keys', metavar="path-to-file-with-private-keys", type=argparse.FileType('r')) parser.add_argument('-p', "--private-key", help='WIF or BIP0032 private key', metavar="private-key", type=str, nargs="+") parser.add_argument('-c', "--coinbase", help='Create a (bogus) coinbase transaction. For testing purposes. You must include exactly one WIF in this case.', action='store_true') parser.add_argument("txinfo", help='either a hex dump of the unsigned transaction, or a list of bitcoin addresses with optional "/value" if they are destination addresses', nargs="+") args = parser.parse_args() if args.coinbase: new_tx = create_coinbase_tx(parser) tx_hash_hex = binascii.hexlify(new_tx.hash()) tx_output_index = 0 tx_out_val = str(satoshi_to_btc(new_tx.txs_out[tx_output_index].coin_value)) tx_out_script_hex = binascii.hexlify(new_tx.txs_out[tx_output_index].script) # product output in the form: # tx_hash_hex/tx_output_index_decimal/tx_out_val/tx_out_script_hex # which can be used as a fake input to a later transaction print("/".join([tx_hash_hex, str(tx_output_index), tx_out_val, tx_out_script_hex])) return unsigned_tx = get_unsigned_tx(parser) actual_tx_fee = check_fees(unsigned_tx) if actual_tx_fee < 0: sys.exit(1) print("transaction fee: %s BTC" % satoshi_to_btc(actual_tx_fee)) if args.generate_unsigned: s = io.BytesIO() unsigned_tx.stream(s) tx_bytes = s.getvalue() tx_hex = binascii.hexlify(tx_bytes).decode("utf8") print(tx_hex) sys.exit(0) secret_exponents = secret_exponents_iterator(args.private_key_file, args.private_key) solver = SecretExponentSolver(secret_exponents) new_tx = unsigned_tx.sign(solver) s = io.BytesIO() new_tx.stream(s) tx_bytes = s.getvalue() tx_hex = binascii.hexlify(tx_bytes).decode("utf8") print("copy the following hex to http://blockchain.info/pushtx to put the transaction on the network:\n") print(tx_hex)
def listaddressgroupings(self): listaddress = [] addresses = self.db.getalladdresses() for address in addresses: listaddress.append([ address['address'], float(satoshi_to_btc(address['balance'])) ]) return [listaddress]
def check_fees(unsigned_tx): total_value, total_spent = calculate_fees(unsigned_tx) actual_tx_fee = total_value - total_spent recommended_tx_fee = tx_fee.recommended_fee_for_tx(unsigned_tx) if actual_tx_fee > recommended_tx_fee: print("warning: transaction fee of exceeds expected value of %s BTC" % satoshi_to_btc(recommended_tx_fee)) elif actual_tx_fee < 0: print("not enough source coins (%s BTC) for destination (%s BTC). Short %s BTC" % (satoshi_to_btc(total_value), satoshi_to_btc(total_spent), satoshi_to_btc(-actual_tx_fee))) elif actual_tx_fee < recommended_tx_fee: print("warning: transaction fee lower than (casually calculated) expected value of %s BTC, transaction might not propogate" % satoshi_to_btc(recommended_tx_fee)) return actual_tx_fee
def listunspent(self, orderby='address', orderdir='ASC', tospend=0): cursor = self.db.cursor() sql = "SELECT address, txid, scriptPubKey, amount, vout, confirmations, updated_at, source FROM unspents ORDER BY " + orderby + " " + orderdir unspents = [] tospend = btc_to_satoshi(tospend) amount = 0 for unspent in cursor.execute(sql): unspents.append({ 'address': unspent[0], 'txid': unspent[1], 'scriptPubKey': unspent[2], 'amount': float(satoshi_to_btc(unspent[3])), 'vout': unspent[4], 'confirmations': unspent[5], 'updated_at': unspent[6], 'source': unspent[7] }) return unspents
def listunspent(self, orderby='address', orderdir='ASC', tospend=0): cursor = self.db.cursor() sql = "SELECT address, txid, scriptPubKey, amount, vout, confirmations, updated_at, source FROM unspents ORDER BY "+orderby+" "+orderdir unspents = [] tospend = btc_to_satoshi(tospend) amount = 0 for unspent in cursor.execute(sql): unspents.append({ 'address': unspent[0], 'txid': unspent[1], 'scriptPubKey': unspent[2], 'amount': float(satoshi_to_btc(unspent[3])), 'vout': unspent[4], 'confirmations': unspent[5], 'updated_at': unspent[6], 'source': unspent[7] }) return unspents
def getbalance(self): balance = D('0') addresses = self.db.getalladdresses() for address in addresses: balance = balance + satoshi_to_btc(address['balance']) return float(balance)
def listaddressgroupings(self): listaddress = [] addresses = self.db.getalladdresses() for address in addresses: listaddress.append([address['address'], float(satoshi_to_btc(address['balance']))]) return [listaddress]
def bitcoin(public, private, address, amount): coins_from = [] coins_sources = blockchain_info.coin_sources_for_address(public) coins_from.extend(coins_sources) value = sum(cs[-1].coin_value for cs in coins_sources) secret_exponents = [encoding.wif_to_secret_exponent(private)] amount = btc_to_satoshi(amount) coins_to = [] coins_to.append((amount, address)) actual_tx_fee = value - amount if actual_tx_fee < 0: print("not enough source coins (%s BTC) for destination (%s BTC). Short %s BTC" % (satoshi_to_btc(total_value), satoshi_to_btc(total_spent), satoshi_to_btc(-actual_tx_fee))) return None; if actual_tx_fee > 0: coins_to.append((actual_tx_fee, public)) unsigned_tx = UnsignedTx.standard_tx(coins_from, coins_to) solver = SecretExponentSolver(secret_exponents) new_tx = unsigned_tx.sign(solver) s = io.BytesIO() new_tx.stream(s) tx_bytes = s.getvalue() tx_hex = binascii.hexlify(tx_bytes).decode("utf8") return tx_bytes