我正在使用 web3.js 1.0.0-beta.36 连接到区块链。
但不幸的是,如果已启用,此代码无法连接到 Metamask。
var metamask = false;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
metamask = true;
web3.eth.defaultAccount = web3.eth.accounts[0];
} else {
web3 = new Web3(new
Web3.providers.HttpProvider(infuraUrl));
var address = web3.eth.accounts.create().address;
}
myContract = new web3.eth.Contract(abi,contractAddress);
myContract.methods.Register((Name,Id).send({},function(error,result){
if (! error)
console.log(result);
else
console.log(error);
});
当我们用 Metamsk 运行这个程序时,它会显示这个错误:
Error: Returned error: Error: MetaMask Tx Signature: User denied transaction signature.
但是当我提到一个固定地址时,它可以工作:
myContract.methods.Register((Name,Id).send({from: '0x...'},function(error,result){
if (! error)
console.log(result);
else
console.log(error);
});
由于某些原因,我无法将地址本身带入源代码,我希望它从 Metamask 中获取地址。我应该怎么办?