Last active
November 4, 2022 17:45
-
-
Save kdmukai/c53accd809b8e90cc71c839609d55aaf to your computer and use it in GitHub Desktop.
Generate a 12-word mnemonic and first receive addr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from embit import bip32, bip39, script | |
from embit.slip39 import secure_randint | |
from embit.wordlists.bip39 import WORDLIST | |
for i in range(0, 100): | |
mnemonic = [] | |
for j in range(0, 11): | |
# note: `secure_randint` is inclusive of the end of the range | |
mnemonic.append(WORDLIST[secure_randint(0, 2047)]) | |
mnemonic.append(WORDLIST[0]) | |
# Convert the resulting mnemonic to bytes, but we `ignore_checksum` validation | |
# because we assume it's incorrect since we either let the user select their own | |
# final word OR we injected the 0000 word from the wordlist. | |
mnemonic_bytes = bip39.mnemonic_to_bytes(" ".join(mnemonic), ignore_checksum=True) | |
# This function will convert the bytes back into a mnemonic, but it will also | |
# calculate the proper checksum bits while doing so. For a 12-word seed it will just | |
# overwrite the last 4 bits from the above result with the checksum; for a 24-word | |
# seed it'll overwrite the last 8 bits. | |
mnemonic = bip39.mnemonic_from_bytes(mnemonic_bytes).split() | |
seed = bip39.mnemonic_to_seed(" ".join(mnemonic)) | |
root = bip32.HDKey.from_seed(seed) | |
xprv = root.derive("m/84'/0'/0'") | |
xpub = xprv.to_public() | |
# generate first receive addr | |
pubkey = xpub.derive([0,0]).key | |
addr = script.p2wpkh(pubkey).address() | |
print(f"""{" ".join(mnemonic)}, {addr}""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment