Skip to content

Commit 3454c64

Browse files
committed
feat(python): add utils
1 parent 5de28fd commit 3454c64

File tree

5 files changed

+1253
-1
lines changed

5 files changed

+1253
-1
lines changed

python/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ dependencies = [
1212
"pytest-asyncio",
1313
"starknet-py>=0.28.0",
1414
"requests",
15-
"pytest-xdist"
15+
"pytest-xdist",
16+
"pydantic>=2.0",
17+
"dune-client==1.7.10",
1618
]
1719

1820
[tool.setuptools]

python/utils/dune_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from dune_client.client import DuneClient
2+
from dune_client.query import QueryBase
3+
from dune_client.types import QueryParameter, DuneRecord, ParameterType
4+
5+
6+
def query_dune(
7+
query_id: int, api_key: str, latest_block: int | None = None
8+
) -> list[DuneRecord]:
9+
"""
10+
Run a Dune query and return the results.
11+
12+
:param query_id: The ID of the Dune query.
13+
:param api_key: The API key Dune.
14+
:param latest_block: The latest block to query (optional).
15+
:return: The results of the Dune query.
16+
"""
17+
dune = DuneClient(api_key=api_key)
18+
if latest_block is not None:
19+
params = [
20+
QueryParameter(
21+
name="max_block",
22+
value=latest_block,
23+
parameter_type=ParameterType.NUMBER,
24+
)
25+
]
26+
else:
27+
params = []
28+
query = QueryBase(
29+
query_id=query_id,
30+
params=params,
31+
)
32+
return dune.run_query(query).get_rows()

python/utils/starkli_utils.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)