Skip to content

Commit e6b38b6

Browse files
committed
Added support for UINT8 Array private key
1 parent b6e7128 commit e6b38b6

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ Before using the SDK, please ensure you have completed the following steps:
6161

6262
1. **Environment Variables**:
6363
Set up your required environment variables.
64-
Example:
64+
Example (base58 string or uint8 array supported):
6565
```sh
66-
export PRIVATE_KEY=your_private_key_here
66+
# Base58 format
67+
export PRIVATE_KEY=your_base58_private_key_here
68+
69+
# OR as a uint8 array (advanced)
70+
export PRIVATE_KEY=[10,229,131,132,213,96,74,22,...]
6771
```
72+
73+
> **Note**: `PRIVATE_KEY` can be either a base58-encoded string (default Solana format), or a uint8 array (e.g. `[181,99,240,...]`). The SDK will automatically detect and parse the format.
6874
6975
2. **Optional Configuration**:
7076
Depending on your credentials and setup, you have a couple of options for initializing the `UltraApiClient`:

jup_python_sdk/clients/jupiter_client.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import json
23
import os
34
from typing import Dict, Optional
45

@@ -47,10 +48,31 @@ def _post_headers(self) -> Dict[str, str]:
4748
headers["x-api-key"] = self.api_key
4849
return headers
4950

51+
def _load_private_key_bytes(self) -> bytes:
52+
"""Loads the private key from the environment
53+
variable as base58 or uint8 array."""
54+
pk_raw = os.getenv(self.private_key_env_var, "")
55+
pk_raw = pk_raw.strip()
56+
if pk_raw.startswith("[") and pk_raw.endswith("]"):
57+
try:
58+
arr = json.loads(pk_raw)
59+
if isinstance(arr, list) and all(
60+
isinstance(x, int) and 0 <= x <= 255 for x in arr
61+
):
62+
return bytes(arr)
63+
else:
64+
raise ValueError
65+
except Exception as e:
66+
raise ValueError(
67+
f"Invalid uint8-array private key format: {e}"
68+
)
69+
try:
70+
return base58.b58decode(pk_raw)
71+
except Exception as e:
72+
raise ValueError(f"Invalid base58 private key format: {e}")
73+
5074
def _get_public_key(self) -> str:
51-
wallet = Keypair.from_bytes(
52-
base58.b58decode(os.getenv(self.private_key_env_var, ""))
53-
)
75+
wallet = Keypair.from_bytes(self._load_private_key_bytes())
5476
return str(wallet.pubkey())
5577

5678
def _sign_base64_transaction(
@@ -65,9 +87,7 @@ def _sign_base64_transaction(
6587
def _sign_versioned_transaction(
6688
self, versioned_transaction: VersionedTransaction
6789
) -> VersionedTransaction:
68-
wallet = Keypair.from_bytes(
69-
base58.b58decode(os.getenv(self.private_key_env_var, ""))
70-
)
90+
wallet = Keypair.from_bytes(self._load_private_key_bytes())
7191
account_keys = versioned_transaction.message.account_keys
7292
wallet_index = account_keys.index(wallet.pubkey())
7393

0 commit comments

Comments
 (0)