123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #!/usr/bin/env python
- #
- # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
- # Copyright (C) 2013 by philemon <mmgen-py@yandex.com>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- """
- mmgen-txsign: Sign a Bitcoin transaction generated by mmgen-txcreate
- """
- import sys
- #from hashlib import sha256
- from mmgen.Opts import *
- from mmgen.license import *
- from mmgen.config import *
- from mmgen.tx import *
- from mmgen.utils import *
- help_data = {
- 'prog_name': sys.argv[0].split("/")[-1],
- 'desc': "Sign a Bitcoin transaction generated by mmgen-txcreate",
- 'usage': "[opts] <transaction file> [mmgen wallet/seed/mnemonic file]",
- '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
- -f, --force-wallet-dat Force the use of wallet.dat as a key source
- -i, --info Display information about the transaction and exit
- -k, --keys-from-file k Provide additional key data from file 'k'
- -q, --quiet Suppress warnings; overwrite files without asking
- -b, --from-brain l,p Generate keys from a user-created password,
- i.e. a "brainwallet", using seed length 'l' and
- hash preset 'p' (comma-separated)
- -m, --from-mnemonic Generate keys from an electrum-like mnemonic
- -s, --from-seed Generate keys from a seed in .{} format
- Data for the --from-<what> options will be taken from a file if a second
- file is specified on the command line. Otherwise, the user will be
- prompted to enter the data.
- """.format(seed_ext)
- }
- short_opts = "hd:efik:qb:ms"
- long_opts = "help","outdir=","echo_passphrase","force_wallet_dat","info",\
- "keys_from_file=","quiet","from_brain=","from_mnemonic","from_seed"
- opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
- # Exits on invalid input
- check_opts(opts, ('outdir','from_brain'))
- if 'keys_from_file' in opts: check_infile(opts['keys_from_file'])
- if debug:
- print "Processed options: %s" % repr(opts)
- print "Cmd args: %s" % repr(cmd_args)
- if len(cmd_args) in (1,2):
- tx_file = cmd_args[0]
- check_infile(tx_file)
- else: usage(help_data)
- # Begin execution
- c = connect_to_bitcoind()
- tx_data = get_lines_from_file(tx_file,"transaction data")
- metadata,tx_hex,sig_data,inputs_data = parse_tx_data(tx_data,tx_file)
- # Are inputs mmgen addresses?
- infile,mmgen_addrs,other_addrs = "",[],[]
- # Check that all the seed IDs are the same:
- for i in inputs_data:
- if verify_mmgen_label(i['account']):
- mmgen_addrs.append(i)
- else:
- other_addrs.append(i)
- if mmgen_addrs:
- a_ids = list(set([i['account'][:8] for i in mmgen_addrs]))
- if len(a_ids) != 1:
- msg("Addresses come from different seeds! (%s)" % " ".join(a_ids))
- sys.exit(3)
- if len(cmd_args) == 2:
- infile = cmd_args[1]
- else:
- if "from_brain" in opts \
- or "from_mnemonic" in opts \
- or "from_seed" in opts:
- infile = ""
- else:
- msg("Inputs contain mmgen addresses. An MMGen wallet file must be specified on the command line (or use the '-b', '-m' or '-s' options).".strip())
- sys.exit(2)
- if other_addrs:
- if 'keys_from_file' in opts:
- add_keys = get_lines_from_file(opts['keys_from_file'])
- else:
- msg("""
- A key file must be supplied (option '-f') for the following non-mmgen
- address%s: %s""" % (
- "" if len(other_addrs) == 1 else "es",
- " ".join([i['address'] for i in other_addrs])
- ))
- sys.exit(2)
- if 'info' in opts:
- view_tx_data(c,inputs_data,tx_hex,metadata)
- sys.exit(0)
- if not 'quiet' in opts and not 'info' in opts: do_license_msg()
- msg("Successfully opened transaction file '%s'" % tx_file)
- if user_confirm("View transaction data? ",default_yes=False):
- view_tx_data(c,inputs_data,tx_hex,metadata)
- if mmgen_addrs:
- seed = get_seed(infile,opts)
- seed_id = make_chksum_8(seed)
- if seed_id != a_ids[0]:
- msg("Seed ID of wallet (%s) doesn't match that of addresses (%s)"
- % (seed_id,a_ids[0]))
- sys.exit(3)
- addr_nums = [int(i['account'].split()[0][9:]) for i in mmgen_addrs]
- from mmgen.addr import generate_addrs
- o = {'no_addresses': True, 'gen_what': "keys"}
- keys = [i['wif'] for i in generate_addrs(seed, addr_nums, o)]
- if other_addrs: keys += add_keys
- try:
- sig_tx = c.signrawtransaction(tx_hex,sig_data,keys)
- except:
- msg("Failed to sign transaction")
- sys.exit(3)
- else:
- prompt = "Enter passphrase for bitcoind wallet: "
- if 'echo_passphrase' in opts:
- password = my_raw_input(prompt)
- else:
- password = my_getpass(prompt)
- wallet_enc = True
- from mmgen.rpc import exceptions
- try:
- c.walletpassphrase(password, 9999)
- except exceptions.WalletWrongEncState:
- msg("Wallet is unencrypted")
- wallet_enc = False
- except exceptions.WalletPassphraseIncorrect:
- msg("Passphrase incorrect")
- sys.exit(3)
- except exceptions.WalletAlreadyUnlocked:
- msg("WARNING: Wallet already unlocked!")
- else:
- msg("Passphrase OK")
- try:
- sig_tx = c.signrawtransaction(tx_hex,sig_data)
- except:
- msg("Failed to sign transaction")
- if wallet_enc:
- c.walletlock()
- msg("Locking wallet")
- sys.exit(3)
- if wallet_enc:
- c.walletlock()
- msg("Locking wallet")
- if sig_tx['complete']:
- msg("Signing completed")
- else:
- msg("Signing failed: 'complete=%s'" % sig_tx['complete'])
- sys.exit(3)
- prompt = "Save signed transaction?"
- if user_confirm(prompt,default_yes=True):
- print_signed_tx_to_file(tx_hex,sig_tx['hex'],metadata,opts)
|