Browse Source

util2.py: add load_cryptodomex() function for monero-python

The MMGen Project 1 year ago
parent
commit
d10ae21a2f
3 changed files with 19 additions and 0 deletions
  1. 13 0
      mmgen/util2.py
  2. 4 0
      test/gentest.py
  3. 2 0
      test/unit_tests_d/ut_testdep.py

+ 13 - 0
mmgen/util2.py

@@ -36,6 +36,19 @@ def removeprefix(s,pfx): # workaround for pre-Python 3.9
 def removesuffix(s,sfx): # workaround for pre-Python 3.9
 	return s[:-len(sfx)] if s.endswith(sfx) else s
 
+# monkey-patch function for monero-python: permits its use with pycryptodome (e.g. MSYS2)
+# instead of the expected pycryptodomex
+def load_cryptodomex():
+	try:
+		import Cryptodome # cryptodomex
+	except ImportError:
+		try:
+			import Crypto # cryptodome
+		except ImportError:
+			die(2,'Unable to import either the ‘pycryptodomex’ or ‘pycryptodome’ package')
+		else:
+			sys.modules['Cryptodome'] = Crypto
+
 # called with no arguments by pyethereum.utils:
 def get_keccak(cfg=None,cached_ret=[]):
 

+ 4 - 0
test/gentest.py

@@ -564,8 +564,12 @@ from mmgen.key import PrivKey
 from mmgen.addr import MMGenAddrType
 from mmgen.addrgen import KeyGenerator,AddrGenerator
 from mmgen.keygen import get_backends
+from mmgen.util2 import load_cryptodomex
 from test.include.common import getrand,get_ethkey,set_globals
 
+# This must be done at top level, not in monero tool __init__
+load_cryptodomex()
+
 gtr = namedtuple('gen_tool_result',['wif','addr','viewkey'])
 sd = namedtuple('saved_data_item',['reduced','wif','addr','viewkey'])
 

+ 2 - 0
test/unit_tests_d/ut_testdep.py

@@ -46,6 +46,8 @@ class unit_tests:
 		return True
 
 	def monero_python(self,name,ut):
+		from mmgen.util2 import load_cryptodomex
+		load_cryptodomex()
 		from monero.seed import Seed
 		Seed('deadbeef' * 8).public_address()
 		return True