Esempio n. 1
0
def write_opreturn(bitcoin_address, bitcoin_private_key, raw_message, fee=5000, push=False):
    message = hexlify(raw_message.encode()).decode('utf8')
    spendables = spendables_for_address(bitcoin_address)
    spendables = [s for s in spendables]
    bitcoin_sum = sum([spendable.coin_value for spendable in spendables])
    inputs = [spendable.tx_in() for spendable in spendables]
    outputs = []
    if (bitcoin_sum > fee):
        change_output_script = standard_tx_out_script(bitcoin_address)
        print change_output_script
        outputs.append(TxOut(bitcoin_sum - fee, change_output_script))

        ## Build the OP_RETURN output with our message
        op_return_output_script = script.tools.compile("OP_RETURN %s" % message)
        outputs.append(TxOut(0, op_return_output_script))

        ## Create the transaction and sign it with the private key
        tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
        tx.set_unspents(spendables)
        sign_tx(tx, wifs=[bitcoin_private_key])
        print tx.as_hex()
        if not push:
            return tx.as_hex()
        else:
            pushtx(tx.as_hex())
    else:
        print "INADEQUATE FUNDS"
Esempio n. 2
0
def write_transfer(sender, sender_priv, recipient, message, fee=m.default_fee, avoid_inputs=[]):
    message = hexlify(message.encode()).decode('utf8')
    spendables = spendables_for_address(sender)
    spendables = [s for s in spendables if not spendable_to_legible(s.tx_in()) in avoid_inputs]
    bitcoin_sum = sum([spendable.coin_value for spendable in spendables])
    inputs = [spendable.tx_in() for spendable in spendables]
    outputs = []
    if bitcoin_sum > fee + m.dust*2:
        remaining = bitcoin_sum - fee - m.dust*2
        dest_output_script = standard_tx_out_script(recipient)
        change_output_script = standard_tx_out_script(sender)
        btc_change_output_script = standard_tx_out_script(sender)
        op_return_output_script = script.tools.compile("OP_RETURN %s" % message)

        outputs.append(TxOut(m.dust, dest_output_script))
        outputs.append(TxOut(m.dust, change_output_script))
        outputs.append(TxOut(remaining, btc_change_output_script))
        outputs.append(TxOut(0, op_return_output_script))

        tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
        tx.set_unspents(spendables)
        sign_tx(tx, wifs=[sender_priv])
        print tx.as_hex()
        return tx.as_hex()
    else:
        print "INADEQUATE FUNDS"
Esempio n. 3
0
def dummy_op_return_tx(key, message, spendables, fee=10000):
    address = key.address()

    if len(message) > 80:
        raise ValueError("message must not be longer than 80 bytes")
    message = hexlify(message).decode()

    bitcoin_sum = sum(spendable.coin_value for spendable in spendables)
    if bitcoin_sum < fee:
        raise Exception("not enough bitcoin to cover fee")

    inputs = [spendable.tx_in() for spendable in spendables]

    outputs = []
    if bitcoin_sum > fee:
        change_output_script = standard_tx_out_script(address)
        outputs.append(TxOut(bitcoin_sum - fee, change_output_script))

    op_return_output_script = script.tools.compile('OP_RETURN %s' % message)
    outputs.append(TxOut(0, op_return_output_script))

    tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
    tx.set_unspents(spendables)
    sign_tx(tx, wifs=[key.wif()])

    return tx
Esempio n. 4
0
def make_signed_tx_from_vote(vote, password, user=PRIMARY, extra_payables=[]):
    key = get_private_key(password, user)
    if key is None:
        raise Exception('Password Incorrect')
    tx = mix_nulldata_into_tx(vote.to_bytes(), create_stock_tx(user, extra_payables=extra_payables))
    sign_tx(tx, [key.wif()])
    return tx
Esempio n. 5
0
def main():
    if len(sys.argv) != 4:
        print("usage: %s tx-hex-file-path wif-file-path p2sh-file-path" %
              sys.argv[0])
        sys.exit(-1)

    # get the tx
    with open(sys.argv[1], "r") as f:
        tx_hex = f.readline().strip()
    tx = Tx.from_hex(tx_hex)

    # get the WIF
    with open(sys.argv[2], "r") as f:
        wif = f.readline().strip()
    assert is_wif_valid(wif)

    # create the p2sh_lookup
    with open(sys.argv[3], "r") as f:
        p2sh_script_hex = f.readline().strip()
    p2sh_script = h2b(p2sh_script_hex)

    # build a dictionary of script hashes to scripts
    p2sh_lookup = build_p2sh_lookup([p2sh_script])

    # sign the transaction with the given WIF
    sign_tx(tx, wifs=[wif], p2sh_lookup=p2sh_lookup)

    bad_signature_count = tx.bad_signature_count()
    print("tx %s now has %d bad signature(s)" % (tx.id(), bad_signature_count))

    include_unspents = (bad_signature_count > 0)
    print("Here is the tx as hex:\n%s" %
          tx.as_hex(include_unspents=include_unspents))
Esempio n. 6
0
def main():
    if len(sys.argv) != 4:
        print("usage: %s tx-hex-file-path wif-file-path p2sh-file-path" % sys.argv[0])
        sys.exit(-1)

    # get the tx
    with open(sys.argv[1], "r") as f:
        tx_hex = f.readline().strip()
    tx = Tx.from_hex(tx_hex)

    # get the WIF
    with open(sys.argv[2], "r") as f:
        wif = f.readline().strip()
    assert is_wif_valid(wif)

    # create the p2sh_lookup
    with open(sys.argv[3], "r") as f:
        p2sh_script_hex = f.readline().strip()
    p2sh_script = h2b(p2sh_script_hex)

    # build a dictionary of script hashes to scripts
    p2sh_lookup = build_p2sh_lookup([p2sh_script])

    # sign the transaction with the given WIF
    sign_tx(tx, wifs=[wif], p2sh_lookup=p2sh_lookup)

    bad_signature_count = tx.bad_signature_count()
    print("tx %s now has %d bad signature(s)" % (tx.id(), bad_signature_count))

    include_unspents = (bad_signature_count > 0)
    print("Here is the tx as hex:\n%s" % tx.as_hex(include_unspents=include_unspents))
Esempio n. 7
0
    def test_segwit_create_tx(self):
        from pycoin.tx.tx_utils import create_tx, sign_tx
        from pycoin.tx.Spendable import Spendable
        from pycoin.tx.pay_to.ScriptPayToAddress import ScriptPayToAddress
        from pycoin.tx.pay_to.ScriptPayToAddressWit import ScriptPayToAddressWit
        from pycoin.tx.pay_to.ScriptPayToScriptWit import ScriptPayToScriptWit
        from pycoin.ui import address_for_pay_to_script_wit, script_obj_from_address
        key1 = Key(1)
        coin_value = 5000000
        script = ScriptPayToAddressWit(b'\0', key1.hash160()).script()
        tx_hash = b'\ee' * 32
        tx_out_index = 0
        spendable = Spendable(coin_value, script, tx_hash, tx_out_index)
        key2 = Key(2)
        tx = create_tx([spendable], [(key2.address(), coin_value)])
        self.check_unsigned(tx)
        sign_tx(tx, [key1.wif()])
        self.check_signed(tx)
        self.assertEqual(len(tx.txs_in[0].witness), 2)

        s1 = ScriptPayToAddress(key1.hash160()).script()
        address = address_for_pay_to_script_wit(s1)
        spendable.script = script_obj_from_address(address).script()
        tx = create_tx([spendable], [(key2.address(), coin_value)])
        self.check_unsigned(tx)
        sign_tx(tx, [key1.wif()], p2sh_lookup=build_p2sh_lookup([s1]))
        self.check_signed(tx)
Esempio n. 8
0
    def test_segwit_create_tx(self):
        from pycoin.tx.tx_utils import create_tx, sign_tx
        from pycoin.tx.Spendable import Spendable
        from pycoin.tx.pay_to.ScriptPayToAddress import ScriptPayToAddress
        from pycoin.tx.pay_to.ScriptPayToAddressWit import ScriptPayToAddressWit
        from pycoin.tx.pay_to.ScriptPayToScriptWit import ScriptPayToScriptWit
        from pycoin.ui import address_for_pay_to_script_wit, script_obj_from_address
        key1 = Key(1)
        coin_value = 5000000
        script = ScriptPayToAddressWit(b'\0', key1.hash160()).script()
        tx_hash = b'\ee' * 32
        tx_out_index = 0
        spendable = Spendable(coin_value, script, tx_hash, tx_out_index)
        key2 = Key(2)
        tx = create_tx([spendable], [(key2.address(), coin_value)])
        self.check_unsigned(tx)
        sign_tx(tx, [key1.wif()])
        self.check_signed(tx)
        self.assertEqual(len(tx.txs_in[0].witness), 2)

        s1 = ScriptPayToAddress(key1.hash160()).script()
        address = address_for_pay_to_script_wit(s1)
        spendable.script = script_obj_from_address(address).script()
        tx = create_tx([spendable], [(key2.address(), coin_value)])
        self.check_unsigned(tx)
        sign_tx(tx, [key1.wif()], p2sh_lookup=build_p2sh_lookup([s1]))
        self.check_signed(tx)
Esempio n. 9
0
    def _signtx(self, unsigned_tx: Tx, in_addrs: Set[str], fee: int) -> None:
        """ Signs Tx and redistributes outputs to include the miner fee.

        :param unsigned_tx: an unsigned Tx to sign and add fee to
        :param in_addrs: a list of our addresses that have recieved coins
        :param fee: an int representing the desired Tx fee
        """
        redeem_scripts = {}  # type: Dict[bytes, bytes]
        wifs = []  # type: List[str]

        # Search our indicies for keys used, given in in_addrs list
        # Populate lists with our privkeys and redeemscripts
        for change in (True, False):
            addresses = self.get_all_known_addresses(change, addr=True)
            for i, addr in enumerate(addresses):
                key = self.get_key(i, change)  # type: SegwitBIP32Node
                if addr in in_addrs:
                    p2aw_script = key.p2wpkh_script()  # type: bytes
                    script_hash = key.p2wpkh_script_hash()  # type: bytes
                    redeem_scripts[script_hash] = p2aw_script
                    wifs.append(key.wif())

        # Include our total fee and sign the Tx
        distribute_from_split_pool(unsigned_tx, fee)
        sign_tx(unsigned_tx,
                wifs=wifs,
                netcode=self.chain.netcode,
                p2sh_lookup=redeem_scripts)
Esempio n. 10
0
def build_tx(signature, address, wif):
    prev = get_spendable(address)
    spend_tx = create_tx([prev], [address])
    signature_script = ScriptNulldata(signature).script()
    signature_out = TxOut(0, signature_script)
    spend_tx.txs_out.append(signature_out)
    sign_tx(spend_tx, wifs=[wif])
    return spend_tx
Esempio n. 11
0
def make_signed_tx_from_vote(vote, password, user=PRIMARY, extra_payables=[]):
    key = get_private_key(password, user)
    if key is None:
        raise Exception('Password Incorrect')
    tx = mix_nulldata_into_tx(
        vote.to_bytes(), create_stock_tx(user, extra_payables=extra_payables))
    sign_tx(tx, [key.wif()])
    return tx
Esempio n. 12
0
def op_return_this(privatekey, text, prefix = "KEYSTAMP:", bitcoin_fee = 10000):

    bitcoin_keyobj = get_key(privatekey)
    bitcoin_address = bitcoin_keyobj.bitcoin_address()

    ## Get the spendable outputs we are going to use to pay the fee
    all_spendables = get_spendables_blockcypher(bitcoin_address)
    spendables = []
    value = 0
    for unspent in all_spendables:
        while value < bitcoin_fee + 10000:
            coin_value = unspent.get("value")
            script = h2b(unspent.get("script_hex"))
            previous_hash = h2b_rev(unspent.get("tx_hash"))
            previous_index = unspent.get("index")
            spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
            value += coin_value

    bitcoin_sum = sum(spendable.coin_value for spendable in spendables)
    if(bitcoin_sum < bitcoin_fee):
        print "ERROR: not enough balance: available: %s - fee: %s" %(bitcoin_sum, bitcoin_fee)
        return False

    ## Create the inputs we are going to use
    inputs = [spendable.tx_in() for spendable in spendables]
    ## If we will have change left over create an output to send it back
    outputs = []
    if (bitcoin_sum > bitcoin_fee):
        change_output_script = standard_tx_out_script(bitcoin_address)
        total_amout = bitcoin_sum - bitcoin_fee
        outputs.append(TxOut(total_amout, change_output_script))

        # home_address = standard_tx_out_script(bitcoin_address)
        # #TODO: it needs some love and IQ on input mananagement stuff
        # outputs.append(TxOut((bitcoin_sum - bitcoin_fee), home_address))

    ## Build the OP_RETURN output with our message
    if prefix is not None and (len(text) + len(prefix) <= 80):
        text = prefix + text

    message = hexlify(text.encode()).decode('utf8')

    op_return_output_script = tools.compile("OP_RETURN %s" % message)
    outputs.append(TxOut(0, op_return_output_script))

    ## Create the transaction and sign it with the private key
    tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
    # print tx.as_hex()
    # print spendables
    tx.set_unspents(spendables)
    sign_tx(tx, wifs=[privatekey])

    print "singed_tx: %s" %tx.as_hex()

    #TODO: uncomment this when its ready to push data to blockchian
    tx_hash = broadcast_tx_blockr(tx.as_hex())
    return tx_hash
Esempio n. 13
0
def do_signing(tx, key_iters, p2sh_lookup, netcode):
    unsigned_before = tx.bad_signature_count()
    unsigned_after = unsigned_before
    if unsigned_before > 0 and key_iters:
        print("signing...", file=sys.stderr)
        sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup, netcode=netcode)

        unsigned_after = tx.bad_signature_count()
        if unsigned_after > 0:
            print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)
    return unsigned_after == 0
Esempio n. 14
0
def do_signing(tx, key_iters, p2sh_lookup, netcode):
    unsigned_before = tx.bad_signature_count()
    unsigned_after = unsigned_before
    if unsigned_before > 0 and key_iters:
        print("signing...", file=sys.stderr)
        sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup, netcode=netcode)

        unsigned_after = tx.bad_signature_count()
        if unsigned_after > 0:
            print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)
    return unsigned_after == 0
Esempio n. 15
0
def send_funds(from_addr, payables, wif):
    '''from_addr is a simple address in a string.
    payables is a list of tuples of (address, amount in duffs)
    wif is the wallet import format version of the private key for from_addr'''
    bc = blockcypher.BlockcypherProvider(netcode='DASH',
                                         api_key=blockcypher_api_key)
    spendables = bc.spendables_for_address(from_addr)
    tx = tx_utils.create_tx(spendables, payables, fee=0)
    tx_utils.sign_tx(tx, [wif])
    rtx = bc.broadcast_tx(tx)
    return tx
Esempio n. 16
0
def do_signing(tx, key_iters, p2sh_lookup, sec_hints, signature_hints, network):
    unsigned_before = tx.bad_signature_count()
    unsigned_after = unsigned_before
    if unsigned_before > 0 and (key_iters or sec_hints or signature_hints):
        print("signing...", file=sys.stderr)
        sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup,
                network=network, sec_hints=sec_hints, signature_hints=signature_hints)

        unsigned_after = tx.bad_signature_count()
        if unsigned_after > 0:
            print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)
    return unsigned_after == 0
Esempio n. 17
0
 def send_payment(self, rec_email, amount):
     """
     Still in flux
     """
     # TO DO: a database with emails and addresses would
     #        be far better than this
     receive_address = get_address_for_account(rec_email)
     # create input from prior transaction
     tx_hex, index = self.utxo(amount)
     tx = Tx.from_hex(tx_hex)
     spendable = tx.tx_outs_as_spendable()[index]
     # create unsigned transaction
     tx = create_tx([spendable], [receive_address])
     print("usigned %s" % tx.as_hex())
     wallet = Wallet.Wallet.from_wallet_key(self.get_wallet_keys()[0])
     sign_tx(tx, [wallet.wif()])
     print("signed %s" % tx.as_hex())
Esempio n. 18
0
    def test_segwit_create_tx(self):
        key1 = Key(1, generator=secp256k1_generator)
        coin_value = 5000000
        script = script_for_p2pkh_wit(key1.hash160())
        tx_hash = b'\ee' * 32
        tx_out_index = 0
        spendable = Tx.Spendable(coin_value, script, tx_hash, tx_out_index)
        key2 = Key(2, generator=secp256k1_generator)
        tx = create_tx([spendable], [(key2.address(), coin_value)])
        self.check_unsigned(tx)
        sign_tx(tx, [key1.wif()])
        self.check_signed(tx)
        self.assertEqual(len(tx.txs_in[0].witness), 2)

        s1 = script_for_p2pkh(key1.hash160())
        address = BitcoinMainnet.ui.address_for_p2s_wit(s1)
        spendable.script = BitcoinMainnet.ui.script_for_address(address)
        tx = create_tx([spendable], [(key2.address(), coin_value)])
        self.check_unsigned(tx)
        sign_tx(tx, [key1.wif()], p2sh_lookup=build_p2sh_lookup([s1]))
        self.check_signed(tx)
Esempio n. 19
0
def send_op_return_tx(key, message, fee=10000):
    """
    Send an transaction with an OP_RETURN output.

    Args:
        key: the Bitcoin Key to send the transaction from.
        message: the message to include in OP_RETURN.
        fee: the miner fee that should be paid in Satoshis.
    Returns:
        The broadcasted Tx.
    """
    address = key.address()

    if len(message) > 80:
        raise ValueError("message must not be longer than 80 bytes")
    message = hexlify(message).decode()

    spendables = spendables_for_address(address)
    bitcoin_sum = sum(spendable.coin_value for spendable in spendables)
    if bitcoin_sum < fee:
        raise Exception("not enough bitcoin to cover fee")

    inputs = [spendable.tx_in() for spendable in spendables]

    outputs = []
    if bitcoin_sum > fee:
        change_output_script = standard_tx_out_script(address)
        outputs.append(TxOut(bitcoin_sum - fee, change_output_script))

    op_return_output_script = script.tools.compile('OP_RETURN %s' % message)
    outputs.append(TxOut(0, op_return_output_script))

    tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
    tx.set_unspents(spendables)
    sign_tx(tx, wifs=[key.wif()])

    broadcast_tx(tx)

    return tx
Esempio n. 20
0
from pycoin import convention

amount = convention.btc_to_satoshi(first_nondust['amount'])
dustbtc = convention.btc_to_satoshi(DUST)
feebtc = convention.btc_to_satoshi(FEE)

unspent = Spendable( amount, standard_tx_out_script(first_address), \
            serialize.h2b_rev(first_nondust['tx']), first_nondust['n'] )

txout = TxOut( dustbtc, standard_tx_out_script(bob_notif.address()) )
change = TxOut( amount - (dustbtc + feebtc), standard_tx_out_script(change_addresses.pop()) )
op_return_script = pay_to.ScriptNulldata(alice_masked_pcode_nosuffix)
op_return_txout = TxOut(0, op_return_script.script())

notif_tx = Tx( 1, [unspent.tx_in()], [txout, change, op_return_txout], unspents=[unspent] )
tx_utils.sign_tx( notif_tx, [first_node.wif()] )
print "Signed Notification TX as hex:\n", notif_tx.as_hex()

data = urlencode(dict( hex=notif_tx.as_hex() ))
response = json.load( urlopen(url="http://tbtc.blockr.io/api/v1/tx/decode", data=data) )
print "Decoded:\n", pretty_json(response), '\n'


alice_notif_exp = alice_notif.secret_exponent()
bob_pcode_nodes = get_nodes(bob_wallet, GAP_LIMIT, pcode=True)
bob_ephem_key = None

for bob_pcode_node in bob_pcode_nodes.values():
    pcode_pp = bob_pcode_node.public_pair()
    pcode_point = ellipticcurve.Point(curve, pcode_pp[0], pcode_pp[1])
    
Esempio n. 21
0
File: tx.py Progetto: stafur/pycoin
def main():
    parser = argparse.ArgumentParser(
        description="Manipulate bitcoin (or alt coin) transactions.",
        epilog=EPILOG)

    parser.add_argument('-t', "--transaction-version", type=int,
                        help='Transaction version, either 1 (default) or 3 (not yet supported).')

    parser.add_argument('-l', "--lock-time", type=parse_locktime, help='Lock time; either a block'
                        'index, or a date/time (example: "2014-01-01T15:00:00"')

    parser.add_argument('-n', "--network", default="BTC",
                        help='Define network code (M=Bitcoin mainnet, T=Bitcoin testnet).')

    parser.add_argument('-a', "--augment", action='store_true',
                        help='augment tx by adding any missing spendable metadata by fetching'
                             ' inputs from cache and/or web services')

    parser.add_argument("-i", "--fetch-spendables", metavar="address", action="append",
                        help='Add all unspent spendables for the given bitcoin address. This information'
                        ' is fetched from web services.')

    parser.add_argument('-f', "--private-key-file", metavar="path-to-private-keys", action="append",
                        help='file containing WIF or BIP0032 private keys. If file name ends with .gpg, '
                        '"gpg -d" will be invoked automatically. File is read one line at a time, and if '
                        'the file contains only one WIF per line, it will also be scanned for a bitcoin '
                        'address, and any addresses found will be assumed to be public keys for the given'
                        ' private key.',
                        type=argparse.FileType('r'))

    parser.add_argument('-g', "--gpg-argument", help='argument to pass to gpg (besides -d).', default='')

    parser.add_argument("--remove-tx-in", metavar="tx_in_index_to_delete", action="append", type=int,
                        help='remove a tx_in')

    parser.add_argument("--remove-tx-out", metavar="tx_out_index_to_delete", action="append", type=int,
                        help='remove a tx_out')

    parser.add_argument('-F', "--fee", help='fee, in satoshis, to pay on transaction, or '
                        '"standard" to auto-calculate. This is only useful if the "split pool" '
                        'is used; otherwise, the fee is automatically set to the unclaimed funds.',
                        default="standard", metavar="transaction-fee", type=parse_fee)

    parser.add_argument('-C', "--cache", help='force the resultant transaction into the transaction cache.'
                        ' Mostly for testing.', action='store_true'),

    parser.add_argument('-u', "--show-unspents", action='store_true',
                        help='show TxOut items for this transaction in Spendable form.')

    parser.add_argument('-b', "--bitcoind-url",
                        help='URL to bitcoind instance to validate against (http://user:pass@host:port).')

    parser.add_argument('-o', "--output-file", metavar="path-to-output-file", type=argparse.FileType('wb'),
                        help='file to write transaction to. This supresses most other output.')

    parser.add_argument("argument", nargs="+", help='generic argument: can be a hex transaction id '
                        '(exactly 64 characters) to be fetched from cache or a web service;'
                        ' a transaction as a hex string; a path name to a transaction to be loaded;'
                        ' a spendable 4-tuple of the form tx_id/tx_out_idx/script_hex/satoshi_count '
                        'to be added to TxIn list; an address/satoshi_count to be added to the TxOut '
                        'list; an address to be added to the TxOut list and placed in the "split'
                        ' pool".')

    args = parser.parse_args()

    # defaults

    txs = []
    spendables = []
    payables = []

    key_iters = []

    TX_ID_RE = re.compile(r"^[0-9a-fA-F]{64}$")

    # there are a few warnings we might optionally print out, but only if
    # they are relevant. We don't want to print them out multiple times, so we
    # collect them here and print them at the end if they ever kick in.

    warning_tx_cache = None
    warning_get_tx = None
    warning_spendables = None

    if args.private_key_file:
        wif_re = re.compile(r"[1-9a-km-zA-LMNP-Z]{51,111}")
        # address_re = re.compile(r"[1-9a-kmnp-zA-KMNP-Z]{27-31}")
        for f in args.private_key_file:
            if f.name.endswith(".gpg"):
                gpg_args = ["gpg", "-d"]
                if args.gpg_argument:
                    gpg_args.extend(args.gpg_argument.split())
                gpg_args.append(f.name)
                popen = subprocess.Popen(gpg_args, stdout=subprocess.PIPE)
                f = popen.stdout
            for line in f.readlines():
                # decode
                if isinstance(line, bytes):
                    line = line.decode("utf8")
                # look for WIFs
                possible_keys = wif_re.findall(line)

                def make_key(x):
                    try:
                        return Key.from_text(x)
                    except Exception:
                        return None

                keys = [make_key(x) for x in possible_keys]
                for key in keys:
                    if key:
                        key_iters.append((k.wif() for k in key.subkeys("")))

                # if len(keys) == 1 and key.hierarchical_wallet() is None:
                #    # we have exactly 1 WIF. Let's look for an address
                #   potential_addresses = address_re.findall(line)

    # we create the tx_db lazily
    tx_db = None

    for arg in args.argument:

        # hex transaction id
        if TX_ID_RE.match(arg):
            if tx_db is None:
                warning_tx_cache = message_about_tx_cache_env()
                warning_get_tx = message_about_get_tx_env()
                tx_db = get_tx_db()
            tx = tx_db.get(h2b_rev(arg))
            if not tx:
                for m in [warning_tx_cache, warning_get_tx, warning_spendables]:
                    if m:
                        print("warning: %s" % m, file=sys.stderr)
                parser.error("can't find Tx with id %s" % arg)
            txs.append(tx)
            continue

        # hex transaction data
        try:
            tx = Tx.tx_from_hex(arg)
            txs.append(tx)
            continue
        except Exception:
            pass

        try:
            key = Key.from_text(arg)
            # TODO: check network
            if key.wif() is None:
                payables.append((key.address(), 0))
                continue
            # TODO: support paths to subkeys
            key_iters.append((k.wif() for k in key.subkeys("")))
            continue
        except Exception:
            pass

        if os.path.exists(arg):
            try:
                with open(arg, "rb") as f:
                    if f.name.endswith("hex"):
                        f = io.BytesIO(codecs.getreader("hex_codec")(f).read())
                    tx = Tx.parse(f)
                    txs.append(tx)
                    try:
                        tx.parse_unspents(f)
                    except Exception as ex:
                        pass
                    continue
            except Exception:
                pass

        parts = arg.split("/")
        if len(parts) == 4:
            # spendable
            try:
                spendables.append(Spendable.from_text(arg))
                continue
            except Exception:
                pass

        # TODO: fix allowable_prefixes
        allowable_prefixes = b'\0'
        if len(parts) == 2 and encoding.is_valid_bitcoin_address(
                parts[0], allowable_prefixes=allowable_prefixes):
            try:
                payables.append(parts)
                continue
            except ValueError:
                pass

        parser.error("can't parse %s" % arg)

    if args.fetch_spendables:
        warning_spendables = message_about_spendables_for_address_env()
        for address in args.fetch_spendables:
            spendables.extend(spendables_for_address(address))

    for tx in txs:
        if tx.missing_unspents() and args.augment:
            if tx_db is None:
                warning_tx_cache = message_about_tx_cache_env()
                warning_get_tx = message_about_get_tx_env()
                tx_db = get_tx_db()
            tx.unspents_from_db(tx_db, ignore_missing=True)

    txs_in = []
    txs_out = []
    unspents = []
    # we use a clever trick here to keep each tx_in corresponding with its tx_out
    for tx in txs:
        smaller = min(len(tx.txs_in), len(tx.txs_out))
        txs_in.extend(tx.txs_in[:smaller])
        txs_out.extend(tx.txs_out[:smaller])
        unspents.extend(tx.unspents[:smaller])
    for tx in txs:
        smaller = min(len(tx.txs_in), len(tx.txs_out))
        txs_in.extend(tx.txs_in[smaller:])
        txs_out.extend(tx.txs_out[smaller:])
        unspents.extend(tx.unspents[smaller:])
    for spendable in spendables:
        txs_in.append(spendable.tx_in())
        unspents.append(spendable)
    for address, coin_value in payables:
        script = standard_tx_out_script(address)
        txs_out.append(TxOut(coin_value, script))

    lock_time = args.lock_time
    version = args.transaction_version

    # if no lock_time is explicitly set, inherit from the first tx or use default
    if lock_time is None:
        if txs:
            lock_time = txs[0].lock_time
        else:
            lock_time = DEFAULT_LOCK_TIME

    # if no version is explicitly set, inherit from the first tx or use default
    if version is None:
        if txs:
            version = txs[0].version
        else:
            version = DEFAULT_VERSION

    if args.remove_tx_in:
        s = set(args.remove_tx_in)
        txs_in = [tx_in for idx, tx_in in enumerate(txs_in) if idx not in s]

    if args.remove_tx_out:
        s = set(args.remove_tx_out)
        txs_out = [tx_out for idx, tx_out in enumerate(txs_out) if idx not in s]

    tx = Tx(txs_in=txs_in, txs_out=txs_out, lock_time=lock_time, version=version, unspents=unspents)

    fee = args.fee
    try:
        distribute_from_split_pool(tx, fee)
    except ValueError as ex:
        print("warning: %s" % ex.args[0], file=sys.stderr)

    unsigned_before = tx.bad_signature_count()
    if unsigned_before > 0 and key_iters:
        def wif_iter(iters):
            while len(iters) > 0:
                for idx, iter in enumerate(iters):
                    try:
                        wif = next(iter)
                        yield wif
                    except StopIteration:
                        iters = iters[:idx] + iters[idx+1:]
                        break

        print("signing...", file=sys.stderr)
        sign_tx(tx, wif_iter(key_iters))

    unsigned_after = tx.bad_signature_count()
    if unsigned_after > 0 and key_iters:
        print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)

    if len(tx.txs_in) == 0:
        print("warning: transaction has no inputs", file=sys.stderr)

    if len(tx.txs_out) == 0:
        print("warning: transaction has no outputs", file=sys.stderr)

    include_unspents = (unsigned_after > 0)
    tx_as_hex = tx.as_hex(include_unspents=include_unspents)

    if args.output_file:
        f = args.output_file
        if f.name.endswith(".hex"):
            f.write(tx_as_hex.encode("utf8"))
        else:
            tx.stream(f)
            if include_unspents:
                tx.stream_unspents(f)
        f.close()
    elif args.show_unspents:
        for spendable in tx.tx_outs_as_spendable():
            print(spendable.as_text())
    else:
        if not tx.missing_unspents():
            check_fees(tx)
        dump_tx(tx, args.network)
        if include_unspents:
            print("including unspents in hex dump since transaction not fully signed")
        print(tx_as_hex)

    if args.cache:
        if tx_db is None:
            warning_tx_cache = message_about_tx_cache_env()
            warning_get_tx = message_about_get_tx_env()
            tx_db = get_tx_db()
        tx_db.put(tx)

    if args.bitcoind_url:
        if tx_db is None:
            warning_tx_cache = message_about_tx_cache_env()
            warning_get_tx = message_about_get_tx_env()
            tx_db = get_tx_db()
        validate_bitcoind(tx, tx_db, args.bitcoind_url)

    if tx.missing_unspents():
        print("\n** can't validate transaction as source transactions missing", file=sys.stderr)
    else:
        try:
            if tx_db is None:
                warning_tx_cache = message_about_tx_cache_env()
                warning_get_tx = message_about_get_tx_env()
                tx_db = get_tx_db()
            tx.validate_unspents(tx_db)
            print('all incoming transaction values validated')
        except BadSpendableError as ex:
            print("\n**** ERROR: FEES INCORRECTLY STATED: %s" % ex.args[0], file=sys.stderr)
        except Exception as ex:
            print("\n*** can't validate source transactions as untampered: %s" %
                  ex.args[0], file=sys.stderr)

    # print warnings
    for m in [warning_tx_cache, warning_get_tx, warning_spendables]:
        if m:
            print("warning: %s" % m, file=sys.stderr)
