Skip to content

Instantly share code, notes, and snippets.

@odudex
Last active August 31, 2023 12:08
Show Gist options
  • Save odudex/c9045ffda459772fc62afbdfcbb943b8 to your computer and use it in GitHub Desktop.
Save odudex/c9045ffda459772fc62afbdfcbb943b8 to your computer and use it in GitHub Desktop.
Uses Embit and qrcode modules generates ascii compact seed QR code.
"""Uses Embit and qrcode modules generates ascii compact seed QR code.
Install Embit:
pip install embit
Exemple: Words as input:
python 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
"""
import sys
from io import StringIO
from embit import bip39, bech32, ec
from embit.wordlists.bip39 import WORDLIST
from qrcode import QRCode
def _compact_seed_qr():
# Krux code snippet
words = mnemonic.split(" ")
checksum_bits = 8 if len(words) == 24 else 4
indexes = [WORDLIST.index(word) for word in words]
bitstring = "".join([f"{bin(index)[2:]:0>11}" for index in indexes])[
:-checksum_bits
]
qr_data = int(bitstring, 2).to_bytes((len(bitstring) + 7) // 8, "big")
# Prints ascii QR code
qr_code = QRCode()
qr_code.add_data(qr_data)
qr_string = StringIO()
qr_code.print_ascii(out=qr_string, invert=True)
print("Compact Seed QR:")
print(qr_string.getvalue())
if len(sys.argv) in (13, 25):
mnemonic = " ".join(sys.argv[1:])
_compact_seed_qr()
else:
print("Inform a 12 or 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