web3.js 实现调用狐狸钱包完成用户登录

web3.js 实现调用狐狸钱包完成用户登录,第1张

用户帐户在以太坊的各种环境中使用,包括作为标识符和用于签署交易。

首先需要判断用户浏览器有没有安装Metamask插件

 if (typeof window.ethereum.isMetaMask === 'undefined') {
              alert('看起来您需要一个 Dapp 浏览器才能开始使用。')
              alert('请安装 MetaMask!')
 }

如果用户没有安装,提示他安装,否则无法继续使用。

下一步 我们 请求狐狸钱包,访问账户,获取当前地址

 ethereum.request({ method: 'eth_requestAccounts' }).catch(function (reason) {
         alert('哦!NO, 好像发生了点意外。别着急, 我们很快修复它'); 
}).then(function (accounts) {
     console.log('account',accounts);

拿到地址之后,赋值给我们的页面

const account = accounts[0]
//alert('登录成功');
console.log("success");
$("#login-address").html("账户地址"+account);

作者接区块链开发,智能合约开发,联系 QQ80164590

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/718665.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存