globalvars.py 8.9 KB

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