bip_eth_key.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #https://github.com/meherett/python-hdwallet
  2. from hdwallet import BIP44HDWallet
  3. from hdwallet.cryptocurrencies import EthereumMainnet
  4. from hdwallet.derivations import BIP44Derivation
  5. from hdwallet.utils import generate_mnemonic
  6. from typing import Optional
  7. import hashlib
  8. import os
  9. import hashlib
  10. import base64
  11. from Crypto.Cipher import AES
  12. from Crypto.Util.Padding import pad,unpad
  13. from Crypto.Random import get_random_bytes #only for AES CBC mode
  14. import random
  15. from tqdm import tqdm
  16. key = ''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 16))
  17. iv = ''.join(random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 16))
  18. def encrypt(raw):
  19. raw = pad(raw.encode(),16)
  20. #cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
  21. cipher = AES.new(key.encode('utf-8'),AES.MODE_CBC,iv.encode('utf-8'))
  22. return base64.b64encode(cipher.encrypt(raw))
  23. def get_mnemonic():
  24. mnemonic = generate_mnemonic(language="english", strength=128)
  25. array_mnemonic = mnemonic.split(' ')
  26. input('print enter to get 1/2 mnemonic...')
  27. print('key',key)
  28. print('mnemonic 1/2:', array_mnemonic[:6])
  29. input('print enter to get 2/2 mnemonic...')
  30. os.system('clear')
  31. print('iv',iv)
  32. print('mnemonic 1/2:', array_mnemonic[6:])
  33. input('print enter to generate keys...')
  34. os.system('clear')
  35. return mnemonic
  36. def generate_key_pair(index_from, index_to, PASSPHRASE: Optional[str] = None):
  37. MNEMONIC: str = get_mnemonic()
  38. # Initialize Ethereum mainnet BIP44HDWallet
  39. bip44_hdwallet: BIP44HDWallet = BIP44HDWallet(cryptocurrency=EthereumMainnet)
  40. # Get Ethereum BIP44HDWallet from mnemonic
  41. bip44_hdwallet.from_mnemonic(
  42. mnemonic=MNEMONIC, language="english", passphrase=PASSPHRASE
  43. )
  44. # Clean default BIP44 derivation indexes/paths
  45. bip44_hdwallet.clean_derivation()
  46. #print("Mnemonic:", bip44_hdwallet.mnemonic())
  47. #print("Base HD Path: m/44'/60'/0'/0/{address_index}", "\n")
  48. # Get Ethereum BIP44HDWallet information's from address index
  49. fp = 'keys.csv'
  50. with open(fp, 'w') as f:
  51. for address_index in tqdm(range(index_from, index_to)):
  52. # Derivation from Ethereum BIP44 derivation path
  53. bip44_derivation: BIP44Derivation = BIP44Derivation(
  54. cryptocurrency=EthereumMainnet, account=0, change=False, address=address_index)
  55. # Drive Ethereum BIP44HDWallet
  56. bip44_hdwallet.from_path(path=bip44_derivation)
  57. # Print address_index, path, address and private_key
  58. #print(bip44_hdwallet.private_key())
  59. encry = encrypt(bip44_hdwallet.private_key())
  60. f.write(f'{address_index} {bip44_hdwallet.address()} {encry.decode("utf-8", "ignore")}\n')
  61. #print('encrypt_pk', encry.decode("utf-8", "ignore"))
  62. #print(f"({address_index}) {bip44_hdwallet.path()} {bip44_hdwallet.address()} 0x{bip44_hdwallet.private_key()}")
  63. # Clean derivation indexes/paths
  64. bip44_hdwallet.clean_derivation()
  65. f.close()
  66. generate_key_pair(0, 100)