main_xmrwallet.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen/main_xmrwallet: Perform various Monero wallet operations for addresses
  20. in an MMGen XMR key-address file
  21. """
  22. from .common import *
  23. from .xmrwallet import xmrwallet_uarg_info,MoneroWalletOps
  24. opts_data = {
  25. 'text': {
  26. 'desc': """Perform various Monero wallet operations for addresses
  27. in an MMGen XMR key-address file""",
  28. 'usage2': [
  29. '[opts] create <xmr_keyaddrfile> [wallets]',
  30. '[opts] sync <xmr_keyaddrfile> [wallets]',
  31. '[opts] transfer <xmr_keyaddrfile> <transfer_spec>',
  32. '[opts] sweep <xmr_keyaddrfile> <sweep_spec>',
  33. '[opts] relay <TX_file>',
  34. ],
  35. 'options': """
  36. -h, --help Print this help message
  37. --, --longhelp Print help message for long options (common
  38. options)
  39. -b, --rescan-blockchain Rescan the blockchain if wallet fails to sync
  40. -d, --outdir=D Save transaction files to directory 'D'
  41. instead of the working directory
  42. -D, --daemon=H:P Connect to the monerod at {D}
  43. -R, --tx-relay-daemon=H:P[:H:P] Relay transactions via a monerod specified by
  44. {R}
  45. -k, --use-internal-keccak-module Force use of the internal keccak module
  46. -p, --hash-preset=P Use scrypt hash preset 'P' for password
  47. hashing (default: '{g.dfl_hash_preset}')
  48. -r, --restore-height=H Scan from height 'H' when creating wallets
  49. -R, --do-not-relay Save transaction to file instead of relaying
  50. -s, --no-start-wallet-daemon Don’t start the wallet daemon at startup
  51. -S, --no-stop-wallet-daemon Don’t stop the wallet daemon at exit
  52. -w, --wallet-dir=D Output or operate on wallets in directory 'D'
  53. instead of the working directory
  54. """,
  55. 'notes': """
  56. All operations require a running Monero daemon. Unless --daemon is specified,
  57. the monerod is assumed to be listening on localhost at the default RPC port.
  58. If --tx-relay-daemon is specified, the monerod at HOST:PORT will be used to
  59. relay any created transactions. PROXY_HOST:PROXY_PORT, if specified, may
  60. point to a SOCKS proxy, in which case HOST may be a Tor onion address.
  61. All communications use the RPC protocol via SSL (HTTPS) or Tor. RPC over
  62. plain HTTP is not supported.
  63. SUPPORTED OPERATIONS
  64. create - create wallet for all or specified addresses in key-address file
  65. sync - sync wallet for all or specified addresses in key-address file
  66. transfer - transfer specified XMR amount to specified address from specified
  67. wallet:account
  68. sweep - sweep funds in specified wallet:account to new address in same
  69. account or new account in another wallet
  70. relay - relay a transaction from a transaction file created using 'sweep'
  71. or 'transfer' with the --do-not-relay option
  72. CREATE AND SYNC OPERATION NOTES
  73. These operations take an optional `wallets` argument: one or more address
  74. indexes (expressed as a comma-separated list, hyphenated range, or both)
  75. in the specified key-address file, each corresponding to a Monero wallet
  76. to be created or synced. If omitted, all wallets are operated upon.
  77. TRANSFER OPERATION NOTES
  78. The transfer operation takes a `transfer specifier` arg with the following
  79. format:
  80. SOURCE:ACCOUNT:ADDRESS,AMOUNT
  81. where SOURCE is a wallet number; ACCOUNT the source account index; and ADDRESS
  82. and AMOUNT the destination Monero address and XMR amount, respectively.
  83. SWEEP OPERATION NOTES
  84. The sweep operation takes a `sweep specifier` arg with the following format:
  85. SOURCE:ACCOUNT[,DEST]
  86. where SOURCE and DEST are wallet numbers and ACCOUNT an account index.
  87. If DEST is omitted, a new address will be created in ACCOUNT of SOURCE and
  88. all funds from ACCOUNT of SOURCE will be swept into it.
  89. If DEST is included, all funds from ACCOUNT of SOURCE will be swept into a
  90. newly created account in DEST, or the last existing account, if requested
  91. by the user.
  92. The user is prompted before addresses are created or funds are transferred.
  93. Note that multiple sweep operations may be required to sweep all the funds
  94. in an account.
  95. RELAY OPERATION NOTES
  96. By default, transactions are relayed to a monerod running on localhost at the
  97. default RPC port. To relay transactions to a remote or non-default monerod
  98. via optional SOCKS proxy, use the --tx-relay-daemon option described above.
  99. WARNING
  100. To avoid exposing your private keys on a network-connected machine, you’re
  101. strongly advised to create all transactions offline using the --do-not-relay
  102. option. For this, a monerod with a fully synced blockchain must be running
  103. on the offline machine. The resulting transaction files are then sent using
  104. the 'relay' operation.
  105. EXAMPLES
  106. Generate an XMR key-address file with 5 addresses from your default wallet:
  107. $ mmgen-keygen --coin=xmr 1-5
  108. Create 3 Monero wallets from the key-address file:
  109. $ mmgen-xmrwallet create *.akeys.mmenc 1-3
  110. After updating the blockchain, sync wallets 1 and 2:
  111. $ mmgen-xmrwallet sync *.akeys.mmenc 1,2
  112. Sweep all funds from account #0 of wallet 1 to a new address:
  113. $ mmgen-xmrwallet sweep *.akeys.mmenc 1:0
  114. Same as above, but use a TX relay on the Tor network:
  115. $ mmgen-xmrwallet --tx-relay-daemon=abcdefghijklmnop.onion:127.0.0.1:9050 sweep *.akeys.mmenc 1:0
  116. Sweep all funds from account #0 of wallet 1 to wallet 2:
  117. $ mmgen-xmrwallet sweep *.akeys.mmenc 1:0,2
  118. Send 0.1 XMR from account #0 of wallet 2 to an external address:
  119. $ mmgen-xmrwallet transfer *.akeys.mmenc 2:0:<monero address>,0.1
  120. Sweep all funds from account #0 of wallet 2 to a new address, saving the
  121. transaction to a file:
  122. $ mmgen-xmrwallet --do-not-relay sweep *.akeys.mmenc 2:0
  123. Relay the created sweep transaction via a host on the Tor network:
  124. $ mmgen-xmrwallet --tx-relay-daemon=abcdefghijklmnop.onion:127.0.0.1:9050 relay *XMR*.sigtx
  125. """
  126. },
  127. 'code': {
  128. 'options': lambda s: s.format(
  129. D=xmrwallet_uarg_info['daemon'].annot,
  130. R=xmrwallet_uarg_info['tx_relay_daemon'].annot,
  131. g=g,
  132. ),
  133. }
  134. }
  135. cmd_args = opts.init(opts_data)
  136. if len(cmd_args) < 2:
  137. opts.usage()
  138. op = cmd_args.pop(0)
  139. infile = cmd_args.pop(0)
  140. if op not in MoneroWalletOps.ops:
  141. die(1,f'{op!r}: unrecognized operation')
  142. wallets = spec = ''
  143. if op == 'relay':
  144. if len(cmd_args) != 0:
  145. opts.usage()
  146. elif op in ('create','sync'):
  147. if len(cmd_args) not in (0,1):
  148. opts.usage()
  149. if cmd_args:
  150. wallets = cmd_args[0]
  151. elif op in ('transfer','sweep'):
  152. if len(cmd_args) != 1:
  153. opts.usage()
  154. spec = cmd_args[0]
  155. ua = namedtuple('uargs',[ 'op', 'infile', 'wallets', 'spec' ])
  156. uo = namedtuple('uopts',[
  157. 'daemon',
  158. 'tx_relay_daemon',
  159. 'restore_height',
  160. 'rescan_blockchain',
  161. 'no_start_wallet_daemon',
  162. 'no_stop_wallet_daemon',
  163. 'do_not_relay',
  164. 'wallet_dir',
  165. ])
  166. uargs = ua( op, infile, wallets, spec )
  167. uopts = uo(
  168. opt.daemon or '',
  169. opt.tx_relay_daemon or '',
  170. opt.restore_height or 0,
  171. opt.rescan_blockchain,
  172. opt.no_start_wallet_daemon,
  173. opt.no_stop_wallet_daemon,
  174. opt.do_not_relay,
  175. opt.wallet_dir,
  176. )
  177. m = getattr(MoneroWalletOps,op)(uargs,uopts)
  178. try:
  179. if run_session(m.main()):
  180. m.post_main()
  181. except KeyboardInterrupt:
  182. ymsg('\nUser interrupt')
  183. finally:
  184. run_session(m.stop_wallet_daemon())