您现在的位置是:首页 >技术交流 >vue 连接区块链钱包网站首页技术交流
vue 连接区块链钱包
简介vue 连接区块链钱包
使用MetaMask
1.安装ethers 库(v5,连接v6版本代码有出入)
npm install ethers@5
示例代码
import { ethers } from 'ethers';
async function connectWallet() {
if (window.ethereum) {
await window.ethereum.request({ method: 'eth_requestAccounts' });
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
console.log("Connected Wallet Address:", await signer.getAddress());
} else {
console.log('Please install MetaMask!');
}
}
使用WalletConnect
WalletConnect支持多种区块链和多种类型的钱包。首先,安装WalletConnect库:
npm install @walletconnect/web3-provider
示例代码(使用@walletconnect/web3-provider):
import WalletConnectProvider from "@walletconnect/web3-provider";
import Web3 from "web3";
async function connectWallet() {
const provider = new WalletConnectProvider({ rpc: { 1: "https://rpc.ankr.com/eth" } });
await provider.enable(); // This will prompt the user to connect their wallet if they are not already connected.
const web3 = new Web3(provider);
const accounts = await web3.eth.getAccounts();
console.log("Connected Wallet Address:", accounts[0]);
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。





U8W/U8W-Mini使用与常见问题解决
QT多线程的5种用法,通过使用线程解决UI主界面的耗时操作代码,防止界面卡死。...
stm32使用HAL库配置串口中断收发数据(保姆级教程)
分享几个国内免费的ChatGPT镜像网址(亲测有效)
Allegro16.6差分等长设置及走线总结