Exemplo n.º 1
0
 def tx_for_tx_hash(self, tx_hash):
     """
     Get a Tx by its hash.
     """
     url = "%s/rawtx/%s" % (self.url, b2h_rev(tx_hash))
     d = urlopen(url).read()
     j = json.loads(d.decode("utf8"))
     tx = Tx.from_hex(j.get("rawtx", ""))
     if tx.hash() == tx_hash:
         return tx
Exemplo n.º 2
0
 def tx_for_tx_hash(self, tx_hash):
     """
     Get a Tx by its hash.
     """
     url = "%s/rawtx/%s" % (self.url, b2h_rev(tx_hash))
     d = urlopen(url).read()
     j = json.loads(d.decode("utf8"))
     tx = Tx.from_hex(j.get("rawtx", ""))
     if tx.hash() == tx_hash:
         return tx
Exemplo n.º 3
0
 def tx_for_tx_hash(self, tx_hash):
     raw_tx = self.connection.getrawtransaction(b2h_rev(tx_hash))
     tx = Tx.from_hex(raw_tx)
     return tx
Exemplo n.º 4
0
 def tx_for_tx_hash(self, tx_hash):
     "Get a Tx by its hash."
     URL = "https://blockchain.info/rawtx/%s?format=hex" % b2h_rev(tx_hash)
     tx = Tx.from_hex(urlopen(URL).read().decode("utf8"))
     return tx
Exemplo n.º 5
0
 def tx_for_tx_hash(self, tx_hash):
     "Get a Tx by its hash."
     URL = self.api_domain + ("/rawtx/%s?format=hex" % b2h_rev(tx_hash))
     tx = Tx.from_hex(urlopen(URL).read().decode("utf8"))
     return tx
Exemplo n.º 6
0
 def tx_for_tx_hash(self, tx_hash):
     "Get a Tx by its hash."
     URL = self.api_domain + ("/rawtx/%s?format=hex" % b2h_rev(tx_hash))
     tx = Tx.from_hex(urlopen(URL).read().decode("utf8"))
     return tx
Exemplo n.º 7
0
 def tx_for_tx_hash(self, tx_hash):
     raw_tx = self.connection.getrawtransaction(b2h_rev(tx_hash))
     tx = Tx.from_hex(raw_tx)
     return tx
Exemplo n.º 8
0
def build_psbt(ctx, xfp, addrs, pubkey=None, xpubs=None, redeem=None):
    locals().update(ctx.obj)
    payout_address = ctx.obj['payout_address']
    out_psbt = ctx.obj['output_psbt']

    if pubkey:
        assert len(addrs) == 1  # can only be single addr in that case
        assert len(pubkey) == 33

    spending = []
    total = 0
    psbt = BasicPSBT()

    for path, addr in addrs:
        print(f"addr: {path} => {addr} ... ", end='')

        rr = explora('address', addr, 'utxo')

        if not rr:
            print('nada')
            continue

        here = 0
        for u in rr:
            here += u['value']

            tt = TxIn(h2b_rev(u['txid']), u['vout'])
            spending.append(tt)
            #print(rr)

            pin = BasicPSBTInput(idx=len(psbt.inputs))
            psbt.inputs.append(pin)

            pubkey = pubkey or calc_pubkey(xpubs, path)

            pin.bip32_paths[pubkey] = str2path(xfp, path)

            # fetch the UTXO for witness signging
            td = explora('tx', u['txid'], 'hex', is_json=False)

            #print(f"txis {u['txid']}:\b{td!r}")
            outpt = Tx.from_hex(td.decode('ascii')).txs_out[u['vout']]

            with BytesIO() as b:
                outpt.stream(b)
                pin.witness_utxo = b.getvalue()

            if redeem:
                pin.redeem_script = redeem

        print('%.8f BTC' % (here / 1E8))
        total += here

        if len(spending) > 15:
            print("Reached practical limit on # of inputs. "
                  "You'll need to repeat this process again later.")
            break

    assert total, "Sorry! Didn't find any UTXO"

    print("Found total: %.8f BTC" % (total / 1E8))

    if payout_address:
        print("Planning to send to: %s" % payout_address)
        dest_scr = BTC.contract.for_address(payout_address)

        txn = Tx(2, spending, [TxOut(total, dest_scr)])
    else:
        print("Output section of PSBT will be empty. Change downstream")
        txn = Tx(2, spending, [])

    fee = tx_fee.recommended_fee_for_tx(txn)

    # placeholder, single output that isn't change
    pout = BasicPSBTOutput(idx=0)
    psbt.outputs.append(pout)

    print("Guestimate fee: %.8f BTC" % (fee / 1E8))

    if txn.txs_out:
        txn.txs_out[0].coin_value -= fee

    # write txn into PSBT
    with BytesIO() as b:
        txn.stream(b)
        psbt.txn = b.getvalue()

    out_psbt.write(psbt.as_bytes())

    print("PSBT to be signed:\n\n\t" + out_psbt.name, end='\n\n')