Last active
August 13, 2023 01:00
-
-
Save ShreyanJain9/29438d5dc0ebf58d28fc57dd585621e6 to your computer and use it in GitHub Desktop.
BIP 39 encode to mnemonic and decode for any string of length 16, 20, 24, 28, or 32 chars
This file contains 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
#!/usr/bin/env python3 | |
import argparse | |
from mnemonic import Mnemonic | |
import sys | |
def encode(mnemonic_str): | |
m = Mnemonic("english") | |
words = m.to_mnemonic(mnemonic_str.encode("utf-8")) | |
return words | |
def decode(mnemonic_words): | |
m = Mnemonic("english") | |
try: | |
mnemonic_str = m.to_entropy(mnemonic_words) | |
return mnemonic_str.decode("utf-8") | |
except: | |
return None | |
def main(): | |
parser = argparse.ArgumentParser(description="BIP39 Encoding and Decoding Tool") | |
parser.add_argument("action", choices=["e", "d"], help="Action to perform: e (encode) or d (decode) ") | |
args = parser.parse_args() | |
if args.action == "e": | |
input_str = sys.stdin.readline().strip() | |
result = encode(input_str) | |
print(result) | |
elif args.action == "d": | |
input_str = sys.stdin.readline().strip() | |
result = decode(input_str) | |
if result: | |
print(result) | |
else: | |
print("Decoding failed. Invalid mnemonic words.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment