def mk_spend_txids(wallet, dst, amount, h): l=len(h) outs = [{'value': int(amount), 'address': dst}] tx = b.mksend(h,outs, wallet["address"], fee) for i in range(l): tx=b.sign(tx, i, wallet["priv"]) return(tx)
def build_tx(inputs, priv, addr, script, fee=0, send=False): """ Build a transaction to post/cancel a book sale Args: inputs (list): list of UTXOs to use. Obtained with bitcoin.unspent() priv (str): private key for wallet addr (str): address to post sale to script (str): script with encoded message of sale/cancelation fee (int): satoshis to pay to miners to write to the blockchain There is no reason to do this. send (bool): if True, send to mempool Returns: signed_tx (str): signed transaction to send to mempool also, sends transaction to mempool """ outputs = [{ 'value': 546, 'address': settings.MARKET_ADDRESS }, { 'value': 0, 'script': script }] fee = fee tx = bitcoin.mksend(inputs, outputs, addr, fee) signed_tx = bitcoin.sign(tx, 0, priv) if send: bitcoin.pushtx(signed_tx) return signed_tx
def mk_spend_txids(wallet, dst, amount, h): l = len(h) outs = [{'value': int(amount), 'address': dst}] tx = b.mksend(h, outs, wallet["address"], fee) for i in range(l): tx = b.sign(tx, i, wallet["priv"]) return (tx)
def send(fromprivkey, toaddr, value): transaction_fee = 20000 print "Sending:", fromprivkey, toaddr, value tx = bitcoin.mksend(bitcoin.history(bitcoin.privtoaddr(fromprivkey)), [toaddr + ":" + str(value)], bitcoin.privtoaddr(fromprivkey), transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def send(fromprivkey, toaddr, value): transaction_fee = 20000 # .0002 BTC fromaddress = bitcoin.privtoaddr(fromprivkey) tx = bitcoin.mksend(bitcoin.history(fromaddress), [{ 'value': value, 'address': toaddr }], fromaddress, transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def create_tx(username, form, op_return=''): privkey, address = get_keychain(username) unspent = get_unspent(address) inputs = unspent['inputs'] balance = int(float(unspent['confirmed']) * 1e8) amount = int(float(form.amount.data) * 1e8) receptor = form.address.data if check_addr(receptor): return "Dirección inválida" elif amount > balance: return "Balance insuficiente" elif amount <= 0: return "Monto inválido" used_utxo = 0 used_inputs = [] for utxo in inputs: used_utxo += utxo['value'] used_inputs.append(utxo) if used_utxo >= amount: break outputs = [{'address': receptor, 'value': amount}] template_tx = mktx(used_inputs, outputs) # size = unsigned tx + (65 bytes * signature) size = len(a2b_hex(template_tx)) + 65 * len(used_inputs) # FEE = 0.01 CHA/kb fee = int((size / 1024) * 0.01 * 1e8) # MAX FEE = 0.1 CHA fee = 1e7 if fee > 1e7 else fee if used_utxo == amount: outputs[0] = {'address': receptor, 'value': int(amount - fee)} tx = mksend(used_inputs, outputs, address, fee) for i, _ in enumerate(used_inputs): tx = sign(tx, i, privkey) return tx
def send_message(recipient_addr, scripts, fee=0, send=False): """ Send encoded message Args: recipient_addr (str): address of user to send message too scripts (list): list of strings, each element is a single encoded message. Multiple encodings are needed if the message is longer than 40 char. fee (int): Satoshis to pay in miner fees. Not actually required. send (bool): If True, send transactions to mempool. Returns: signed_txs (list): list of strings where each element is a signed transaction encoding a message Also sends transaction to the mempool. Notes: This is currently sending the message to blockchain.info, but this is trivially changed if needed. """ priv, pub, addr = books.read_wallet() signed_txs = [] for script in scripts: outputs = [{'value': 546, 'address': recipient_addr}] inputs = bitcoin.unspent(addr) for _input in inputs: if _input > 1092: input_tx = _input break outputs.append({'value': 0, 'script': script}) fee = fee tx = bitcoin.mksend(input_tx, outputs, addr, fee) signed_tx = bitcoin.sign(tx, 0, priv) if send: bitcoin.pushtx(signed_tx) signed_txs.append(signed_tx) if send: _write_message(recipient_addr, signed_txs) return signed_txs
def send(privkey, pubkey, sendto, message="", force=False): op_return = message addr = pubkey confirmed_balance, inputs, unconfirmed = insight.getunspentbalance(addr) if not force: if not crypto.pubkey_is_valid(addr): # Invalid Address # print("Invalid Address") return False elif constants.FEE_MINIMUM > confirmed_balance: # Not enough chauchas # print("No minimum fee") return False elif len(op_return) > MAX: # Message too long # print("Message to long") return False # Transformar valores a Chatoshis used_amount = int(constants.FEE_MINIMUM * constants.COIN) # Utilizar solo las unspent que se necesiten used_balance = 0 used_inputs = [] for i in inputs: used_balance += i["value"] used_inputs.append(i) if used_balance > used_amount: break # Output outputs = [{"address": sendto, "value": used_amount}] # OP_RETURN if len(op_return) > 0 and len(op_return) <= MAX: payload = __payload(op_return) script = constants.OP_RETURN + b2a_hex(payload).decode("utf-8", errors="ignore") outputs.append({"value": 0, "script": script}) # Transaction template_tx = mktx(used_inputs, outputs) size = len(a2b_hex(template_tx)) # FEE = 0.01 CHA/kb # MAX FEE = 0.1 CHA fee = int((size / constants.KYLOBYTE) * constants.FEE_RECOMMENDED * constants.COIN) fee = constants.FEE_MAX if fee > constants.FEE_MAX else fee if used_balance == confirmed_balance: outputs[0] = {"address": sendto, "value": used_amount - fee} tx = mktx(used_inputs, outputs) else: tx = mksend(used_inputs, outputs, addr, fee) for i in range(len(used_inputs)): tx = sign(tx, i, privkey) broadcasting = insight.broadcast(tx) try: msg = insight.endpoint() + "/tx/%s" % broadcasting.json()["txid"] except Exception: msg = broadcasting.text return msg
def send(fromprivkey, toaddr, value): transaction_fee = 20000 print "Sending:", fromprivkey, toaddr, value tx = bitcoin.mksend(bitcoin.history(bitcoin.privtoaddr(fromprivkey)), [toaddr+":"+str(value)], bitcoin.privtoaddr(fromprivkey), transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def send(fromprivkey, toaddr, value): transaction_fee = 20000 # .0002 BTC fromaddress = bitcoin.privtoaddr(fromprivkey) tx = bitcoin.mksend(bitcoin.history(fromaddress), [{'value': value, 'address': toaddr}], fromaddress, transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)