티스토리 뷰
테스트 환경에서 스마트 컨트랙트 배포 후 테스트시 web3 에 web3.eth.getAccounts().then 사용시 메타마스크와 연동되지 않고 빈 배열로 반환되는 오류가 있었다.
원인
web3는 (MetaMask v4.14.0부터 입니다)이제 사용자 메타마스크의 사용자의 계정을 얻으려면 사용자에게 권한을 요청 해야 하며,
Metamask는 더 이상 기본적으로 계정을 노출하지 않는 것으로 보이므로 해당 계정에 액세스하려면 사용자에게 권한을 요청해야 한다.
web3.eth.getAccounts().then(function(accounts) {
var account = accounts[0];
EcommerceStore.deployed().then(function(f) {
return f.addProductToStore(
product["product-name"],
product["product-category"],
"imageLink",
"descLink",
Date.parse(product["product-start-time"]) / 1000,
web3.utils.toWei(parseFloat(product["product-price"]).toString(), 'ether'),
product["product-condition"],
{ from: account, gas: 470000}
);
}).then(function(result) {
alert("등록이 완료되었습니다.");
}).catch(function(err) {
console.error(err);
alert("오류 발생: " + err.message);
});
});
}
해결
따라서 getAccounts() 대신 다음을 requestAccounts() 를 사용하면 된다.

레퍼런스
Why web3.eth.getAccounts().then(console.log) return empty array?
I got empty array after I tried to web3.eth.getAccounts().then(console.log);and also got a warning which is./node_modules/web3-eth-accounts/src/scrypt.js Critical dependency: the request of a depen...
stackoverflow.com
https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8
'블록체인' 카테고리의 다른 글
| Bitcoin: A Peer-to-Peer Electronic Cash System (0) | 2021.03.24 |
|---|---|
| 블록체인의 현실과 미래: 혁명의 전망과 도전에 대한 고찰 (0) | 2021.03.24 |