globalvars.py 8.2 KB

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