ethereum.enable() - 请求用户授权
ethereum.enable()
方法请求用户授权应用访问MetaMask中的用户账号信息。
该方法返回一个Promise对象,其解析值为以太坊地址数组。
示例代码
下面的ES2015代码使用async/await方式来调用ethereum.enable()
方法,
请求用户授权web应用访问其账号信息:
try {
const accounts = await ethereum.enable()
// You now have an array of accounts!
// Currently only ever one:
// ['0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825']
} catch (error) {
// Handle error. Likely the user rejected the login:
console.log(reason === "User rejected provider access")
}
下面的是ES5代码,实现同样的功能:
ethereum.enable()
.then(function (accounts) {
// You now have an array of accounts!
// Currently only ever one:
// ['0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825']
})
.catch(function (reason) {
// Handle error. Likely the user rejected the login:
console.log(reason === "User rejected provider access")
})