globalvars.py 8.9 KB

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