Last active
December 7, 2023 18:05
-
-
Save wphan/e21fc16eb2558cee820615cdf4ba2bbd to your computer and use it in GitHub Desktop.
convert between common solana keypair formats
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/python3 | |
# python env needs base58 package | |
# this format is suitable for raycast script commands! | |
# https://github.com/raycast/script-commands | |
# Required parameters: | |
# @raycast.schemaVersion 1 | |
# @raycast.title Convert Solana Private Key | |
# @raycast.mode fullOutput | |
# Optional parameters: | |
# @raycast.icon 🔒 | |
# @raycast.argument1 { "type": "text", "placeholder": "Private key or filepath" } | |
# @raycast.packageName Solana utils | |
# Documentation: | |
# @raycast.description Converts solana private key from file to/from byte array and base58 | |
# @raycast.author wphan | |
# @raycast.authorURL https://github.com/wphan | |
import sys | |
import base58 | |
import json | |
import os | |
def handle_privkey_str(contents): | |
if '[' in contents and ']' in contents: | |
print("Parsing private key as numbers array...") | |
pk_array = bytes(json.loads(contents)) | |
base58_encoded = base58.b58encode(pk_array) | |
print("Base58 private key:") | |
print(base58_encoded.decode('utf-8')) | |
else: | |
print("Parsing private key as base58 string...") | |
pk_b58 = base58.b58decode(contents) | |
print("ByteArray private key:") | |
print(json.dumps(list(pk_b58)).replace(" ", "")) | |
abs_path = os.path.expanduser(sys.argv[1]) | |
if os.path.exists(abs_path): | |
print(f"opening {abs_path}") | |
with open(abs_path, "r") as f: | |
contents = f.read().strip() | |
handle_privkey_str(contents) | |
else: | |
handle_privkey_str(sys.argv[1].strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment