globalvars.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. globalvars.py: Constants and configuration options for the MMGen suite
  20. """
  21. import sys,os
  22. from collections import namedtuple
  23. from .devtools import *
  24. from .base_obj import Lockable
  25. def die(exit_val,s=''):
  26. if s:
  27. sys.stderr.write(s+'\n')
  28. sys.exit(exit_val)
  29. class GlobalContext(Lockable):
  30. """
  31. Set global vars to default values
  32. Globals are overridden in this order:
  33. 1 - config file
  34. 2 - environmental vars
  35. 3 - command line
  36. """
  37. _autolock = False
  38. _set_ok = ('user_entropy','session')
  39. _reset_ok = ('stdout','stderr','accept_defaults')
  40. _use_class_attr = True
  41. # Constants:
  42. proj_name = 'MMGen'
  43. proj_url = 'https://github.com/mmgen/mmgen'
  44. prog_name = os.path.basename(sys.argv[0])
  45. author = 'The MMGen Project'
  46. email = '<mmgen@tuta.io>'
  47. Cdates = '2013-2022'
  48. stdin_tty = sys.stdin.isatty()
  49. stdout = sys.stdout
  50. stderr = sys.stderr
  51. http_timeout = 60
  52. err_disp_timeout = 0.7
  53. short_disp_timeout = 0.3
  54. min_time_precision = 18
  55. # Variables - these might be altered at runtime:
  56. user_entropy = b''
  57. dfl_hash_preset = '3'
  58. usr_randchars = 30
  59. tx_fee_adj = 1.0
  60. tx_confs = 3
  61. # Constant vars - some of these might be overridden in opts.py, but they don't change thereafter
  62. coin = ''
  63. token = ''
  64. debug = False
  65. debug_opts = False
  66. debug_rpc = False
  67. debug_addrlist = False
  68. debug_subseed = False
  69. quiet = False
  70. no_license = False
  71. force_256_color = False
  72. testnet = False
  73. regtest = False
  74. accept_defaults = False
  75. # rpc:
  76. rpc_host = ''
  77. rpc_port = 0
  78. rpc_user = ''
  79. rpc_password = ''
  80. ignore_daemon_version = False
  81. monero_wallet_rpc_host = 'localhost'
  82. monero_wallet_rpc_user = 'monero'
  83. monero_wallet_rpc_password = ''
  84. aiohttp_rpc_queue_len = 16
  85. session = None
  86. cached_balances = False
  87. # regtest:
  88. bob = False
  89. alice = False
  90. # miscellaneous features:
  91. use_internal_keccak_module = False
  92. enable_erigon = False
  93. # test suite:
  94. bogus_send = False
  95. debug_utf8 = False
  96. traceback = False
  97. test_suite = False
  98. test_suite_deterministic = False
  99. test_suite_popen_spawn = False
  100. terminal_width = 0
  101. mnemonic_entry_modes = {}
  102. color = bool(
  103. ( sys.stdout.isatty() and not os.getenv('MMGEN_TEST_SUITE_PEXPECT') ) or
  104. os.getenv('MMGEN_FORCE_COLOR')
  105. )
  106. for k in ('linux','win','msys'):
  107. if sys.platform.startswith(k):
  108. platform = { 'linux':'linux', 'win':'win', 'msys':'win' }[k]
  109. break
  110. else:
  111. die(1,f'{sys.platform!r}: platform not supported by {proj_name}')
  112. if os.getenv('HOME'): # Linux or MSYS2
  113. home_dir = os.getenv('HOME')
  114. elif platform == 'win': # Windows without MSYS2 - not supported
  115. die(1,f'$HOME not set! {proj_name} for Windows must be run in MSYS2 environment')
  116. else:
  117. die(2,'$HOME is not set! Unable to determine home directory')
  118. data_dir_root,data_dir,cfg_file = (None,None,None)
  119. daemon_data_dir = '' # set by user
  120. daemon_id = ''
  121. # must match CoinProtocol.coins
  122. core_coins = ('btc','bch','ltc','eth','etc','zec','xmr')
  123. # global var sets user opt:
  124. global_sets_opt = (
  125. 'debug',
  126. 'minconf',
  127. 'quiet',
  128. 'tx_confs',
  129. 'tx_fee_adj',
  130. 'use_internal_keccak_module',
  131. 'usr_randchars' )
  132. # user opt sets global var:
  133. opt_sets_global = ( 'cached_balances', )
  134. # 'long' opt sets global var (subset of common_opts_data):
  135. common_opts = (
  136. 'accept_defaults',
  137. 'aiohttp_rpc_queue_len',
  138. 'alice',
  139. 'bob',
  140. 'coin',
  141. 'color',
  142. 'daemon_data_dir',
  143. 'daemon_id',
  144. 'force_256_color',
  145. 'http_timeout',
  146. 'ignore_daemon_version',
  147. 'no_license',
  148. 'regtest',
  149. 'rpc_backend',
  150. 'rpc_host',
  151. 'rpc_password',
  152. 'rpc_port',
  153. 'rpc_user',
  154. 'testnet',
  155. 'token' )
  156. # opts not in common_opts but required to be set during opts initialization
  157. init_opts = ('show_hash_presets','yes','verbose')
  158. incompatible_opts = (
  159. ('help','longhelp'),
  160. ('bob','alice'),
  161. ('label','keep_label'),
  162. ('tx_id','info'),
  163. ('tx_id','terse_info'),
  164. ('batch','rescan'), # TODO: still incompatible?
  165. )
  166. cfg_file_opts = (
  167. 'color',
  168. 'daemon_data_dir',
  169. 'debug',
  170. 'force_256_color',
  171. 'hash_preset',
  172. 'http_timeout',
  173. 'max_input_size',
  174. 'max_tx_file_size',
  175. 'mnemonic_entry_modes',
  176. 'monero_wallet_rpc_host',
  177. 'monero_wallet_rpc_password',
  178. 'monero_wallet_rpc_user',
  179. 'no_license',
  180. 'quiet',
  181. 'regtest',
  182. 'rpc_host',
  183. 'rpc_password',
  184. 'rpc_port',
  185. 'rpc_user',
  186. 'subseeds',
  187. 'testnet',
  188. 'tx_fee_adj',
  189. 'usr_randchars',
  190. 'bch_max_tx_fee',
  191. 'btc_max_tx_fee',
  192. 'eth_max_tx_fee',
  193. 'ltc_max_tx_fee',
  194. 'bch_ignore_daemon_version',
  195. 'btc_ignore_daemon_version',
  196. 'etc_ignore_daemon_version',
  197. 'eth_ignore_daemon_version',
  198. 'ltc_ignore_daemon_version',
  199. 'eth_mainnet_chain_names',
  200. 'eth_testnet_chain_names' )
  201. # Supported environmental vars
  202. # The corresponding vars (lowercase, minus 'mmgen_') must be initialized in g
  203. # 'DISABLE_' env vars disable the corresponding var in g
  204. env_opts = (
  205. 'MMGEN_DEBUG_ALL', # special: there is no g.debug_all var
  206. 'MMGEN_TEST_SUITE',
  207. 'MMGEN_TEST_SUITE_DETERMINISTIC',
  208. 'MMGEN_TEST_SUITE_POPEN_SPAWN',
  209. 'MMGEN_TERMINAL_WIDTH',
  210. 'MMGEN_BOGUS_SEND',
  211. 'MMGEN_DEBUG',
  212. 'MMGEN_DEBUG_OPTS',
  213. 'MMGEN_DEBUG_RPC',
  214. 'MMGEN_DEBUG_ADDRLIST',
  215. 'MMGEN_DEBUG_UTF8',
  216. 'MMGEN_DEBUG_SUBSEED',
  217. 'MMGEN_QUIET',
  218. 'MMGEN_FORCE_256_COLOR',
  219. 'MMGEN_MIN_URANDCHARS',
  220. 'MMGEN_NO_LICENSE',
  221. 'MMGEN_RPC_HOST',
  222. 'MMGEN_RPC_FAIL_ON_COMMAND',
  223. 'MMGEN_TESTNET',
  224. 'MMGEN_REGTEST',
  225. 'MMGEN_TRACEBACK',
  226. 'MMGEN_RPC_BACKEND',
  227. 'MMGEN_IGNORE_DAEMON_VERSION',
  228. 'MMGEN_USE_STANDALONE_SCRYPT_MODULE',
  229. 'MMGEN_ENABLE_ERIGON',
  230. 'MMGEN_DISABLE_COLOR',
  231. 'MMGEN_DISABLE_MSWIN_PW_WARNING',
  232. )
  233. infile_opts = (
  234. 'keys_from_file',
  235. 'mmgen_keys_from_file',
  236. 'passwd_file',
  237. 'keysforaddrs',
  238. 'comment_file',
  239. 'contract_data',
  240. )
  241. # Auto-typechecked and auto-set opts. These have no corresponding value in g.
  242. # First value in list is the default
  243. ov = namedtuple('autoset_opt_info',['type','choices'])
  244. autoset_opts = {
  245. 'fee_estimate_mode': ov('nocase_pfx', ['conservative','economical']),
  246. 'rpc_backend': ov('nocase_pfx', ['auto','httplib','curl','aiohttp','requests']),
  247. }
  248. if platform == 'win':
  249. autoset_opts['rpc_backend'].choices.remove('aiohttp')
  250. _skip_type_check = ('stdout','stderr')
  251. auto_typeset_opts = {
  252. 'seed_len': int,
  253. 'subseeds': int,
  254. 'vsize_adj': float,
  255. }
  256. min_screen_width = 80
  257. minconf = 1
  258. max_tx_file_size = 100000
  259. max_input_size = 1024 * 1024
  260. passwd_max_tries = 5
  261. max_urandchars = 80
  262. min_urandchars = 10
  263. force_standalone_scrypt_module = False
  264. if os.getenv('MMGEN_TEST_SUITE'):
  265. err_disp_timeout = 0.1
  266. short_disp_timeout = 0.1
  267. if os.getenv('MMGEN_TEST_SUITE_POPEN_SPAWN'):
  268. stdin_tty = True
  269. if prog_name == 'unit_tests.py':
  270. _set_ok += ('debug_subseed',)
  271. _reset_ok += ('force_standalone_scrypt_module','session')
  272. if os.getenv('MMGEN_DEBUG_ALL'):
  273. for name in env_opts:
  274. if name[:11] == 'MMGEN_DEBUG':
  275. os.environ[name] = '1'
  276. def _get_importlib_resources_files(self):
  277. """
  278. this is an expensive import, so do only when required
  279. """
  280. try:
  281. from importlib.resources import files # Python 3.9
  282. except ImportError:
  283. from importlib_resources import files
  284. return files
  285. @property
  286. def version(self):
  287. files = self._get_importlib_resources_files()
  288. return files('mmgen').joinpath('data','version').read_text().strip()
  289. @property
  290. def release_date(self):
  291. files = self._get_importlib_resources_files()
  292. return files('mmgen').joinpath('data','release_date').read_text().strip()
  293. g = GlobalContext()