4

I am trying to transfer some funds using my private key in Python. Here's the code but I don't know how I can use my existing phantom wallet's secret key to make the transaction. Here's the code, can anyone explain?

from solana.publickey import PublicKey
from solana.rpc.api import Client
from solana.keypair import Keypair
from solana.transaction import Transaction
from solana.system_program import TransferParams, transfer

LAMPORT_PER_SOL = 1000000000

client: Client = Client("https://api.mainnet-beta.solana.com")

sender = Keypair.from_secret_key(<private key here>)
receiver = PublicKey()
#print(sender.public_key)
#print(sender.secret_key)
lamp = client.get_balance(pubkey= sender.public_key)['result']['value']

transaction = Transaction()
transaction.add(
  transfer(
    TransferParams(
      from_pubkey= sender.public_key,
      to_pubkey= receiver, 
      lamports= lamp/10
    )
  )
)
#transaction.add_signature(sender.sign(transaction.serialize()))
client.send_transaction(transaction, sender)
1
  • put your wallet's private key as the from_secret_key parameter. you may need to add .encode() after the string. also, make sure to put a public key in the receiver area or it will send your Solana to a new wallet. besides that, you should be good to go
    – Pixeled
    Commented Jul 31, 2022 at 23:24

1 Answer 1

4

Step 1 – Export your private key from your wallet

Phantom lets you export your private key from this menu.

The ‘export private key’ button in the Phantom wallet

Click ‘Export private key’ and save the string that results somewhere safe.

NOTE: Anyone who holds this key has complete control of your wallet and can drain all of the assets from it. Take great care not to leak it to the public.

Step 2 – Materialize a keypair using the exported material

import bs58 from 'bs58';
import {Keypair} from '@solana/web3.js';

// The string you exported from Phantom.
const privateKeyBase58Encoded = '...';

// Decode that string to a byte array.
const privateKeyBytes = bs58.decode(privateKeyBase58Encoded);

// Materialize a `Keypair` using that private key.
const keypair = Keypair.fromSecretKey(privateKeyBytes);

Step 3 – Use that keypair to sign transactions as you normally would

Here's an example transaction you can use to test your imported keypair. It will execute a transaction and write the string ‘Hello world’ to the transaction's log.

const ix = new TransactionInstruction({
  keys: [
    {
      pubkey: keypair.publicKey,
      isSigner: true,
      isWritable: true,
    },
  ],
  programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
  data: Buffer.from("Hello world", "utf8"),
});
const transaction = new Transaction({
  feePayer: keypair.publicKey,
  ...(await connection.getLatestBlockhash()),
}).add(ix);
const signature = await connection.sendTransaction(transaction, [keypair]);
console.log(signature);
1
  • 1
    This example uses JavaScript, but the principles will apply equally to Python. Decode the exported key using a base58-string-to-python-bytes conversion, then import it using Keypair.from_secret_key(). Commented Jul 30, 2022 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.