globalvars.py 8.8 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.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 = ()
  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. is_txprog = prog_name == 'mmgen-regtest' or prog_name.startswith('mmgen-tx')
  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. 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. debug_tw = 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. cached_balances = False
  87. # regtest:
  88. bob = False
  89. alice = False
  90. carol = False
  91. regtest_user = None
  92. # miscellaneous features:
  93. use_internal_keccak_module = False
  94. enable_erigon = False
  95. # test suite:
  96. bogus_send = False
  97. debug_utf8 = False
  98. exec_wrapper = False
  99. test_suite = False
  100. test_suite_deterministic = False
  101. test_suite_popen_spawn = 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. 'tx_confs',
  133. 'tx_fee_adj',
  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. 'force_256_color',
  175. 'hash_preset',
  176. 'http_timeout',
  177. 'max_input_size',
  178. 'max_tx_file_size',
  179. 'mnemonic_entry_modes',
  180. 'monero_wallet_rpc_host',
  181. 'monero_wallet_rpc_password',
  182. 'monero_wallet_rpc_user',
  183. 'no_license',
  184. 'quiet',
  185. 'regtest',
  186. 'rpc_host',
  187. 'rpc_password',
  188. 'rpc_port',
  189. 'rpc_user',
  190. 'subseeds',
  191. 'testnet',
  192. 'tx_fee_adj',
  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_QUIET',
  224. 'MMGEN_FORCE_256_COLOR',
  225. 'MMGEN_MIN_URANDCHARS',
  226. 'MMGEN_NO_LICENSE',
  227. 'MMGEN_RPC_HOST',
  228. 'MMGEN_RPC_FAIL_ON_COMMAND',
  229. 'MMGEN_TESTNET',
  230. 'MMGEN_REGTEST',
  231. 'MMGEN_EXEC_WRAPPER',
  232. 'MMGEN_RPC_BACKEND',
  233. 'MMGEN_IGNORE_DAEMON_VERSION',
  234. 'MMGEN_USE_STANDALONE_SCRYPT_MODULE',
  235. 'MMGEN_ENABLE_ERIGON',
  236. 'MMGEN_DISABLE_COLOR',
  237. 'MMGEN_DISABLE_MSWIN_PW_WARNING',
  238. )
  239. infile_opts = (
  240. 'keys_from_file',
  241. 'mmgen_keys_from_file',
  242. 'passwd_file',
  243. 'keysforaddrs',
  244. 'comment_file',
  245. 'contract_data',
  246. )
  247. # Auto-typechecked and auto-set opts. These have no corresponding value in g.
  248. # First value in list is the default
  249. ov = namedtuple('autoset_opt_info',['type','choices'])
  250. autoset_opts = {
  251. 'fee_estimate_mode': ov('nocase_pfx', ['conservative','economical']),
  252. 'rpc_backend': ov('nocase_pfx', ['auto','httplib','curl','aiohttp','requests']),
  253. }
  254. if platform == 'win':
  255. _skip_type_check = ('stdout','stderr')
  256. auto_typeset_opts = {
  257. 'seed_len': int,
  258. 'subseeds': int,
  259. 'vsize_adj': float,
  260. }
  261. min_screen_width = 80
  262. minconf = 1
  263. max_tx_file_size = 100000
  264. max_input_size = 1024 * 1024
  265. passwd_max_tries = 5
  266. max_urandchars = 80
  267. min_urandchars = 10
  268. force_standalone_scrypt_module = False
  269. if os.getenv('MMGEN_TEST_SUITE'):
  270. err_disp_timeout = 0.1
  271. short_disp_timeout = 0.1
  272. if os.getenv('MMGEN_TEST_SUITE_POPEN_SPAWN'):
  273. stdin_tty = True
  274. if prog_name == 'unit_tests.py':
  275. _set_ok += ('debug_subseed',)
  276. _reset_ok += ('force_standalone_scrypt_module',)
  277. if os.getenv('MMGEN_DEBUG_ALL'):
  278. for name in env_opts:
  279. if name[:11] == 'MMGEN_DEBUG':
  280. os.environ[name] = '1'
  281. def get_mmgen_data_file(self,filename,package='mmgen'):
  282. """
  283. this is an expensive import, so do only when required
  284. """
  285. # Resource will be unpacked and then cleaned up if necessary, see:
  286. # https://docs.python.org/3/library/importlib.html:
  287. # Note: This module provides functionality similar to pkg_resources Basic
  288. # Resource Access without the performance overhead of that package.
  289. # https://importlib-resources.readthedocs.io/en/latest/migration.html
  290. # https://setuptools.readthedocs.io/en/latest/pkg_resources.html
  291. try:
  292. from importlib.resources import files # Python 3.9
  293. except ImportError:
  294. from importlib_resources import files
  295. return files(package).joinpath('data',filename).read_text()
  296. @property
  297. def version(self):
  298. return self.get_mmgen_data_file(
  299. filename = 'version',
  300. package = 'mmgen_node_tools' if self.prog_name.startswith('mmnode-') else 'mmgen'
  301. ).strip()
  302. @property
  303. def release_date(self):
  304. return self.get_mmgen_data_file(filename='release_date').strip()
  305. g = GlobalContext()