I’m working on creating NFTs on the solana network and running into some issues. Every tutorial I find seems to use a temporary keypair that gets generated first to mint the tokens, then they transfer everything to the actual wallet afterwards.
Is it possible to skip this step and mint directly to my phantom wallet instead? The current approach feels unnecessarily complicated.
Here’s what I’ve been working with:
import * as solanaWeb3 from '@solana/web3.js';
import * as tokenProgram from '@solana/spl-token';
const connectWallet = async () => {
if (window.solana && window.solana.isPhantom) {
return window.solana;
}
throw new Error('Phantom wallet not found');
};
const createToken = async () => {
const wallet = await connectWallet();
const userPublicKey = wallet.publicKey;
const network = new solanaWeb3.Connection(
solanaWeb3.clusterApiUrl('devnet'),
'confirmed'
);
// This is the part I want to avoid - creating temporary keypair
const tempKeypair = solanaWeb3.Keypair.generate();
console.log('User wallet:', userPublicKey.toString());
console.log('Temp keypair:', tempKeypair.publicKey.toString());
// Airdrop for testing
const airdropTx = await network.requestAirdrop(
tempKeypair.publicKey,
solanaWeb3.LAMPORTS_PER_SOL
);
await network.confirmTransaction(airdropTx);
// Create new token mint
const tokenMint = await tokenProgram.Token.createMint(
network,
tempKeypair,
tempKeypair.publicKey,
null,
9,
tokenProgram.TOKEN_PROGRAM_ID
);
// Create token accounts
const tempAccount = await tokenMint.getOrCreateAssociatedAccountInfo(
tempKeypair.publicKey
);
const userAccount = await tokenMint.getOrCreateAssociatedAccountInfo(
userPublicKey
);
// Mint tokens to temp account
await tokenMint.mintTo(
tempAccount.address,
tempKeypair.publicKey,
[],
1000000000
);
// Transfer to user wallet
const transferTx = new solanaWeb3.Transaction().add(
tokenProgram.Token.createTransferInstruction(
tokenProgram.TOKEN_PROGRAM_ID,
tempAccount.address,
userAccount.address,
tempKeypair.publicKey,
[],
1000000000
)
);
const txSignature = await solanaWeb3.sendAndConfirmTransaction(
network,
transferTx,
[tempKeypair]
);
console.log('Transaction signature:', txSignature);
console.log('Token mint address:', tokenMint.publicKey.toString());
};
Can someone help me figure out how to mint directly without this two-step process?