[英]How to get a user's keypair from their public key only (Solana)?
我正在制作一個 dApp,我想添加一個按鈕,用戶(連接錢包的用戶)可以將 0.01 SOL 發送給另一個用戶。 我已經在我的 Rust 程序中編寫了該函數,並且在使用anchor test
對其進行測試后,當我使用我自己的個人錢包的密鑰對來簽署交易時,它似乎正在工作。 但是,現在我正在我的 Web 應用程序的前端編寫事件處理程序函數,如果我希望用戶簽署交易,我不確定要為signers
參數傳遞什么。 如果我不知道他們的密鑰,我該怎么辦? 有沒有一種方法可以單獨從用戶的公鑰生成用戶的密鑰對,或者我需要為此使用 Solana 錢包適配器嗎? 任何幫助,將不勝感激。 這是我第一次與 Solana 合作!
這是功能:
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);
};
// ...
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.