我希望如果有人能指出我正确的方向......我正在尝试将“钱包连接”扩展(例如 sollet / phantom)功能添加到我的网站,但我不知道从哪里开始。Metamask 有一个很好的解释/示例,但我不确定 Solana 是否相同?
问问题
6077 次
2 回答
5
您可以将https://github.com/project-serum/sol-wallet-adapter用于 Sollet Web 或 Sollet 扩展
对于幻影钱包,文档在这里https://docs.phantom.app/integrating/establishing-a-connection
请注意,元掩码等 EVM 钱包之间的一个主要区别是,大多数 EVM 钱包还将处理在链上发送交易,而 Solana 钱包仅处理对交易进行签名,并且发送由应用程序完成。
下面是如何与 Sollet 和 Phantom 集成的示例
import { Connection, SystemProgram, Transaction, clusterApiUrl } from '@solana/web3.js';
import Wallet from '@project-serum/sol-wallet-adapter';
let provider = 'https://www.sollet.io';
// For sollet extension use
// provider = window.sollet
let wallet = new Wallet(provider);
wallet.on('connect', publicKey => console.log('Connected to ' + publicKey.toBase58()));
wallet.on('disconnect', () => console.log('Disconnected'));
await wallet.connect();
// Sending a transaction
let connection = new Connection(clusterApiUrl('devnet'));
let transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: wallet.publicKey,
lamports: 100,
})
);
let { blockhash } = await connection.getRecentBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = wallet.publicKey;
let signed = await wallet.signTransaction(transaction);
let txid = await connection.sendRawTransaction(signed.serialize());
await connection.confirmTransaction(txid);
// Phantom Wallet
window.solana.connect();
window.solana.on("connect", () => console.log("connected!"))
console.log(window.solana.publicKey);
Solana 团队还编写了一个扩展库,用于与 Solana 上的一堆现有钱包集成 - https://github.com/solana-labs/wallet-adapter
于 2021-08-31T08:47:39.857 回答
1
使用幻影
<html>
<head>
<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script>
</head>
<body>
<script>
const getProvider = async () => {
if ("solana" in window) {
await window.solana.connect(); // opens wallet to connect to
const provider = window.solana;
if (provider.isPhantom) {
console.log("Is Phantom installed? ", provider.isPhantom);
return provider;
}
} else {
document.write('Install https://www.phantom.app/');
}
};
window.onload = () => {
getProvider().then(provider => {
console.log('key', provider.publicKey.toString())
})
.catch(function(error){
console.log(error)
});
}
</script>
</body>
</html>
于 2021-10-21T08:19:53.010 回答