Skip to content

Instantly share code, notes, and snippets.

@odudex
Last active March 21, 2024 16:16
Show Gist options
  • Save odudex/1d5706b3a61ef867e83c58a1bfa14fdc to your computer and use it in GitHub Desktop.
Save odudex/1d5706b3a61ef867e83c58a1bfa14fdc to your computer and use it in GitHub Desktop.
Uses Trezor python-mnemonic to convert hex keys to BIP39 seed words and vice-versa
"""Uses Trezor python-mnemonic to convert hex keys to BIP39 seed words and vice-versa
Install Trezor python-mnemonic:
pip install mnemonic
Exemple: Convert hex key to words:
python nostr_seed_trezor.py a4431c0a96a4997ed5ec763fb58b7f532530b9cf916219f3d2e2118f47cb56bb
Seed words: picture body actor coil end satoshi fish mom distance proof thank play fantasy friend dinner clump boring ozone review cart virtual toss foot infant
BIP39 seed numbers: [1315, 200, 22, 363, 589, 1532, 702, 1143, 510, 1379, 1791, 1331, 665, 744, 499, 355, 208, 1269, 1477, 281, 1956, 1838, 728, 923]
Exemple: Convert words to hex key:
python nostr_seed_trezor.py picture body actor coil end satoshi fish mom distance proof thank play fantasy friend dinner clump boring ozone review cart virtual toss foot infant
Hex Key: a4431c0a96a4997ed5ec763fb58b7f532530b9cf916219f3d2e2118f47cb56bb
"""
import sys
from binascii import unhexlify
from mnemonic import Mnemonic
mnemo = Mnemonic("english")
if len(sys.argv) == 2:
input_data = sys.argv[1]
if len(input_data) == 64: #bytes input
try:
entropy_bytes = unhexlify(input_data)
except IOError:
print ("Invalid string, must be a string of bytes")
seed_words = mnemo.to_mnemonic(entropy_bytes)
print("Seed words: " + str(seed_words))
seed_numbers = []
for word in seed_words.split():
seed_numbers.append(mnemo.wordlist.index(word)+1)
print("BIP39 seed numbers: " + str(seed_numbers))
else:
print ("Invalid key size, must be 64 bytes")
print(str(len(input_data)) + " bytes were given")
else:
if len(sys.argv) == 25:
mnemonic = " ".join(sys.argv[1:])
entropy = mnemo.to_entropy(mnemonic)
print("Hex Key: " + str(entropy.hex()))
else:
print("Inform a 64 bytes private key or a 24 BIP39 words seed")
print(str(len(sys.argv)-1) + " words were given")
@odudex
Copy link
Author

odudex commented Mar 21, 2024

how to make it in bulk?? I need convert big txt file to mnemonics. it is a possible?

Please elaborate, convert a big text into a single mnemonic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment