Skip to content

Instantly share code, notes, and snippets.

@odudex
Last active May 10, 2024 09:43
Show Gist options
  • Save odudex/93cfb5628b22f8675ab1939fd43133f4 to your computer and use it in GitHub Desktop.
Save odudex/93cfb5628b22f8675ab1939fd43133f4 to your computer and use it in GitHub Desktop.
Uses Embit to convert hex or bech32 keys to BIP39 seed words and vice-versa
"""Uses Embit and qrcode modules to convert hex and bech32 keys to BIP39 seed words and vice-versa.
Also generates ascii compact seed QR codes.
Install Embit:
pip install embit
Exemple: Words as input:
python nostr_c_seed_qr.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
Bech32 key: nsec153p3cz5k5jvha40vwclmtzml2vjnpww0j93pnu7juggc737t26ascxcmgr
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: Hex key as input:
python nostr_c_seed_qr.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
Bech32 key: nsec153p3cz5k5jvha40vwclmtzml2vjnpww0j93pnu7juggc737t26ascxcmgr
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: Bech32 key as input:
python nostr_c_seed_qr.py nsec153p3cz5k5jvha40vwclmtzml2vjnpww0j93pnu7juggc737t26ascxcmgr
Hex Key: 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]
"""
import sys
from embit import bip39, bech32
from embit.wordlists.bip39 import WORDLIST
def _seed_numbers():
seed_numbers = []
words = mnemonic.split(" ")
for word in words:
seed_numbers.append(WORDLIST.index(word) + 1)
print("BIP39 seed numbers: " + str(seed_numbers))
if len(sys.argv) == 2:
input_data = sys.argv[1]
if len(input_data) == 64: # bytes input
try:
entropy_bytes = bytes.fromhex(input_data)
except IOError:
print("Invalid string, must be a string of bytes")
sys.exit()
mnemonic = bip39.mnemonic_from_bytes(entropy_bytes)
print("Seed words: " + str(mnemonic))
converted_bits = bech32.convertbits(bytearray.fromhex(input_data), 8, 5)
print(
"Bech32 key: "
+ bech32.bech32_encode(bech32.Encoding.BECH32, "nsec", converted_bits)
)
_seed_numbers()
elif len(input_data) == 63 and input_data.startswith("nsec"): # bech32 input
spec, hrp, data = bech32.bech32_decode(input_data)
decoded = bech32.convertbits(data, 5, 8, False)
print("Hex Key: " + bytes(decoded).hex())
mnemonic = bip39.mnemonic_from_bytes(bytes(decoded))
print("Seed words: " + str(mnemonic))
_seed_numbers()
else:
print("Invalid keys")
else:
if len(sys.argv) == 25:
mnemonic = " ".join(sys.argv[1:])
entropy = bip39.mnemonic_to_bytes(mnemonic, ignore_checksum=True)
print("Hex Key: " + str(entropy.hex()))
converted_bits = bech32.convertbits(entropy, 8, 5)
print(
"Bech32 key: "
+ bech32.bech32_encode(bech32.Encoding.BECH32, "nsec", converted_bits)
)
_seed_numbers()
else:
print("Inform a 64 bytes private key or a 24 BIP39 words seed")
print(str(len(sys.argv) - 1) + " words were given")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment