如何仅从他们的公钥(Solana)中获取用户的密钥对?



我正在制作一个dApp,我想添加一个按钮,用户(连接钱包的人)可以向另一个用户发送0.01 SOL。我已经在我的Rust程序中编写了这个函数,在用anchor test测试它之后,当我使用我自己的个人钱包的Keypair来签署交易时,它似乎正在工作。然而,现在我正在编写事件处理程序函数在我的web应用程序的前端,我不确定什么传递signers参数,如果我想让用户签署交易。我如果我不知道他们的秘密钥匙吗?有一种我可以生成一个用户的密钥对的公钥单独或我需要使用索拉纳钱包适配器吗?任何帮助都会很感激。这是我第一次和索拉纳合作!

功能如下:

const tipSol = async (receiverAddress) => {
try {
const provider = getProvider();
const program = new Program(idl, programID, provider);
const lamportsToSend = LAMPORTS_PER_SOL / 100;
const amount = new anchor.BN(lamportsToSend);
await program.rpc.sendSol(amount, {
accounts: {
from: walletAddress,
to: receiverAddress,
systemProgram: SystemProgram.programId,
},
signers: ?
})
console.log('Successfully sent 0.01 SOL!')
window.alert(`You successfully tipped ${receiverAddress} 0.01 SOL!`)
} catch (error) {
console.error('Failed to send SOL:', error);
window.alert('Failed to send SOL:', error);
}
}

前端从不访问私钥。相反,流程是这样的:

  • 前端创建事务
  • 前端发送交易到钱包
  • 钱包签署交易
  • 钱包将签名的交易返回给前端
  • 前端发送事务

你可以使用@solana/wallet-adapter来实现这在你的前端https://github.com/solana-labs/wallet-adapter

实际上,在你的前端中应该是这样的

export const Component = () => {
const { connection } = useConnection();
const { sendTransaction } = useWallet();
const handle = async () => {
const ix: TransactionInstruction = await tipSol(receiverKey);
const tx = new Transaction().add(ix);
const sig = await sendTransaction(tx, connection);
};
// ...
};