Skip to content

Commit ec33e3c

Browse files
committed
allow existing wallets
1 parent 40104cc commit ec33e3c

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

agixt/extensions/solana_wallet.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ class solana_wallets(Extensions):
1818
def __init__(
1919
self,
2020
SOLANA_API_URI: str = "https://api.devnet.solana.com",
21-
WALLET_ADDRESS: str = None,
21+
WALLET_PRIVATE_KEY: str = None,
2222
**kwargs,
2323
):
2424
self.SOLANA_API_URI = SOLANA_API_URI
2525
self.client = Client(SOLANA_API_URI)
26-
self.wallet_address = WALLET_ADDRESS # Stored as a base58 string
27-
self.wallet_keypair = None # This will hold the Keypair if created
26+
27+
# If an existing wallet private key is provided, load the keypair
28+
if WALLET_PRIVATE_KEY:
29+
# Here we assume the private key is a base58-encoded string.
30+
# (You might instead have a hex string; adjust as needed.)
31+
self.wallet_keypair = Keypair.from_base58_string(WALLET_PRIVATE_KEY)
32+
self.wallet_address = self.wallet_keypair.pubkey().to_string()
33+
else:
34+
self.wallet_keypair = None
35+
self.wallet_address = None
36+
2837
self.commands = {
2938
"Create Solana Wallet": self.create_wallet,
3039
"Get Solana Wallet Balance": self.get_wallet_balance,
@@ -39,17 +48,19 @@ def __init__(
3948
"Get Token Price": self.get_token_price,
4049
"Get Wallet Token Accounts": self.get_wallet_token_accounts,
4150
}
51+
# Additional initialization as needed
4252

4353
async def create_wallet(self):
4454
"""
4555
Creates a new Solana wallet by generating a new keypair.
46-
Updates self.wallet_address and self.wallet_keypair.
56+
This method can be used if no wallet was connected via the init params.
4757
"""
4858
new_keypair = Keypair.generate()
4959
self.wallet_keypair = new_keypair
50-
# Get the public key as a base58 string:
5160
self.wallet_address = new_keypair.pubkey().to_string()
52-
secret_hex = new_keypair.secret().hex()
61+
secret_hex = (
62+
new_keypair.secret().hex()
63+
) # for display; store securely in practice
5364
return (
5465
f"Created new Solana wallet.\n"
5566
f"Public Key: {self.wallet_address}\n"

0 commit comments

Comments
 (0)