Esempio n. 22
0
def signTxLegacy(txHex, fromWif):
    tx = Tx.from_hex(txHex)
    sign_tx(tx, wifs=[fromWif])
    return tx.as_hex()
Esempio n. 23
0
File: tx.py Progetto: Zibbo/pycoin
def main():
    parser = create_parser()
    args = parser.parse_args()

    (txs, spendables, payables, key_iters, p2sh_lookup, tx_db,
     warning_tx_cache, warning_tx_for_tx_hash, warning_spendables) = parse_context(args, parser)

    txs_in = []
    txs_out = []
    unspents = []

    # we use a clever trick here to keep each tx_in corresponding with its tx_out
    for tx in txs:
        smaller = min(len(tx.txs_in), len(tx.txs_out))
        txs_in.extend(tx.txs_in[:smaller])
        txs_out.extend(tx.txs_out[:smaller])
        unspents.extend(tx.unspents[:smaller])
    for tx in txs:
        smaller = min(len(tx.txs_in), len(tx.txs_out))
        txs_in.extend(tx.txs_in[smaller:])
        txs_out.extend(tx.txs_out[smaller:])
        unspents.extend(tx.unspents[smaller:])
    for spendable in spendables:
        txs_in.append(spendable.tx_in())
        unspents.append(spendable)
    for address, coin_value in payables:
        script = standard_tx_out_script(address)
        txs_out.append(TxOut(coin_value, script))

    lock_time = args.lock_time
    version = args.transaction_version

    # if no lock_time is explicitly set, inherit from the first tx or use default
    if lock_time is None:
        if txs:
            lock_time = txs[0].lock_time
        else:
            lock_time = DEFAULT_LOCK_TIME

    # if no version is explicitly set, inherit from the first tx or use default
    if version is None:
        if txs:
            version = txs[0].version
        else:
            version = DEFAULT_VERSION

    if args.remove_tx_in:
        s = set(args.remove_tx_in)
        txs_in = [tx_in for idx, tx_in in enumerate(txs_in) if idx not in s]

    if args.remove_tx_out:
        s = set(args.remove_tx_out)
        txs_out = [tx_out for idx, tx_out in enumerate(txs_out) if idx not in s]

    tx = Tx(txs_in=txs_in, txs_out=txs_out, lock_time=lock_time, version=version, unspents=unspents)

    fee = args.fee
    try:
        distribute_from_split_pool(tx, fee)
    except ValueError as ex:
        print("warning: %s" % ex.args[0], file=sys.stderr)

    unsigned_before = tx.bad_signature_count()
    unsigned_after = unsigned_before
    if unsigned_before > 0 and key_iters:
        def wif_iter(iters):
            while len(iters) > 0:
                for idx, iter in enumerate(iters):
                    try:
                        wif = next(iter)
                        yield wif
                    except StopIteration:
                        iters = iters[:idx] + iters[idx+1:]
                        break

        print("signing...", file=sys.stderr)
        sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup)

        unsigned_after = tx.bad_signature_count()
        if unsigned_after > 0:
            print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)

    if len(tx.txs_in) == 0:
        print("warning: transaction has no inputs", file=sys.stderr)

    if len(tx.txs_out) == 0:
        print("warning: transaction has no outputs", file=sys.stderr)

    include_unspents = (unsigned_after > 0)
    tx_as_hex = tx.as_hex(include_unspents=include_unspents)

    if args.output_file:
        f = args.output_file
        if f.name.endswith(".hex"):
            f.write(tx_as_hex.encode("utf8"))
        else:
            tx.stream(f)
            if include_unspents:
                tx.stream_unspents(f)
        f.close()
    elif args.show_unspents:
        for spendable in tx.tx_outs_as_spendable():
            print(spendable.as_text())
    else:
        if not tx.missing_unspents():
            check_fees(tx)
        dump_tx(tx, args.network, args.verbose_signature, args.disassemble, args.trace, args.pdb)
        if include_unspents:
            print("including unspents in hex dump since transaction not fully signed")
        print(tx_as_hex)

    if args.cache:
        if tx_db is None:
            warning_tx_cache = message_about_tx_cache_env()
            warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network)
            tx_db = get_tx_db(args.network)
        tx_db.put(tx)

    if args.bitcoind_url:
        if tx_db is None:
            warning_tx_cache = message_about_tx_cache_env()
            warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network)
            tx_db = get_tx_db(args.network)
        validate_bitcoind(tx, tx_db, args.bitcoind_url)

    if tx.missing_unspents():
        print("\n** can't validate transaction as source transactions missing", file=sys.stderr)
    else:
        try:
            if tx_db is None:
                warning_tx_cache = message_about_tx_cache_env()
                warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(args.network)
                tx_db = get_tx_db(args.network)
            tx.validate_unspents(tx_db)
            print('all incoming transaction values validated')
        except BadSpendableError as ex:
            print("\n**** ERROR: FEES INCORRECTLY STATED: %s" % ex.args[0], file=sys.stderr)
        except Exception as ex:
            print("\n*** can't validate source transactions as untampered: %s" %
                  ex.args[0], file=sys.stderr)

    # print warnings
    for m in [warning_tx_cache, warning_tx_for_tx_hash, warning_spendables]:
        if m:
            print("warning: %s" % m, file=sys.stderr)
Esempio n. 24
0
def create_signed_tx(txio_tuple):
    txins, txouts, spendables, wifs = txio_tuple
    tx = Tx( 1, txins, txouts, unspents=spendables )
    tx_utils.sign_tx( tx, wifs )
    return tx
Esempio n. 25
0
    exit("Message must be 80 characters or less")
message = hexlify(raw_message.encode()).decode('utf8')

## Get the spendable outputs we are going to use to pay the fee
spendables = spendables_for_address(bitcoin_address)
bitcoin_sum = sum(spendable.coin_value for spendable in spendables)
if(bitcoin_sum < bitcoin_fee):
    exit("Not enough satoshis to cover the fee. found: {sum} need: {fee}"
    .format(sum=bitcoin_sum,fee=bitcoin_fee))

## Create the inputs we are going to use
inputs = [spendable.tx_in() for spendable in spendables]

## If we will have change left over create an output to send it back
outputs = []
if (bitcoin_sum > bitcoin_fee):
    change_output_script = standard_tx_out_script(bitcoin_address)
    outputs.append(TxOut(bitcoin_sum - bitcoin_fee, change_output_script))

## Build the OP_RETURN output with our message
op_return_output_script = script.tools.compile("OP_RETURN %s" % message)
outputs.append(TxOut(0, op_return_output_script))

## Create the transaction and sign it with the private key
tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
tx.set_unspents(spendables)
signed_tx = sign_tx(tx, wifs=[bitcoin_private_key])

## Send the signed transaction to the network through bitcoind
## Note: that os.system() prints the response for us
system("bitcoin-cli sendrawtransaction %s" % tx.as_hex())
Esempio n. 26
0
def main():
    parser = create_parser()
    args = parser.parse_args()

    (txs, spendables, payables, key_iters, p2sh_lookup, tx_db,
     warning_tx_cache, warning_tx_for_tx_hash,
     warning_spendables) = parse_context(args, parser)

    txs_in = []
    txs_out = []
    unspents = []

    # we use a clever trick here to keep each tx_in corresponding with its tx_out
    for tx in txs:
        smaller = min(len(tx.txs_in), len(tx.txs_out))
        txs_in.extend(tx.txs_in[:smaller])
        txs_out.extend(tx.txs_out[:smaller])
        unspents.extend(tx.unspents[:smaller])
    for tx in txs:
        smaller = min(len(tx.txs_in), len(tx.txs_out))
        txs_in.extend(tx.txs_in[smaller:])
        txs_out.extend(tx.txs_out[smaller:])
        unspents.extend(tx.unspents[smaller:])
    for spendable in spendables:
        txs_in.append(spendable.tx_in())
        unspents.append(spendable)
    for address, coin_value in payables:
        script = standard_tx_out_script(address)
        txs_out.append(TxOut(coin_value, script))

    lock_time = args.lock_time
    version = args.transaction_version

    # if no lock_time is explicitly set, inherit from the first tx or use default
    if lock_time is None:
        if txs:
            lock_time = txs[0].lock_time
        else:
            lock_time = DEFAULT_LOCK_TIME

    # if no version is explicitly set, inherit from the first tx or use default
    if version is None:
        if txs:
            version = txs[0].version
        else:
            version = DEFAULT_VERSION

    if args.remove_tx_in:
        s = set(args.remove_tx_in)
        txs_in = [tx_in for idx, tx_in in enumerate(txs_in) if idx not in s]

    if args.remove_tx_out:
        s = set(args.remove_tx_out)
        txs_out = [
            tx_out for idx, tx_out in enumerate(txs_out) if idx not in s
        ]

    tx = Tx(txs_in=txs_in,
            txs_out=txs_out,
            lock_time=lock_time,
            version=version,
            unspents=unspents)

    fee = args.fee
    try:
        distribute_from_split_pool(tx, fee)
    except ValueError as ex:
        print("warning: %s" % ex.args[0], file=sys.stderr)

    unsigned_before = tx.bad_signature_count()
    unsigned_after = unsigned_before
    if unsigned_before > 0 and key_iters:

        def wif_iter(iters):
            while len(iters) > 0:
                for idx, iter in enumerate(iters):
                    try:
                        wif = next(iter)
                        yield wif
                    except StopIteration:
                        iters = iters[:idx] + iters[idx + 1:]
                        break

        print("signing...", file=sys.stderr)
        sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup)

        unsigned_after = tx.bad_signature_count()
        if unsigned_after > 0:
            print("warning: %d TxIn items still unsigned" % unsigned_after,
                  file=sys.stderr)

    if len(tx.txs_in) == 0:
        print("warning: transaction has no inputs", file=sys.stderr)

    if len(tx.txs_out) == 0:
        print("warning: transaction has no outputs", file=sys.stderr)

    include_unspents = (unsigned_after > 0)
    tx_as_hex = tx.as_hex(include_unspents=include_unspents)

    if args.output_file:
        f = args.output_file
        if f.name.endswith(".hex"):
            f.write(tx_as_hex.encode("utf8"))
        else:
            tx.stream(f)
            if include_unspents:
                tx.stream_unspents(f)
        f.close()
    elif args.show_unspents:
        for spendable in tx.tx_outs_as_spendable():
            print(spendable.as_text())
    else:
        if not tx.missing_unspents():
            check_fees(tx)
        dump_tx(tx, args.network, args.verbose_signature, args.disassemble,
                args.trace, args.pdb)
        if include_unspents:
            print(
                "including unspents in hex dump since transaction not fully signed"
            )
        print(tx_as_hex)

    if args.cache:
        if tx_db is None:
            warning_tx_cache = message_about_tx_cache_env()
            warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(
                args.network)
            tx_db = get_tx_db(args.network)
        tx_db.put(tx)

    if args.bitcoind_url:
        if tx_db is None:
            warning_tx_cache = message_about_tx_cache_env()
            warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(
                args.network)
            tx_db = get_tx_db(args.network)
        validate_bitcoind(tx, tx_db, args.bitcoind_url)

    if tx.missing_unspents():
        print("\n** can't validate transaction as source transactions missing",
              file=sys.stderr)
    else:
        try:
            if tx_db is None:
                warning_tx_cache = message_about_tx_cache_env()
                warning_tx_for_tx_hash = message_about_tx_for_tx_hash_env(
                    args.network)
                tx_db = get_tx_db(args.network)
            tx.validate_unspents(tx_db)
            print('all incoming transaction values validated')
        except BadSpendableError as ex:
            print("\n**** ERROR: FEES INCORRECTLY STATED: %s" % ex.args[0],
                  file=sys.stderr)
        except Exception as ex:
            print(
                "\n*** can't validate source transactions as untampered: %s" %
                ex.args[0],
                file=sys.stderr)

    # print warnings
    for m in [warning_tx_cache, warning_tx_for_tx_hash, warning_spendables]:
        if m:
            print("warning: %s" % m, file=sys.stderr)
Esempio n. 27
0
File: tx.py Progetto: xbee/icebox
print('Your Bitcoin address is...', address)
print('Your --privkey-bytes', hexlify(key_bytes).decode())

try:
    spendables = spendables_for_address(address, None)
    print('Spending', spendables)
except HTTPError as e:
    print(
        'Blockchain throws a 500 error if there are no spendables. Try sending some coins to',
        address, 'and try again. Remeber to copy privkey-bytes.')
    sys.exit()

tx = create_tx(spendables, [args.send_all_to])
print('TX created:', repr(tx))

sign_tx(tx, [private_key.wif(False), private_key.wif(True)])
print('Final TX:', tx)

# print('TX Send Attempt:', send_tx(tx))
'''
tx_in = TxIn("<utxo hash in binary here>", <utxo position, usually between 0 and 5>)
script = standard_tx_out_script(address)
tx_out = TxOut(<btc amount to send - fee>, script)
tx = Tx(1, [tx_in], [tx_out])
lookup = <this part you have to figure out>
tx.sign(lookup)
print tx.as_hex()



def privateKeyToWif(key_hex):
Esempio n. 28
0
def main(params=[]):
    """
    Main function that deals with the information

    Args:
    -----
        params: List of arguments received from the command line.
    """
    banner = """
     ____  _            _        _           _          ____ ____   ____
    | __ )| | ___   ___| | _____| |__   __ _(_)_ __    / ___|___ \ / ___|
    |  _ \| |/ _ \ / __| |/ / __| '_ \ / _` | | '_ \  | |     __) | |
    | |_) | | (_) | (__|   < (__| | | | (_| | | | | | | |___ / __/| |___
    |____/|_|\___/ \___|_|\_\___|_| |_|\__,_|_|_| |_|  \____|_____|\____|

                    A PoC to push data to several blockchains by:
                            - Yaiza Rubio (@yrubiosec)
                            - Félix Brezo (@febrezo)"""
    print(banner)
    # Grabbing the parser
    parser = getParser()

    if params != None:
        args = parser.parse_args(params)
    else:
        args = parser.parse_args()

    # Getting the brain wallet
    network = raw_input(
        "\n> Choose type of network (BTC, XTN, LTC, XLT...) [XTN] ") or "XTN"
    while network not in VALID_NETWORKS:
        print("> The value provided '" + network +
              "' is not a possible option. Choose one of the following:\n" +
              json.dumps(VALID_NETWORKS.keys(), indent=2))
        network = raw_input(
            "\n> Choose type of network (BTC, XTN, LTC, XLT...) [XTN] "
        ) or "XTN"
    print("\tNetwork: " + network)

    # Defining the target address
    admin_type = None
    while admin_type not in ["brain", "other"]:
        admin_type = raw_input(
            "\n> Set ADMIN address: 'brain' or 'other'? [brain] ") or "brain"
        if admin_type == "brain":
            brain = raw_input("\n> Say word for the ADMIN brainwallet: ")
            print("\tWe will generate the details for the target address:")
            priv = generatePrivateKey(brain)
            src_address = priv.address()
        elif admin_type == "other":
            print(
                "\tNote that you might not know the private key of this address so you can lose your balance:"
            )
            src_WIF = raw_input(
                "\n> Set the admin private key in WIF format: ")
            #TODO:
            priv = Key.from_text(src_WIF)
        else:
            admin_type = raw_input(
                "\n> Set ADMIN address: 'brain' or 'other'? [brain] "
            ) or "brain"

    # Get the spendable outputs we are going to use to pay the fee
    print("\n> Choosing a provider!")
    provider = getProvider(network=network)
    spendables = provider.spendables_for_address(priv.address())

    # Summing it up
    source_balance = sum(spendable.coin_value for spendable in spendables)
    print("\tBalance: " + str(source_balance))

    if source_balance <= 0:
        print(
            "\n> No money in the account! You will need an account with some! Exiting!"
        )
        sys.exit()

    # Defining the message to be send
    message_str = raw_input("\n> Set message to send: ")
    # No more than 80 caracters please!
    if (len(message_str) > 80):
        sys.exit("Message must be 80 characters or less")
    print("\tMessage (string): " + message_str)
    message_hex = hexlify(message_str.encode()).decode('utf8')
    print("\tMessage (hexadecimal): " + message_hex)

    # Defining the target address
    target_type = None
    while target_type not in ["brain", "other"]:
        target_type = raw_input(
            "\n> Set target address: 'brain' or 'other'? [brain] ") or "brain"
        if target_type == "brain":
            brain_dst = raw_input("\n> Say word for the TARGET brainwallet: ")
            print("\tWe will generate the details for the target address:")
            target = generatePrivateKey(brain_dst)
            dst_address = target.address()
        elif target_type == "other":
            print(
                "\tNote that you might not know the private key of this address so you can lose your balance:"
            )
            dst_address = raw_input("\n> Set the target address: ")
        else:
            target_type = raw_input(
                "\n> Set target address: 'brain' or 'other'? [brain] "
            ) or "brain"

    # Defining the default fee
    try:
        fee = int(raw_input("\n> Set the fee [10000]: "))
    except:
        fee = 10000
    print("\tFee assigned: " + str(fee))

    # Generating the transaction
    print(
        "\n> We'll try to create the transaction using all the spendables for "
        + priv.address())
    # Creating the transaction
    tx = tx_utils.create_tx(spendables, [(dst_address, source_balance - fee)],
                            fee=fee)
    print("\tTx:\n" + str(tx))

    print("\n> We will create the OP_RETURN script for the message '" +
          message_str + "'.")
    # We need the hexadecimal representation of the message
    op_return_output_script = script.tools.compile("OP_RETURN %s" %
                                                   message_hex)
    print("\tOP_RETURN script:\n" + str([str(op_return_output_script)]))

    print("\n> Appending the new OP_RETURN script to the transaction:")
    tx.txs_out.append(TxOut(0, op_return_output_script))
    print("\tTx:\n" + str(tx))
    print("\tNumber of outputs: " + str(len(tx.txs_out)))
    print("\tDisplaying the outputs:\n")
    for o in tx.txs_out:
        print("\t\t- " + str(o))
    #print "\tDictionary representing the transaction:\n" + tx.__dict__

    print("\n> Signing the transaction:")
    tx_utils.sign_tx(tx, netcode=network, wifs=[priv.wif()])
    print("\tNow tx is a signed transaction:")
    print("\tTx:\n" + str(tx))

    print("\n> Showing the hexadecimal information of the SIGNED transaction:")
    print(tx.as_hex())

    if args.push:
        print("\n> We will try to push the signed transaction now:")
        pushTx(network=network, tx_hex=tx.as_hex())
    else:
        print("\n> You can push this transaction manually using curl:")
        print("\tcurl -d 'tx_hex=" + tx.as_hex() +
              "' https://chain.so/api/v2/send_tx/" + VALID_NETWORKS[network])
        print(
            "\n> You can also manually push this with the Web UI of BlockExplorer at <https://testnet.blockexplorer.com/tx/send>."
        )
        print(
            "\n> You might want to do it using Tor Browser Bundle or torify to not let even a trace to know who you are in the provider."
        )
Esempio n. 29
0
 def sign_tx(self, priv, tx):
     sign_tx(tx, wifs=[priv])
     return tx