|
| 1 | +import subprocess |
| 2 | + |
| 3 | +# from pathlib import Path |
| 4 | + |
| 5 | + |
| 6 | +def get_starkli_private_key(keystore_file: str, keystore_password: str): |
| 7 | + """ |
| 8 | + Get the private key from the starkli keystore. |
| 9 | +
|
| 10 | + :param keystore_file: The path to the keystore file. |
| 11 | + :param keystore_password: The password for the keystore file. |
| 12 | + :return: The private key. |
| 13 | + """ |
| 14 | + # Verify keystore file. |
| 15 | + # path = Path(keystore_file) |
| 16 | + # if not path.exists(): |
| 17 | + # raise FileNotFoundError(f"Keystore file {keystore_file} not found.") |
| 18 | + # if not path.is_file(): |
| 19 | + # raise FileNotFoundError(f"Keystore file {keystore_file} is not a file.") |
| 20 | + |
| 21 | + # Get private key. |
| 22 | + password_format = "--password={}" |
| 23 | + command = [ |
| 24 | + "starkli", |
| 25 | + "signer", |
| 26 | + "keystore", |
| 27 | + "inspect-private", |
| 28 | + keystore_file, |
| 29 | + password_format.format(keystore_password), |
| 30 | + ] |
| 31 | + |
| 32 | + try: |
| 33 | + result = subprocess.run(command, capture_output=True, text=True) |
| 34 | + |
| 35 | + if result.returncode == 0: |
| 36 | + return result.stdout.split()[2] |
| 37 | + else: |
| 38 | + raise Exception( |
| 39 | + f"Private key extraction failed with error: {result.stderr}" |
| 40 | + ) |
| 41 | + except Exception as e: |
| 42 | + raise Exception(f"An error occurred while getting private key: {e}") |
| 43 | + |
| 44 | + |
| 45 | +def ledger_sign_tx(tx_hash: str, ledger_path: str) -> str: |
| 46 | + """ |
| 47 | + Sign a transaction with the ledger account. |
| 48 | +
|
| 49 | + :param tx_hash: The hash of the transaction to sign. |
| 50 | + :param ledger_path: The derivation_path of the ledger. |
| 51 | + :return: The signature of the transaction. |
| 52 | + """ |
| 53 | + command = [ |
| 54 | + "starkli", |
| 55 | + "ledger", |
| 56 | + "sign-hash", |
| 57 | + "--path", |
| 58 | + ledger_path, |
| 59 | + tx_hash, |
| 60 | + ] |
| 61 | + try: |
| 62 | + input( |
| 63 | + "Open ledger and press enter to continue, then sign the tx in your legder." |
| 64 | + ) |
| 65 | + result = subprocess.run(command, capture_output=True, text=True) |
| 66 | + |
| 67 | + if result.returncode == 0: |
| 68 | + print(f"Signing successful: {result.stdout.strip()}") |
| 69 | + return result.stdout.strip() |
| 70 | + else: |
| 71 | + print(f"Signing failed with error: {result.stderr}") |
| 72 | + except Exception as e: |
| 73 | + print(f"An error occurred while signing transaction: {e}") |
| 74 | + |
| 75 | + |
| 76 | +def get_ledger_public_key(ledger_path: str) -> str: |
| 77 | + """ |
| 78 | + Get the public key from the ledger account. |
| 79 | +
|
| 80 | + :param ledger_path: The derivation_path of the ledger. |
| 81 | + :return: The public key of the ledger account. |
| 82 | + """ |
| 83 | + command = [ |
| 84 | + "starkli", |
| 85 | + "ledger", |
| 86 | + "get-public-key", |
| 87 | + "--no-display", |
| 88 | + ledger_path, |
| 89 | + ] |
| 90 | + try: |
| 91 | + input("Getting public key. Open ledger and press enter to continue.") |
| 92 | + result = subprocess.run(command, capture_output=True, text=True) |
| 93 | + |
| 94 | + if result.returncode == 0: |
| 95 | + print(f"Public key: {result.stdout.strip()}") |
| 96 | + return result.stdout.strip() |
| 97 | + else: |
| 98 | + print(f"Getting public key failed with error: {result.stderr}") |
| 99 | + except Exception as e: |
| 100 | + print(f"An error occurred while getting public key: {e}") |
0 commit comments