123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- """
- mmgen-txcreate: Create a BTC transaction, sending to specified addresses
- """
- import sys
- from mmgen.Opts import *
- from mmgen.license import *
- from mmgen.config import *
- from mmgen.tx import *
- from mmgen.utils import check_opts, msg, user_confirm
- from decimal import Decimal
- prog_name = sys.argv[0].split("/")[-1]
- help_data = {
- 'prog_name': prog_name,
- 'desc': "Create a BTC transaction, sending to specified addresses",
- 'usage': "[opts] <address:amount>[,...] <transaction fee> <change address>",
- 'options': """
- -h, --help Print this help message
- -d, --outdir d Specify an alternate directory 'd' for output
- -e, --echo-passphrase Print passphrase to screen when typing it
- -i, --info Display unspent outputs and exit
- -q, --quiet Suppress warnings; overwrite files without asking
- Outputs to spend are chosen by the user via a menu.
- Ages of transactions are approximate based on an estimated block discovery
- time of %s minutes.
- """ % mins_per_block
- }
- short_opts = "hd:eiq"
- long_opts = "help","outdir=","echo_passphrase","info","quiet"
- opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
- check_opts(opts, ('outdir',))
- if debug:
- print "Processed options: %s" % repr(opts)
- print "Cmd args: %s" % repr(cmd_args)
- if len(cmd_args) == 3:
- rcpt_arg,tx_fee,change_addr = cmd_args
- check_address(change_addr)
- elif len(cmd_args) == 2:
- rcpt_arg,tx_fee = cmd_args
- change_addr = ""
- elif len(cmd_args) == 0 and 'info' in opts:
- pass
- else: usage(help_data)
- if not 'info' in opts:
- tx_out = make_tx_out(rcpt_arg)
- for i in tx_out.keys(): check_address(i)
- for i in tx_out.values(): check_btc_amt(i)
- tx_fee = check_btc_amt(tx_fee)
- c = connect_to_bitcoind()
- if not 'quiet' in opts and not 'info' in opts: do_license_msg()
- us = c.listunspent()
- unspent = sort_and_view(us)
- total = trim_exponent(sum([i.amount for i in unspent]))
- msg("Total unspent: %s BTC" % total)
- if 'info' in opts: sys.exit(0)
- send_amt = sum(tx_out.values())
- msg("Total amount to spend: %s BTC\n%s unspent outputs total" %
- (send_amt, len(unspent)))
- while True:
- sel_nums = select_outputs(unspent,"Choose the outputs to spend: ")
- msg("Selected outputs: %s" % " ".join(str(i) for i in sel_nums))
- sel_unspent = [unspent[i-1] for i in sel_nums]
- lbls = set([verify_mmgen_label(
- i.account,return_str=True,check_label_len=True)
- for i in sel_unspent])
- lbls.discard("")
- if lbls and len(lbls) < len(sel_unspent):
- msg(txmsg['mixed_inputs'] % ", ".join(sorted(lbls)))
- if not user_confirm("Accept?"):
- continue
- total_in = trim_exponent(sum([o.amount for o in sel_unspent]))
- change = trim_exponent(total_in - (send_amt + tx_fee))
- if change >= 0:
- prompt = "Transaction produces %s BTC in change. OK?" % change
- if user_confirm(prompt,default_yes=True):
- break
- else:
- msg(txmsg['not_enough_btc'] % change)
- if change > 0 and not change_addr:
- msg(txmsg['throwaway_change'] % (change, total_in-tx_fee))
- sys.exit(2)
- tx_in = [{"txid":i.txid, "vout":i.vout} for i in sel_unspent]
- for i in tx_out.keys(): tx_out[i] = float(tx_out[i])
- if change: tx_out[change_addr] = float(change)
- tx_hex = c.createrawtransaction(tx_in,tx_out)
- prompt = "Transaction successfully created\nView decoded transaction?"
- if user_confirm(prompt,default_yes=False):
- view_tx_data(c,[i.__dict__ for i in sel_unspent],tx_hex)
- prompt = "Save transaction?"
- if user_confirm(prompt,default_yes=True):
- print_tx_to_file(tx_hex,sel_unspent,send_amt,opts)
|