Created
April 6, 2024 11:23
-
-
Save Mleekko/f3c5b0183aff31322bb8c563839ae7e5 to your computer and use it in GitHub Desktop.
Read Private Key from node-keystore.ks
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
import hashlib | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption, pkcs12 | |
from ecdsa import keys | |
from getpass import getpass | |
from radix_engine_toolkit import Address, PrivateKey, PublicKey, derive_virtual_account_address_from_public_key, OlympiaNetwork | |
NETWORK_ID: int = OlympiaNetwork.MAINNET.value | |
# network id is 0x01 for mainnet, or 0x02 for Stokenet | |
network_id = 0x01 | |
print("Enter your Keystore Password:") | |
pw = getpass() | |
password = pw.encode() | |
print('\n') | |
# Read the Radix Keystore File (which is in PKCS12 format) | |
with open("./node-keystore.ks", "rb") as f: | |
private_key, certificate, additional_certificates = pkcs12.load_key_and_certificates(f.read(), password, default_backend()) | |
# Extract the unencrypted Private Key bytes | |
private_key_bytes = private_key.private_bytes(Encoding.DER, PrivateFormat.PKCS8, NoEncryption()) | |
# Convert into Elliptic Curve Digital Signature Algorithm (ecdsa) private key object | |
private_key = keys.SigningKey.from_der(private_key_bytes, hashfunc=hashlib.sha256) | |
private_key_string = private_key.to_string() | |
olympia_private_key = private_key_string.hex() | |
# UNCOMMENT THIS TO VEW YOUR RAW PRIVATE KEY HEX | |
# print(f"Node Account private key: {olympia_private_key}") | |
private_key_bytes_ret: bytes = bytes.fromhex(olympia_private_key) | |
# Note this is set for secp256k1 but can be changed to ed25519 | |
private_key_ret: PrivateKey = PrivateKey.new_secp256k1(private_key_bytes_ret) | |
public_key: PublicKey = private_key_ret.public_key() | |
account: Address = derive_virtual_account_address_from_public_key( | |
public_key, network_id | |
) | |
print(f"Babylon Address of Keystore: {account.as_str()}") | |
print('\n') | |
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
radix-engine-toolkit==1.0.12 | |
cryptography==42.0.5 | |
ecdsa==0.18.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment